var vactorRotationBath=0;
var timerChangeBath=0;
var timeStepBath=200;
var lenImages=0;
var indexImage=0;
var displayChanger=false;
var startPay=0;
var dataArray=Array();
var objC;
var spaCash=0;
var startCashGlobal=0;

$(document).ready(function(){
	startCashGlobal=parseInt(decreatePayMoney($("#payPayBath").text()));
	$(".form_submit").click(function(){
		$("#sendMe").submit();
		return false;
	});
	
	$("#closePopUp").click(function(){
		$(this).parent().slideUp();								
	});
	
	$("#filter_ajax").slideUp(0);

	$(document).ajaxError(function(e, xhr, settings, exception) {
alert('error in: ' + settings.url + ' \\n'+'error:\\n' + exception);
}); 
	var uri=window.location;
	var posUri=String(uri).indexOf("#", 0);
	if(posUri)
		uri=String(uri).substr(0,posUri);
	$.getJSON(uri+"?ajax=true",function(data){	
		dataArray=data;		
		var length=dataArray.length;
		for(i=0;i<length;i++)
		{
			//alert(dataArray[i].name);
			var lCH=$(".change_size").size();
			for(e=0;e<lCH;e++)
			{
				var test=$(".change_size").eq(e).text();
				if(test==dataArray[i].name)
					$(".change_size").eq(e).attr("idFind",i);
			}
		}
	});
	startInit();				  
	objC=$("input:checkbox,input:radio");	   
	var l=objC.length;
	for(i=0;i<l;i++)
	{
		if($(objC[i]).attr("notvisualme")=="select")
			continue;
		$(objC[i]).addClass("hide_me");
		$(objC[i]).attr("my_id",i);
		$(objC[i]).bind("change",function(){
			//$(".checkboxes[idp="+$(this).attr("my_id")+"]")
			if($(this).attr("checked"))
			{
				$(".checkboxes[idp="+$(this).attr("my_id")+"]").find('div:first').addClass("selected");
			}else{
				$(".checkboxes[idp="+$(this).attr("my_id")+"]").find('div:first').removeClass("selected");
			}
			var ea=$("input:radio");
			var l=ea.length;
			for(i=0;i<l;i++)
			{
				if($(ea[i]).attr("checked"))
				{
					$(ea[i]).next().find('div:first').addClass("selected");
				}else{
					$(ea[i]).next().find('div:first').removeClass("selected");
				}
			}
		});
		$(objC[i]).after('<A href="#" class="checkboxes" idp="'+ i +'"><div class="'+($(objC[i]).attr("checked")?"selected":" ") +'"><img src="/design/standart/style/empty.png" width="16" height="20" /></div></a>');
	}
	$(".checkboxes").click(function(){
		
		objC=$("input:checkbox,input:radio");	
		//alert($(objC[parseInt($(this).attr("idp"))]).attr("disabled"));
		if($(objC[parseInt($(this).attr("idp"))]).attr("disabled"))
			return false;
		var lenParams=$(objC[parseInt($(this).attr("idp"))]).attr("hlength");
		var i=0;
		for(i=1;i<=lenParams;i++)
		{
			var id_find=$(objC[parseInt($(this).attr("idp"))]).attr("set_id"+i);
			var type_find=$(objC[parseInt($(this).attr("idp"))]).attr("set_type_s"+i);
			var method_find=$(objC[parseInt($(this).attr("idp"))]).attr("set_type"+i);
			if($(objC[parseInt($(this).attr("idp"))]).attr("checked"))
			{
				if(type_find=="s")
					type_find="as";
				var iter=$("input[value='" + type_find + "_" + id_find + "']").attr("iteration");
				if(method_find=="sel")
				{
					if(typeof iter==="undefined")
						iter=1;
				}
			}else{
			}
		}
		//alert(lenParams);
		if($(objC[parseInt($(this).attr("idp"))]).attr("checked"))
		{
			//if($(objC[parseInt($(this).attr("idp"))]).attr("type")!="radio")
			//{
				$(objC[parseInt($(this).attr("idp"))]).attr("iteration","-1");
				$(objC[parseInt($(this).attr("idp"))]).removeAttr("checked");
				$(objC[parseInt($(this).attr("idp"))]).trigger("change");
				$(this).find('div:first').removeClass("selected");
				if(typeof $(objC[parseInt($(this).attr("idp"))]).attr("hidePhoto")!="undefined")
				{
					$("#phid_" + parseInt($(objC[parseInt($(this).attr("idp"))]).attr("hidePhoto"))).addClass("hide");
					$(".imgC:first").click();
				}
				
				if(typeof $(objC[parseInt($(this).attr("idp"))]).attr("hidephoto_m")!="undefined")
				{
					
					$("#photo_" + parseInt($(objC[parseInt($(this).attr("idp"))]).attr("hidephoto_m"))).addClass("hide");
					$(".imgC:eq(1)").click();
					$("#photo_" + parseInt($(this).attr("hidephoto_m"))).attr("show","false");
					
				}
			//}
		}else{
			$(objC[parseInt($(this).attr("idp"))]).attr("iteration","1");
			$("input:checkbox:checked[grp_des='"+ $(objC[parseInt($(this).attr("idp"))]).attr("grp_des") +"']").removeAttr("checked").trigger("change");
			$(objC[parseInt($(this).attr("idp"))]).attr("checked","checked");
			$(objC[parseInt($(this).attr("idp"))]).trigger("change");
			$(this).find('div:first').addClass("selected");
			/*if(typeof $(objC[parseInt($(this).attr("idp"))]).attr("hidePhoto")!="undefined")
			{
				$("#phid_" + parseInt($(objC[parseInt($(this).attr("idp"))]).attr("hidePhoto"))).removeClass("hide").click();
			}
			if(typeof $(objC[parseInt($(this).attr("idp"))]).attr("hidephoto_m")!="undefined")
			{
				$("#photo_" + parseInt($(objC[parseInt($(this).attr("idp"))]).attr("hidephoto_m"))).removeClass("hide").attr("show","true");
				$(".imgC:eq(1)").click();
				
			}*/
			
		}
		var ea=$("input:radio");
		var l=ea.length;
		for(i=0;i<l;i++)
		{
			if($(ea[i]).attr("checked"))
			{
				$(ea[i]).next().find('div:first').addClass("selected");
			}else{
				$(ea[i]).next().find('div:first').removeClass("selected");
			}
		}
		$(this).find('div:first').removeClass("selected-hover");
		$(this).find('div:first').removeClass("hover");
		return false;
	});
	
	$(".checkboxes").hover(function(){
			objC=$("input:checkbox,input:radio");
			if($(objC[parseInt($(this).attr("idp"))]).attr("checked"))
			{
				$(this).find('div:first').removeClass("selected");
				$(this).find('div:first').addClass("selected-hover");
			}else{
				$(this).find('div:first').addClass("hover");
			}
	},function(){		
		$(this).find('div:first').removeClass("selected");
		$(this).find('div:first').removeClass("selected-hover");
		$(this).find('div:first').removeClass("hover");
		objC=$("input:checkbox,input:radio");
		if($(objC[parseInt($(this).attr("idp"))]).attr("checked"))
			$(this).find('div:first').addClass("selected");			
	});
	
	$("#change_filter_info").click(function(){
		$("#filter_ajax").slideToggle();
		return false;
	});
	
	createSPADown();
	
	$(".clear_form_filter").click(function(){
		var vMin=$("#slider-range").slider("option","min");	
		var vMax=$("#slider-range").slider("option","max");	
		$("#slider-range").slider("values",0,vMin);
		$("#slider-range").slider("values",1,vMax);
		var op=$(this).parent().parent().parent();
		$(op).find("input[name='widthT']").val(0).attr("disabled","disabled");
		$(op).find("input[name='heightT']").val(0).attr("disabled","disabled");
		$(op).find("input[name='width']:first").click();
		$(op).find("input[name='height']:first").click();
		$(op).find("input[name='format[]']").removeAttr("checked");
		$(op).find("input[name='send']").click();
	});	
	
	$("#another_clear").click(function(){

		$("input[name='another[]']:checked").removeAttr("checked").change();

		return false;					  
	});
	
});

