integrio.js.include("integrio.effects.utils.opacity");

function startPageCategoryInit(cN) {
    var td = integrio.Element.initElement(document.getElementById("start_page_category_" + cN).rows.item(0).cells.item(0).getElementsByTagName("DIV")[0].getElementsByTagName("TABLE")[0].rows.item(0).cells.item(0));
    var img = td.getElementsByTagName("IMG")[0];
    var date = integrio.Element.initElement(td.getElementsByTagName("TABLE")[0]);
    td.style.backgroundImage = "url(" + img.src + ")";
    date.setOpacity(0.6);
}

function loadPageComplete() {
    integrio.js.include("integrio.utils.events");
    integrio.js.include("integrio.effects.MoveEffect");
    integrio.js.include("integrio.effects.FadeEffect");
    integrio.js.include("integrio.effects.AppearanceEffect");
    integrio.js.include("integrio.gui.rebuild.RotatePanel");
    iniTopNews();
    initRotatePlace();
}

integrio.utils.FireEventNotificator.addWaiter(integrio.utils.FireEventNotificatorTypes.LOAD_PAGE_COMPLETE, window, loadPageComplete);

var topArticles = Array();
var topArticleImage = null;
var topArticleTop = 0;
var topNewsRotateInterval = null;
function iniTopNews() {
    var topNewsContent = integrio.Element.getElementById("topNewsContent");
    topNewsContent.onmouseover = function() {
        window.clearInterval(topNewsRotateInterval);
    };
    topNewsContent.onmouseout = function() {
        topNewsRotateInterval = setInterval(moveTopArticles, 10000);
    };
    var news = topNewsContent.childNodes;
    for (var i = 0; i < news.length; i++) {
        if (news[i].nodeType == 1 && news[i].tagName == "DIV") {
            if (news[i].id == "topNewsImage") {
                continue;
            }
            var div = integrio.Element.initElement(news[i]);
            var topArticle = {div:div};
            var img = div.getElementsByTagName("IMG")[0];
            topArticle.img = img;
            topArticles.push(topArticle);
        }
    }
    topArticleImage = integrio.Element.getElementById("topNewsImage");
    topArticleImage.getElementsByTagName("IMG")[0].src = topArticles[0].img.src;
    topArticleImage.getElementsByTagName("A")[0].href = topArticles[0].img.getAttribute("articleLink");
    for (i = 0; i < topArticles.length; i++) {
        topArticles[i].div.style.borderBottom = "6px solid #AE0000";
        topArticles[i].height = topArticles[i].div.getDimensions().height;
        topArticles[i].div.style.width = "400px";
    }
    topNewsContent.style.height = topNewsContent.getDimensions().height + "px";
    topArticleTop = topArticleImage.getPosition().y + topArticleImage.getDimensions().height;
    var top = topArticleTop;
    for (i = 0; i < topArticles.length; i++) {
        topArticles[i].div.style.position = "absolute";
        topArticles[i].position = i;
        topArticles[i].div.style.top = top + "px";
        topArticles[i].top = top;
        topArticles[i].div.style.zIndex = 100 + topArticles.length - i;
        top += topArticles[i].height;
    }
    if (integrio.Ext.Browser.isIE) {
        integrio.Element.initElement(document.body).setEventListener("resize", redrawTopArticles, true, document.body);
    } else {
        integrio.Object.extend(window, integrio.js.utils.eventsMethods);
        window.setEventListener("resize", redrawTopArticles, true, window);
    }
    topNewsRotateInterval = setInterval(moveTopArticles, 10000);
}

function moveTopArticles() {
    for (var i = 0; i < topArticles.length; i++) {
        topArticles[i].div.className = i != 1 ? "article" : "articleSel";
        nextArrowTopArticleProcess(topArticles[i].div, i == 1);
    }
    for (var i = 1; i < topArticles.length; i++) {
        topArticles[i].position = i - 1;
    }
    topArticles[0].position = topArticles.length - 1;
    topArticles.sort(function (a, b) {
        return a.position - b.position;
    });
    var top = topArticleTop;
    for (var i = 0; i < topArticles.length; i++) {
        topArticles[i].top = top;
        topArticles[i].div.style.zIndex = 100 + topArticles.length - i;
        top += topArticles[i].height;
    }
    for (var i = 0; i < topArticles.length; i++) {
        if (topArticles[i].moveEffect == null) {
            topArticles[i].moveEffect = integrio.effects.MoveEffect.create();
        }
        topArticles[i].moveEffect.init(topArticles[i].div, 500, {toY:topArticles[i].top,toX:topArticles[i].div.getPosition().x});
    }
    if (topArticleImage.fadeEffect == null) {
        topArticleImage.fadeEffect = integrio.effects.FadeEffect.create();
        topArticleImage.fadeEffect.afterEnd = function() {
            topArticleImage.getElementsByTagName("IMG")[0].src = topArticles[0].img.src;
            topArticleImage.getElementsByTagName("A")[0].href = topArticles[0].img.getAttribute("articleLink");
            if (topArticleImage.appearanceEffect == null) {
                topArticleImage.appearanceEffect = integrio.effects.AppearanceEffect.create();
            }
            topArticleImage.appearanceEffect.init(topArticleImage, 250);
        };
    }
    topArticleImage.fadeEffect.init(topArticleImage, 250);
}

function nextArrowTopArticleProcess(div, show) {
    var img = div.getElementsByTagName("TABLE")[0].rows.item(0).cells.item(1).childNodes[0];
    img.src = show ? "/templates/woman/images/start_page/accent_arrow.gif" : "images/none.gif";
}

function redrawTopArticles() {
    for (var i = 0; i < topArticles.length; i++) {
        topArticles[i].div.style.left = topArticleImage.getPosition().x + "px";
    }
}
var rotateP = null;
function initRotatePlace() {
    rotateP = integrio.gui.rebuild.RotatePanel.create("rotateStartPagePlace", {global:{autoRotateInterval:5000}});
    rotateP.initAutoRotate();
    rotateP.areaElement.setEventListener("mouseenter", function() {
        rotateP.iniFile.getPart("global").setValue("cyclic", false);
        window.clearTimeout(rotateP.ciclicInterval);
    }, true, rotateP);
    rotateP.areaElement.setEventListener("mouseleave", function() {
        rotateP.iniFile.getPart("global").setValue("cyclic", true);
        integrio.gui.rebuild.RotatePanelEvents.bind("rotateStartPagePlace");

    }, true, rotateP.areaElement);
}
