$(document).ready(function(){
	extCss.apply();
	Forms.click();
	IndexTab.init();
	Cart.init();
	OrderForm.init();
	SpaFilter.init();
	ExtremeFilter.init();
	Certificate.init();
		
	$("#show_video").colorbox({
		href: "#video_src",
		inline: true,
		onOpen:function(){ 
			var img = $("<img></img>");
			img.attr("src","http://www.googleadservices.com/pagead/conversion/1039091337/?label=jqPYCN3U6AEQiY297wM&amp;guid=ON&amp;script=0"); 
		}
	});
	
	$("#show_map").colorbox({
		href: "#mapa_src",
		inline: true
	});
	
	$("#menu-tab td.selected a").css("backgroundPosition","bottom");
	
	$("a.mn-item").bind("mouseenter",function(){
		$(this).css("backgroundPosition","bottom");
	});
	$("a.mn-item").bind("mouseleave",function(){
		if(!$(this).parent().hasClass("selected")){
			$(this).css("backgroundPosition","top");	
		}
	});
	
	if($("#gtec").length==1){
		$("#gtec").bind("click",function(){
			$.post("/default/cart/vbb3", {"a":null}, function(ret){
				$("#vbb2c").html(ret);
				$("form#ecard").submit();
			});
			
		});
	}
	
	$("#magnifier").colorbox();
	$(".psh").colorbox();
});

var Newsletter = {
	add : function(id){
		$("#s_mail").val($("#"+id).val());
		$("#nl-form").submit();
	}
};

var SpaFilter = {
	locId : 0,
	visibleNo : 0,
	init : function(){
		if($("#spa-loc").length == 1){
			$("#spa-loc").bind("change",function(){
				SpaFilter.locId = $("#spa-loc").val(); 
				if(SpaFilter.locId == "all"){
					$(".sub-list").show();
				}else{
					show = false;
					SpaFilter.visibleNo = $(".sub-list:visible").length; 
					if($(".sub-list:visible").length==0){
						SpaFilter.show();
					}else{
						$(".sub-list:visible").slideUp(200,SpaFilter.endd);
					}
				}
			});
			
			$("#spa-loc").val(7);
			SpaFilter.locId = $("#spa-loc").val(); 
			show = false;
			SpaFilter.visibleNo = $(".sub-list:visible").length; 
			$(".sub-list:visible").slideUp(200,SpaFilter.endd);
		}
	},
	endd : function(){
		SpaFilter.visibleNo --;
		if(SpaFilter.visibleNo == 0){
			SpaFilter.show();
		}
	},
	show : function(){
		$(".loc-"+SpaFilter.locId).slideDown("slow");
		//$(".loc-"+SpaFilter.locId).fadeIn();
	}
};


var cboxClose = function(){
	$.colorbox.close();
	return false;
}

var ExtremeFilter = {
		locId : 0,
		etId : 0,
		visibleNo : 0,
		init : function(){
			if($("#extreme-loc").length == 1){
				$("#extreme-loc").bind("change",ExtremeFilter.change);
				$("#extreme-etype").bind("change",ExtremeFilter.change);
				ExtremeFilter.show();
				
				ExtremeFilter.change();
			}
			
		},
		change : function(){
			$("#empty-res").hide();
			$(".sub-list").removeClass("ext-show");
			if($(".sub-list:visible").length == 0){
				ExtremeFilter.show();
			}else{
				ExtremeFilter.visibleNo = $(".sub-list:visible").length; 
				$(".sub-list:visible").slideUp(200,ExtremeFilter.endd);
			}
		},
		endd : function(){
			ExtremeFilter.visibleNo --;
			if(ExtremeFilter.visibleNo == 0){
				ExtremeFilter.show();
			}
		},
		show : function(){
			$(".sub-list").each(function(){
				if($("#extreme-loc").val() == "all"){
					$(this).addClass("ext-show");
				}else{
					if($(this).hasClass("loc-"+$("#extreme-loc").val()) && $(this).hasClass("et-"+$("#extreme-etype").val())){
						$(this).addClass("ext-show");
					}
				}
				
			});
			if($(".ext-show").length>0){
				$(".ext-show").slideDown("slow");
			}else{
				$("#empty-res").show();
			}
			//$(".loc-"+SpaFilter.locId).fadeIn();
		}
	};

