$(window).load(function(){
	var smwidth = 118;
	if ($.browser.msie)
	{
		//$("div>#menu #mpc #subdiv #subdivp").width("117px");
		//$("div>#menu #mpc #subdiv #subdivp_sel").width("117px");
		//smwidth = 112;
	}
	
	$("#slide_down").click(
			function(){
				$("#previews").animate({top:"-=92px"}, "fast", function(){if (parseInt($("#previews").css("top"))+parseInt($("#previews").height())<parseInt($("#preview_container").height())) $("#previews").animate({top:(parseInt($("#preview_container").height())-parseInt($("#previews").height()))+"px"}); } );
			}
			)
	$("#slide_up").click(
			function(){
				$("#previews").animate({top:"+=92px"}, "fast", function(){if (parseInt($("#previews").css("top"))>0) $("#previews").animate({top:"0px"})});
			}
			)
	
	$("table#button").click(
	function(){
		($(this).attr("id")=="button")?$(this).attr("id", "cbutton"):$(this).attr("id", "button");
		var parents = "";
		var delimiter = "";
		$.each($("table#button"), function(){
			parents+=delimiter+"'"+$(this).parent().attr("id")+"'";
			delimiter = ",";
			});
		$("#news_cont").load("/getnews.php?parents[]="+parents, function(){
			var h = $("#test").position().top-$("#lcol").position().top;
			//alert($("#news_cont").height())
			$("#lcol").height($("#news_cont").height()+170);
		});
	}
)
	$("table#cbutton").click(
	function(){
		($(this).attr("id")=="button")?$(this).attr("id", "cbutton"):$(this).attr("id", "button");
		var parents = "";
		var delimiter = "";
		$.each($("table#button"), function(){
			parents+=delimiter+"'"+$(this).parent().attr("id")+"'";
			delimiter = ",";
			});
		$("#news_cont").load("/getnews.php?parents[]="+parents, function(){
			var h = $("#test").position().top-$("#lcol").position().top;
			//$("#lcol").height(h);
			$("#lcol").height($("#news_cont").height()+170);
		});
	}
)

var fadespeed = 50;
var fadeout = 180;
$("#main_menu", "#menu").mouseover(
		function(){
			var io = $(this);
			if (io.attr("src").search("_s.jpg")==-1)
			{
				io.animate({opacity:0.5}, fadespeed, function(){
					msrc = io.attr("src").replace(".jpg","");
					msrc = msrc+"_s.jpg";
					msrc = io.attr("src", msrc);
					io.animate({opacity:1}, fadeout);				
				});
			}
		}
		)

$("#main_menu", "#menu").mouseout(
		function(){
			var io = $(this);
			if (io.attr("sel")!=1 && io.attr("src").search("_s.jpg")!=-1)
			{
				io.animate({opacity:0.5}, fadeout, function(){
					msrc = io.attr("src").replace("_s.jpg","");
					if (msrc.search(".jpg")==-1) msrc = msrc+".jpg";
					
					msrc = io.attr("src", msrc);
					io.animate({opacity:1}, fadespeed);				
				});
			}
		}
		)		
		
$("#mpc", "#menu").mouseover(
		function(){
			var io = $(this).find("#main_menu");
			//var pardiv = $(this).parent().parent();
			$(this).attr("mover",1);
			var offset = io.position();
			var mysubdiv = $(this).find("#subdiv div"); 
			var cntdiv = mysubdiv.length;
			cntdiv = Math.round(cntdiv/2)-1;
			var smoffset = parseInt(offset.left);
			smoffset-=(cntdiv*smwidth);
			if (smoffset<260)
			{
				while(smoffset<=250)
				{
					smoffset+=(smwidth);
				}
			}
			var rightpart = smoffset+parseInt($(this).find("#subdiv").width());
			if (rightpart>1021)
			{
				while(rightpart>1021) 
				{
					smoffset-=(smwidth+3);
					rightpart = smoffset+parseInt($(this).find("#subdiv").width());
				}
			}
			$(this).find("#subdiv").css({"top":(parseInt(offset.top)+33)+"px","left":smoffset+"px"});
			$(this).find("#subdiv").show(200);
			
		}
		)

$("#mpc", "#menu").mouseout(
		function(){
			//$(this).find("#subdiv").hide(300);
			$(this).attr("mover",0);
		}
		)
		
$("#mpc").everyTime(600, function(){
			var isBlock = 0;
			$.each($("#mpc", "#menu"), function(){
				if ($(this).attr("mover")=="0") {
					$(this).find("#subdiv").hide(150);
				}
				io = $(this).find("img");
				if (io.attr("src").search("_s.jpg")!=-1 && $(this).attr("mover")=="0")
				{
					if (io.attr("sel")!=1) 
					{
							msrc = io.attr("src").replace("_s.jpg","");
							if (msrc.search(".jpg")==-1) msrc = msrc+".jpg";
							msrc = io.attr("src", msrc);				
					}
				}
			});
			//if (mouse_over_menu==0 && isBlock==1) $("#mpc", "#menu").children("#subdiv").hide(300);
		}
		)
/*
$("#subdivp", "#subdiv").mouseover(
		function(){
			$(this).animate({backgroundColor: "#dae3d6", color:"#587b5c"}, 200);
			})

$("#subdivp", "#subdiv").mouseout(
		function(){
			$(this).animate({backgroundColor: "#587b5c", color:"#fff"}, 200);
		}
		)		
			*/
$("div>#menu #mpc #subdiv #subdivp").mouseover(
		function(){
			$(this).animate({opacity:0.5}, fadespeed, function(){
				$(this).attr("id", "subdivp_sel");
				$(this).animate({opacity:1}, fadeout);
			})
		}
		)		

$("div>#menu #mpc #subdiv #subdivp").mouseout(
		function(){
			$(this).animate({opacity:0.5}, fadespeed, function(){
				$(this).attr("id", "subdivp");
				$(this).animate({opacity:1}, fadeout);
			})
		}
		)			
$("div>#menu #mpc #subdiv #subdivp").click(
		function(){
			document.location = $(this).attr("target");
		}
		)


$("#left_image").click(
		function(){
			document.location='/projectlist/';
		}
		)
$("div#terr").click(
		function(){
			$("div#terr_s").animate({height:"47px"});
			$("div#terr_s").attr("id","terr");
			$(this).animate({height:"164px"});
			$(this).attr("id","terr_s");
			$.cookie("last_terr", $(this).attr("did"), {path:"/"});
			document.location="#"+$(this).attr("did");
			$("#txt_div").load("/getplist.php?par="+$(this).attr("did"), function(){
				$("#lcol").height($("#txt_div").height())
			});
		}
		)

$("#previews>img").click(
		function(){
			$(".active", "#previews").removeClass("active");
			$(this).addClass("active");
			var src = $(this).attr("src");
			src = src.replace("/pimg_small.php?url=","");
			//obj.attr("src", "/pimg_big.php?url="+src);
			$("#pbig_preview").remove();
			$.each($("#lbox a"), function(){
				href = $(this).attr("href");
				href = href.replace("/pimg_full.php?url=","");
				if (href==src) $(this).append('<img src="/pimg_big.php?url='+href+'" class="pbig_preview" id="pbig_preview" />');
			}
			)
			//alert(obj.attr("src"));
		}
		)		

$("#printpage").click(
		function(){
			var newwin = window.open("/print_contacts.php", "forprint", "menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=700, height=400");
			newwin.onload = newwin.print();
		}
)
$("#sendmail").click(
		function(){
			if ($("#name").attr("value")=="" || $("#name").attr("value")=="Ваше имя" )
			{
				alert("Требуется ввести Ваше имя!");
				$("#name").focus();
				return;
			}
			if ($("#email").attr("value")=="" || $("#email").attr("value")=="Ваш e-mail")
			{
				alert("Требуется ввести Ваш e-mail!");
				$("#email").focus();
				return;
			}
			var objRegExp  = /^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/;
			if ($("#email").attr("value").match(objRegExp)==null)
			{
				alert('E-mail заполнен некорректно');
				$("#email").focus();
				return;
			}
			if ($("#message").attr("value")=="" || $("#message").attr("value")=="Введите текст сообщения")
			{
				alert("Введите пожалуйста сообщение!");
				$("#message").focus();
				return;
			}
			$.post("/mailsendcb.php", {name: $("#name").attr("value"), email: $("#email").attr("value"), message:$("#message").attr("value")}, 
					function(){
				alert("Спасибо!\nВаше сообщение отправлено");
				$("#message").attr("value", "Введите текст сообщения");
				$("#email").attr("value", "Ваш e-mail");
				$("#name").attr("value", "Ваше имя");
			});
		}
)

$("#previews>img:first").addClass("active");		
var spath = document.location+"";
spath = spath.replace("http://"+document.location.host+"/","");
spath = spath.replace(document.location.hash,"");
spath = spath.split("/");
if (spath[0]=="projectlist" && spath[1]+""=="")
{
	if (document.location.hash=="")
	{
		$("div#terr:first").animate({height:"164px"});
		$("div#terr:first").attr("id","terr_s");
		$("#txt_div").load("/getplist.php", function(){$("#lcol").height($("#txt_div").height())});
	}
	else 
	{
		$.each($("div#terr"), function(){
			if ("#"+$(this).attr("did")==document.location.hash)
			{
				$(this).animate({height:"164px"});
				$(this).attr("id","terr_s");
				$("#txt_div").load("/getplist.php?par="+$(this).attr("did"), function(){$("#lcol").height($("#txt_div").height())});
			}
		});
	}
}
})


