// ------------------------------------------------------------------------------------------------
// SHOP

function shop_index_goToShop() {
	document.location.href = "/shop" + SID("?");
}

function shop_index_updateOrderProcess() {
	$.get("/shop/index/order-process-panel" + SID("?"), { },
		function(data){
			$("#order-process-include").html(data);
		}, "html");
}

// ------------------------------------------------------------------------------------------------
// PRODUCTS

function shop_product_selectProductParam(nr, id) {
	$("#product-param-value-input-dyn-" + nr).html($("#product-param-value-input-src-" + nr + "-" + id).html());
}

function shop_product_showProductGroupItems(index) {
	var state = document.getElementById("product-menu-items-"  + index).style.display;
	var i = 0;
	while ((itemNode = document.getElementById("product-menu-items-"  + i)) != null) {
		if (i == index) {
			i++;
			continue;
		}
		itemNode.style.display = 'none';
		$("#product-menu-item-"  + i).removeClass("selected");
		i++;
	}
	itemNode = document.getElementById("product-menu-items-"  + index);
	if (state == 'block') {
		itemNode.style.display = 'none';
		$("#product-menu-item-" + index).removeClass("selected");
	} else {
		itemNode.style.display = 'block';
		$("#product-menu-item-" + index).addClass("selected");
	}
}

function updateDetailContent(ifCalcOnly) {
	var form = $("[name=calcForm]");
	if (!ifCalcOnly || document.calcForm) {
		url = "/shop/product/detail-content" + SID("?");
		$.post(url, form.serialize(),
			function(data){
				$("#product-detail-area").html(data);
			}, "html");
	}
}

// ------------------------------------------------------------------------------------------------
// CART

function shop_cart_reloadCart() {
	$.get("/shop/cart/cart-area" + SID("?"), { },
		function(data) {
			$("#cart-area").html(data);
		}, "html"
	);
}

function addItem() {
	var form = $("[name=calcForm]");
	if (document.calcForm) {
		$.post("/shop/cart/add-item-ajax" + SID("?"), form.serialize(),
		function(data){
			$("#cart-panel").html(data);
			$("#added-item-message").css("display", "block");
			$("#calc-form-area").css("display", "none");
		}, "html");
	}
}

function shop_cart_showItemAddedMessage() {
	$(function() {
		$("#cart-message-dialog").dialog({
			position: [309,150], width: 340, modal: true, autoOpen: true, //height: 140,
			open: function() {
				$(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
			},
			buttons: {
				"Zur Produktauswahl" : function() {
					$(this).dialog('close');
				},
				"Zum Warenkorb" : function() {
					document.location.href = "/shop/cart" + SID("?");;
				}
			}
		});
	});
}

function removeItem(item) {
	if (document.selectForm && item == document.selectForm.cartItemId.value) {
		$.post("/shop/cart/remove-selected-item-ajax" + SID("?"), { id: item },
			function(data){
				$("#cart-panel").html(data);
				shop_index_updateOrderProcess();
				if (document.calcForm) {
					document.calcForm.cartItemId.value = "";
				}
				updateDetailContent(false);
			}, "html");
	}
	else {
		$.post("/shop/cart/remove-item-ajax" + SID("?"), { item: item },
			function(data){
				$("#cart-panel").html(data);
				shop_index_updateOrderProcess();
				updateDetailContent(true);
			}, "html");
	}
}

function shop_cart_changeCartItem() {
	var f = document.calcForm;
	f.action = "/shop/cart/change-item";
	f.submit();
}

function shop_cart_cancelCartItem() {
	var f = document.calcForm;
	f.action = "/shop/cart/cancel-item-edit";
	f.submit();
}

function selectDeliveryType(type) {
	var url = "/shop/cart/select-delivery-type" + SID("?");
	$.post(url, { type: type},
		function(data){
			$("#cart-panel-summary").html(data);
		}, "html");

	if (type == 1) {
		$('#delivery2').removeClass('selected'); $('#delivery1').addClass('selected');
	}
	else {
		$('#delivery1').removeClass('selected'); $('#delivery2').addClass('selected');
	}
}


