$(document).ready(function(){
	
	// color menu toggle slide
	
	loc=window.location.toString();
	if (loc.search("#zakaz")!=-1) {
	o=$(".zakazform")[0];
	hs.htmlExpand( o, { objectType: 'iframe', outlineWhileAnimating: true, preserveContent: false, width: 1024, height: 700,contentId: 'highslide-html', cacheAjax: false } );
	
	}
});

/* CALCULATOR */

//var mat="nerzh";
var work="rezka";
//var weight="до 1";
var mkol=1;
var vkol=0;
var skidka=0;
var itog=0;

var s=[]; // copy from excel prices
/*s[0] = [];
s[0][0] = "35,00|38,00|42,00|46,00|48,00|53,00|63,00|75,00|90,00|108,00|153,00|198,00|220,00" // stal
//s[0][50] = "22,00	25,00	27,00	30,00	35,00	37,00	42,00	50,00	60,00	70,00	80,00	150,00	200,00"
//s[0][500] = "19,00	22,00	24,00	27,00	28,00	35,00	37,00	47,00	57,00	67,00	78,00	132,00	180,00"
//s[0][1000] = "15,00	17,00	20,00	23,00	25,00	28,00	31,00	47,00	56,00	65,00	74,00	120,00	150,00"
s[1] = "0,5|0,8|1|1,5|2|2,5|3|4|5|6|8|10|12"

s[2] = [];
s[2][0]="18.00|26.00|35.00|40.00|45.00|75.00|100.00|130.00|295.00|400.00|430.00"; // nerg
s[2][1500] = "1.50|1.50|1.50|1.70|2.00|2.20|2.80|4.50|5.40|6.80|9.00"
//s[2][500] = "25,00	28,00	33,00	37,00	44,00	52,00	60,00	85,00	91,00	260,00"
//s[2][1000] = "22,00	25,00	30,00	32,00	40,00	48,00	57,00	78,00	85,00	210,00"
s[3] = "до 1|1,5|2|2,5|3|4|5|6|8|10|12"

s[4] = [];
s[4][0]="20.50|25.00|29.00|33.00|40.00|80.00|105.00|145.00|280.00|350.00"; // alum
s[4][1500] = "1.00|1.00|1.00|1.20|1.80|2.00|3.50|3.70|5.00|6.50"
//s[4][500] = "34,00	35,00	37,00	39,00	42,00	58,00	67,00	97,00	109,00	115,00"
//s[4][1000] = "16,50	18,00	20,50	25,00	29,00	33,00	34,00	52,00	80,00	100,00"
s[5]="до 1|1,5|2|2,5|3|4|5|6|8|10";

s[6] = [];
s[6][0]="35,00|38,00|42,00|46,00|49,00|52,00"; //latun
//s[6][50] = "31,00|34,00|38,00|41,00|46,00|50,00"
s[7]="0,5|0,8|1|1,5|2|3";

s[8] = [];
s[8][0]="60,00|65,00|68,00|72,00|78,00|85,00"; //med
//s[8][50] = "57,00|61,00|63,00|67,00|66,00|79,00"
s[9]="0,5|0,8|1|1,5|2|2,5";

s[10] = [];	
s[10][0]="35,00|55,00|70,00"; //bron
//s[10][50] = "33,00|52,00|67,00"
s[11]="2,5|4,5|6,5";

s[12] = [];	
s[12][0]="14.00|17.00|20.00|24.00|27.00|40.00|50.00|60.00|80.00|110.00|120.00|200.00|250.00|420.00|500.00"; //chern
s[12][1500] = "1.50|1.50|1.70|1.70|2.00|2.10|2.80|3.00|4.00|4.30|4.50|5.50|9.50|10.00|10.00"
s[13]="до 1|1,5|2|2,5|3|4|5|6|8|10|12|14|16|18|20";

var types = {
	"stal": 0,
	"nerzh": 2,
	"alum": 4,
	"latun": 6,
	"med": 8,
	"bron": 10,
	"chern": 12
};

var mats=new Array("stal", "nerzh", "alum", "latun", "med", "bron", "chern"); //materials


var ws=[]; //weights for each material
ws["stal"]=s[1].toString().split("|");
ws["nerzh"]=s[3].toString().split("|");
ws["alum"]=s[5].toString().split("|");
ws["latun"]=s[7].toString().split("|");
ws["med"]=s[9].toString().split("|");
ws["bron"]=s[11].toString().split("|");
ws["chern"]=s[13].toString().split("|");

var pprice=new Array();
l=0;
for(i=0;i<mats.length;i++){
	if (mats[i]!=""){
	ss1=s[l].toString().split("|");
	l++;
	ss2=s[l].toString().split("|");
	l++;
	pprice[mats[i]]=new Array();
		pprice[mats[i]]["rezka"]=new Array();
	for(j=0;j<ws[mats[i]].length;j++){

		pprice[mats[i]]["rezka"]["w"+ws[mats[i]][j]]=ss1[j];

	
	}
	
}
}*/


function setWeight(o){
weight=o.value;
itogo();
}

function setSkidka(o){
skidka=o.value;
itogo();
}

