var actImgId, actTimerId, startTimerId, actMenuId, ActLeftMenuId;
var browWidthCorr = 0;
var browLeftCorr = 0;
var arLeftCorr = [];
var actWidthSubstr = 0;
var actLeftSubstr = 0;
var hoverFlag = ActLeftMenuFlag = false;

function stopActTimer(){
    if (actTimerId) {
        clearTimeout(actTimerId);
        actTimerId = false;
        return true;
    };
    return false;
}

function stopStartTimer(){
    if (startTimerId) {
        clearTimeout(startTimerId);
        startTimerId = false;
        return true;
    };
    return false;
}

/*lightbox*/

function initlightbox(TEPATH, car_id){
    $(function(){
        $("a." + car_id).lightBox({
            overlayBgColor: "#fff",
            overlayOpacity: 0.6,
            imageBlank: TEPATH + "import/i/lightbox-blank.gif",
            imageLoading: TEPATH + "import/i/lightbox-ico-loading.gif",
            imageBtnClose: TEPATH + "import/i/lightbox-btn-close.gif",
            imageBtnPrev: TEPATH + "import/i/lightbox-btn-prev.gif",
            imageBtnNext: TEPATH + "import/i/lightbox-btn-next.gif",
            txtImage: "Фото",
            txtOf: "из"
        });
    });
}

/*\lightbox*/

/*Обратная анимация наведения курсора для меню "объекты" внутри раздела каталог*/
function backhoveranimation(){
    $("#objmenu .substrate").animate({
        width: actWidthSubstr,
        left: actLeftSubstr
    }, 500, "swing");
}

/*\Обратная наведения курсора для меню "объекты" внутри раздела каталог*/

/*Инициализация блоков .switcher*/
function initswitcher(){
    var actSwicthElemID = $(".switchmenu .selected").attr("id");
    actSwicthElemID = actSwicthElemID.substr(11, (actSwicthElemID.length));
    $(".switcher_elem h3").css("display", "none");
    $(".switcher_elem").css({
        "display": "none",
        "height": "190px",
        "overflow": "auto"
    });
    $(".switchmenu, #switcherelem_" + actSwicthElemID).css("display", "block");
}

/*\Инициализация блоков .switcher*/




/*Коррекция положения плашки в меню*/

/*Определение ширины меню*/
function getObjMenuWidth(){
    var res = $("#index").width();
    res = 1 * res;
    return res;
}

/*\Определение ширины меню*/
/*Коррекция положения плашки в меню*/
/*Фикс ширины для ие*/
if ($.browser.msie) {
    if ($.browser.version == 6) {
        browWidthCorr = 1;
    }
    if ($.browser.version == 7) {
        /*browLeftCorr = -37;*/
        browWidthCorr = 1;
    }
};
/*\Фикс ширины для ие*/

function inintObjMenu(){
    curIndexWidth = getObjMenuWidth();    
    if (1040 > getObjMenuWidth()) {
        browLeftCorr = -30;
    }
    else 
        if (1080 > getObjMenuWidth()) {
            browLeftCorr = -31;
        }
        else 
            if (1100 > getObjMenuWidth()) {
                browLeftCorr = -32;
            }
            else 
                if (1140 > getObjMenuWidth()) {
                    browLeftCorr = -33;
                }
                else 
                    if (1180 > getObjMenuWidth()) {
                        browLeftCorr = -34;
                    }
                    else 
                        if (1200 > getObjMenuWidth()) {
                            browLeftCorr = -35;
                        }
                        else {
                            browLeftCorr = -Math.floor((curIndexWidth - 1200) / 2 + 36);
                        };
}


