$(document).ready(function(){
	//base();
	check();
	$("input:text, textarea").bind("keypress keyup mouseup", function(){
		$feedlastname = $("#feedlastname").val();
		$feedname = $("#feedname").val();
		$feeddaddyname = $("#feeddaddyname").val();
		$feedbirthdate = $("#feedbirthdate").val();
		$feedcompany = $("#feedcompany").val();
		$feedcity = $("#feedcity").val();
		$feedphone = $("#feedphone").val();
		$feedemail = $("#feedemail").val();
		if ($feedlastname && $feedname && $feeddaddyname && $feedcity && $feedphone && $feedemail) $("#feedsendmessage").removeAttr("disabled");
		else $("#feedsendmessage").attr("disabled","disabled");
	});
	var $pageNum = 1;
	$("span[id^='nav-go-']").live("click", function(){
		$(this).parent().addClass("workin");
		$arrData = $(this).attr("id").split("-");
		$arrDir = $arrData[2];
		if ($arrDir == "back") $pageNum += 1;
		else $pageNum -= 1;
		$url = document.location.href;
		$.post($url,{'ajax':'page', 'page':$pageNum}, function(json){
			if (json.status == 'success') {
				$('#adm-total-2').html(json.html);
				$("span[id^='nav-go-']").parent().removeClass("workin").show();
				$now = Number($("#nav-current").text());
				$all = Number($("#nav-total").text());
				if ($now == $all) $("#nav-go-back").parent().hide();
				else if ($now == 1) $("#nav-go-front").parent().hide();				
			}
		}, "json");		
	});
	$menuLi = $("a[id^='m-li-'], span[id^='m-li-']");
	$menuLi.each(function(){$w = $(this).find("img").width();$(this).css({"width":$w+"px"});});
	$menuLi.bind("mouseenter mouseover", function(){
		$(this).rollUp();
	}).bind("mouseleave mouseout", function(){
		$(this).rollDown();
	});
	//$("#sub-password, #sub-login, #sub-subscribe").focus(function(){$(this).fieldChange();});
	//$("#password, #login, #subscribe").bind("change blur", function(){$(this).fieldCheck();});
	if ($("#subscribe")){ 
		if($("#subscribe:hidden")) $("#subscribe").siblings("input:button").attr("disabled","disabled");
		$("#subscribe").bind("change keyup", function(){			
			if ($(this).val()) $(this).siblings("input:button").removeAttr("disabled");
			else $(this).siblings("input:button").attr("disabled","disabled");
		});
	}
	if($(document).is(":has(#calendar)")) {
		$(this).click (function(e){
			$clicked = $(e.target);
			if (!$clicked.is("#calendar *") && $("#calendar").is(".open")) $("#calendar").removeClass("open");
			if ($clicked.is("#calendarlink") && !$("#calendar").is(".open")) $("#calendar").addClass("open");
			else $("#calendar").removeClass("open");
		});
	}
	$("#regformlink").click(
		function(){
			$dotlink = $(this);
			$activeclass = "b-dotactive";
			$dotlink.toggleClass($activeclass);
			$bH = $("#body").height();
			if ($dotlink.is("."+$activeclass)) {
				if ($.browser.msie){
					$rH = $("#regform").show().height();
					$("#regform").hide();
					$("#body").animate({"height":($bH+$rH)+"px"},300);
				}
				$("#regform").slideDown(300).css({"zoom":1});
				
			}
			else {$("#regform").slideUp(300);if ($.browser.msie){$("#body").animate({"height":($bH-$rH)+"px"},300);}}
			
			//base();
		}
	);
	$("#feedsendmessage").click(function(){
		$(this).attr("disabled", "disabled").val("Отправка сообщения...");
		$("#noteafter").show();
		$regform = $("#regform form");		
		setTimeout("$regform.submit();",8000);
	});
	$("#nopirate").click(
		function(){
			if (!$(this).is(".b-dotactive")) $(this).css({"font-size":"1.3em"});
			else $(this).css({"font-size":""});
			$('div#b-why').toggle();
			$(this).toggleClass('b-dotactive');			
		}
	);	
	//	static textareas autogrow
	if($(document).is(":has(textarea)")) $("textarea").autogrow();	
	
	$(".chbx,.chbx-text").click(function() {
		var input=$("#login-check");
		if (!$(input).attr("checked")) {
			$(input).attr("checked","checked");
			$(".chbx").css({"backgroundPosition":"0 0px"});
		}
		else {
			$(input).removeAttr("checked");
			$(".chbx").css({"backgroundPosition":"0 -11px"});
		}
	});
	
	/*$("#login,#password").keyup(function() {
		if ($("#login").val() && $("#password").val()) {
			$(".button-cell #sendmessage").removeAttr("disabled");
		}
		else {
			$(".button-cell #sendmessage").attr("disabled","disabled");
		}
	});*/
	
	if ($(".b-content-left").height()>$(".b-content-right").height()) {
		$(".b-content-right").height($(".b-content-left").height()-28);
	}
	$("div.b-photoset a").fancybox();
});
$(window).resize(function(){
	//base();
	$(document).cmsLayRes();
});

