(function($){  
	$.fn.extend({
		printPage: function(options) {
		
			var m_this = $(this);
			var shopProxy = new ShopProxy();
			
			// setup
			function setup() {
				if (m_this.data("__setup"))
					return;
				m_this.data("__setup", true);
			}
		
			function displayPrintPageResult(orderData) {
				$("#itpl_buyer").empty();
				
				
				var shippingAddressAdd4 = !orderData.order.shippingAddress.address   ? orderData.order.address.address : orderData.order.shippingAddress.address;
				var shippingCap4 = !orderData.order.shippingAddress.cap  ? orderData.order.address.cap : orderData.order.shippingAddress.cap;
				var shippingCity4 = !orderData.order.shippingAddress.city  ?  orderData.order.address.city : orderData.order.shippingAddress.city;
				var shippingProvince4 = !orderData.order.shippingAddress.province  ? orderData.order.address.province : orderData.order.shippingAddress.province;
				var shippingCountry4 = !orderData.order.shippingAddress.country ? orderData.order.address.country : orderData.order.shippingAddress.country;
							
							
				var buyerAddress='';
				buyerAddress = buyerAddress + '<b>' + orderData.order.name + ' ' + orderData.order.surname + '</b><br />';
				buyerAddress = buyerAddress + ''+ orderData.order.address.address + ', ' + orderData.order.address.cap + ' - '+   orderData.order.address.city + ' ('+ orderData.order.address.province +'), ' + orderData.order.address.country + '<br/>';
 				buyerAddress = buyerAddress + '<br />';
				buyerAddress = buyerAddress + '<b class="focus">DATI SPEDIZIONE</b><br />';
				buyerAddress = buyerAddress + ''+ shippingAddressAdd4 + ', ' + shippingCap4 + ' '+  shippingCity4 + ' ('+ shippingProvince4 +'), ' + shippingCountry4 + '<br/>';
				buyerAddress = buyerAddress + '<br />';
				buyerAddress = buyerAddress + 'Modalit&agrave; di pagamento: <b>';
    			if (orderData.order.paymentType == 'ca') { buyerAddress = buyerAddress + 'contrassegno'; } else if (orderData.order.paymentType == 'cc') { buyerAddress = buyerAddress + 'carta di credito'; }
    			buyerAddress = buyerAddress + '</b>';
				$("#itpl_buyer").html(buyerAddress);
				
				$("#itpl_order_data").empty();
				var orderCode='';
				orderCode = orderCode + 'N&deg; Ordine <b>' + orderData.order.code + '</b> del <b>' + orderData.order.date + '</b><br />';
				$("#itpl_order_data").html(orderCode);
				
				$("#itpl_order_rows").empty();
				var orderRows='';
				$.each(orderData.rows, function(i,aelement) {
					if (i == orderData.rows) {
						orderRows = orderRows + '<tr id="itpl_order_rows" class="last">';
					} else {
						orderRows = orderRows + '<tr id="itpl_order_rows">';
					}
					orderRows = orderRows + '<td>' + aelement.product.code + '</td>';
					orderRows = orderRows + '<td><b class="focus">' + aelement.product["name"] + '</b></td>';
					orderRows = orderRows + '<td> </td>';
 					orderRows = orderRows + '<td>Quantità:' + aelement.quantity + '</td>';
					orderRows = orderRows + '<td>Prezzo: &euro; ' + aelement.product.priceB2C + '</td>';
					orderRows = orderRows + '<td class="price">Totale: &euro; ' + aelement.totalRow + '</td>';
					orderRows = orderRows + '</tr>';
				});
				$("#itpl_order_rows").replaceWith(orderRows);
				
				$("#itpl_pink_card").empty();
				if ( (!isEmptyOrNull(orderData.order.discountPK)) || (orderData.order.discountPK > 0) ) {
					var pinkcard='';
					pinkcard = pinkcard + '<td colspan="4">Sconto <b class="focus">pink card</b></td>';
					pinkcard = pinkcard + '<td colspan="3" class="right"><span class="focus">&euro; -' + orderData.order.discountPK + '</span></td>';
					$("#itpl_pink_card").html(pinkcard);
				} else {
					$("#itpl_pink_card").hide();
				}
				
				$("#itpl_promo_discount").empty();
				if ( (!isEmptyOrNull(orderData.order.promotionDiscount)) || (orderData.order.promotionDiscount > 0) ) {
					var promoDiscount='';
					promoDiscount = promoDiscount + '<td colspan="4">Sconto <b class="focus">promozionale</b></td>';
					promoDiscount = promoDiscount + '<td colspan="3" class="right"><span class="focus">&euro; -' + orderData.order.promotionDiscount + '</span></td>';
					$("#itpl_promo_discount").html(promoDiscount);
				} else {
					$("#itpl_pink_card").hide();
				}
				
				$("#itpl_shipping_charges").empty();
				var shippingCharges='';
				shippingCharges = shippingCharges + '<td colspan="4">Spese di spedizione</td>';
				shippingCharges = shippingCharges + '<td colspan="3" class="right"><span class="focus">&euro; ' + orderData.order.shippingCharges + '</span></td>';
				$("#itpl_shipping_charges").html(shippingCharges);
				
				$("#itpl_final_amount").empty();
				var finalAmount='';
				finalAmount = finalAmount + '<th class="tot" colspan="4">Totale</th>';
				finalAmount = finalAmount + '<td class="right" colspan="3"><b class="focus">&euro; ' + orderData.order.totalAmount + '</b></td>';
				$("#itpl_final_amount").html(finalAmount);
				

    			$("#btnPrint").unbind('click');
    			$("#btnPrint").click(function () {
    				window.print();
    				return false;
    			});
				
			}
			
    		function _displayPrintPage(codTrans) {
	
	    		shopProxy.getCart().showResult(codTrans, function(orderData) {
	    				displayPrintPageResult(orderData);
	   			});
    			
    		}
    		
    		this.displayPrintPage = function (codTrans) {
    			_displayPrintPage(codTrans);
    		}
			
    		setup();
    		
    		return this;
		}
	});
	
})(jQuery); 