// hor. menu
$.fn.hm=function() {
	var $td=$(this).find("td:not('.br')"); var w=0; var $br=$(this).find(".br");
	return this.each(function() {
		$td.each(function() { w+=$(this).find("> a").innerWidth(); });
		w=parseInt(($(this).innerWidth()-w)/($td.size()-1));
		$br.width(w);
	});
};
// blocks for main
$.fn.block=function() {
	var $a=$(this).find("a"); var $tr=$(this).find("tr");
	return this.each(function() {
		$tr.each(function() {
			h=0;
			$(this).find("a").each(function() { h=h<$(this).find("span").height()?$(this).find("span").height():h; });
			$(this).find("a").each(function() {
				if ($(this).find("span").height()<h) { $(this).find("span").css("padding-top",(h-$(this).find("span").height())/2); }
			});
		});
		$a.hover(function() { $(this).addClass("on"); },function() { $(this).removeClass("on"); });
	});
};

// show colors
$.fn.color=function() {
	var $col=$(this).find("div"); var data=[]; var h=0; var num=0; var id=0;
	return this.each(function() {
		$("#add-to-smeta").dialog({
			autoOpen:false,
			height:262,
			modal:true,
			title:"Добавить в смету выбранную позицию",
			width:523
		});
		$("#add-to-smeta-pic").dialog({
			autoOpen:false,
			height:410,
			modal:true,
			width:554
		});
		// одинаковая высота для всех элементов
		while (num<$col.size()) {
			div=$col.slice(num,num+3); num+=3;
			for (j=0;j<div.length;j++) { h=h<$(div[j]).height()?$(div[j]).height():h; }
			for (j=0;j<div.length;j++) { $(div[j]).height(h); }
			h=0;
		}
		// данные для элементов
		$col.each(function(i) {
			$(this).attr("id",i)
			name=$(this).find("a").text(); img=$(this).find("img").attr("src"); dat=$(this).find("span").text().split("|");
			data[i]={id:dat[0],name:name,img:img,cost:dat[1],firm:dat[2],goods:dat[3]};
		});
		setData(0);
		$(this).jScrollPane({scrollbarWidth:20,scrollbarMargin:10});
		$col.click(function() { id=$(this).attr("id"); setData(id); });
		$("#addcart a").click(function() { $("#add-to-smeta").dialog("open"); });
		$("#add-to-smeta a").click(function() { $("#add-to-smeta").dialog("close"); });
		$('#add-to-smeta input[name="add"]').click(function() { 
         toCart($('#color_id').val(),$('#square').val());
         $("#add-to-smeta").dialog("close");
      });
		$("#scroll-coating #big-pic").click(function() {
			//n=data[id].img.indexOf("."); img=data[id].img.substring(0,n)+"-big1.jpg";
         img=data[id].img.replace( /\/small\//i, '/big/' );
			$("#add-to-smeta-pic").html("<img src=\""+img+"\">");
			$("#add-to-smeta-pic").dialog("option","title",data[id].goods+"&nbsp;&mdash;&nbsp;"+data[id].name);
			$("#add-to-smeta-pic").dialog("option",'height', 412);
			$("#add-to-smeta-pic").dialog("open");
		});
		// активный элемент
		function setData(id) {
			var $obj=$("#scroll-coating");
			var $name=$obj.find("#name"); // Название
			var $cost=$obj.find("#cost"); // Цена
			var $firm=$obj.find("#manuf"); // Производитель
			var $img=$obj.find("#big-pic > div > img"); setImg($img,data[id].img); // Изображение
			$name.html(data[id].goods+"&nbsp;&mdash;&nbsp;"+data[id].name); $cost.text(data[id].cost); $firm.text(data[id].firm);
			$("#add-to-smeta").find(".name").html(data[id].goods+"&nbsp;&mdash;&nbsp;"+data[id].name);
			$("#color_id").val(data[id].id);
		};
		// смена картинки
		function setImg(obj,img) {
			//n=img.indexOf("."); img=img.substring(0,n)+"-big.jpg";
         img=img.replace( /\/small\//i, '/med/' );
			$("#add-to-smeta").find(".pic").html("<img src=\""+img+"\" height=\"169\" width=\"267\">");
			$(obj).parent().animate({opacity:0},500,function() {
				$(this).html("<img src=\""+img+"\" height=\"169\" width=\"267\">").find("img").bind("load",function() {
					$(this).parent().animate({opacity:1},500);
				});
			});
		}
	});
};

// input type=file style
$.fn.inp=function() {
	var $input=$(this).find("input[type='file']");
	return this.each(function() {
		$input.each(function() {
			$(this).wrap("<div class=\"cl\"></div>");
			$(this).before("<div class=\"inputtxt\"><input type=\"text\" readonly></div>")
			$(this).wrap("<div class=\"inputbtn\"></div>");
			$(this).css({opacity:0});
			$(this).change(function() {
				v=$(this).val().split(/[\/\\]/); l=v.length;
				$(this).parent().prev().find("input[type='text']").val(v[l-1]);
			});
		});
	});
};


// contacts
var flag;
$.fn.contact=function() {
	var $book=$(this).find("div"); var $bookTxt=$(".bookmark-text");
	return this.each(function() {
		$bookTxt.css({opacity:0});
		$book.each(function(i) { $(this).attr("id",i); ($(this).hasClass("activ"))?flag=i:null; });
		$(this).is('#bookmark')&&$($bookTxt[flag]).css({display:"block"}).animate({opacity:1},300);
		$book.click(function() {
			var id=$(this).attr("id");
			if (id!=flag) {
            $($bookTxt[flag]).animate({opacity:0},300,function() {
               $(this).css({display:"none"});
               $('#bookmark>div').removeClass("activ");
               $('#bookmark>div:eq('+id+')').addClass("activ");
               $($bookTxt[id]).css({display:"block"}).animate({opacity:1},300,function() { flag=id; });
            });
			}
		});
	});
};

// maps
var officeFlag;
function showOffice(id) {
	var $office=$("#map-moscow").find(".office");
	if (officeFlag!=id) {
      $("#map-moscow").find("#office"+officeFlag).fadeOut(300,function() {
         $(this).hide(); officeFlag=id;
         $("#map-moscow").find("#office"+id).fadeIn(300);
      });
	}
}
function showOfficeBoss(id) {
   $("#map-moscow").find(".office:visible").hide();
   $("#map-moscow").find("#office"+id).show(); officeFlag=id;
}

var storeFlag;
function showStore(id) {
	var $store=$("#map-store").find(".office");
	if (storeFlag!=id) {
		$("#map-store").find("#store"+storeFlag).fadeOut(300,function() {
			$(this).hide(); storeFlag=id;
			$("#map-store").find("#store"+id).fadeIn(300);
		});
	}
}
function showStoreBoss(id) {
   $("#map-store").find(".office:visible").hide();
   $("#map-store").find("#store"+id).show(); storeFlag=id;
}

function callExternalInterface(o) { clearTimeout(myID); getMovieName("myMove").vStore(o); }
function getMovieName(movieName) {
	if (navigator.appName.indexOf("Microsoft")!=-1) { return window[movieName]; }
 else { return document[movieName]; }
}

var myID;
function goStore(id) {
	$('#bookmark>div:eq(1)').click();
	myID=setTimeout("callExternalInterface("+id+")",1000);
}

$(function() {
	$("#hmenu").hm();
	$("#top-blocks").block();
	$("#coat").color();
	if ($("#design-life-pic").is("#design-life-pic")) $("#design-life-pic").find("a").lightBox();
	if ($("#design-works").is("#design-works")) $("#design-works").find("a").lightBox();
	if ($(".links-gallery").is("#color-list")) $(".links-gallery").find("a.l-box").lightBox();
	if ($("#gallery-pic").is("#gallery-pic")) {
		$("#gallery-pic").find(".cl").each(function() { $(this).find("a").lightBox(); });
	}
	if ($(".branch-images").length) {
		if (/^\/(dealers|contacts)\/$/.test(location.pathname)) {
   $(".branch-images").find("a").lightBox({navigationDisabled:true});
  } else { $(".branch-images").find("a").lightBox(); }
 }
	$("form.sinput").inp();
	$("#bookmark").contact();
	$("#booknew").contact();
	$("#booknew2").contact();
   
 if (!$('#search-text').val()) { $('#search-text').val('Поиск'); }
 $('#search-text').focus(function() { if ($(this).val()=='Поиск') { $(this).val(''); }});
 $('#search-text').blur(function() { if ($(this).val()=='') { $(this).val('Поиск'); }});
 $('#search-form').submit(function() { if ($('#search-text').val()==''||$('#search-text').val()=='Поиск') {return false; }});
});

function ClientBrowse() { this.w=0; this.h=0;
if (self.screen) { this.w=screen.width; this.h=screen.height; }
else if (self.java) { var jkit=java.awt.Toolkit.getDefaultToolkit(); var scrsize=jkit.getScreenSize(); this.w=scrsize.width; this.h=scrsize.height; } return this; }
function OpenWin(url,nwin,w,h,s) { p=new ClientBrowse();
if (w) { leftPos=(p.w-w)/2; } else { w=p.w-100; leftPos=(p.w-w)/2; }
if (h) { topPos=(p.h-h)/2; } else { h=p.h-100; topPos=10; }
(s==1)?null:s=0; (nwin)?null:nwin="win"; var property="left="+leftPos+",top="+topPos+",width="+w+",height="+h+",scrollbars="+s;
window.open (url,nwin,property); }