$(document).ready(function(){
	initTargetBlanks();
	initSearchInput();
	initAccordeon();
	initTabs();
	initHighFooter();
	if (!$.browser.msie){
		wait_to_load(["/company/img/zavod_part.png","/company/img/zavod_part.png","/company/img/auto_part.png","/company/img/zavod.png","/company/img/auto.png","/company/img/keys.png","/company/img/engine.png","/company/img/chas.png","/company/img/truba2.png","/company/img/truba.png","/company/img/kolesiko.png"],initSplash);
	};
})

var right_part = false;
var left_part = false;
var left_div = false;
var right_div = false;

function trim(value){
        val = value.replace(/^([\s]*)/g,"");
		val = val.replace(/([\s]*)$/g,"");
        return val;
    };
	

var loaded = 0;
function wait_to_load (imgs,todo) {	
	var s = $('body').html();
	var i=0;
	for(var i=0; i<imgs.length; i++) {
		s = s+'<img src="'+imgs[i]+'" id="to_load_'+i+'" width="1" height="0">';
		$('body').html(s);
		$('#to_load_'+i).bind('load',check_load(imgs.length,todo));
	}
}
function check_load(max, todo) {
	loaded++;
	if (loaded>=max) todo();
}
    
initTargetBlanks = function(){
	$(".target_blank").click(function(){
		window.open($(this).attr("href"));
		return false;
	})
}

initSearchInput = function(){
	$("input.text").focus(function(){
		alert(123);
		if ($(this).attr("value") == $(this).attr("title")) {
			$(this).attr("value", "");
		}
		$(this).addClass("input_focus");
	});
	$("input.text").blur(function(){
		if (trim($(this).attr("value")) == "") {
			$(this).attr("value", $(this).attr("title"));
			$(this).removeClass("input_focus");
		}
	});
}

var timeout_id;
function animate_queue (s) {
	clearTimeout(timeout_id);
	timeout_id = setTimeout(s,200);
};

var timeout_id2;
function animate_queue2 (s) {
	clearTimeout(timeout_id2);
	timeout_id2 = setTimeout(s,200);
};



initSplash = function(){
	$("#right_side").mouseenter(function(){
		animate_queue('showRightParts()');		
	}).mouseleave(function(){
		animate_queue('hideRightParts();hideRightDivs();');
	});
	
	$("#left_side").mouseenter(function(){
		animate_queue2('showLeftParts();');
	}).mouseleave(function(){
		animate_queue2('hideLeftParts(); hideLeftDivs();');
	});
}

function showRightParts(){
	$("#auto").css({"z-index": 25});
	$("#auto").animate({
			"opacity": 1
		}, {duration: 250, queue: false, complete: function(){
				right_div = true;
				$("#chas").show();
				$("#chas").animate({
					right: "-11px"
				}, {duration: 500, queue: false});
				$("#engine").show();
				$("#engine").animate({
					bottom: "-70px"
				}, {duration: 200, queue: false})
				$("#keys").show();
				$("#keys").animate({
					top: "-40px"
				}, {duration: 350, queue: false})
			}
		});
	$("#zavod_part").animate({
			"opacity": 0.35
		}, {duration: 250, queue: false, complete: function(){left_div = false;}});
	$("#link_auto").addClass("hover");
	$("#link_zavod").addClass("forhover");
}

function hideRightParts(){
	$("#chas").animate({
			right: "100px"
		}, {duration: 100, queue: false, complete:function(){$("#chas").hide();}});
		
		$("#engine").animate({
			bottom: "0px"
		}, {duration: 100, queue: false, complete:function(){$("#engine").hide();}})
		
		$("#keys").animate({
			top: "80px"
		}, {duration: 100, queue: false, complete:function(){$("#keys").hide();}})
}

function hideRightDivs(){
	$("#auto").animate({
			"z-index": 10,
			"opacity": 0.2
	}, {duration: 250, queue: false, complete: function(){right_div = false;}});
	$("#zavod_part").animate({
			"opacity": 0.8
		}, {duration: 250, queue: false, complete: function(){left_div = false;}});
	$("#link_auto").removeClass("hover");
	$("#link_zavod").removeClass("forhover");
}

function showLeftParts(){
	$("#zavod").css({"z-index": 25});
		
	$("#zavod").animate({
			"opacity": 1
		}, {duration: 250, queue: false, complete:function(){
				left_div = true;
				$("#truba").show();
				$("#truba").animate({
					bottom: "-9px"
				}, {duration: 500, queue: false})
				$("#kolesiko").show();
				$("#kolesiko").animate({
					left: "8px"
				}, {duration: 200, queue: false})
				$("#truba2").show();
				$("#truba2").animate({
					left: "82px",
					top: "-117px"
				}, {duration: 350, queue: false})
			}
		});
	
	$("#auto_part").animate({
			"opacity": 0.35
		}, {duration: 250, queue: false, complete: function(){right_div = false;}});
	
	$("#link_zavod").addClass("hover");
	$("#link_auto").addClass("forhover");
}

function hideLeftParts(){
	$("#kolesiko").animate({
		left: "119px"
	}, {duration: 100, queue: false, complete:function(){$("#kolesiko").hide();}});
	
	$("#truba").animate({
		bottom: "61px"
	}, {duration: 100, queue: false, complete:function(){$("#truba").hide();}})
	
	$("#truba2").animate({
		top: "-77px",
		left: "100px"
	}, {duration: 100, queue: false, complete:function(){$("#truba2").hide();}})
}

function hideLeftDivs(){
	$("#zavod").css({"z-index": 15});
	$("#zavod").animate({
			"opacity": 0.2
		}, {duration: 250, queue: false, complete: function(){
			$("#zavod").css({"z-index": 15});
			left_div = false;
			}});
	
	$("#auto_part").animate({
			"opacity": 0.8
			}, {duration: 250, queue: false, complete: function(){right_div = false;}});
	
	$("#link_zavod").removeClass("hover");
	$("#link_auto").removeClass("forhover");
}

initAccordeon = function(){
	$(".accordeon_list span").click(function(){$(this).parent(".item").toggleClass("active");})
}

initTabs = function(){
	var index=0;
	$(".tabs .this_tab li").eq(0).addClass("active");
	$(".tabs .tab_content").hide();
	$(".tabs .tab_content").eq(0).show();
	
	$(".tabs .this_tab li span").click(function(){
		if (!$(this).parent("li").hasClass("active")){
			$(".tabs .this_tab li").removeClass("active");
			$(this).parent("li").addClass("active");
			index = $(".tabs .this_tab li span").index($(this));
			$(".tabs .tab_content").hide();
			$(".tabs .tab_content").eq(index).show();
		}
	})
}

initHighFooter = function(){
	var height = $(window).height();
	var c_height = $("#content_inner").height();
	
	if (c_height < (height-267)) {$("#content_inner").css("height", height-269);}
}
