var isRegionsClosed = true;
var isSearchClosed = true;
$(document).ready(function () {
    $("#firstVideo").animate({
        opacity: 0.35
    }, "fast");
    $("#firstVideo").find("img").addClass("act");
    //if(window.location.pathname.length<=1)setInterval(function(){$(".fotos").hide();$.ajax({url:"/?loadPeopleToHeader=true",contentType:"text/html; charset=windows-1251",success:function(html){$(".fotos").empty();$(".fotos").append(html);$(".fotos").fadeIn(500)}})},30000)
    $(document.body).click(function () {
        $("#regionList").fadeOut();
        isRegionsClosed = true
    });
    $("#regionSelect").toggle(function () {
        $("#regionList").fadeIn();
        isRegionsClosed = false
    }, function () {
        if (isRegionsClosed) $("#regionList").fadeIn();
        else {
            $("#regionList").fadeOut();
            isRegionsClosed = true
        }
    });
    $("#regionList > ul > li").each(function (i) {
        $(this).click(function () {
            $("#regionSelect > span").empty();
            $("#regionSelect > span").append($(this).attr("title"));
            $("#regionList").fadeOut();
            location.href = $(this).find("a").attr("href")
        })
    });
    var div = $("#regionList"),
        ul = $("#regionItems"),
        ulPadding = 15;
    var divHeight = div.height();
    div.css({
            overflow: 'hidden'
        });
    var lastLi = ul.find('li:last-child');
    div.mousemove(function (e) {
            var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight() + ulPadding;
            var top = (e.pageY - div.offset().top) * (ulHeight - divHeight) / divHeight;
            div.scrollTop(top)
        });
    $(".quote-list").show();
    $("marquee").show();
    $("marquee").marquee().mouseover(function () {
            $(this).trigger("stop")
        }).mouseout(function () {
            $(this).trigger("start")
        }).mousemove(function (event) {
            if ($(this).data("drag") == true) {
                this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX)
            }
        }).mousedown(function (event) {
            $(this).data('drag', true).data("x", event.clientX).data("scrollX", this.scrollLeft)
        }).mouseup(function () {
            $(this).data("drag", false)
        });
    $(window).scroll(function () {
            moveHeader("#hdMain");
            moveHeader("#hdPosition")
        });

    function moveHeader(headerId) {
            currentTop = $(window).scrollTop();
            startTop = $(headerId).parent().offset().top;
            stopTop = $(headerId).parent().height() + $(headerId).parent().offset().top - ($(headerId).height() * 2) - 50;
            currentPadding = currentTop - startTop;
            if (currentTop > startTop && currentTop < stopTop) $(headerId).animate({
                paddingTop: (currentPadding + 10) + "px"
            }, 8);
            if (currentTop <= startTop) $(headerId).animate({
                paddingTop: "0px"
            }, 8)
        }
    $(".tv").each(function (i) {
            if ($(this).attr("rel")) {
                $(this).css("cursor", "pointer");
                $(this).click(function () {
                    location.href = $(this).attr("rel")
                })
            }
        });
    $(".newsblocks").hover(function () {
            $(this).parent().find("div.moretext").fadeIn(100)
        }, function () {
            $(this).parent().find("div.moretext").hide()
        });
    $("#fastFilter").keyup(function () {
            $("ul[class=people] li a strong").removeHighlight();
            if ($("#fastFilter").val() == "") {
                $("ul[class=people] li").show();
                $(this).next().hide()
            } else {
                $(this).next().show();
                try {
                    if ($("#fastFilter").val() != "") $("ul[class=people] li a strong").highlight($("#fastFilter").val())
                } catch (e) {}
                $("ul[class=people] li a strong").each(function () {
                    if ($(this).text().toLowerCase().indexOf($("#fastFilter").val().toLowerCase()) == -1) $(this).parent().parent().hide();
                    else $(this).parent().parent().show()
                })
            }
        });
    var currentPage = 0;
    $(".tr .td1").click(function () {
            currentNode = $(".td1[rel=" + $("#nodeId").attr("rel") + "]");
            currentVisible = $(this).parent().next().eq(0).css("display");
            if (currentVisible == "none") {
                $(".trinfo").hide();
                $(".tr .td1").text("+");
                $(this).text("-");
                if ($("#isPeople").attr("rel") == "true") {
                    $(".trinfo .td2").find("p").find("ul").remove();
                    infoPanel = $(this).parent().next().eq(0);
                    infoPanel.show();
                    items = infoPanel.find(".td2").attr("rel").split(';');
                    bio = "<li><a href='" + $(this).attr("rel") + ".php'>Биография</a> (1)</li>";
                    news = "<li><a href='" + $("#newsUri").attr("rel") + "?human=" + $(this).attr("rel") + "'>Новости</a> (" + items[0] + ")</li>";
                    smi = "<li><a href='" + $("#smiUri").attr("rel") + "?human=" + $(this).attr("rel") + "'>Публикации в СМИ</a> (" + items[1] + ")</li>";
                    position = "<li><a href='" + $("#positionUri").attr("rel") + "?human=" + $(this).attr("rel") + "'>Позиция</a> (" + items[2] + ")</li>";
                    linkList = infoPanel.find(".td2").find("p").append("<ul>" + bio + news + "" + smi + "" + position + "</ul>")
                }
                if ($("#isTopics").attr("rel") == "true") {
                    $(".trinfo .td2").find("p").find("ul").remove();
                    infoPanel = $(this).parent().next().eq(0);
                    infoPanel.show();
                    items = infoPanel.find(".td2").attr("rel").split(';');
                    news = "<li><a href='" + $("#topicUri").attr("rel") + "?topic=" + $(this).attr("rel") + "&open=news'>Новости</a> (" + items[0] + ")</li>";
                    smi = "<li><a href='" + $("#topicUri").attr("rel") + "?topic=" + $(this).attr("rel") + "&open=smi'>Публикации в СМИ</a> (" + items[1] + ")</li>";
                    position = "<li><a href='" + $("#topicUri").attr("rel") + "?topic=" + $(this).attr("rel") + "&open=positions'>Позиция</a> (" + items[2] + ")</li>";
                    first_face = "<li><a href='" + $("#topicUri").attr("rel") + "?topic=" + $(this).attr("rel") + "&open=first_face'>От первого лица</a> (" + items[3] + ")</li>";
                    linkList = infoPanel.find(".td2").find("p").append("<ul>" + news + smi + position + first_face + "</ul>")
                }
                if ($("#isBook").attr("rel") == "true") {
                    infoPanel = $(this).parent().next().eq(0);
                    if (infoPanel.find(".td2").html().length > 10) infoPanel.show()
                }
            } else {
                $(this).text("+");
                $(this).parent().next().eq(0).hide()
            }
        });
    pageContainer = $(".peoplenav");
    trObject = $(".openclose").find("table tr");
    trOnPage = 32;
    allRows = trObject.length;
    allPages = Math.floor(allRows / trOnPage);
    for (i = 0; i <= allPages; i++) {
            s = "";
            if (allPages == 0) s = "style='display:none;'";
            pageContainer.append("<span rel='" + i + "' " + s + ">" + (i + 1) + "</span>")
        }
    pageContainer.find("span").click(function () {
            page = $(this).attr("rel");
            pageContainer.find("span").removeClass("active");
            pageContainer.find("span[rel='" + page + "']").addClass("active");
            trObject.hide();
            rowForm = page * trOnPage;
            rowTo = rowForm + trOnPage;
            $(".loader").hide();
            for (i = rowForm; i <= rowTo; i++) {
                trObject.eq(i).show();
                i++
            }
        });
    currentNode = $(".td1[rel=" + $("#nodeId").attr("rel") + "]");
    currentVisible = $(currentNode).parent().next().eq(0).css("display");
    currentIndex = $(".openclose").find("table").find("tr").index($(currentNode).parent());
    currentPage = Math.floor(currentIndex / 32);
    pageContainer.find("span").eq(currentPage).click();
    if ($("#isPeople").attr("rel") == "true" || $("#isTopics").attr("rel") == "true" || $("#isBook").attr("rel") == "true") {
            $(".td1[rel=" + $("#nodeId").attr("rel") + "]").click()
        }
    $(".doc").click(function () {
            $("#fileType").val("RTF");
            $("#documentText").val($("#rtfPdfContent").html());
            $("#pdfRtfForm").submit()
        });
    $(".pdf").click(function () {
            $("#fileType").val("PDF");
            $("#documentText").val($("#rtfPdfContent").html());
            $("#pdfRtfForm").submit()
        });

    $("li[rel=mainMenu]").mouseover(function () {
            $(this).addClass("over");
        }).mouseout(function () {
            $(this).removeClass("over");
        });

/*    $("li[rel=mainMenu] > a").hover(function () {
            $(this).find("img").attr("src", "/" + $(this).attr('rel'));
        }, function () {
            $(this).find("img").attr("src", "/" + $(this).attr('ref'));
       });
*/
});