function resizeObjMenu(){
    //actLeftSubstr -= browLeftCorr;
    inintObjMenu();
    //$("#testconteiner").html(browLeftCorr);
    if($("#objmenu .hormenu li.active a").width()){
		widthSubstr = $("#objmenu .hormenu li.active a").width();
		
		widthSubstr += 1 * $("#objmenu .hormenu li.active a").css("padding-left").substr(0, ($("#objmenu .hormenu li.active a").css("padding-left").length - 2));
		widthSubstr += 1 * $("#objmenu .hormenu li.active a").css("padding-right").substr(0, ($("#objmenu .hormenu li.active a").css("padding-right").length - 2));
	};
    widthSubstr += browWidthCorr;
    leftSubstr = Math.floor($("#objmenu .hormenu li.active a").offset().left);
    //alert(leftSubstr);
    leftSubstr += browLeftCorr;
    $("#objmenu .substrate").animate({
        width: widthSubstr,
        left: leftSubstr
    }, 500, "swing");
	/*$("#objmenu .substrate").css({
		"width": widthSubstr,
        "left": leftSubstr
	})*/
    actWidthSubstr = widthSubstr;
    actLeftSubstr = leftSubstr;
}

/*\Коррекция положения плашки в меню*/

/*Анимация шапки*/
function headeranimate(){
	$("#collage .start_img").animate({
		opacity: "toggle"
	}, 700);
	if(1 > actLeftSubstr) $("#objmenu .substrate").animate({
		left: actLeftSubstr
	}, 500, "swing");
}
function startheaderanimate(){
    $("#collage .start_img .screen").animate({
        opacity: "toggle"
    }, 700, false, function(){
        startTimerId = setTimeout('headeranimate()', 7000);
    });
}

function testload(){
    if (0 < $("#collage .start_img img").width()) {
        startheaderanimate();
    }
    else {
        setTimeout("testload()", 100);
    }
}

/*\Анимация шапки*/

function animateBg(elemId){
    $("#" + elemId + " span.bg").animate({
        left: 0
    }, 300, false, function(){
    });
    
}

/*Перерассчет меню при ресайзе окна*/
$(window).resize(function(){
	if($("#objmenu").width()){
		resizeObjMenu();
	};
});
/*\Перерассчет меню при ресайзе окна*/