var extCss = {
	color:'',
	apply: function(){
		$("div#category-menu-box a").each(function(){
			if ($(this).hasClass("selected")) {
				$(this).css("backgroundColor", extCss.color);
				$(".idh4").css("color",extCss.color);
			}
		});
	}
};

var gateOK = function(){
	$("#gate-msg").fadeOut(function(){
		$("#gate-box").fadeIn();
	});
	return false;
};

var Forms = {
	click : function(){
		$("a.ui-button").bind("click",Forms.send);
		$("a#gts2").bind("click",Forms.send);
		$("a#finish-order").bind("click",Forms.send);
	},
	send : function(event){
		var formId = $(this).attr("rel");
		switch (formId){
			case 'gts2':
				OrderForm.gotoStep2();	
			break;
			case 'odf':
				OrderForm.confirmOrder();	
			break;

			default:
				$("#"+formId).submit();
			break;
		}
		return false;
	}
};

var Filter  = {
	apply : function(){
		$("#fl-form").submit();
		return false;
	}
};

var OrderForm = {
	bc : null,
	init : function(){
		if($("#adrdess-form").length ==1){
			$("#is_company").bind("click",function(){
				if($(this).attr('checked')==true){
					$("#is-company-box").slideDown();
				}else{
					$("#is-company-box").slideUp();
				}
			});
			if($("#dlvd").attr('checked')==true){
				$(".dlvd").attr("disabled","");
				$(".dlvd").css("backgroundColor","#fff");
			}else{
				$(".dlvd").attr("disabled","true");
				$(".dlvd").css("backgroundColor","#e3e3e3");
			}
			$("#dlvd").bind("click",function(){
				if($(this).attr('checked')==true){
					$(".dlvd").attr("disabled","");
					$(".dlvd").css("backgroundColor","#fff");
				}else{
					$(".dlvd").attr("disabled","true");
					$(".dlvd").css("backgroundColor","#e3e3e3");
				}
			});
			$(".pd-box .dlv_id").bind("click",function(){
				OrderForm.setDeliveryPriceId($(this).val());
			});
			$(".pd-box .payment_type").bind("click",function(){
				OrderForm.setPaymentType($(this).val());
			});
		}
	},
	setPaymentType : function(val){
		$.post("/default/cart/set-payment-type", {"payment":val}, function(ret){
		});
	},
	setDeliveryPriceId : function(val){
		$.post("/default/cart/set-dlv-price-id", {"dlvid":val}, function(ret){
		});
	},
	gotoStep2 : function(){
		var orderAddressData = $("#adrdess-form").serializeArray();
		$.post("/default/cart/create-order", {"odata":orderAddressData}, function(ret){
			if (ret ==  1) {
				$("#step1").fadeOut("slow", function(){
					$.post("/default/cart/vbb2", {"a":null}, function(ret){
						$("#vbb2c").html(ret);
					});
					$("#cart-path span").removeClass("selected");
					$("#cart-path span").eq(2).addClass("selected");
					OrderForm.gotoStep1();
					$("#step2").fadeIn();
				});
			}
			else {
				$.colorbox({width:"300", inline:true, href:"#error_msg"});
				$("#is-company-box label").css("color","#4D4D4D");
				$("#address-form label").css("color","#4D4D4D");
				$("#is-company-box label span").css("color","#4D4D4D");
				$(".check_bx").css("color","#ADADAD");
				
				var obj = jQuery.parseJSON(ret);
				
				for(x in obj){
					
					var index = $("#"+x).index();
					var parent = $("#"+x).parent();
					switch(x){
						case 'invoice_zip_code':
							parent.children().eq(index-1).children().eq(0).css("color","red");
						break;
						case 'invoice_city':
							parent.children().eq(index-2).children().eq(1).css("color","red");
						break;
						case 'personal_data_check':
						case 'information_check':
						case 'regulation_check':
							parent.children().eq(index+1).css("color","red");
						break;
						default:
							parent.children().eq(index-1).css("color","red");
						break;
					}
					
					//$("#"+x).css("border","1px solid red");
				}
			}
			
			return false;
		});
	},
	gotoStep1 : function(){
		$("#gts1").bind("click",function(){
			$("#step2").fadeOut("slow",function(){
				$.post("/default/cart/vbb1", {"a":null}, function(ret){
					$("#vbb2c").html(ret);
				});
				$("#cart-path span").removeClass("selected");
				$("#cart-path span").eq(1).addClass("selected");
				$("#step1").fadeIn();
			});
		});
	},
	confirmOrder : function(){
		if($(".payment_type:checked").length == 1 && $(".dlv_id:checked").length == 1){
			$("#delivery_type_id").val($(".dlv_id:checked").val());
			$("#payment-form").submit();
		}else{
			$.colorbox({width:"300", inline:true, href:"#error_msg1"});
		}
	}
};

