$(document).ready(function() {	
$(".added").hide();

// hash halløj inde på butikssiden
function setHash(id){
	$(id).fancybox({
		'scrolling': 'false'
	});
	$(id).click(function() {
		var id = $(this).attr('id');
		document.location.hash = "produkt"+id;
	});
}

setHash("a.moreInfo");
//setHash("a.productLeft");
//setHash("a.productRight");

if(document.location.hash){ //hvis der er hash
	s = document.location.hash.replace("produkt","");
	$(s).fancybox().trigger('click');	
}
// fiks jquery glaleri halløj, så når man trykker på id så emulerer denet klik på billedet, så der ikke kommer tre af det samme slide
$("a.productLeft").click(function() {
	var id = $(this).attr('id');
	id = id.replace("prod","");
	$("#"+id).fancybox().trigger('click');
	document.location.hash = "produkt"+id;
});

$("a.productRight").click(function() {
	var id = $(this).attr('id');
	id = id.replace("prod","");
	$("#"+id).fancybox().trigger('click');
	document.location.hash = "produkt"+id;
});
// fancybox
$(".fancy").fancybox({
	'cyclic': 'true',
	'index': '2'
});
$("a#fancy").fancybox();
$("a#terms").fancybox();
$("a.moreInfo").fancybox();
$("a.mediaLink").fancybox();
$("a.factoryImg").fancybox();



}); // document.ready end


function get_cookie ( cookie_name ){
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	if ( results )
		return ( unescape ( results[2] ) );
	else
		return null;
}
var lang = get_cookie("kendamadk_lang");
var currency = get_cookie("kendamadk_currency");

var shipping;

if(!shipping){
	shipping = 1;
}
function plusProduct(id, currency) {
	addBasket(id, currency);
	showBasketDetail(currency, shipping);
}
function minusProduct(id) {
	deleteBasket(id, currency);
}

function deleteBasket(id, currency){
	$(document).ready(function()
	{
		$.ajax({
			data: {
				cmd: "deleteBasket",
				id: id,
				currency: currency
			},
			type: "POST",
			url: "inc/basket.php?cmd=deleteBasket&id="+id,
			timeout: 20000,
			dataType: "html",
			contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			complete: function(data){ 
				//$("#debug").html(data);
				
				showBasketDetail(currency, shipping);
				showBasket(currency);
			}
		});
	});
}
var notAvai = new Array();
notAvai[0] = "Ikke flere på lager";
notAvai[1] = "Not anymore in stock";

function checkStock(id){
	$.get("inc/basket.php", { cmd: "checkStock", id: id },
		function(data){
			if(data <= "0"){
				//alert("no more!"+data);
				$("#addBasketId"+id).html("<span class=\"white\">"+notAvai[lang]+"</span>");
				$("#addBasketViewer"+id).html("<span class=\"white\">"+notAvai[lang]+"</span>");
				
			} else {
				//alert(data);
			}
			
			return data;
		});
}
function addBasket(id, currency){
	$(document).ready(function()
	{

		$.ajax({
			data: {
				cmd: "addBasket",
				id: id,
				currency: currency
			},
			type: "POST",
			url: "inc/basket.php?cmd=addBasket&id="+id,
			timeout: 20000,
			dataType: "html",
			contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			complete: function(data){ 
				
			}, 
			success: function(data){
				
				showBasket(currency);
				$("#product"+id).hide();
				$("#product"+id).fadeIn();
				$("#added"+id).fadeIn("added");
				$("#addedToBasket").fadeIn();
				checkStock(id);
			}
		});
	});
}
function emptyBasket(){
$(document).ready(function()
	{
		$.ajax({
			type: "GET",
			url: "inc/basket.php?cmd=emptyBasket",
			dataType: "html",
			contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			success: function(data){ 
			$(".added").hide();
				$("#basket").hide();
				$("#basket").html(data);
				$("#basket").fadeIn();
				showBasketDetail(currency, shipping);
				
			}
		});
	});
	
}

function showBasket(currency){
$(document).ready(function()
	{
		$("#basket").load("inc/basket.php?cmd=showBasket&currency="+currency).hide().fadeIn();
	});
		
}
function showBasketDetail(currency, shipping){
$(document).ready(function()
	{
	$("p.basketDetail").load("inc/basket.php?cmd=showBasketDetail&currency="+currency+"&shipping="+shipping).hide().fadeIn();
		/*$.ajax({
			type: "GET",
			url: "inc/basket.php?cmd=showBasketDetail&currency="+currency+"&shipping="+shipping,
			dataType: "html",
			contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			success: function(data) {
				$("p.basketDetail").html(data);
			}
		});*/
	});
		
}

