var defaultMap;

jQuery().ready(function() {
    defaultMap = $("#map-block").find("img").attr("src");
    for (var i = 1; i <= 7; i++) {
        $("#conf" + i).mouseover(function() {
            var id = this.id;
            id = id.substring(4, id.length);
            id = "lands" + id;
            var src = $("#" + id).find("img").attr("src");
            $("#map-block").find("img").attr("src", src);
        });
        $("#conf" + i).mouseout(function() {
            $("#map-block").find("img").attr("src", defaultMap);
        });
    }
    var imagesToPreload = [];
    $("div[id*='lands']").each(function() {
        imagesToPreload[imagesToPreload.length] = $(this).find("img").attr("src");
    });
    preloadImages(imagesToPreload);

    var competitioinsBlock = $("div.competitioins-block");

    $("div.competitioin-block").show();
    
    competitioinsBlock.sortable({
        tolerance: 'pointer',
        revert: 100,
        stop: function (event, ui) {
            var ordersStr = "";
            $("div.competitioins-block").find("div.portlet").each(function(index, el) {
                var id = el.id;
                id = id.substring("competition".length);
                ordersStr += id + ";";
            });
            var expireDate = new Date();
            expireDate.setTime(expireDate.getTime() + (365 * 24 * 60 * 60 * 1000));
            $.cookie("portlets-order", ordersStr, {expires: expireDate});
        }
    });

    initScrollingBlock("div.competitioins-block", "div.competitioins-block-wrapper", "div.competitioin-block", 9);
    initScrollingBlock("div.articles-block-div", "div.articles-block-wrapper", "div.article-block", 3);

});

function initScrollingBlock(blockPath, blockWrapperPath, aBlockPath, blocksInVisibility) {
    var upArrowDiv = $("<div class='move-arrow'><div class='up'></div></div>");
    var downArrowDiv = $("<div class='move-arrow'><div class='down active'></div></div>");

    var competitioinsBlock = $(blockPath);

    competitioinsBlock.parents("td:first").prepend(upArrowDiv);
    competitioinsBlock.parents("td:first").append(downArrowDiv);

    upArrowDiv.find("div").click(function() {
        if ($(this).hasClass('active'))
            arrowsClickHandler(1, blockPath, blockWrapperPath, aBlockPath, blocksInVisibility);
    });

    downArrowDiv.find("div").click(function() {
        if ($(this).hasClass('active'))
            arrowsClickHandler(-1, blockPath, blockWrapperPath, aBlockPath, blocksInVisibility);
    });
}

function arrowsClickHandler(sign, blockPath, blockWrapperPath, aBlockPath, blocksInVisibility) {
    var competitionsBlock = $(blockPath);
    if (competitionsBlock.is(":animated"))
        return;
    var competitionsBlockWrapper = $(blockWrapperPath);
    var competitionsBlockHeight = competitionsBlockWrapper.height();
    var competitionsBlockTop = parseInt(competitionsBlock.css("top"));
    competitionsBlock.animate({top: (competitionsBlockTop + (sign * competitionsBlockHeight)) + "px"}, 500, function() {
        var competitionsBlock = $(blockPath);
        var competitionsBlockWrapper = $(blockWrapperPath);
        var competitionsBlockWrapperHeight = competitionsBlockWrapper.height();
        var competitionsBlockHeight = competitionsBlockWrapperHeight * (Math.ceil(competitionsBlock.find(aBlockPath).length / blocksInVisibility));
        var competitionsBlockTop = parseInt(competitionsBlock.css("top"));
        var upDiv = competitionsBlock.parents("td:first").find("div.move-arrow div.up");
        var downDiv = competitionsBlock.parents("td:first").find("div.move-arrow div.down");
        if (competitionsBlockTop == 0)
            upDiv.removeClass("active");
        else
            upDiv.addClass("active");
        if (-1 * competitionsBlockHeight == competitionsBlockTop - competitionsBlockWrapperHeight)
            downDiv.removeClass("active");
        else
            downDiv.addClass("active");

    });
}

function preloadImages(imgs) {
    for (var i = 0; i < imgs.length; i++) {
        var img = new Image();
        img.src = imgs[i];
    }
}