function changeBath()
{
	indexImage+=vectorRotationBath;
	if(indexImage>=lenImages)
		indexImage=0;
	if(indexImage<0)
		indexImage=lenImages-1;
		
	var lastImage=indexImage-vectorRotationBath;
	if(lastImage<0)
		lastImage=lenImages-1;	
	if(lastImage>=lenImages)
		lastImage=0;	
		
	$($("#changer img").get(lastImage)).hide();
	$($("#changer img").get(indexImage)).show();	
}

function createPayMoney(text)
{
	text=String(text);
	var lenText=text.length;
	var i=0;
	var nText="";
	for(i=lenText-1;i>=0;i--)
	{
		if(i%3==2)
			nText=nText+" ";
		nText=nText+text.charAt(lenText-i-1);
	}
	return nText;
}

function decreatePayMoney(text)
{
	text=String(text);
	var lenText=text.length;
	var i=0;
	var nText="";
	for(i=lenText-1;i>=0;i--)
	{
		if(text.charAt(lenText-i-1)==" ")
			continue;
		nText=nText+text.charAt(lenText-i-1);
	}
	return nText;
}

function startInit()
{
	$("input[name='another[]'],input:not([notvisualme='select'])").removeAttr("checked");
	$("#changer").css("display","block");
						   $($("#changer img").get(indexImage)).show();
						   lenImages=$("#changer img").size();
						   vectorRotationBath=0;
						   displayChanger=true;						   
						   $(".imgC").click(function(){
													
													 if($(this).attr("ibig")=="true")
													 {
														 $("#changer").css("display","block");
														 displayChanger=true;
														 $(".spacer_images a,.spacer_images span").fadeIn();
													 }else{
													 	$("#changer").css("display","none");
														 displayChanger=false;
														 $(".spacer_images a,.spacer_images span").fadeOut();
													 }
													 if($(this).attr("inohide")=="true")
													 {
														 $(".specia_hide_all[show=true]").removeClass("hide");
													 }else{
														 $(".specia_hide_all").addClass("hide");
													 }
													 $(".imgC").removeClass("select");
													 $(this).addClass("select");
													 var img=$(this).attr("href");
													 var to=$(this).attr("meTo");
													 $(to).attr("src",img);	
													 return false;
													 
							});
						  /* $(".left_i, .right_i").mouseenter(function(){
										if($(this).attr("class")=="left_i")
											vectorRotationBath=-1;
										else
											vectorRotationBath=1;
										indexImage-=vectorRotationBath;
										changeBath();
						   				timerChangeBath=setInterval(changeBath,timeStepBath);			
									}).mouseleave(function(){
										vectorRotationBath=0;
						   				clearInterval(timerChangeBath);
									});*/
						  
						  $(".left_i, .right_i").mousedown(function(){
									
										if($(this).attr("class")=="left_i")
											vectorRotationBath=-1;
										else
											vectorRotationBath=1;
										//indexImage-=vectorRotationBath;
										changeBath();
						   				timerChangeBath=setInterval(changeBath,timeStepBath);			
									}).mouseup(function(){
										vectorRotationBath=0;
						   				clearInterval(timerChangeBath);
									});
						  
						 	$(".left_i, .right_i").click(function(){return false;});
						   
						   $(".change_size").click(function(){
								$(".change_size").removeClass("select");
								$(this).addClass("select");
								changeSize(this);
								return false;
							});
						   
						   startPay=parseInt($("#payPayBath").text());
						   
						   $("#payPayBath").text(createPayMoney(startPay));
						   $(".subpay").text(createPayMoney(startPay));
						   
						   $("input[name='another[]']").change(function(){	
								var len=$(this).attr("hlength");
								///opa change here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
								var i=0;
								if($(this).attr("checked"))
									$(this).attr("__mechecked","true");
								for(i=1;i<=len;i++)
								{
									var id_find=$(this).attr("set_id"+i);
									var type_find=$(this).attr("set_type_s"+i);
									var method_find=$(this).attr("set_type"+i);
									if($(this).attr("checked"))
									{
													    if(type_find=="s")
															type_find="as";
														
														if(method_find=="sel")
														{
															
															if(!$("input[value='" + type_find + "_" + id_find + "']").attr("checked"))
															{
																if(!$("input[value='" + type_find + "_" + id_find + "']").parent().parent().find("input:checked").size())
																{
																	$("input[value='" + type_find + "_" + id_find + "']").attr("checked","check").trigger("change");
																	var idP=$("input[value='" + type_find + "_" + id_find + "']").attr("my_id");
																}
																
															}
																var iter=$("input[value='" + type_find + "_" + id_find + "']").attr("iteration");																
																if(typeof iter==="undefined")
																	iter=0;
																if(iter==-1)
																	iter=0;
																iter++;
																$("input[value='" + type_find + "_" + id_find + "']").attr("iteration",iter);															
														}
														
														if(method_find=="blck")
														{
															//alert("block");
															$("input[value='" + type_find + "_" + id_find + "']").attr("disabled","disabled").parent().find(".title_s2").addClass("gray");
															$("input[value='" + type_find + "_" + id_find + "']").parent().find(".title_s").addClass("gray");
						
															//var iter=$("input[value='" + type_find + "_" + id_find + "']").attr("iteration");
															
														//	if(typeof iter==="undefined")
															//	iter=0;
														//	iter++;
															//$("input[value='" + type_find + "_" + id_find + "']").attr("iteration",iter);
														}
														var foundChecked=false;
														$(this).parent().parent().find("input[name='another[]']").each(function(){
															if($(this).attr("checked"))
																foundChecked=true;
														});														
													
														if(method_find=="unsck" && foundChecked)
														{
															if($("input[value='" + type_find + "_" + id_find + "']").attr("__mechecked")=="true")
															{
																$("input[value='" + type_find + "_" + id_find + "']").attr("__mechecked",false);
																$("input[value='" + type_find + "_" + id_find + "']").attr("checked",true).change();
															}
														}
														

									}else{																										
										
										if(type_find=="s")
											type_find="as";
										var iter=$("input[value='" + type_find + "_" + id_find + "']").attr("iteration");
										
										if(method_find=="sel")
										{
											/*if(typeof iter==="undefined")
												iter=1;
											if(iter==-1)
												continue;*/
											if(iter)
												iter--;
											$("input[value='" + type_find + "_" + id_find + "']").attr("iteration",iter);
										
											if(!iter)
												$("input[value='" + type_find + "_" + id_find + "']").removeAttr("checked","check").change();
										}
										
										if(method_find=="blck")
										{
											//alert("block");
											$("input[value='" + type_find + "_" + id_find + "']").removeAttr("disabled").parent().find(".title_s2").removeClass("gray").parent().parent().find(".title_s").removeClass("gray");
											$("input[value='" + type_find + "_" + id_find + "']").parent().find(".title_s").removeClass("gray");
											//var iter=$("input[value='" + type_find + "_" + id_find + "']").attr("iteration");											
										//	if(typeof iter==="undefined")
											//	iter=0;
										//	iter++;
											//$("input[value='" + type_find + "_" + id_find + "']").attr("iteration",iter);
										}
										
										if(method_find=="unsck")
										{										
										
											if($("input[value='" + type_find + "_" + id_find + "']").attr("checked"))
											{
												$("input[value='" + type_find + "_" + id_find + "']").removeAttr("checked").change();
												$("input[value='" + type_find + "_" + id_find + "']").attr("__mechecked","true");
											}//else
											//	$("input[value='" + type_find + "_" + id_find + "']").attr("__mechecked","false");
											//var iter=$("input[value='" + type_find + "_" + id_find + "']").attr("iteration");
											
										//	if(typeof iter==="undefined")
											//	iter=0;
										//	iter++;
											//$("input[value='" + type_find + "_" + id_find + "']").attr("iteration",iter);
										}
									}
								}
					
								if(!$(this).attr("checked"))
									$(this).attr("__mechecked","false");
								if($(this).attr("checked")) /*UNKOMENT IF RETURN CASH PAY SYSTEM BELOVE 02.08.10*/
								{
									/*
									var add=parseInt(decreatePayMoney($(this).parent().find(".pay").text()));
									var summ=parseInt(decreatePayMoney($("#payPayBath").text()));
									if(String(add)=="NaN")
										return;							
									
									summ=parseInt(summ)+parseInt(add);
									$("#payPayBath").text(createPayMoney(summ));
									$(".subpay").text(createPayMoney(summ));		*/						
									if(typeof $(this).attr("hidePhoto")!="undefined")
									{
										$("#phid_" + parseInt($(this).attr("hidePhoto"))).removeClass("hide").click();
									}
									if(typeof $(this).attr("hidephoto_m")!="undefined")
									{
										$("#photo_" + parseInt($(this).attr("hidephoto_m"))).removeClass("hide").attr("show","true");
										$(".imgC:eq(1)").click();										
									}
								}else{
									/*var sub=parseInt(decreatePayMoney($(this).parent().find(".pay").text()));
									var summ=parseInt(decreatePayMoney($("#payPayBath").text()));
									
									if(String(sub)=="NaN")
										return;							
									
									summ=parseInt(summ)-parseInt(sub);
									$("#payPayBath").text(createPayMoney(summ));
									$(".subpay").text(createPayMoney(summ));*/
									
									if(typeof $(this).attr("hidephoto_m")!="undefined")
									{
										$("#photo_" + parseInt($(this).attr("hidephoto_m"))).addClass("hide");
										$("#photo_" + parseInt($(this).attr("hidephoto_m"))).attr("show","false");
										//alert("HIDE");
										
									}
								}
								cashCalcHere();
							});
						   
						   $("input[name='SPA']").change(function(){				  
								if($(this).attr("checked"))
								{
								
									var add=parseInt(decreatePayMoney($(this).parent().find("span").text()));
									var summ=parseInt(decreatePayMoney($("#payPayBath").text()));
									
									if(String(add)=="NaN")
										return;							
									
									summ=parseInt(summ)+parseInt(add)-spaCash;
									$("#payPayBath").text(createPayMoney(summ));
									$(".subpay").text(createPayMoney(summ));
									spaCash=add;
								}else{
									//alert("A");
								//	var sub=parseInt(decreatePayMoney($(this).parent().find("span").text()));
									var summ=parseInt(decreatePayMoney($("#payPayBath").text()));
									
								//	if(String(sub)=="NaN")
									//	return;							
									
									summ=parseInt(summ)-spaCash;
									spaCash=0;
									$("#payPayBath").text(createPayMoney(summ));
									$(".subpay").text(createPayMoney(summ));
								}
							});
}

