var nm=[];
nm[1]=0;nm[2]=0;nm[3]=0;
nm[4]=0;nm[5]=0;nm[6]=0;nm[7]=0;
var $CurrentDomain;

function CheckBonusButton(){
	var kolvo=0;
	$(".summapr").each(function(){
		//price=parseFloat(price)+parseFloat(this.innerHTML);
		kolvo=parseFloat(kolvo)+parseFloat(this.id);
	});
	if (kolvo>=25) {$("#getbonus").show();} else {$("#getbonus").hide(); $("#getbonus input").attr({checked : false});}
	
	return kolvo;
}
	
function delBlockX(x){
	var kol=0;
	$("#"+x.id).parent().remove();
	GetItogoKomplekt();
	$("#zakaz p").each(function(){kol=kol+1;});
	if (kol==0) {
		$("#price_ext").text("---");
		//$("#predv_price").hide();
		$("#itogo_zakaz").hide();
		$("#summa_diff").text(0);
	}
	kolvo = CheckBonusButton();
	return false;
}
function GetItogoKomplekt(){
	var z_p=[];
	var num=0;
	$(".hide").each(function(){
		z_p[num]=txt(this)+"|";
		num=num+1; 		
	});
	z_p=z_p.toString();	
	if (num!=0){
		$.post($CurrentDomain+'/php/getdiffitogo.php', { text: z_p}, function(data){
			$("#summa_diff").text(data);
			PriceCalculation();
		});
	}
    return false;	
}
function PriceCalculation(){
	var price=0;
	var kolvo=0;
	var add_price=0;
	var KolColor=0;
	var minusbonus=0;
	kolvo = CheckBonusButton();
	price = txt($("#summa_diff")[0]);
	if (!price) {price=0;}
	$("#grav input, #upack input, #dyspdate input, #logo_color option").each(function(){
		if (this.checked||this.selected) {
			tval = this.value.replace("KolVo",kolvo);
			add_price=add_price+"+"+tval+"+";
			if (this.selected){
				KolColor	= parseInt(this.id);
			}
		}
	});
	$("#getbonus input").each(function(){
		if (this.checked){
			minusbonus = this.value;
		}
	});
	add_price = add_price.replace(/\++/g,"+");
	plus_model		= $("#block_plus")[0].rel;
	add_price=add_price+plus_model;
	if (add_price.indexOf("KolColor1")>-1) {
		if (kolvo>=6&&KolColor>0) {
				KolColor=KolColor-1;
			}
		add_price = add_price.replace(/KolColor1/g,KolColor);		
	}
	if (add_price.indexOf("KolColor2")>-1) {
		if (kolvo>=1&&KolColor>0) {
			if (KolColor>2)	{KolColor=KolColor-2;} else {KolColor=0;}
			}
		add_price = add_price.replace(/KolColor2/g,KolColor);		
	}	
	add_price		= (add_price.replace("KolVo",kolvo));
	if (add_price.substring(add_price.length,add_price.length-1)=="+"){add_price=add_price+"0";}
	if (price!=0){
		$("#price_ext").text(eval(price+"+"+add_price+"-"+minusbonus));
		//$("#price_ext").attr({title: price+"+"+add_price+"-"+minusbonus});
	} else {$("#price_ext").text("---"); //$("#price_ext").attr({title: 0});
	}
	return false;
}

