$(function(){
	InstallSize();
	InstallEnter();
	
	if($(".user_settings").length) {
		$(".user_settings").find("input[name='DocumentType']").bind(
			"click",
			function(){
				$(".client0, .client1").hide();
				$(".client" + $(this).attr("value")).show();
			}
		);
		if($("#show_all_settings").length) {
			
			$("#show_all_settings").bind(
				"click",
				function(){
					$(".show_all_settings_span").show();
					$(".client" + $(".user_settings").find("input[name='DocumentType']").attr("value")).show();
					$("#show_all_settings").remove();
					return false;
				}
			);
		} else {
			
			$(".client" + $(".user_settings").find("input[name='DocumentType']:checked").attr("value")).show();
		}
	}
	
	if ($("#mega-block").length) {
		var length = $(".sell-front").length;
		for(i = 0; i < length; i++) {
			$e1 = $($(".sell-front")[i]);
			$e2 = $($(".sell-back")[i]);
			
			if($e1.height() > 18) {
				$e1.css({top: 83 + 18 - parseInt($e1.height()) + "px"});
				$e2.css({top: 85 + 18 - parseInt($e2.height()) + "px"});
			}
		}
	}
	
	/*
	 * Форма поиска
	 */
	if( $(".search-form").length ) {
		$elem = $(".search-form").find("input[name='search']");
		$elem.bind("focus", function(){
			if($elem.attr("value") == "Поиск") {
				$elem.attr("value", "");
			}
		});
		$elem.bind("blur", function(){
			if($elem.attr("value") == "") {
				$elem.attr("value", "Поиск");
			}
		});
	}
});

function OnResize(){
	var WidthOne = 1228;  //первый предел ширины окна
	var WidthTwo = 1500;  //второй предел ширины окна
	var MBW      = 522;   //изначальная ширина блока
	var MBWOne   = 783;   //первый предел ширины блока
	var MBWTwo   = 1044;  //второй предел ширины блока
	
	/*
	 * Главная страница
	 */
	if ($("#mega-block").length) {

		if(/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
			MBW = 525;
			MBWOne = 786;
			MBWTwo = 1047;
		}
		
		if ($("#insi-body").width() > WidthTwo) {
			$("#mega-block").width(MBWTwo + 'px');
			$(".hidden-one").show();
			$(".hidden-two").show();
		}
		else 
			if ($("#insi-body").width() > WidthOne) {
				$("#mega-block").width(MBWOne + 'px');
				$(".hidden-one").show();
				$(".hidden-two").hide();
			}
			else {
				$("#mega-block").width(MBW + 'px');
				$(".hidden-one").hide();
				$(".hidden-two").hide();
			}
	}
	
	/*
	 * position
	 */
	if ($("#images").length) {
		var w = MBW;
		if ($("#insi-body").width() > WidthTwo) {
			$("#inner-block, #content").width(MBWTwo + 'px');
			$("#images").width(MBWTwo + 'px');
			w = MBWTwo;
		}
		else 
			if ($("#insi-body").width() > WidthOne) {
				$("#inner-block, #content").width(MBWOne + 'px');
				$("#images").width(MBWOne + 'px');
				w = MBWOne;
			}
			else {
				$("#inner-block, #content").width(MBW + 'px');
				$("#images").width(MBW + 'px');
			}
		$("#images").height($('#v-1').height() + 'px');
		w = w - $("#pre-images").find(".pre-view").length * ($("#pre-images").find(".pre-view").width() + 2) - 2;
		$("#pre-images").find(".pre-ff").css({width: w + "px"}).show();
	}
	
	/*
	 * list
	 */
	if ($("#insi_java_list").length || $("#insi_java_order").length) {
		
		if($("#insi_java_order").length) {
			var MBW      = 885;
			var MBWOne   = 1100;
			var MBWTwo   = 1407;
		}
		
		if ($("#insi-body").width() > WidthTwo) {
			$("#inner-block").width(MBWTwo + 'px');
		}
		else 
			if ($("#insi-body").width() > WidthOne) {
				$("#inner-block").width(MBWOne + 'px');
			}
			else {
				$("#inner-block").width(MBW + 'px');
			}
	}
	
	/*
	 * footer
	 */
	var w = MBW;
	if ($("#insi-body").width() > WidthTwo) {
		var w = MBWTwo;
	}
	else 
		if ($("#insi-body").width() > WidthOne) {
			var w = MBWOne;
		}
		
	if ($("#insi_java_list").length || $("#insi_java_order").length) {
		w -= 295;
		$("#footer").find(".developer").css({left: 210 + Math.floor( (w - 86) * 0.85) + "px"});
		$("#footer").find(".counters").css({left: 310 + w + "px"});
	}
	else {
		$("#footer").find(".developer").css({left: 80 + w + "px"});
		$("#footer").find(".counters").css({left: 310 + w + "px"});
	}
}

function InstallSize(){
	OnResize();
	$(window).resize(function(){
		OnResize();	
	});
}

function InstallEnter(){
	$('.enter').click(function(){
		var wff = (document.documentElement&&document.documentElement.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (document.documentElement&&document.documentElement.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		var h   = ( $("#body").height() > hff ) ? $("#body").height() : hff;
		$("#box-bg").css({height: h + 20 + "px"}).show();
		$("#box-enter").show();
		return false;
	});
	
	$('#box-bg').click(function(){
		$("#box-bg").hide();
		$("#box-enter").hide();
		return false;
	});
	
	$('#close').click(function(){
		$("#box-bg").hide();
		$("#box-enter").hide();
		return false;
	});
	
}
