﻿var showingNews = 0;
var ready = false;
var autoSlideTimer;

var anmImageHide;
var anmImageShow;
var anmBoxHide;
var anmBoxShow;


YUI().use('node-base', function(Y) {
    function init() {
        ready = true;
        //showButtons();
        //doShowNews(1, true);
    }
    Y.on("domready", init);
    Y.on("available", showButtons, "#news1-image");
});


function showButtons() {
    for (var i = 1; i <= 6; i++) {
        window.setTimeout("showButton(" + i + ")", 1000 + (i - 1) * 100);
    }
    window.setTimeout("doShowNews(1, true)", 20);
}

function showButton(n) {
    YUI({ combine: true, timeout: 10000 }).use("anim", function(Y) {

        var animShow = new Y.Anim({
            node: '#news' + n + '-inactive',
            to: { opacity: 0.93 },
            duration: 0.6
        });
        animShow.run();
    });
}

function initAutoSlide() {
    autoSlideTimer = window.setTimeout("autoSlide()", 8000);
}

function autoSlide() {
    doShowNews(((showingNews) % 6) + 1, true);
}

function showNews(n) {
    if (ready) {
        window.clearTimeout(autoSlideTimer);
        doShowNews(n, false);
    }
}

function doShowNews(n, setTimer) {
    if (ready) {

        if (n != showingNews) {

            YUI({ combine: true, timeout: 10000 }).use("anim", function(Y) {

                if (showingNews > 0) {
                    anmBoxHide = new Y.Anim({
                        node: '#news' + showingNews + '-active',
                        to: { opacity: 0 },
                        easing: Y.Easing.easeOutStrong,
                        duration: 0.5
                    });
                    anmBoxHide.run();

                    anmImageHide = new Y.Anim({
                        node: '#news' + showingNews + '-image',
                        to: { opacity: 0 },
                        duration: 0.8
                    });
                    anmImageHide.run();

                    document.getElementById("news" + showingNews + "-clickarea").style.left = "207px";
                }


                anmBoxShow = new Y.Anim({
                    node: '#news' + n + '-active',
                    to: { opacity: 1 },
                    easing: Y.Easing.easeOutStrong,
                    duration: 0.5
                });
                anmBoxShow.run();

                document.getElementById("news" + n + "-clickarea").style.left = "500px";

                anmImageShow = new Y.Anim({
                    node: '#news' + n + '-image',
                    to: { opacity: 1 },
                    duration: 0.5
                });
                anmImageShow.run();

                showingNews = n;
            });

            if (setTimer) {
                initAutoSlide();
            }
        }
    } else {
        // Not ready - wait and try again
        window.setTimeout("doShowNews(" + n + ", true)", 100);
    }
}