$(document).ready(function() {

	$(".order-item-quantity").change(quantityChangeHandler);
	
	$(".order-delete").live("click",function(event) {
		event.preventDefault();
		var rowId = "tr#" + $(this).attr("id");
		$.get($(this).attr("href"),function(){
			$(rowId).remove();
			refreshOrderPrice();
		});
	});

	$("#order-update").click(function(event) {
		event.preventDefault();
		for(var itemId in changedQuantities) {
			$.get("/service/cart/setitemquantity/newQuantity/" + changedQuantities[itemId] + "/item/" + itemId,function() {
				refreshOrder();
			});
		}
		changedQuantities = new Array();
	});
	
	$(".order").click(function(event) {
		$(".shopping-cart-empty").remove();
		event.preventDefault();
		$.get($(this).attr("href"),function(){
			refreshCartWidget();
		});
	});
	
	imagePreview();
});	

var changedQuantities = new Array();

function quantityChangeHandler(event) {
	changedQuantities[$(this).attr("id")] = $(this).val();
}

function refreshCartWidget() {
	$.getJSON("/service/cart/getitems/",function(data) {
		$(".shopping-cart-item").remove();
		$.each(data,function(i,item) {
			$("#shopping-cart-table").prepend (
					'<tr class="shopping-cart-item">' +
						'<td><h2>'+ item.quantity +' x </h2></td>' +
						'<td><h2>&nbsp;&nbsp;' + item.shortName + '</h2></td>' +
						'<td><h2 class="right">'+ item.quantity * item.price +' лв.</h2></td>' +
					'</tr>'
			);
		});
		if(data.length == 1) {
			$(".shopping-cart-empty").remove();
			$("#shopping-cart-table").append(
				'<tr>' +
					'<td colspan="3">' +
						'<h2>..............................................</h2>' +
					'</td>' +
				'</tr>' +
				'<tr>' +
					'<td colspan="3"><h2 id="shopping-cart-price" class="right">Сума : 0 лв.</h2></td>' +
				'</tr>' +
				'<tr>' +
					'<td>' +
					'</td>' +
				'</tr>' +
				'<tr>' +
					'<td>' +
					'</td>' +
				'</tr>' +
				'<tr>' +
					'<td colspan="3">' +
						'<a href="/order/" title="Към покупките">Към покупките</a>' +
					'</td>' +
				'</tr>'
			);
		}
		
		$.getJSON("/service/cart/getorderprice/",function(data) {
			if(data == null) {
				data = 0;
			}
			$("#shopping-cart-price").text("Сума : " + data + "лв.");
		});
	});
}

function refreshOrder() {
	$.getJSON("/service/cart/getitems/",function(data) {
		$(".order-item").remove();
		$.each(data,function(i,item) {
			var classIndex = i + 1;
			var cellClass = classIndex % 2 == 0 ? "dark-cell" : "light-cell";
			$("#order-head").after(
				'<tr align="center" class="' + cellClass + ' order-item" id="' + item.id + '">' +
                	'<td><input id="' + item.id +'" class="order-item-quantity" type="text" value="' + item.quantity + '" size="1"/></td>' +
                    '<td><h2>' + item.number + '</h2></td>' +
                    '<td><h2>' + item.name + '</h2></td>' +
                    '<td><h2>' + item.embroideryType + '</h2></td>' +
                    '<td><h2>' + item.size + ' см.</h2></td>' +
                    '<td><h2>' + (item.quantity * item.price) + ' лв.</h2></td>' +
                    '<td><a id="' + item.id + '" href="/service/cart/deleteitem/item/' + item.id + '" class="delete order-delete">Изтрий</a></td>' +
                '</tr>'
			);
		});
		$(".order-item-quantity").change(quantityChangeHandler);
		refreshOrderPrice();
	});
}

function refreshOrderPrice() {
	$.getJSON("/service/cart/getorderprice/",function(data) {
		if(data == null) {
			window.location = "/";
			data = 0;
		}
		$("#order-price").text(data + "лв.");
	});	
}

function hideCartWidget() {
	$("#cart-widget-wrapper").hide();
}