$(document).ready(function(){
	
	if($("#page.print").length > 0) {
		if(confirm("Přejete si vytisknout tuto stránku?")) {
			window.print();
		}
	}
	
	$("#vertnav a").removeAttr("title");
	$("#nav a").removeAttr("title");
	
	$(function() {
		$.pir.options.php="./_lib/pir.php";
		$(".sideBlock .title").pir({font: "arial.ttf", color: "#29406E", size: "9px"});
		$(".ixBlockLabel").pir({font: "arial.ttf", color: "#29406E", size: "11px"});
	});
	
	lightboxes();
	tabs();
	pgSignin();
	gRegister();
	cart();
	itemsList();
	photoLister();
});

this.lightboxes = function() {
	
	// lb musi byt <a rel="lightbox-galerie"..
	
	str = '';
	$('a[rel^=lightbox]').each(function(){
		uq = $(this).attr("rel").substr(9);
		if(str.indexOf("<" + uq + ">") == -1) {
			str += "<" + uq + ">";
		}
	});
	
	gals = str ? str.substr(1, str.length-2).split("><") : false;
	if(gals) {
		for(i = 0; i < gals.length; i++) {
			$("a[rel=lightbox-" + gals[i] + "]").lightBox({
				containerResizeSpeed: 400,
				txtImage: "Obrázek / Image :",
				txtOf: "/",
				imageLoading: "./img/lb-loading.gif",
				imageBtnClose: "./img/lb-btn-close.gif",
				imageBtnPrev: "./img/lb-btn-prev.gif",
				imageBtnNext: "./img/lb-btn-next.gif"
			});
		}
	}
}

this.tabs = function() {
	
	$(".tabs").each(function() {
		
		$(this).addClass("t-active");
		$(this).children("ul:first").each(function(){
			tnav = $(this);
			$(this).addClass("t-nav clear");
			
			// vypnout panely
			$(this).siblings().each(function(){
				$(this).addClass("t-panel").hide();
			});
			
			// projit odkazy
			$(this).children("li").each(function(){
				
				$(this).children("a").each(function(){				
					$(this).click(function(){
						
						// vypnout aktivni
						$(this).parent().siblings(".selected").removeClass("selected");
						$(this).parents(".tabs").children("div:not(:hidden)").hide();
						// zapnout vybrany
						$(this).parent().addClass("selected");
						$(this).parents(".tabs").children($(this).attr("href")).show();
						return false;
					});
				});
				

				if($(this).hasClass("selected")) {
					$(this).parents(".tabs").children($(this).children("a").attr("href")).show();
				}
			});
		});
	});

}

this.pgSignin = function() {

	if($("#blForgot:not(.err)").length > 0) {
		$("#blForgot:not(.err)").hide();
		$("#frmLogin").after('<a href="javascript:void(0)" id="blForgotLink" title="Zapoměli jste heslo?">Zapoměli jste heslo?</a>');
		$("#blForgotLink").click(function(){
				$(this).hide();
				$("#blForgot").fadeIn();
		});
	}
	
	var billing = ["ADDRESS", "ZIP", "CITY", "COUNTRY"];
	
	if($("#frmNoaccount #Ff_adrcopy").length) {
		gR_udpate_billing($("#Ff_adrcopy").attr("checked"));
		$("#frmNoaccount #Ff_adrcopy").change(function(){
			gR_udpate_billing($(this).attr("checked"), "click");
		});
	}
	
	function gR_udpate_billing(todo, type) {
		on = todo ? 1 : 0;
		for(i = 0; i < billing.length; i++) {
			if(todo) {
				$("#Ff_" + billing[i] + "_BILLING").attr("disabled","disabled")
					.parent().parent().hide();
			} else {
				$("#Ff_" + billing[i] + "_BILLING").removeAttr("disabled").parent().parent().fadeIn();
				if(type == "click") {
					$("#Ff_" + billing[i] + "_BILLING").val(billing[i] == "COUNTRY" ? "CZ" : "");
				}
			}
		}
	}	
}


this.gRegister = function() {

	var postal = ["ADDRESS", "ZIP", "CITY", "COUNTRY"];
	
	if($("#frmRegister #Ff_adrcopy").length) {
		gR_udpate_postal($("#Ff_adrcopy").attr("checked"));
		$("#frmRegister #Ff_adrcopy").change(function(){
			gR_udpate_postal($(this).attr("checked"), "click");
		});
	}
	
	function gR_udpate_postal(todo, type) {
		on = todo ? 1 : 0;
		for(i = 0; i < postal.length; i++) {
			if(todo) {
				$("#Ff_" + postal[i] + "_POSTAL").attr("disabled","disabled")
					.parent().parent().hide();
			} else {
				$("#Ff_" + postal[i] + "_POSTAL").removeAttr("disabled").parent().parent().fadeIn();
				if(type == "click") {
					$("#Ff_" + postal[i] + "_POSTAL").val(postal[i] == "COUNTRY" ? "CZ" : "");
				}
			}
		}
	}	
}

this.cart = function(){
	if($("#cartShowAll").length) {
		$("#cartShowAll").click(function(){
			$(".cartHide").removeClass("cartHide");
		});
	}
	
	if($(".crtTransport").length) {
		
		var vl = $(".crtTransport select option[value=" + $(".crtTransport select").val() + "]").attr("title");
		$("#crtTransport").text( vl ? vl : "zdarma" );
		$(".crtTransport select").change(function(){
			vl = $(".crtTransport select option[value=" + $(this).val() + "]").attr("title");
			$("#crtTransport").text( vl ? vl : "zdarma" );
			cart_update_final();
		});
	}
	
	if($(".crtPayment").length) {
		
		var vl = $(".crtPayment select option[value=" + $(".crtPayment select").val() + "]").attr("title");
		$("#crtPayment").text( vl ? vl : "zdarma" );
		$(".crtPayment select").change(function(){
			vl = $(".crtPayment select option[value=" + $(this).val() + "]").attr("title");
			$("#crtPayment").text( vl ? vl : "zdarma" );
			cart_update_final();
		});
	}	
	
	function cart_update_final() {
		var paym = parseInt($(".crtPayment select option[value=" + $(".crtPayment select").val() + "]").attr("title"));
		var trans = parseInt($(".crtTransport select option[value=" + $(".crtTransport select").val() + "]").attr("title"));
		var price = parseInt($("#crtPrice").val());
		
		$("#crtFinal").text( ( (isNaN(paym) ? 0 : paym) + (isNaN(trans) ? 0 : trans) + (isNaN(price) ? 0 : price) ) + ",- Kč");
	}
	
}

this.itemsList = function(){
	if($("#ilBtns").length) {
		$("#ilBtns a").click(function(){
			$(this).toggleClass("ilbtBox");
			$(this).toggleClass("ilbtRow");
			$("#ILList").toggleClass("ILBox");
			$("#ILList").toggleClass("ILRow");
		});
	}
}

this.photoLister = function(){
	if($(".itPhotoLister a.itImageThumb").length > 0) {
		$(".itPhotoLister a.itImageThumb").each(function(){
			$(this).hover(function(){
				$("#itPhotoMain img").attr("src", $(this).attr("href"));
			},function(){
				return false;
			});
		});
	}
}