$(document).ready(function(){
    /*Инициализируем анимацию шапки*/
    testload();
    /*\Инициализируем анимацию шапки*/
    /*Инициализируем меню*/
    inintObjMenu()
    /*\Инициализируем меню*/
    //Устраняем глюки
    if ($.browser.opera) {
        $("#foot").css("top", $("#index").height() - $("#foot").height() + "px");
    }
 
    //отключаем css версии без скриптов
    $("#objmenu .hormenu a, #infonews .state .ico_cont, #infonews .news .news_elem .star, #main .left_coll .menu a").css("background", "none");
    $("#infonews .state .ico_cont img, #infonews .news .news_elem .star img, #main .left_coll .menu li a span.bg").css("display", "block");
    $("#infonews .news .news_elem .star img").css("left", "6px");
    $("#collage .start_img").css("z-index", "30");
    $("#collage a.selected").css("z-index", "0");
	$("#objmenu .hormenu li.substrate").css("z-index", "-1");
    
    //определяем стартовую позицию подложки меню "объекты"
    if ($("#objmenu a.selected").css("padding-left")) {
        var widthSubstr = $("#objmenu a.selected").width();
        widthSubstr += 1 * $("#objmenu a.selected").css("padding-left").substr(0, ($("#objmenu a.selected").css("padding-left").length - 2));
        widthSubstr += 1 * $("#objmenu a.selected").css("padding-right").substr(0, ($("#objmenu a.selected").css("padding-right").length - 2));
        widthSubstr += browWidthCorr;
        var leftSubstr = Math.floor($("#objmenu a.selected").offset().left);
        leftSubstr += browLeftCorr;
        //leftSubstr += arWidthCorr[getIndexWidth()];
        $("#objmenu .substrate").css("width", widthSubstr);
        //Работает ли в ие?
		if(1 > leftSubstr){
			$("#objmenu .substrate").not($("#objmenu.catalog .substrate")).css("left", -1*widthSubstr);
		} else{
			$("#objmenu .substrate").not($("#objmenu.catalog .substrate")).css("left", leftSubstr);
		};
		$("#objmenu.catalog .substrate").css("left", leftSubstr);
        actWidthSubstr = widthSubstr;
        actLeftSubstr = leftSubstr;
    };
    
    //анимация наведения курсора для меню "объекты"	
    $("#objmenu .hormenu li a").not($("#objmenu.catalog .hormenu li a")).hover(function(){
        stopStartTimer();
        var curImgId = this.id;
        if ("block" == $("#collage .start_img").css("display")) {
                if (actImgId != curImgId.substr(13, curImgId.length)) {
                    widthSubstr = $("#" + curImgId).width();
                    $("#objmenu .hormenu li").removeClass("active");
                    $("#" + curImgId).parent().addClass("active");
                    widthSubstr += 1 * $("#" + curImgId).css("padding-left").substr(0, ($("#" + curImgId).css("padding-left").length - 2));
                    widthSubstr += 1 * $("#" + curImgId).css("padding-right").substr(0, ($("#" + curImgId).css("padding-right").length - 2));
                    widthSubstr += browWidthCorr;
                    leftSubstr = Math.floor($("#" + curImgId).offset().left);
                    leftSubstr += browLeftCorr;
                    $("#objmenu .substrate").animate({
                        width: widthSubstr,
                        left: leftSubstr
                    }, 500, "swing");
                    actWidthSubstr = widthSubstr;
                    actLeftSubstr = leftSubstr;
                    actImgId = curImgId.substr(13, curImgId.length);
                    $("#collage a").not($(".selected")).css("z-index", 0);
                    $("#collage a.selected").css("z-index", 10);
                    $("#collage a").removeClass("selected");
                    $("#collage_img_" + actImgId).addClass("selected");
                    $("#collage_img_" + actImgId).animate({
                        opacity: 0
                    }, 1, false, function(){
                        $("#collage_img_" + actImgId).css("z-index", 20);
                        $("#collage_img_" + actImgId).animate({
                            opacity: 1
                        },1, false, function(){
		                    $("#collage .start_img").animate({
								opacity: 0
							}, 700, false, function(){
								$("#collage .start_img").css("display","none");
							});							
						});
                    });
                };
        }
        else {
            if (actImgId != this.id.substr(13, this.id.length)) {
                widthSubstr = $(this).width();
				$("#objmenu .hormenu li").removeClass("active");
				$("#" + curImgId).parent().addClass("active");
                widthSubstr += 1 * $(this).css("padding-left").substr(0, ($(this).css("padding-left").length - 2));
                widthSubstr += 1 * $(this).css("padding-right").substr(0, ($(this).css("padding-right").length - 2));
                widthSubstr += browWidthCorr;
                leftSubstr = Math.floor($(this).offset().left);
                leftSubstr += browLeftCorr;
                $("#objmenu .substrate").animate({
                    width: widthSubstr,
                    left: leftSubstr
                }, 500, "swing");
                actImgId = this.id.substr(13, this.id.length);
                $("#collage a").not($(".selected")).css("z-index", 0);
                $("#collage a.selected").css("z-index", 10);
                $("#collage a").removeClass("selected");
                $("#collage_img_" + actImgId).addClass("selected");
                $("#collage_img_" + actImgId).animate({
                    opacity: 0
                }, 1, false, function(){
                    $("#collage_img_" + actImgId).css("z-index", 20);
                    $("#collage_img_" + actImgId).animate({
                        opacity: 1
                    });
                });
            };
                    };
            });
    
    $("#collage a").hover(function(){
        var effectSpeed = 50;
        $("#collage .sflash").animate({
            opacity: 0.5
        }, effectSpeed, false, function(){
            $("#collage .sflash").animate({
                opacity: 1
            }, effectSpeed);
        })
    });
    
    //анимация наведения курсора для меню "объекты" внутри раздела каталог	
    $("#objmenu.catalog .hormenu li a").not($("#objmenu.catalog .hormenu li a.selected")).hover(function(){
        stopActTimer();
        widthSubstr = $(this).width();
        widthSubstr += 1 * $(this).css("padding-left").substr(0, ($(this).css("padding-left").length - 2));
        widthSubstr += 1 * $(this).css("padding-right").substr(0, ($(this).css("padding-right").length - 2));
        widthSubstr += browWidthCorr;
        leftSubstr = Math.floor($(this).offset().left);
        leftSubstr += browLeftCorr;
        $("#objmenu .substrate").animate({
            width: widthSubstr,
            left: leftSubstr
        }, 500, "swing");
    }, function(){
        actTimerId = self.setTimeout("backhoveranimation()", 600);
    });
    
    $("#servis .hormenu li a, .substrate_link").hover(function(){
        var effectSpeed = 150;
        $("#" + this.id + " span.bg").animate({
            opacity: 0.5
        }, effectSpeed, false, function(){
            $(this).animate({
                opacity: 1
            }, effectSpeed);
        })
    });
    
    $(".submit").hover(function(){
        var effectSpeed = 150;
        $(this).animate({
            opacity: 0.5
        }, effectSpeed, false, function(){
            $(this).animate({
                opacity: 1
            }, effectSpeed);
        })
    });
    
    $("#infonews .state").hover(function(){
        $("#infonews .state .ico_cont img").animate({
            left: 25,
            top: 0
        }, 100);
    }, function(){
        $("#infonews .state .ico_cont img").animate({
            left: 15,
            top: 30
        }, 100);
    });
    
    $("#infonews .news a.news_elem").not(".morelink").hover(function(){
        $("#" + this.id + " .star img.pas").css("display", "none");
        $("#" + this.id + " .star img").animate({
            left: 0
        }, 100);
    }, function(){
        $("#" + this.id + " .star img").animate({
            left: 6
        }, 100, false, function(){
            if ("pas" == this.className) 
                $(this).css("display", "block");
        });
    });
    
    
    $("#main .left_coll .menu li a").not($(".selected, .substrate_link")).hover(function(){
        if (ActLeftMenuId != this.id) {
            ActLeftMenuId = this.id;
            actMenuId = setTimeout("animateBg(ActLeftMenuId)", 100);
        }
    }, function(){
        clearTimeout(actMenuId);
        var leftPosition = -1 * $(this).width();
        $("#" + this.id + " span.bg").animate({
            left: leftPosition
        }, 300, false, function(){
        });
        ActLeftMenuId = null;
    });
    
    //Экшен для свитчера	
    $(".switchmenu a").click(function(){
        $(".switcher_elem").css("display", "none");
        $(".switchmenu a").removeClass("selected");
        $("#" + this.id).addClass("selected");
        var actSwicthElemID = this.id;
        actSwicthElemID = actSwicthElemID.substr(11, (actSwicthElemID.length));
        $("#switcherelem_" + actSwicthElemID).css("display", "block");
    })
    //\Экшен для свитчера
   
	/*Вертикальный */
	$(".slider .switcher").click(function(){
		//alert($(this).attr("title"));
		if("none" == $(this).parent().children($(".haidtext_conteiner")).css("display")){
			$(this).parent().children($(".haidtext_conteiner")).not($(".switcher")).show("slow");
			$(this).html("Свернуть");
			$(this).attr("title","Свернуть");
		} else{
			$(this).parent().children($(".haidtext_conteiner")).not($(".switcher")).hide("slow");
			$(this).html("Подробнее")
			$(this).attr("title","Развернуть");
		};
	});
	/*\Вертикальный слайдер*/
	
	/*Вертикальный слайдер для списков типа dl*/

		$(".sliderlist dt a").live("click",function(){
			$(".sliderlist dt").not($(this).closest("dt")).removeClass("selected");
			$(this).closest("dt").addClass("selected");
			var obj = $(".sliderlist .expanded");
			obj.animate({height: 'toggle', opacity: 'toggle'},function()
			{
				obj.removeClass("expanded");
			});
			$(this).parents("dt").next().not("dd.expanded").animate({height: 'toggle', opacity: 'toggle'});
			$(this).parents("dt").next().not("dd.expanded").addClass("expanded");
			if ($.browser.msie && $.browser.version == 6) $("#foot").height();
		});

	/*\\Вертикальный слайдер для списков типа dl*/
	
});

