var Promo = {
    initialize: function () {
        Promo.__items = [];
        Promo.__index = -1;
        Promo.__isIE6 = (Promo.getInternetExplorerVersion() < 7.0 && Promo.getInternetExplorerVersion() != -1);
    },
    initializeDOM: function () {
        Promo.__anchor = $("#promo>.anchor")[0];
        Promo.__image = $("#promo .image")[0];
        Promo.__swatch = $("#promo>.swatch")[0];
        Promo.__initSwatch();
        Promo.__startTimer();
        Promo.moveFirst(false);
    },
    add: function (source, url, title) {
        Promo.__items[Promo.__items.length] = {
            source: source,
            title: title,
            url: url
        }
    },
    __initSwatch: function () {
        if (Promo.__anchor) {
            var items = $(Promo.__swatch).find("a");
            if (Promo.__items.length <= 1) {
                Promo.__swatch.style.display = "none";
            }
            else {
                for (var i = 0; i < items.length; i++) {
                    if (i > 0 && i < items.length - 1 && i > Promo.__items.length) {
                        items[i].parentNode.style.display = "none";
                    }
                    if (i == 1) {
                        Promo.__switch(0, true);
                    }
                    if (i == 0) items[i].href = "javascript:Promo.movePrevious();";
                    else if (i == items.length - 1) items[i].href = "javascript:Promo.moveNext();";
                    else items[i].href = "javascript:Promo.moveTo(" + (i - 1) + ");";
                }
            }
        }
    },
    __change: function () {
        var item = Promo.__items[Promo.__index];
        Promo.__locked = false;
        if (item) {
            Promo.__image.src = "/fileadmin/templates/css/images/blank.gif";
            Promo.__image.alt = "";
            Promo.__anchor.href = "";

            Promo.__image.src = item.source;
            Promo.__image.alt = item.title;
            Promo.__anchor.href = item.url;
        }
    },
    __stopTimer: function () {
        clearTimeout(Promo.__timer);
        Promo.__timer = null;
    },
    __startTimer: function () {
        if (!Promo.__timer)
            Promo.__timer = setTimeout("Promo.moveNext(true);", 7000);
    },
    __switch: function (index, auto) {
        if ((Promo.__index != index) && !Promo.__locked && Promo.__anchor) {
            Promo.__stopTimer();
            Promo.__locked = true;
            var items = $(Promo.__swatch).find("li");
            $(items[Promo.__index + 1]).removeClass("selected");
            $(items[index + 1]).addClass("selected");
            Promo.__index = index;
            if (Promo.__anchor) {
                if (Promo.__isIE6) {
                    Promo.__change();
                    Promo.__startTimer();
                }
                else if (auto) {
                    //Stephan
                    //Added the 200ms delay between fade out and fade in to make transition look smooth
                    $(Promo.__anchor).fadeOut(500, Promo.__change).delay(200).fadeIn(500, Promo.__startTimer);
                } else {
                    //Stephan
                    //Added the 200ms delay between fade out and fade in to make transition look smooth
                    $(Promo.__anchor).fadeOut(500, Promo.__change).delay(200).fadeIn(500, Promo.__startTimer);
                }
            } else Promo.__change();
        }
    },
    moveTo: function (index) {
        if (Promo.__index != index && index >= 0 && index < Promo.__items.length) Promo.__switch(index, false);
    },
    moveNext: function (auto) {
        if (Promo.__index < Promo.__items.length - 1) Promo.__switch(Promo.__index + 1, auto);
        else Promo.moveFirst(auto);
    },
    moveFirst: function (auto) { Promo.__switch(0, auto); },
    movePrevious: function () {
        if (Promo.__index > 0) Promo.__switch(Promo.__index - 1, false);
        else Promo.moveLast();
    },
    moveLast: function () { Promo.__switch(Promo.__items.length - 1, false); },
    //Stephan
    //Added this method to ensure that Promo "class" can live completely on its own.
    getInternetExplorerVersion: function () {
        // Returns the version of Internet Explorer or a -1
        // (indicating the use of another browser).
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    }
}
Promo.initialize();