var Cart = {
	tOut : 0,
	milliseconds : 1000,
	cid : 0,
	code : "",
	crVal : 0,
	init : function(){
		if($("#main-cart-tab").length==1){
			$.post("/default/cart/cart-tab", {'null':null}, function(ret){
				$("#main-cart-tab").html(ret);
				Cart.bindChangeVol();	
				Cart.bindEnterCode();		
			});
		}
	},
	isInt : function(x){
   		var y=parseInt(x);
   		if (isNaN(y)){
			return false;
		} 
   		return x==y && x.toString()==y.toString();
 	} ,
	bindEnterCode : function(){
		$(".code").focusin(function(){
			$(this).select();
		});
		$(".code").keyup(function(){
			Cart.cid = $(this).attr("id").replace(/c/,"");
			Cart.checkCode();
		});
	},
	bindChangeVol : function(){
		$(".cart-vol").focusin(function(){
			Cart.crVal = $(this).val();
			$(this).select();
		});
		$(".cart-vol").keyup(function(){
			Cart.cid = $(this).attr("id").replace(/i/,"");
			clearTimeout(Cart.tOut);
			Cart.tOut = setTimeout(Cart.chVolNew,Cart.milliseconds);
		});
	},
	checkCode : function(){
		var code = $("#c"+Cart.cid).val();
		if (code.length == 13) {
			$("#c"+Cart.cid).attr("disabled","disabled");
			$("#cc"+Cart.cid).fadeIn();
			
			
			$.post("/default/cart/check-code", {'product_id':Cart.cid,'code':code}, function(ret){
				if(ret==0){
					$("#cc"+Cart.cid).hide();
					$("#cerror"+Cart.cid).fadeIn();
				}else{
					
					$.post("/default/cart/cart-tab", {'null':null}, function(ret){
						$("#main-cart-tab").html(ret);
						Cart.bindChangeVol();	
						Cart.bindEnterCode();		
					});
					
				}
			});
		}
	},
	chVolNew : function(){
		var newVal = $("#i"+Cart.cid).val();
		if (Cart.isInt(newVal) && newVal.length >0) {
			if (Cart.crVal != newVal) {
				$(".cart-vol").attr("disabled", "disabled");
				var ile = newVal - Cart.crVal;
				Cart.chVol(Cart.cid, ile);
			}
		}else{
			$("#i"+Cart.cid).val(Cart.crVal);
			alert('takie rzeczy tylkow Erze');
		}
	},
	add : function(productId){
		$.post("/default/cart/add-to-cart", {'product_id':productId}, function(ret){
			$.post("/default/cart/get-total", {'n':null}, function(ret){
				$("#cart-top").html(ret);
				if($("#cart-big").length == 1){
					$.post("/default/cart/cart-big", {'n':null}, function(ret){
						$("#cart-big").replaceWith(ret);
					});
				}
				return false;
			});
		});
		return false;
	},
	addRedirect : function(productId){
		$("#cat_product_id").val(productId);
		$("#atc_form").submit();
		
		return false;
	},
	removeAll : function(){
		$.post("/default/cart/clear-cart", {'null':null}, function(ret){
			$("#main-cart-tab").html(ret);
			Cart.bindChangeVol();
			$(".cart-vol").attr("disabled","");
			$.post("/default/cart/get-total", {'null':null}, function(ret){
				$("#cart-top").html(ret);
			});
		});
		return false;
	},
	chVol : function(productId,vol){
		$.post("/default/cart/change-vol", {'product_id':productId,'vol':vol}, function(ret){
			$("#main-cart-tab").html(ret);
			Cart.bindChangeVol();
			$.post("/default/cart/get-total", {'n':null}, function(ret){
				$("#cart-top").html(ret);
				return false;
			});
		});
		return false;
	},
	removeItem : function(productId,open){
		$.post("/default/cart/remove-item", {'product_id':productId}, function(ret){
			$("#main-cart-tab").html(ret);
			$.post("/default/cart/get-total", {'null':null}, function(ret){
				$("#cart-top").html(ret);
				if(open==true){
					$("#iframe").show();
					$("#cart-details").show();
				}
			});
		});
		
		return false;
	},
	toggleDetails : function(){
		if ($("#cart-details").css("display") == "block") {
			$("#cart-details").fadeOut(); 
		}
		else 
			if ($("#cart-details").css("display") == "none") {
				if($.browser.msie != true){
					$("#iframe").show();
				}
				$("#cart-details").fadeIn();
		}
		
		return false;
	}
};