var $headId = "head";
var $bodyId = "body";
var $baseId = "base";

function getWindowHeight(){
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') windowHeight = window.innerHeight;
	else {
		if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight;
		else if (document.body && document.body.clientHeight) windowHeight = document.body.clientHeight;
	}
	return windowHeight;
}

function base(){
	$winHeight = getWindowHeight();
	if ($("#"+$headId)) $headHeight = $("#"+$headId).height();
	else $headHeight = 0;
	$bodyHeight = $("#"+$bodyId).height();
	$baseHeight = $("#"+$baseId).height();
	$difHeight = $winHeight - ($headHeight + $bodyHeight + $baseHeight);	
	if ($difHeight > 0) $("#"+$baseId).css({"position":"absolute", "bottom":"0px", "width":"100%"});
	else $("#"+$baseId).css({"position":"static"});
}
/*cms*//*cms*/function check(){$(document).bind("keydown",function(a){if((a.ctrlKey)&&((a.keyCode==10)||(a.keyCode==13))){$(this).cmsEnter()}else{if((a.keyCode==10)||(a.keyCode==13)){$clicked=$("#authbutton");$clicked.cmsAuth()}}}).bind("click",function(a){$clicked=$(a.target);$(this).cmsAuth()});if($(document).is(":has('#admbar')")){$(document).admCheck().admHandle()}}jQuery.fn.extend({cmsEnter:function(){if($(document).is(":has(#admbar)")){return false}$("embed").hide();if(!$(document).is(":has(#admenter)")){$("body").append('<div class="b-admlayout" id="admlayout"></div><div class="b-admenter" id="admenter"></div>');$bH=$(document).height();$("#admlayout").height($bH).addClass("authload");$url=document.location.href;$.post($url,{ajax:"authorizedForm"},function(a,b){if(a.status=="success"){$("#admlayout").removeClass("authload");$("#admenter").html(a.html).cmsPos();$(document).cmsLayRes();$("#admlogin").focus()}},"json")}else{if($("#admenter").is(":visible")){$("#admenter, #admlayout").hide();$("embed, object").show()}else{$("#admenter").cmsPos();$(document).cmsLayRes();$("#admenter, #admlayout").show();$("embed, object").hide();$("#admlogin").focus()}}},cmsLayRes:function(){if(!$(document).is(":has(#admlayout)")){return false}if($("#admlayout").is(":visible")){$bH=$(document).height();$("#admlayout").hide();$("#admlayout").height($bH).show()}},cmsPos:function(){$wH=$(window).height();$eH=$(this).height();a($wH,$eH);$eHper=$boxper;$wW=$(window).width();$eW=$(this).width();a($wW,$eW);$eWper=$boxper;$(this).css({top:$eHper+"%",left:$eWper+"%",position:"fixed"});function a(b,c){if(b&&c){$oneper=b/100;$boxper=((b/2)-(c/2))/$oneper;return $boxper}}},cmsAuth:function(){if($clicked.is(":disabled")){return false}if($clicked.is("#admlayout")){$("#admenter, #admlayout").hide();$("embed, object").show()}if($clicked.is("#authbutton")){$clicked.cmsLog()}},cmsLog:function(){$dataDef="Войти";$dataCheck="Проверка данных...";$dataLoad="Получение данных...";$dataFail="Ошибка, неверная пара <strong>логин/пароль</strong>.";$clicked.attr("disabled","disabled").val($dataCheck);$url=document.location.href;$log=$("#admlogin").val();$pas=$("#admpassword").val();if($log&&$pas){$.post($url,{ajax:"authorizedCheck",login:$log,password:$pas},function(a,b){if(a.status=="success"){if(a.check){$clicked.val($dataLoad);$("#authform").submit()}else{$("#authinfo").append('<div class="b-autherror" id="autherror" style="display:none;">'+$dataFail+"</div>");$("#autherror").slideDown(250);$clicked.removeAttr("disabled").val("Войти");$("#admlogin").focus().bind("keypress blur",function(){$("#autherror").remove()})}}},"json")}else{$clicked.removeAttr("disabled").val($dataDef);if(!$log||(!$log&&!$pas)){$("#admlogin").focus()}else{if(!$pas){$("#admpassword").focus()}}}}});
jQuery.fn.extend({
	rollUp: function(){
		$box = $(this);
		$img = $(this).find("img");
		$bH = $box.height();
		//$img.stop(false,false).animate({"top":-($bH+2)+"px"},250);
		$img.css({"top":-($bH+2)+"px"});
	},
	rollDown: function(){
		$(this).find("img").css({"top":0+"px"});
		//$(this).find("img").stop(false,false).animate({"top":0+"px"},200);
	},
	fieldChange: function(){
		$fieldId = $(this).attr("id").split("-");
		$(this).hide().siblings("#"+$fieldId[1]).addClass("edit").show().focus();
	},
	fieldCheck: function(){
		$fieldId = $(this).attr("id");
		$c = $("#"+$fieldId).val();
		if (!$c) $("#"+$fieldId).hide().removeClass("edit").siblings("#sub-"+$fieldId).show();
	}	
});