function txt(i,t){
	if(t){if(i.innerText){i.innerText=t}else{i.textContent=t}}else
	{if(i.innerText){i=i.innerText}else{i=i.textContent}}return i;
}
$(document).ready(function(){
	$CurrentDomain = "http://"+document.domain;
	$(".model_ajaxload").hide();
	$("#reg_type").change(function(){
		tmprt = $("#reg_type option:selected")[0].id;
		if (tmprt=="rt2"){
			//hide org
			$("#reg_org").hide();
			//show pers
			$("#reg_pers").show();
		} else {
			//hide pers
			$("#reg_pers").hide();
			//show org
			$("#reg_org").show();
		}
	});
	$(".go").click(function(){
		tmphref = $("#country option:selected").val();
		if (tmphref) {
			location.href = tmphref;
		}
	});
	$(".button_authorization").click(function(){
		CurrentLogin	= $("#login_input input").get(0).value;
		CurrentPass		= $.md5($("#pass_input input").get(0).value);	
		$.post($CurrentDomain+'/php/login.php', { login: CurrentLogin, pass: CurrentPass }, function(data){
			location.href = '/myimc.php?lang='+data;
            return false;
		});
	});
	$("#getbonus input").click(function(){
		PriceCalculation();
	});
	$("#formupload, #fakefile, #fileToUpload").livequery("change",function(){
		$("#eventupload").text("");
		try {
			txt($("#fakefile")[0],$("#fileToUpload")[0].files[0].fileName);
			if ($("#fileToUpload")[0].files[0].fileSize>=5242880){
				txt($("#fakefile")," ");
				$("#eventupload").text("Big file");
				$("#fileToUpload").val("");	
			}
		} catch (e) {
			$("#fakefile")[0].innerText=$("#fileToUpload")[0].value;		
		} finally {}
	});
	$(".button_exit").click(function(){
		$.post($CurrentDomain+'/php/exit.php', function(){
			location.href = $CurrentDomain;
            return false;
		});
	});
	
	$('#block_variants img').fadeTo('fast', 0.6);
		$('#block_variants img').hover(function(){
			$(this).stop(true, true).fadeTo('normal', 1.0);
		},function(){
			$(this).stop(true, true).fadeTo('normal', 0.6);
	});
  
    $("#gallery a, a.fancy_up, .picture_model a, .picture_zakaz a, #upack a, #grav a, #dyspdate a, .akcia_pict").fancybox({
    	hideOnContentClick: true,
    	overlayShow: true,
    	overlayOpacity: 0.3,
    	zoomSpeedIn: 0,
    	zoomSpeedOut:0
    });
	$('.dock').Fisheye(
		{
			maxWidth: 10,
			items: 'a',
			itemsText: 'span',
			container: '.dock-container',
			itemWidth: 45,
			proximity: 55,
			alignment : 'left',
			valign: 'bottom',
			halign : 'center'
		}
	)
	$("a.dock-item, #block_variants a").click(function(){
		CurrentBlock      = "#img_"+this.rel;
		CurrentAjaxLoad   = "#ajax_"+this.rel;
		CurrentLupa   	  = "#lupa_"+this.rel;
		NeedNamePicture   = this.rel;
		NeedNumberPicture = this.id;
		CurrentArticul	  = NeedNumberPicture.replace(NeedNamePicture, "");
		thisVariant=$("#variantpage")[0].rel;
		if (thisVariant=="2"){
			needP   = "c_";
			variant = 2;
		} else { // список моделей
			needP	= "";
			variant = 1;
		}
		
		$(CurrentLupa).hide();
		$(CurrentBlock).fadeOut(100,function(){
			$(CurrentAjaxLoad).fadeIn(100,function(){
				if (variant == 2){
					$.post($CurrentDomain+'/php/getdiff.php',{articul:CurrentArticul},function(data){
						$("#diffprice").replaceWith(data);
					});
					$.post($CurrentDomain+'/php/getpol.php',{articul:CurrentArticul},function(data){
						$("#spol").replaceWith(data);
					});				
				}
				$.post($CurrentDomain+'/php/getpicture.php', { pict: NeedNamePicture, nomer: needP+NeedNumberPicture, articul:CurrentArticul, variant: 1, thisVar: thisVariant}, function(xml){
					var kol=1;
					$(xml).find('rd').each(function(){
						nm[kol]=$(this).find('name').text();
						kol=kol+1;
					});
					if (variant == 2){
						wWidth  = nm[7];
						hHeight = nm[8];					
						$(CurrentBlock).attr({alt: nm[6]});
						$(CurrentBlock).parent()[0].title=nm[6];
						txt($("#name_spec_model")[0],nm[6]);
						$("#change_variant").text(nm[3]);
						$("#change_variant1").text(nm[3]);
						$("#block_model").attr({rel	  : CurrentArticul});
						$("#block_plus").attr({ rel	  : nm[4]});					
					} else {
						wWidth  = nm[7];
						hHeight = nm[8];
					}
					$(CurrentBlock).attr({
						src   : nm[1],
						width : wWidth,
						height: hHeight
					});
					BigImage=nm[2];
					$(CurrentBlock).parent().attr('href',BigImage);
					$(CurrentAjaxLoad).fadeOut(100,function(){
						$(CurrentBlock).fadeIn(100,function(){
							$(CurrentLupa).show();
						});
					});
				});
			});
		});
	});
	$("#logo_color").change(function(){
		PriceCalculation();	
	});
	$("input.radio").click(function(){
		PriceCalculation();
		CurrentDiv		= this.name;
		CurrentImg  	= this.id;
		CurrentBlock	= "#"+CurrentDiv+" div a";
		widthP			= "200px";
		heightP			= "200px";
		$(CurrentBlock).fadeOut(100,function(){
			$.post($CurrentDomain+'/php/getpicture.php', { pict: CurrentImg, div: CurrentDiv, variant: 2}, function(xml){
				var kol=1;
				$(xml).find('rd').each(function(){
					nm[kol]=$(this).find('name').text();
					kol=kol+1;
				});
				$(CurrentBlock).attr({
					href   : nm[1],
					width  : widthP,
					height : heightP
				});
				$(CurrentBlock+" img").attr("src",nm[2]);
				$(CurrentBlock).fadeIn(100);
			});
		});
	});
	$("#correct_info").click(function(){
		var c_i=[];
		var num=0;
		$("#anketa input, #anketa textarea").each(function(){c_i[num]="ar[]="+this.id+" = "+this.value;num=num+1;});
		c_i=c_i.toString();
		var my_div = $("<div id='fonfixedsellang'></div><div id='fonwhite'>Send data ...<br /><br /><img src='"+$CurrentDomain+"/images/ajax_load.gif'><br /></div>");
		my_div.appendTo("body");
		$.post($CurrentDomain+'/php/correct.php',{mas:c_i},function(xml){
			var kol=1;
			var answer=[];
			$(xml).find('rd').each(function(){
				answer[kol]=$(this).find('name').text();
				kol=kol+1;
			});
			$("#contact")[0].style.borderColor="#969696";
			$("#firma")[0].style.borderColor="#969696";
			$("#tel")[0].style.borderColor="#969696";
			$("#email")[0].style.borderColor="#969696";
			$("#post")[0].style.borderColor="#969696";
			lang = answer[3];
			$("#fonwhite").replaceWith('<div id="fonwhite">'+answer[1]+'<br /><br /><center><a class="bt_close" id="bt_close_'+lang+'" onclick=closeError()></a></center></div>');			
		});
	});
	$("#addzakaz, #addzakaz2").click(function(){
		kolvo_model			= $("#kolvo").val();
		if (kolvo_model>0) {
			text_model		= $("#change_variant").get(0).innerHTML;
			articul_model	= $("#block_model").attr("rel");
			price_model		= "None";
			$.post($CurrentDomain+'/php/getprice.php', { text: text_model, kolvo: kolvo_model, articul: articul_model }, function(xml){
				var kol=1;
				var pr=[];
				$(xml).find('rd').each(function(){
					pr[kol]=$(this).find('name').text();
					kol=kol+1;
				});	
				text_s1			=pr[1];
				var ValuePol="";
				$(".select_pol").each(function(){
					if (this.checked==true) ValuePol=txt(this.parentNode);
				});
				text_s1			= text_s1.replace(",",", "+ValuePol+", ");
				text_s1			= text_s1.replace("(","");
				text_s1			= text_s1.replace(")","");
				text_s2			=pr[2];
				kolvo_1			=pr[3];
				summa			=pr[4];
				valute			=pr[5];
				idstr			=pr[6];
				if (kol>2){
					$("#kolvo").val("");
					$("#zakaz").append("<p><span class='hide'>"+articul_model+"|"+kolvo_1+"</span><span class='delblock' id='"+idstr+"' onclick='delBlockX(this)'></span>"+text_s1+kolvo_1+text_s2+"<b><span class='summapr' id='"+kolvo_1+"'>"+summa+"</span>"+valute+"</b></p>");
					GetItogoKomplekt();
					$("#itogo_zakaz").show();
					$("#summa_diff").css({'color': 'red'}).animate({opacity: 'show'});
					//$("#predv_price").show();
				} else {
					alert(text_s1);
				}
			});
		}
	});
	
	$("#zakaz_komplect").click(function(){
		var z_p=[];
		var num=0;
		try{
		z_p[num] = "ar[]=model = "+txt($("#name_spec_model")[0]);
		$("#zakaz p").each(function(){
			num=num+1; 
			z_p[num]="ar[]=komplekt = "+txt(this);
		});
		if (num==0){
			$("#change_variant").each(function(){
				num=num+1;
				z_p[num]="ar[]=No quantity = "+txt(this);
				num=num+1;
				$(".select_pol").each(function(){if (this.checked) {num=num+1;z_p[num]="ar[]="+"pol"+" = "+txt(this.parentNode)}});				
			});
		}
		$("#change_variant1").each(function(){
			num=num+1;z_p[num]="ar[]="+"obrazec"+" = "+txt(this);
			$(".select_pol").each(function(){if (this.checked) {num=num+1;z_p[num]="ar[]="+"pol"+" = "+txt(this.parentNode)}});
		});		
		$("input.radio").each(function(){if (this.checked) {num=num+1;z_p[num]="ar[]="+this.name+" = "+this.id;}});
		$("#logo_color option").each(function(){if (this.selected) {num=num+1;z_p[num]="ar[]="+"logocolor"+" = "+this.id;}});		
		$("#price_ext").each(function(){
			num=num+1; 
			z_p[num]="ar[]=full price = "+txt(this);
		});
		$("#eventupload").each(function(){
			num=num+1;
			z_p[num]="ar[]="+txt(this);
		});
		$("#getbonus input").each(function(){if (this.checked){num=num+1;z_p[num]="ar[]=bonus = "+this.value;}});
		}catch(e){}
		finally{
		$("#anketa input, #anketa textarea").each(function(){num=num+1;z_p[num]="ar[]="+this.id+" = "+this.value;});
		z_p=z_p.toString();
		var my_div = $("<div id='fonfixedsellang'></div><div id='fonwhite'>Send data ...<br /><br /><img src='"+$CurrentDomain+"/images/ajax_load.gif'><br /></div>");
		my_div.appendTo("body");
		$.post($CurrentDomain+'/php/sendzakaz.php',{mas:z_p},function(xml){
			var kol=1;
			var err=[];
			$(xml).find('rd').each(function(){
				err[kol]=$(this).find('name').text();
				kol=kol+1;
			});
			if (err[1]==1){
				lang = err[9];
				$("#fonwhite").replaceWith('<div id="fonwhite">'+err[7]+'<br /><br /><center><a class="bt_close" id="bt_close_'+lang+'" onclick=closeError()></a></center></div>');
				if (err[2]==1){$("#contact")[0].style.borderColor="red";} 	else {$("#contact")[0].style.borderColor="#969696";}
				if (err[3]==1){$("#firma")[0].style.borderColor="red";} 	else {$("#firma")[0].style.borderColor="#969696";}
				if (err[4]==1){$("#tel")[0].style.borderColor="red";} 		else {$("#tel")[0].style.borderColor="#969696";}
				if (err[5]==1){$("#email")[0].style.borderColor="red";} 	else {$("#email")[0].style.borderColor="#969696";}
				if (err[6]==1){$("#post")[0].style.borderColor="red";} 		else {$("#post")[0].style.borderColor="#969696";}
			} else {
				$("#contact")[0].style.borderColor="#969696";
				$("#firma")[0].style.borderColor="#969696";
				$("#tel")[0].style.borderColor="#969696";
				$("#email")[0].style.borderColor="#969696";
				$("#post")[0].style.borderColor="#969696";
				lang = err[3];
				myimc = err[4];
				$("#fonwhite").replaceWith('<div id="fonwhite">'+err[1]+'<br /><br /><center><a class="bt_close" id="bt_close_'+lang+'" onclick=closeOk("'+lang+'","'+myimc+'")></a></center></div>');
			}
		});	
		}
	});
});

function closeError(){
	$("#fonwhite").remove();
	$("#fonfixedsellang").remove();
    return false;	
}
function closeOk(lang, myimc){
	closeError();
	if (!myimc){myimc="/";}
	location.href = $CurrentDomain+myimc+"?lang="+lang;
    return false;
}

function ajaxFileUpload(){
	$("#loading").ajaxStart(function(){
		$("#formupload").hide();
		$("#loading").show();
	}).ajaxComplete(function(){
		$("#loading").hide();
		$("#formupload").show();
	});

	$.ajaxFileUpload({
		url:$CurrentDomain+'/php/doajaxfileupload.php',
		secureuri:false,
		fileElementId:'fileToUpload',
		dataType: 'json',
		success: function (data, status){
			if(typeof(data.error) != 'undefined'){
				if(data.error != ''){
					$("#eventupload").text(data.error);
				} else {
					$("#eventupload").text(data.msg);
				}
			}
		},
		error: function (data, status, e){
			$("#eventupload").text(e);
		}
	});
	txt($("#fakefile")[0]," ");
	return false;
}