var IndexTab = {
	tabsHide: 0,
	init : function(){
		if($("#tabs").length == 1){
			IndexTab.bindClick();
			$("div#tabs a:first").click();
		}
	},
	bindClick : function(){
		$("div#tabs a").bind("click",function(){
			if(!$(this).hasClass("open") && $("div#tabs a:animated").length == 0){
				IndexTab.tabsHide = 0;
				var id = $("#tabs").children("div").index($(this).parent());
				//console.log(id);
				$("div.tab").fadeOut('250',function(){
					IndexTab.tabsHide ++;
					var ts = IndexTab.tabsHide - $(".tab").length;
					if(ts==0){
						$("#visual div.tab").eq(id).fadeIn(250);
					}
				});
				$("div#tabs a").each(function(){
					$(this).animate({
						width:26
					},{
						'duration': 500,
						'queue': false
					});
					$(this).removeClass("open");
				});
				
				var w = $(this).children("span.tab-desc").children("span.is").width();
				$(this).animate({
					width:w+45
				},{
						'duration': 500,
						'queue': false
					});
				$(this).addClass("open");
			}
			return false;
		});
		return false;
	}
};


var Certificate = {
	chForm : function(){
		if($("#ch_phone_no").val() == ""){
			$("#ch_phone_no").css("borderColor", "red");
		}else{
			$("#cform-re").submit();
		}
		return false;
	},
	init : function(){
		if($("#check-cert").length == 1){
			$("#check-cert").bind("click", function(){
				$("#cert-form").submit();
			});
		}
		
		if($(".cert_sub_items").length>0){
			$(".cert_sub_items").bind("click", function(){
				$.post("/default/certificate/set-gift", {'hash':$(this).val()}, function(ret){
					
				});
			});
		}	
	},
	gotoStep3 : function(el){
		if($(".cert_sub_items:checked").length==1){
			document.location.href = $(el).attr("rel"); 
		}else{
			$.colorbox({width:"300", inline:true, href:"#error_msg"});
		}
		return false;
	},
	checkOrderData : function(){
		var orderAddressData = $("#adrdess-form").serializeArray();
		$.post("/default/certificate/create-order", {"odata":orderAddressData}, function(ret){
			if (ret ==  1) {
				$("#adrdess-form").submit();
			}
			else {
				$.colorbox({width:"300", inline:true, href:"#error_msg"});
				$("#address-form label").css("color","#4D4D4D");
				$("#is-company-box label span").css("color","#4D4D4D");
				$(".check_bx").css("color","#ADADAD");
				
				var obj = jQuery.parseJSON(ret);
				
				for(x in obj){
					
					var index = $("#"+x).index();
					var parent = $("#"+x).parent();
					switch(x){
						case 'personal_data_check':
						case 'information_check':
						case 'regulation_check':	
							parent.children().eq(index+1).css("color","red");
						break;
						default:
							parent.children().eq(index-1).css("color","red");
						break;
					}
				}
			}
			
			return false;
		});
	}
};


var qas = function(el){
	var p = $('.qaf').serializeArray();
	if(p.length!=$('.qaa').length){
		$.colorbox({width:"300", inline:true, href:"#confirm_msg"});
	}else{
		$.post("/default/certificate/qa", {"qa":p, 'product_id':$("#pid").val()}, function(ret){
			document.location.href = $(el).attr("rel");
		});
	}
	
	return false;
}

var crtSend = function(){
	$.colorbox({inline:true, href:"#cform-re-form"});
	//$("#cform-re").submit();
}
