$(document).ready (function () {
	var root = "http://www.kimsiderius.nl/";
	$(".btnShop").click(function () {
		var idProd = $(this).parent().attr('id').replace("prod_","");
		$.post(root+"/aj_scripts/save_carts.php", {id:idProd}, function (data) {
			$("#itemsInCart span").text(data);
			$("#cartInfo").slideDown("slow");
			setTimeout(hideCartMess,5000);
			$("#cartInfo a").click(function () {
				hideCartMess ();
			});
		});
	});
	
	function hideCartMess () {
		$("#cartInfo").slideUp("slow");
	}
	
	$.get(root+"/aj_scripts/save_carts.php", function (data) {
		$("#itemsInCart span").text(data);
	});

	var changeImg = String($("#big").attr("src")).replace("/medium/","/large/");

	var percWidth = 0;
	var percHeight = 0;
	var width = setWidth ($("#big"));
	var height = setHeight ($("#big"));
	var zoomImgUrl = "";


	$("#big").attr("width", width);
	$("#big").attr("height", height);
	$("#big").attr("src", changeImg);
	$("#big").wrap('<div id="bigImg"></div>');
	$("#big").parent().css({"width" : width+"px", "height" : height+"px", "position": "relative"});
	$("#big").wrap('<a id="'+$("#big").attr('id')+ 'btn'+'" href="'+$("#big").attr('src')+'"></a> ');
	$("#big").parent().parent().append('<span></span>')
	$("#bigbtn").zoomple();
	asignZoom ();
		
	$(".leftProds img").click(function () {
		var smallDim = {height:setNormHeight($("#big")), width:setNormWidth($("#big"))};
		var thisDim = {  width:setWidth($(this)), height:setHeight($(this))};
		var thisSource = $(this).attr('src').replace('/medium/', '/large/');
		var bigSource = $("#big").attr('src').replace('/large/', '/medium/');
		
		$(this).attr('src', bigSource);
		$(this).width(smallDim.width);
		$(this).height(smallDim.height);
		
		
		
		$("#big").attr('src', thisSource);
		$("#big").width(thisDim.width);
		$("#big").height(thisDim.height);
		asignZoom ();
		
		
		reposElem ($(this));
		
	});

	function asignZoom () {
		$("#big").parent().attr('href', $("#big").attr('src'));
		zoomImgUrl = $("#big").attr('src');
		$("#big").imageZoom();
		$("#bigbtn").css({'display':'block'});

	}
	
	function startZooming () {
		$("div#bigImg").zooma('bigImg', 'zoomBlock', zoomImgUrl).activate();
	}
	
	function setWidth (object) {
		width =  270;
		percWidth = 270 / object.width();
		return width;
	}
	function setHeight (object) {
		height = object.height() * percWidth;
		return height;
	}
	function setNormWidth (object) {
		width = object.width() * percHeight;
		return width;
	}
	function setNormHeight (object) {
		height =  120;
		percHeight = 120 / object.height();
		return height;
	}
	function reposElem (object) {
		object.css({"position":"absolute", "left":"50%", "top":"50%", "marginTop": -(object.height() / 2)+"px","marginLeft": -(object.width() / 2)+"px"});
	}
	
	
	//-----------Cart 
	$("#persGegevens").hide();
	$("#factTempl a").click(function (){
		var id  = $(this).attr('id').replace("d", "");
		$.post(root+"/aj_scripts/rem_prod.php", {id:id}, function (data){
			alert("Produkt is verwijdert/ Product is deleted");
			window.location.reload();
		}
		);
	});

	$("#persGegevens").validateform({
		onComplete:function () {
			sendForm();
		}
	});
	$("#persGegevensAdded").validateform({
		btn:"#sendFormChange",
		onComplete:function () {
			sendFormChange();
		}
	});
	
	function sendForm () {
		var postData = new Object();
		$("#formGeg input, #formGeg select").each(function () {
			postData[$(this).attr('name')] = $(this).val();
		});
		$.post(root+'aj_scripts/save_det.php', postData, function (data){
			console.log(data);
			if (data.length > 5) {
				alert("Sorry! het email adres bestaat al kies een ander / Sorry! Your email address already exists, please choose another");
			}else{
				alert("Bedankt uw gegevens zijn bewaard/ Thank you! your details hase been saved.");
				$("#persGegevens").fadeOut("slow", function () {
					$("#payLink").fadeIn("slow");
				});
			}
			
		});
	}
	function sendFormChange () {
		var postData = new Object();
		$("#formGeg input, #formGeg select").each(function () {
			postData[$(this).attr('name')] = $(this).val();
		});
		$.post(root+'aj_scripts/change_det.php', postData, function (){
			$('#formGeg').fadeOut('slow', function (){
					$('#formGegMessage').fadeIn("slow");
			});
		});
	}
	$("#loginBtn").click (function (){
		$('#formLog').submit();
	});
	$("#messageLog a").click(function (){
		$(this).parent().fadeOut("slow");
		$("#formLog").fadeOut('slow', function (){
			$("#messaForgot").fadeIn("slow");
		});
	});
	$("#frAddLogin a").click(function () {
		$("#loginHolder, #notLogged").fadeOut('slow', function() {
			$("#persGegevens").fadeIn("slow");
		});
	});
	$("#messaForgot a").click(function (){
	
		$.post(root+"/aj_scripts/send_pass.php", {account:$("#accountFieldForgot").val()}, 
			function (data){
				$("#messPass").html(data);
				$("#logAgain").click(function (){
					$("#messPass, #messaForgot").fadeOut('slow', function() {
					$("#formLog").fadeIn("slow");
					});
				});
			}
		);
	});
	$('.kado input').click(function (){
		if ($(this).is(':checked')) {
			$.post(root+"/aj_scripts/kado_optie_on.php", {on:'ja'}, function (data) {});
		}else{
			$.post(root+"/aj_scripts/kado_optie_on.php", {on:'of'}, function (data) {});
		}
	
	});
	
	$(".buttonPay").hide();
	$("#paypalBtn").click(function () {
		$("#paypal").submit();
	});
	$("#uitloggenBtn").click(function (){
		$.post(root+"/aj_scripts/sign_out.php", {}, function (){
			alert("Je bent nu uitgelogd, you are now logged out");
			window.location = "|INLOGGEN|";
		});
	});
	
});
