﻿var curLeftPad = null;
var curRightPad = null;
var CarouselTimeout = 5000;
var timeout = null;
$(window).load(function () {
    $(".column").each(function () {
        $(this).find(".data").eq(0).show().end().end().find("div.tab").css("cursor", "pointer").click(openTab).end().find(".tab").each(function (i) {
            var $this = $(this);
            $this.css({
                "z-index": 10 - i

            });

            if ($this.prev().css("display") != "block") {
                $this.css("cursor", "pointer");
            }
        });
    });
    $('.Pager a[href]').each(function () {
        var $this = $(this);
        $this.attr("href", $this.attr("href") + "#pager");
    });
    var ph = ((/=ENG/i).test(window.location.href)) ? "Enter text..." : "Введите фразу...";
    $(".searchBox").attr("placeholder", ph);
    curLeftPad = $(".leftcol").find(".data").eq(0);
    curRightPad = $(".rightcol").find(".data").eq(0);
    timeout = setTimeout(carousel, CarouselTimeout);
    $(".carousel .direction").click(function () {
        if ($(this).parent().find(".direction:animated").length == 0) {
            if ($(this).hasClass("tr")) {
                $(".carousel .direction").each(function () {
                    var $this = $(this);
                    if ($this.hasClass("tl")) {
                        $this.switchClass("tl", "tr", 600);
                    } else if ($this.hasClass("tr")) {
                        $this.switchClass("tr", "front", 600);
                    } else if ($this.hasClass("front")) {
                        $this.switchClass("front", "tl", 600);
                    }
                });
            } else if ($(this).hasClass("tl")) {
                $(".carousel .direction").each(function () {
                    var $this = $(this);
                    if ($this.hasClass("tl")) {
                        $this.switchClass("tl", "front", 600);
                    } else if ($this.hasClass("tr")) {
                        $this.switchClass("tr", "tl", 600);
                    } else if ($this.hasClass("front")) {
                        $this.switchClass("front", "tr", 600);
                    }
                });
            }
        }
    });

    $(".carousel").mouseenter(function () {
        clearTimeout(timeout);
    });
    $(".carousel").mouseleave(function () {
        timeout = setTimeout(carousel, CarouselTimeout);
    });
    $("li.TopMenuItem").hover(enterMenu, leaveMenu);
    $("li.MenuItem:not(:has(li))").add("li.MenuItem:has(li) > a").hover(function () {
        $(this).addClass("padItemHover");
    }, function () {
        $(this).removeClass("padItemHover");
    });
    $(".LeftMenu li:has(ul)").each(
        function () {
            $(this).prepend("<a class='plus' title='Открыть' hred='#'></a>").find("ul").hide()
            .end().find("a.minus,a.plus").toggle(
            function () {
                $(this).addClass("minus").removeClass("plus").attr("title", "Закрыть")
                .closest("li").find("ul").show();

            },
            function () {
                $(this).addClass("plus").removeClass("minus").attr("title", "Открыть")
                .closest("li").find("ul").hide(); ;
            });
        }
        );
    $(".LeftMenu ul:has(a.padItemSel)").show().closest("li").find("a.plus").removeClass("plus")
	.addClass("minus").attr("title", "Закрыть");
});

var openTab = function (event) {
    var $this = $(this);
    var $curpad = $(event.currentTarget);
    if (!($curpad.hasClass("tab"))) return;
    if ($curpad.hasClass("tab")) $curpad = $curpad.prev();
    $curpad.stop(false, true);
    $curpad.css("cursor", "default");
    $curpad.next().css("cursor", "default")
    if ($this.prev().is(":visible")) {
        if ($(this).closest(".column").hasClass("leftcol")) {
            curLeftPad = $curpad;
        } else {
            curRightPad = $curpad;
        }
        return;
    }
    $Opn = $this.parent().find(".data:visible");
    $Opn.stop().animate({
        "height": "0px"
    }, 300, function () { $(this).hide(); $(this).next().css("cursor", "pointer") });
    $curpad.stop().show().animate({
        "height": "430px"
    }, 300, function () {
        $(this).next().css("cursor", "default");
        if ($(this).closest(".column").hasClass("leftcol")) {
            curLeftPad = $(this);
        } else {
            curRightPad = $(this);
        }
    });
}


var carousel = function () {
    $(".carousel .direction").each(function () {
        var $this = $(this);
        if ($this.hasClass("tl")) {
            $this.switchClass("tl", "tr", "normal");
        } else if ($this.hasClass("tr")) {
            $this.switchClass("tr", "front", "normal");
        } else if ($this.hasClass("front")) {
            $this.switchClass("front", "tl", "normal");
        } else {
		    $this.addClass("tr"); 
		}
    });
    timeout = setTimeout(carousel, CarouselTimeout);
}

var enterMenu = function () {
    var $this = $(this);
    var maxHeight = 0;
    $this.find("ul.menupad").addClass("menupadhover").each(function () {
        var $pad = $(this);
        var $prevPad = $pad.prev(".menupad");
        if ($prevPad.length > 0) {
            $pad.css("left", $prevPad.position().left + $prevPad.outerWidth());
            maxHeight = Math.max(maxHeight, $prevPad.outerHeight());
            $pad.append("<div class='sep'></div>");
        } else {
            $pad.css("left", 0);
			maxHeight = $pad.outerHeight();
            if (jQuery.browser.msie&&parseFloat(jQuery.browser.version)<8) {
                $pad.css("width", 200);
            }
        }

    });
    $this.find(".menupadhover").css("height", maxHeight);

}
var leaveMenu = function () {
    var $this = $(this);
    $this.find("ul.menupad").removeClass("menupadhover").css("height", "auto"); ;
    $this.find("ul.menupad .sep").remove();
}