var changeSize=function(element)
{
	var id=$(element).attr("idFind");
	var cash=parseInt(decreatePayMoney($("#payPayBath").text()));//startCash
	/*
	cash-=startPay;
	startPay=dataArray[id].startCash;
	cash+=startPay;*/
	startCashGlobal=dataArray[id].startCash;
	
	var lo=dataArray[id].otherProducts.length;
	$("#litrs").text(dataArray[id].size);
	$("#bath_description").text(dataArray[id].description);
	for(i=0;i<lo;i++)
	{
		var obj=$("input[value='" + dataArray[id].otherProducts[i].id + "']");
		
		var pay=obj.parent().find(".pay");
		var later=parseInt(decreatePayMoney(pay.text()));
		/*if(obj.attr("checked"))
		{
			cash-=later;
			cash+=dataArray[id].otherProducts[i].cash;
		}*/
		pay.text(createPayMoney(dataArray[id].otherProducts[i].cash));
	}
	
	$("#payPayBath").text(createPayMoney(cash));
	$(".subpay").text(createPayMoney(cash));
	$("input[name='size']").val($(element).text());
	 cashCalcHere();
}

function createSPADown()
{
	$(".inSpaInfo").slideUp(0);
	$(".inSpaInfo").parent().find("label a:last").click(function(){
																 
	 	$(".inSpaInfo").slideUp();
		
		if($(this).parent().parent().find("label a:last img").attr("src")==="/design/standart/style/up-icon.png")
		{
			$(this).parent().parent().find(".inSpaInfo:first").slideUp();
			$(".spaInfo label a:last img").attr("src","/design/standart/style/down-icon.png");
			$(this).parent().parent().find("label a:last img").attr("src","/design/standart/style/down-icon.png");
			return false;
		}
		
		$(this).parent().parent().find(".inSpaInfo:first").slideDown();	
		$(".spaBTN img").attr("src","/design/standart/style/down-icon.png");
		$(this).parent().parent().find("label a:last img").attr("src","/design/standart/style/up-icon.png");
		
		return false;
	 });
	
	$('.mini_staf img[width!="16"]').bt({
	  contentSelector: "'<img src=\"'+$(this).attr('big')+'\"/>'",
	  padding: 10,
	  width: 'auto',
	  spikeLength: 10,
	  spikeGirth: 10,
	  cornerRadius: 10,
	  fill: 'rgba(0, 0, 0, .8)',
	  strokeWidth: 3,
	  strokeStyle: '#CC0',
	  cssStyles: {color: '#FFF', fontWeight: 'bold'},
	  showTip: function(box){
		$(box).fadeIn(500);
	  },
	  hideTip: function(box, callback){
		$(box).animate({opacity: 0}, 500, callback);
	  },	  
	  shrinkToFit: true,
	  hoverIntentOpts: {
		interval: 200,
		timeout: 5000
	  }
	});
}

function cashCalcHere()
{
	var cash=0;
	$("input[name='another[]']").each(function(){
		if($(this).attr("checked"))
		{
			var add=parseInt(decreatePayMoney($(this).parent().find(".pay").text()));
			cash+=add;
		}
	});
	$("#payPayBath").text(createPayMoney(startCashGlobal+cash));
	$(".subpay").text(createPayMoney(startCashGlobal+cash));	
}
