var lastImage = '';
var tmp = "";
var shipcost = 30;

$(document).ready( function() {
	//$(document).pngFix();
	$.ImageBox.init({ loaderSRC: 'templates/img/loading.gif', closeHTML: '<img src="templates/img/close.gif" />' });

	jQuery.each($(".plus"), function(e, obj) {
		$(obj).click(function (o) {
			var val = parseInt(this.parentNode.nextSibling.firstChild.firstChild.innerHTML);
			if(val >= 0) {
				this.parentNode.nextSibling.firstChild.firstChild.style.color = "#6D7078";
				tmp = this.parentNode.nextSibling.firstChild.firstChild;
				setTimeout("tmp.setAttribute('style', '')", 300);
				this.parentNode.nextSibling.firstChild.firstChild.innerHTML = val+1;
				this.parentNode.nextSibling.lastChild.value = val+1;
				var tmpValue = parseFloat(this.parentNode.parentNode.parentNode.lastChild.firstChild.childNodes[1].innerHTML) + parseFloat(this.getAttribute('rel'));
				this.parentNode.parentNode.parentNode.lastChild.firstChild.childNodes[1].innerHTML = tmpValue.toFixed(2);
			}
		});
	});
	jQuery.each($(".minus"), function(e, obj) {
		$(obj).click(function (o) {
			var val = parseInt(this.parentNode.nextSibling.firstChild.firstChild.innerHTML);
			if(val > parseInt(this.getAttribute('limit'))) {
				this.parentNode.nextSibling.firstChild.firstChild.style.color = "#6D7078";
				tmp = this.parentNode.nextSibling.firstChild.firstChild;
				setTimeout("tmp.setAttribute('style', '')", 300);
				this.parentNode.nextSibling.firstChild.firstChild.innerHTML = val-1;
				this.parentNode.nextSibling.lastChild.value = val-1;
				var tmpValues = parseFloat(this.parentNode.parentNode.parentNode.lastChild.firstChild.childNodes[1].innerHTML) - parseFloat(this.getAttribute('rel'));
				this.parentNode.parentNode.parentNode.lastChild.firstChild.childNodes[1].innerHTML = tmpValues.toFixed(2);
			}
		});
	});
	
	$("input[name='addressem']").focus( function(e) {
		if(!CheckEmail($("input[name='addressem']").val())) {
			$("input[name='addressem']").val("");
		}
	}).blur( function(e) {
		if(!CheckEmail($("input[name='addressem']").val())) {
			$("input[name='addressem']").val("wpisz swój e-mail");
		}
	});
	
	$("#newsadd").click( function(e) {
		if(!CheckEmail($("input[name='addressem']").val())) {
			alert("Wpisz poprawny adres email.");
		} else {
			$('.loader').css('display', 'block');
			$.ajax({
				type: "POST",
			   	url: "index.php",
			   	data: "action=newsadd&addressem="+$("input[name='addressem']").val(),
			   	success: function(data) {
			    	$('.newsletter').html(data);
			    	$('.loader').css('display', 'none');
			   	}
			});
		}
	});
	
	$("#newsdel").click( function(e) {
		if(!CheckEmail($("input[name='addressem']").val())) {
			alert("Wpisz poprawny adres email.");
		} else {
			$('.loader').css('display', 'block');
			$.ajax({
				type: "POST",
			   	url: "index.php",
			   	data: "action=newsdel&addressem="+$("input[name='addressem']").val(),
			   	success: function(data) {
					$('.newsletter').html(data);
			    	$('.loader').css('display', 'none');
			   	}
			});
		}
	});
	
	jQuery.each($("a[rel!='']"), function(e, obj) {
		$(obj).mouseover(function (o) {
			lastImage = this.firstChild.getAttribute("src");
			this.firstChild.setAttribute("src", "templates/img/menu/"+this.getAttribute('rel')+"_on.jpg");
		}).mouseout(function (o) {
			if(lastImage != '')
				this.firstChild.setAttribute("src", lastImage);
		});
	});
	
	if($('form[name="contactform"]')) {
		$('form[name="contactform"]').submit( function() {
			if($("input[name='fname']").val() == '') {
				alert("Wpisz imię.");
				return false;
			}
			if($("input[name='lname']").val() == '') {
				alert("Wpisz nazwisko.");
				return false;
			}
			if(!CheckEmail($("input[name='adem']").val())) {
				alert("Wpisz poprawny adres email.");
				return false;
			}
			if($("input[name='title']").val() == '') {
				alert("Wpisz tytuł wiadomości.");
				return false;
			}
			if($("textarea[name='tresc']").val() == '') {
				alert("Wpisz treść wiadomości.");
				return false;
			}
			
			return true;
		});
	}
	
	$(".catinput").keyup(function(e) {
		if(this.value != '') {
			this.value = parseInt(this.value, 10);
			if (isNaN(this.value)) {
				this.value = 1;
			}
			var newprice = parseInt(this.value) * parseFloat(this.getAttribute('rel'));
			$(this).parent().next().next().find('span').html(newprice.toFixed(2));
			
			var tmpname = this.name;
			var tmpvalue = this.value;
			$.ajax({
				type: "POST",
			   	url: "index.php",
			   	data: "action=updateCart&"+tmpname + "=" + tmpvalue,
			   	success: function(data) {
					
			   	}
			});
			
			recountCart();
		}
	});
});

