$(document).ready(function()
{
	$(".image-popup").lightBox();
	$(".download a.download_add").removeAttr("href");
	$(".download a.download_add:not(.added)").click(function(){add_download_to_basket($(this).attr("id_download"));});
	$("#Images .image a.image_add").removeAttr("href");
	$("#Images .image a.image_add:not(.added)").click(function(){add_image_to_basket($(this).attr("id_image"));});
	update_basket();
	/*
	$.post("basket.php",
		{"do": "getBasketItems", "ajax": "1"},
		function(res)
		{
			$("#basket .body").html(res);
			if($("#basket .error").length)
				$("#basket .error").css({"display":"none"}).fadeIn("slow");
				
			update_basket();
			
			$("#basket .foot").removeAttr("href").click(function(){checkBasketContent();});
		}
	);
	*/
});
function update_basket()
{
	$("#basket .body .item .del").removeAttr("href").click(function(){delete_from_basket($(this).attr("id_line"),$(this).attr("type"),($(this).attr("type")=="download"?$(this).attr("id_download"):$(this).attr("id_image")));});
	if(!$("#basket .body .item").length)
		$("#basket .foot").hide();
	else
		$("#basket .foot").show();
}
function showError()
{
	var errorMsg = "Keine Artikel in der Download-Box vorhanden!";
	if(!$("#basket .error").length)
		$("#basket .body").append('<div class="error"></div>');
	$("#basket .error").css({display: "none"}).html(errorMsg).fadeIn("slow");
}
function add_download_to_basket(id)
{
	$.post("basket.php",
		{"do": "add_download", "id_download": id, "ajax": "1"},
		function(res)
		{
			$("#basket .body").html(res);
			if($("#basket .error").length)
				$("#basket .error").css({"display":"none"}).fadeIn("slow");
				
			update_basket();
			
			$(".download a[id_download="+id+"]").addClass("added").html("vorhanden").unbind("click");
		}
	);
}
function add_image_to_basket(id)
{
	$.post("basket.php",
		{"do": "add_image", "id_image": id, "ajax": "1"},
		function(res)
		{
			$("#basket .body").html(res);
			if($("#basket .error").length)
				$("#basket .error").css({"display":"none"}).fadeIn("slow");
			
			update_basket();
			
			$("#Images .image a[id_image="+id+"]").addClass("added").html("vorhanden").unbind("click");
		}
	);
}
function delete_from_basket(id_line,type,id_type)
{
	$.post("basket.php",
		{"do": "delete", "id": id_line, "ajax": "1"},
		function(res)
		{
			$("#basket .body").html(res);
			if($("#basket .error").length)
				$("#basket .error").css({"display":"none"}).fadeIn("slow");
			
			update_basket();
			
			$((type=="download"?".download":"#Images .image")+" a[id_"+type+"="+id_type+"]").removeClass("added").html("Pressemappe").click(function(){eval("add_"+type+"_to_basket(id_type)");});	
		}
	);
}
function checkBasketContent()
{
	if($("#basket .body .item").length>0)
		document.location.href = "basket.php";
	else
		showError(1);
}
