// übergibt die Id einer angeklickten Topkategorie
function setTopCategoryId(topCategoryId) {
	var response = $.ajax({
		type: "POST",
		url: '/php/pages/produkte_shop.php',
		async: false,
		data: "topCategoryId="+topCategoryId
	}).responseText;
	$('#content').html(response);
}

// übergibt Id der Topkategroie sowie der Unterkategorie die angeklickt wurde
function setCategoryIds(topCategoryId, subCategoryId) {
	var response = $.ajax({
		type: "POST",
		url: '/php/pages/produkte_shop.php',
		async: false,
		data: "topCategoryId="+topCategoryId+"&subCategoryId="+subCategoryId
	}).responseText;
	$('#content').html(response);
}

// übergibt die Id eines angeklickten Artikels und die Anzahl
function addArticleToSession(articleId, count) {
	if(count == undefined) {
		count = -1;
	}
	
	if(count == 0) {
		deleteArticleFromSession(articleId);
	} else {
		$.ajax({
			type: "POST",
			url: '/php/ajax/add_article_to_session.php',
			data: "articleId="+articleId+"&count="+count,
			success: function() {
				loadMiniShoppingCart();	
				if(count != -1) {
					loadShoppingCart();	
				}
			}
		})
	}
}

function deleteArticleFromSession(articleId) {
	$.ajax({
		type: "POST",
		url: '/php/ajax/delete_article_from_session.php',
		data: "articleId="+articleId,
		success: function() {
			loadMiniShoppingCart();
			loadShoppingCart();
		}
	})
}

function deleteShoppingCart() {
	$.ajax({
		type: "POST",
		url: '/php/ajax/delete_shopping_cart.php',
		success: function() {
			loadShopMenu();
			loadMiniShoppingCart();
			loadShoppingCart();
		}
	})
}

function loadMiniShoppingCart() {
	$.ajax({
		type: "POST",
		url: "/php/ajax/mini_shopping_cart.php",
		success: function(data) {
			$("#littleShoppingCart").html(data);	
		}
	});	
}

function loadShopMenu(categoryId) {
	if(categoryId == undefined) categoryId = 0;

	$.ajax({
		type: "POST",
		url: "/php/ajax/shop_menu.php",
		data: "categoryId="+categoryId,
		success: function(data) {
			$("#shopMenu").html(data);	
		}
	});			
}

function loadArticles(categoryId) {
	$.ajax({
		type: "POST",
		url: "/php/ajax/articles.php",
		data: "categoryId="+categoryId,
		success: function(data) {
			$("#articles").html(data);	
		}
	});
}

function loadShoppingCart() {
	$.ajax({
		type: "POST",
		url: "/php/ajax/shopping_cart.php",
		success: function(data) {
			loadShopMenu();
			$("#articles").html(data);	
		}
	});		
}

function loadCashpoint() {
	$.ajax({
		type: "POST",
		url: "/php/ajax/cashpoint.php",
		success: function(data) {
			loadShopMenu();
			$("#articles").html(data);	
		}
	});
}

/* wird nicht mehr benutzt */
function makeOrder() {
	$("#cashpoint").ajaxSubmit({
		success: function(data) {
			loadMiniShoppingCart();
			$("#articles").html(data);
		}
	});
}

function showSingleArticle(articleId, articleTitle) {
	$.ajax({
		type: "POST",
		url: "/php/ajax/show_single_article.php",
		data: "articleId="+articleId,
		success: function(data) {
			$("#articles").html(data);	
            $(".article-image").click(function() {
            	// vom Bildfpad ab dem letzten / den String nehmen
            	var oldFileName = $(this).attr("src").substr($(this).attr("src").lastIndexOf("/")+1);
            	// die letzten Zeichen ab dem Punkt nehmen (-> .gif, .jpg oder .png)
            	fileType = oldFileName.substr(oldFileName.lastIndexOf("."));
            	// Bildname zusammensetzen -> z.B. big_1.jpg
            	var fileName = "big_" +articleId+fileType;
            	$("#bigImage").attr("src", "/media/images/articles/" + fileName);
                $("#showArticleImages").overlay({      				 
				    // disable this for modal dialog-type of overlays 
				    closeOnClick: false, 
				    // we want to use the programming API 
				    api: true 
				// load it immediately after the construction 
				}).load();
				$("#overlayText").html("<br/><br><strong style='font-size:12px;'>" + articleTitle + "</strong>");
          	});
		}
	});
}
