$(document).ready(function(){
	if(errCapcha){
		window.scroll(0,10000);
		$("#capcha").focus();
		alert("Вы не правильно ввели символы на картинке!");
	}else{
		$(".zakazoformit:not(#feedback)").hide();
	}

	if ($.browser.msie){IEpngFix();};

	var maxWidth = 1260; // max width
	var minWidth = 948; // min width
	var selector = $("#mainholder"); // selector
	
	var windowWidthTemp = 0;
	var windowWidth = 0;
	if($.browser.msie){
		mmwidth();
		$(window).resize(function(){
								windowWidth = $(window).width();
								if (windowWidth != windowWidthTemp){
									windowWidthTemp = windowWidth;mmwidth();
									}});};
	function mmwidth(){
		$(selector).css({width:100+"%"});
		bodyWidth = $(window).width();
		if (bodyWidth < minWidth){
			$(selector).width(minWidth);
		}else{
			if (bodyWidth > maxWidth){
				$(selector).width(maxWidth);
			}else{
				$(selector).width(100+"%");
			}}};

	//png fix for ie
	function IEpngFix(){
		if ($.browser.msie){
			var transparentImage = "i/tr.gif";
			
			oImg = $("img[src$=.png]");
			lImg = $("img[src$=.png]").length;
			
			for (i=0;i<lImg;i++){
				srcImg = $(oImg[i]).attr("src");
				$(oImg[i]).attr({src:transparentImage});
				oImg[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + srcImg + "',sizingMethod='scale')";
				oImg[i].style.display = "inline-block";
			}
		}
	}
	
	//tabs
	var activeTab = $("#menu .active").attr("id");
	var time = 750;
	$(".tabsholder ul").hide();
	$("."+activeTab).show();
	
	$("#menu li").mouseover(
		function(){
			$("#menu li").removeClass("hover");
			$(".tabsholder ul").hide();
			var thisID = $(this).attr("id");
			$(this).addClass("hover");
			$("."+thisID).show();
		},
		function(){
		});
	
	$(".menuholder").hover(
		function(){},
		function(){
						setTimeout(function() {
							$("#menu li").removeClass("hover");
							$(".tabsholder ul").hide();
							$("."+activeTab).show();
						}, time);
		});
	//block height fix for ie
	/*function heightFixForIE(){
		if ($.browser.msie){
			block = $(".block");
			blockLength = $(".block").length;
			for (i=0;i<blockLength;i++){
				bh = $(block[i]).height();
				$(block[i]).height(bh);
			}
		}
	};*/

	tovarHeightFix();
	function tovarHeightFix(){
		var tovar = $(".tovar");
		var tovarLength = tovar.length;
		var maxHeightTov = 0;
		
		$(window).load(function(){
									for (i=0;i<tovarLength;i++){
										tovHeight = tovar[i];
										tovHeight = $(tovHeight).height();
										if (maxHeightTov<tovHeight){
											maxHeightTov=tovHeight;
										}
									}
									$(".tovar").height(maxHeightTov);
									/*heightFixForIE();*/
								});
		};
	
	$(".zakaz h4").toggle(
			function (){
				$(".zakazoformit").slideDown();
				$(".zakaz .br").hide();
				$(".zakaz .bl").hide();
				$(this).addClass("open");
				},
			function (){
				$(".zakazoformit").slideUp();
				$(".zakaz .br").show();
				$(".zakaz .bl").show();
				$(this).removeClass("open");
				});
	
	val = $(".search .searchinput").attr("value");
	$(".search .searchinput").focus(function(){
										valtemp = $(this).attr("value");
										if (valtemp == val){
											$(this).attr({value:""});
										};
									});
	$(".search .searchinput").blur(function(){
										valtemp = $(this).attr("value");
										sizeVal = valtemp.length;
										k=0;
										for (i=0; i<sizeVal;i++){
											if (valtemp[i] == " "){k++;}
										}
										if ((k == sizeVal)||(!valtemp)){
											$(this).attr({value:val});
										}
									});
	
	val2 = $("#podbor").attr("value");
	$("#podbor").focus(function(){
										valtemp = $(this).attr("value");
										if (valtemp == val2){
											$(this).attr({value:""});
										};
									});
	$("#podbor").blur(function(){
										valtemp = $(this).attr("value");
										sizeVal = valtemp.length;
										k=0;
										for (i=0; i<sizeVal;i++){
											if (valtemp[i] == " "){k++;}
										}
										if ((k == sizeVal)||(!valtemp)){
											$(this).attr({value:val2});
										}
									});
	
//	$("#zakaz").click(function(event){ event.preventDefault();});
//	$("#zakaz").click(function(){
//									comSename = $("#sename").attr("value");
//									comMail = $("#mail").attr("value");
//									comCapcha = $("#capcha").attr("value");
//									posUrl = $("#ajaxlink").attr("action");
//									if (!comSename){alert("vvedite Familiu")}
//										else{
//											if (!comMail){alert("vvedite EMAIL");}
//												else{
//													if (!comCapcha){alert("eprst CAPCHA!!!!!")}
//														else{
//															//alert(posUrl);
//															$.post(posUrl, {capcha: comCapcha}, comment, "json");
//													}
//												}
//										}
//									});

	$("#zakaz").click(function(event){ event.preventDefault();});
	$("#zakaz").click(function(){
									var errorVal = "поле обязательно для заполнения";
									var starArr = new Array;
									var starLenght;
									var k = 0;
									star = $("#form .star");
									starLenght = $(star).length;
									for (i=0; i<starLenght; i++ ){
										idInput = $(star[i]).parent().attr("for");
										valInput = $("#"+idInput).val();
										if ((valInput=="")||(valInput==errorVal)){
											k++;
											$("#"+idInput).addClass("star2").val(errorVal);
										}
									}
									if (k==0){
										$(this).parents("form").submit();
									}else{
										formInpuVal();
									}
								});

	function formInpuVal(){
		var asInitVals = new Array();
		$("#form input").each( function (i) {
				asInitVals[i] = this.value;
				
			});
		
		$("#form input").focus( function () {
				if ( this.className == "star2" ){
					this.className = "";
					this.value = "";
				}
			});
		$("#form input").blur( function (i) {
					thisValue = str = jQuery.trim(this.value);
				if (thisValue == "" ){
					this.className = "star2";
					this.value = asInitVals[$("#form input").index(this)];
				}
			});
	}







	function comment(data){
		res = data.res
		if (res==1){
			$("#ajaxlink").submit()
		}else{
			$("#err").html("<span>neverniy vvod</span>");
			$("#capcha").attr({value:""});
			$("#err span").fadeOut(1500);
			$("#dle-captcha a").triggerHandler("click");
		}
	}
	
	attrvalue1 = $("#attrvalue");
	attrvalue2 = $("label[for=attrvalue]");
	if ((attrvalue1.length)&(attrvalue2.length)){
		$(attrvalue1).hide();
		$(attrvalue2).hide();
		$("#att").change(function(){
						$(attrvalue1).show();
						$(attrvalue2).show();
					});
	}
	
	var textSearch;
	$(".exemple a").click(function(){
									textSearch = $(this).text();
									$("#person").val(textSearch);
								});
});

