$(document).ready(function() {
	$("#popupContactClose").click(function(){
	disablePopup();
	});
	$("#backgroundPopup").click(function(){
	disablePopup();
	});
	$(".remove_btn").click(function(){
		var id = $(this).val();
		var money = $("#totalcalc_"+id).val();
		var total_pred = $("#total_pred").html();
		var total  = $("#total_konech").html();
		
		total_pred = total_pred.split(" ");
		total = total.split(" ");
		
		var total_pred1 = parseFloat(parseFloat(total_pred[0]) - parseFloat(money));
		var total1 = parseFloat(parseFloat(total[0]) - parseFloat(money));
		$("#total_pred").html(total_pred1+" &euro;");
		$("#total_konech").html(total1+" &euro;");
		$("#price_"+id).remove();
		$("#totalcalc_"+id).remove();
		$(".qty").each(function(i){
			var pre_id = $(this).attr('name');
			var p_id = parseInt(pre_id.replace('qty_',''));
			if (p_id == id)
				$(this).attr('value',0);
		});
		$("#tr_"+id).fadeOut('slow');
	});
	$(".qty").keypress(function(e){
		if (e.which >= 48 && e.which <= 57 || e.which == 8 || e.which == 0)
		{
			if (e.which >= 48 && e.which <= 57)
				var cnt = parseInt($(this).val()+String.fromCharCode(e.which));
			if (e.which == 8)
			{
				var val = $(this).val();
				var pre_cnt = val.substr(0,val.length-1);
				if (pre_cnt != '')
				{
					var cnt = parseInt(pre_cnt);
				}
				else
				{
					var cnt = 0;
				}
			}
			//recalculation
			var myname = $(this).attr('name');
			var myid = myname.replace('qty_','');
			var ppp  = $("#price_"+myid).attr('value');
			var total  = cnt*ppp;
			var totalsumm = total+" &euro;";
			$("#totalcalc_"+myid).attr('value',totalsumm);
			$("#totalsum_"+myid).html(totalsumm);
			
			var totpred = 0;
			$(".totalcalcs").each(function(i){
				totpred += parseFloat($(this).attr('value'));
			});
			var totalkonech = totpred+shipping_price;
			$("#total_pred").html(totpred+" &euro;");
			$("#total_konech").html(totalkonech+" &euro;");
			//
			if (cnt != 0)
			{
				$(this).attr('value',cnt);
				return false;
			}
			else
			{
				$(this).attr('value','0');
				return false;
			}
		}
		else
			return false;
 	});
	$("#back_btn").click(function (){
		window.location = '/?mod=basket&action=contact';
	});
	$("#next_btn").click(function (){
		var i = 0;
		var submit_array = new Object();
		
		$(".qty").each(function(i){
			var pre_id = $(this).attr('name');
			var id = pre_id.replace('qty_','');
			var count = $(this).attr('value');
			var ind = "prod_"+i;
			submit_array[ind] = id+"_"+count;
			i = i + 1;
		});
		var sub_js = $.toJSON(submit_array);
	    //alert(submit_array);
		//alert('aaaaaaaa');
		$.getJSON("/?mod=basket&action=setCounts", submit_array ,function(data){
										
			if (data.error == 1)
			{
				alert(data.errorValue);
			}
			else if (data.add_gift == 1)
			{
				window.location = '/?mod=category&action=showCategory&categoryId=87';
			}
			else if (data.go_away == 1)
			{
				window.location = '/';
			}
			else
			{
				window.location = '/?mod=basket&action=contact';
			}
		});
	});
	$("#buy_btn").click(function (){
			if (isPressed == 0)
			{
				isPressed=1;
				$("#buy_btn").attr('src','/images/loading.gif');
				
				$.getJSON("/?mod=basket&action=add", {artId: $("#article_id").attr('value')} ,function(data)
					{
						if (data.error == 1)
						{
							$("#popupContainer").html(data.errorValue);
						}
						else if (data.free_gift == 1)
						{
							window.location = '/?mod=basket&action=contact';
						}
						else
						{
							$("#popupContainer").html(data.successValue);
						}
						$("#buy_btn").attr('src','/img/btn-cart.png');
						if (data.free_gift != 1)
						{
							centerPopup();
							loadPopup();
							isPressed=0;
						
							// basket count
							$.getJSON("/?mod=basket&action=summCount", {id:0} ,function(data){
									$("#basket_a").html(data.count);
								});
						}
					});
			}
		});
});
var isPressed = 0;
var popupStatus = 0;
var shipping_price = 10;
//json functions

// when values on count changed
function recalculate()
{
	
}

//loading popup with jQuery magic!
function loadPopup(){
//loads popup only if it is disabled
	if(popupStatus==0)
	{
		$("#backgroundPopup").css({
			"opacity": "0.7"
			});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$("#backgroundPopup").fadeOut("slow");
$("#popupContact").fadeOut("slow");
popupStatus = 0;
}
}


//centering popup
function centerPopup(){
//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
	"position": "absolute",
	"top": (windowHeight-popupHeight/2)-100,
	"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
	"height": windowHeight
	});
}