function recountCart() {
	var price = 0;
	jQuery.each($(".catinput"), function(e, obj) {
		price += parseInt(this.value) * parseFloat(this.getAttribute('rel'));
	});
	$("#cartsum").find('span').find('b').html(price.toFixed(2));
	$("#allprice").html(price.toFixed(2));
	if(price >= 1200)
		var shc = 0;
	else
		var shc = shipcost;
	
	$("#shipcost").html(shc.toString());
	var tmpPrice = price + shc;
	$("#sumallprice").find('b').html(tmpPrice.toFixed(2));
}

function showProduct(th, id) {
	$(".blend").css("display", "block");
	$(".blendbox").css("display", "block");
	$(".blendbox1").html("");
	$.ajax({
		type: "POST",
	   	url: "index.php",
	   	data: "action=showProduct&pid=" + id,
	   	success: function(data) {
			var heigth = getPageSize();
			$(".blend").css("display", "block").css("height", heigth[1]+"px");
			$(".blendbox").css("marginTop", f_scrollTop() + 30 + "px");
	    	$(".blendbox1").css("width", "400px").html(data).css("display", "block");
	   	}
	});
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function zamknijPopup() {
	$(".blend").css("display", "none");
	$(".blendbox").css("display", "none").css("marginTop", "0px");
}

function getPageSize(){

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else 
		if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
	
	var windowWidth, windowHeight;
	
	//	console.log(self.innerWidth);
	//	console.log(document.documentElement.clientWidth);
	
	if (self.innerHeight) { // all except Explorer
		if (document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	}
	else 
		if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else 
			if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
	
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	}
	else {
		pageHeight = yScroll;
	}
	
	//	console.log("xScroll " + xScroll)
	//	console.log("windowWidth " + windowWidth)
	
	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth) {
		pageWidth = xScroll;
	}
	else {
		pageWidth = windowWidth;
	}
	//	console.log("pageWidth " + pageWidth)
	
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
	return arrayPageSize;
}

function checkOrderForm() {
	if(!CheckEmail($("input[name='adem']").val())) {
		alert("Wpisz poprawny adres email.");
		return false;
	}
	
	if($("input[name='fname']").val() == '') {
		alert("Wpisz imię.");
		return false;
	}
	
	if($("input[name='lname']").val() == '') {
		alert("Wpisz nazwisko.");
		return false;
	}
	
	if($("input[name='street']").val() == '') {
		alert("Podaj ulicę.");
		return false;
	}
	
	if($("input[name='number']").val() == '') {
		alert("Wpisz numer domu/mieszkania.");
		return false;
	}
	
	if($("input[name='postcode']").val() == '') {
		alert("Wpisz kod pocztowy.");
		return false;
	}
	if($("input[name='city']").val() == '') {
		alert("Wpisz miasto.");
		return false;
	}
	
	if($("input[name='phone']").val() == '') {
		alert("Wpisz telefon kontaktowy.");
		return false;
	}
	
	return true;
}

function CheckEmail(y) {
	if (y == "") { return false; }

	var x= new String(y);
	x.toLowerCase();
	var ret= new String();
	var znaki1= ".-+_@";
	var znaki= "0123456789zxcvbnmasdfghjklqwertyuiop" + znaki1;

	if ( x.length < 1 ) return false;
	if ( x.indexOf("@") == -1 ) { ok= 0; return false; }
	if ( x.indexOf("@") != x.lastIndexOf("@") ) { ok= 0; return false; }
	for ( i=0; i < x.length; ++i) 
		if ( znaki.indexOf(""+x.charAt(i)) == -1 ) { ok= 0; return false; }
	if ( znaki1.indexOf(x.charAt(x.length-1)) != -1 ) { ok= 0; return false; }

	return true;
}

function showPresentation(width, height, name) {
	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		var flash = AC_FL_RunContent(
			"codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0",
			"width", width,
			"height", height,
			"src", name,
			"quality", "best",
			"pluginspage", "http://www.macromedia.com/go/getflashplayer",
			"align", "top",
			"play", "true",
			"loop", "true",
			"scale", "noscale",
			"wmode", "opaque",
			"devicefont", "false",
			"id", name,
			"bgcolor", "#ffffff",
			"name", name,
			"menu", "true",
			"allowFullScreen", "false",
			"allowScriptAccess","sameDomain",
			"movie", name,
			"salign", ""
			); //end AC code
			
		$(".blend").css("display", "block");
		$(".blendbox").css("display", "block").css("marginTop", "237px");
		var heigth = getPageSize();
		$(".blend").css("display", "block").css("height", heigth[1]+"px");
		$(".blendbox1").css("width", "976px").html("<div style='width: 976px;'>"+flash+"<div class='sep'></div><a class='hand' onclick='zamknijPopup();'><img src='templates/img/close.jpg'/></a><div class='sep'></div></div>");
	}
}