function setKol(){
//alert(12);
	//if (o.value=="") o.value="0";
		//mkol=o.value;
	if ($('#kol')[0].value=="") $('#kol')[0].value="0";
		mkol=$('#kol')[0].value;
	if ($('#vrezka')[0].value=="") $('#vrezka')[0].value="0";
		vkol=$('#vrezka')[0].value;
	itogo();
}

function itogo(){
	if ($('#itogo')[0]){
	
	//	console.log(mat+";"+work+";"+weight+";"+mkol+"=="+pprice[mat]+"*"+mkol);
	
	var iidd = types[mat];
	var col =  parseInt(mkol);
	
	prices = s[iidd][0];
	
	/*if (col >= 50 && array_key_exists(50, s[iidd]))
		prices = s[iidd][50];
	if (col >= 500 && array_key_exists(500, s[iidd]))
		prices = s[iidd][500];
	if (col >= 1000 && array_key_exists(1000, s[iidd]))
		prices = s[iidd][1000];*/
	
	for (var key in ws[mat]) {
		if (ws[mat][key] == weight) {
			we_id = key;
		}
	}
	//alert(we_id);
	
	//ono=pprice[mat][work]["w"+weight] * parseInt(mkol);
	//alert(weight);
	var vrez=0.0;
	var tmpar;
	if (array_key_exists(1500, s[iidd]))
	{
	tmpar=s[iidd][1500];
	tmpar=tmpar.split("|");
	vrez=parseFloat(tmpar[we_id]);
	}
	prices = prices.split("|");
	ono= parseFloat(prices[we_id]) * parseInt(mkol)+parseFloat(vrez)*parseInt(vkol);
	if (ono<30000.0 && skidka==0)
		ono=ono*1.15;
	if (skidka==1)
		ono=ono;
	if (skidka==2)
		ono=ono*0.9;
	itog=ono.toFixed(2);
	ono=ono.toFixed(2).toString();
	ps=ono.split(".");
	/*if (typeof(ps[1])!="undefined") sp="."+(ps[1].substr(0,2));
	else sp="";
	ono=ps[0]+sp;*/
	
	$('#itogo')[0].innerHTML=ono;
	$('#hits')[0].value=ono;
	}
}

/* CALCULATOR */


/*function checflds(){
	if ($("#fio")[0].value=="" || $("#tel")[0].value=="" ||$("#code")[0].value=="") 
	{
	alert("Не все обязательные поля введены");
	return false;
	}
	return true;
}

function checkprice(){
df=parseFloat($('#hits')[0].value);
	if (df<3000.0) 
	{
	alert("Минимальный заказ должен быть не менее 3000р");
	return false;
	}
	return true;
}*/

function checkf(){
	if ($("#fio")[0].value=="" || $("#tel")[0].value=="" ||$("#code")[0].value=="") {alert("Не все обязательные поля введены");return false;}
	return true;
}

$(document).ready(function(){
	
		itogo();
	
	$('.material').click(function(){
	mat=this.value;
	str='<select name="weight" onchange="setWeight(this)">';
	for(j=0;j<ws[mat].length;j++){
		if (j==0) weight=ws[mat][j];
		str+='<option value="'+ws[mat][j]+'">'+ws[mat][j]+'</option>';
	}
	str+='</select>  мм';
	$("#selec")[0].innerHTML=str;
	itogo();
	});
	$('.work').click(function(){
	work=this.value;
	itogo();
	});
	
	//itogo();
	
	lout=new Image();
	lout.src="/images/lar.png";
	lover=new Image();
	lover.src="/images/lar_.png";
	
	rout=new Image();
	rout.src="/images/rar.png";
	rover=new Image();
	rover.src="/images/rar_.png";
	
//	alert($("body")[0].offsetHeight+"px");
//	$("#gradi").css("height", (($("body")[0].offsetHeight)/2)+"px");
	
	//wsh=parseInt($(".shattle")[0].offsetWidth);
	
	$('.lar img').mouseover(function(){
	
		this.src=lover.src;
		
	});
	
	$('.lar img').mouseout(function(){
		
		this.src=lout.src;
		
	});
	
	$('.rar img').mouseover(function(){
		
		this.src=rover.src;
		
	});
	
	$('.rar img').mouseout(function(){
		
		this.src=rout.src;
		
	});
	
	$('.lar img').click(function(){
		o=$('.shattle')[0];
		vychislyandor=(parseInt(o.style.marginLeft)+139);

		if (vychislyandor>8) return;
		if (vychislyandor<(-1*(wsh-843))) return;
		o.style.marginLeft=vychislyandor+"px";
		
		
	});
	
	
	$('.rar img').click(function(){
		o=$('.shattle')[0];
		vychislyandor=(parseInt(o.style.marginLeft)-139);

		if (vychislyandor<(-1*(wsh-750))) return;
		o.style.marginLeft=vychislyandor+"px";
		
		
	});  
	

});
function array_key_exists ( key, search ) {
	    if( !search || (search.constructor !== Array && search.constructor !== Object) ){
	        return false;
	    }
	 
	    return search[key] !== undefined;
}
