﻿var ATS = {

    Rotator: {
        objImgBack: null,
        objImgFront: null,
        urls: null,
        interval: null,
        index: 0,
        opacity: 0,
        init: function(objImg, urls, interval, randomise) {
            this.interval = interval;
            if (randomise) {
                this.urls = new Array(urls.length);
                for (var i = 0; i < this.urls.length; i++) this.urls[i] = urls.splice(Math.floor(Math.random() * urls.length), 1)[0];
            } else {
                this.urls = urls;
            }
            for (var i = 0; i < this.urls.length; i++) {
                if (this.urls[i].substr(this.urls[i].lastIndexOf("/") + 1) == objImg.src.substr(objImg.src.lastIndexOf("/") + 1)) {
                    var s = this.urls.splice(i, 1)[0];
                    this.urls.push(s);
                    break;
                }
            }

            this.objImgBack = objImg;
            this.objImgFront = document.createElement("img");
            this.objImgFront.className = "Rotator";
            //this.objImgFront.setAttribute("class", "Rotator");
            this.objImgFront.setAttribute("style", "visibility:hidden;opacity:0;-moz-opacity:0;-khtml-opacity:0;filter:alpha(opacity=0);width:".concat(objImg.offsetWidth, "px;height:", objImg.offsetHeight, "px;"));
            this.objImgFront.setAttribute("alt", "");
            this.objImgFront.setAttribute("src", this.urls[0]);
            /* IE doesn't use predefinied css string */this.objImgFront.style.filter = "alpha(opacity=0)";
            objImg.parentNode.appendChild(this.objImgFront);

            setTimeout(function() { ATS.Rotator.fadeInNext(); }, this.interval);
        },
        fadeInNext: function() {
            this.opacity = 0;
            this.objImgFront.style.visibility = "visible";
            setTimeout(function() { ATS.Rotator.fadeStep(); }, 40);
        },
        fadeStep: function() {
            if (this.objImgFront.style.opacity >= 1) {
                this.onAfterFade();
            } else {
                this.opacity += 0.06;
                this.objImgFront.style.opacity = this.objImgFront.style.MozOpacity = this.objImgFront.style.KhtmlOpacity = this.opacity.toString();
                this.objImgFront.style.filter = "alpha(opacity=" + (this.opacity * 100).toString() + ")";
                setTimeout(function() { ATS.Rotator.fadeStep(); }, 40);
            }
        },
        onAfterFade: function() {
            this.index = this.index == this.urls.length - 1 ? 0 : this.index + 1;
            this.objImgBack.src = this.objImgFront.src; // TODO: Is this sometimes causing flicker?
            this.objImgFront.style.visibility = "hidden";
            this.objImgFront.style.opacity = this.objImgFront.style.MozOpacity = this.objImgFront.style.KhtmlOpacity = "0";
            this.objImgFront.style.filter = "alpha(opacity=0)";
            this.objImgFront.src = this.urls[this.index];
            setTimeout(function() { ATS.Rotator.fadeInNext(); }, this.interval);
        }
    },

    validateMailingList: function() {
        return Phizz.Validation.Value("MailingList_Name") && Phizz.Validation.Email("MailingList_Email", false);
    },

    windowResizeHandler: function() {
        var objFooter = Phizz.Dom.ByID("Footer");
        objFooter.style.top = "0px";

        var yFooter = Phizz.Dom.GetAbsolutePosition(objFooter).y;
        var bodyHeight = document.body.offsetHeight;

        if (yFooter < bodyHeight - 219) objFooter.style.top = (bodyHeight - 219 - yFooter) + "px";
    },

    documentLoadedHandler: function() {
        if (Phizz != null && Phizz.SWFText != null) {
            Phizz.SWFText.minHeight = 45;
            Phizz.SWFText.embedByTagAndClass("H1", "SWFText", "/i/Optima.swf", "32", "4f6033", "left");
        }

        var obj, objs;

        if (Phizz.Browser.IsIE6) {
            objs = Phizz.Dom.ByClass("Alpha", "IMG").concat(Phizz.Dom.ByClass("Alpha", "INPUT")); for (var i = 0; i < objs.length; i++) Phizz.Tools.EnableIE6ImageTransparency(objs[i]);
            obj = Phizz.Dom.ByID("Logo"); if (obj != null) Phizz.Tools.EnableIE6ImageTransparency(obj);
            obj = Phizz.Dom.ByID("Navigation"); if (obj != null) Phizz.Tools.EnableIE6ImageTransparency(obj);
            obj = Phizz.Dom.ByID("WelcomeBox"); if (obj != null) Phizz.Tools.EnableIE6BackgroundTransparency(obj, "/i/WelcomeBox-BG.png");
            objs = Phizz.Dom.ByClass("SpecialOffer", "DIV"); for (var i = 0; i < objs.length; i++) Phizz.Tools.EnableIE6BackgroundTransparency(objs[i], "/i/OfferPanel-Bg.png");

        }
    }

}



Phizz.Dom.RegisterWindowResizeEvent(ATS.windowResizeHandler);
Phizz.Dom.RegisterWindowLoadEvent(ATS.windowResizeHandler);