// this code is very similar code to /scripts/mediawall.js
var AdRotator = {
    currentAd: 0,
    delay: 10,           // seconds between ad flips
    transitionPeriod: 2, // tenths of a second - default period for a fade to take

    flipAd: function() {
        this.currentAd += 1;
        if (this.currentAd == this.ads.length) { 
            this.currentAd = 0; 
        }
        this.timeout = window.setTimeout("AdRotator.flipAd();", this.delay * 1000);
        this.transitionAd();
    },

    preloadImages: function(ads) {
        var imagePreload = new Array();
        for (var i=0; i<ads.length; i++) {
            if (ads[i].image) {
                imagePreload[i] = new Image;
                imagePreload[i].src = ads[i].image;
            }
        }
    },

    // takes a element id and sets it to fade from opactityStart to
    // opacityEnd over the default transition period.
    doFade: function(id, opacityStart, opacityEnd) {
        var transitionPeriod = this.transitionPeriod;
        var timer = 0;
        var increment = transitionPeriod / 100;
        var changeRate = 10;
         
        if (opacityStart > opacityEnd) {
            for (i = opacityStart; i >= opacityEnd; i -= changeRate) {
                setTimeout("AdRotator.setOpacity(" + i + ",\"" + id + "\")", (timer * transitionPeriod));
                timer += changeRate;
            }
        } else if (opacityStart < opacityEnd) {
            for (i = opacityStart; i <= opacityEnd; i += changeRate) {
                setTimeout("AdRotator.setOpacity(" + i + ",\"" + id + "\")", (timer * transitionPeriod));
                timer += changeRate;
            }
        }
    },

    // does the actual work of setting the element opacity
    setOpacity: function(opacity, id) {
        if (opacity == 100) { opacity = 99.99; }; // hack to fix flicker in Mozilla and Safari - doesnt seem to bother IE
        var el = document.getElementById(id)
        if (el) { 
            el.style.opacity = (opacity / 100);
            el.style.filter = "alpha(opacity=" + opacity + ")";
            el.style.MozOpacity = (opacity/100); 
        }
    },

    // given a adnum, it fades out the image
    // then sets the url and image source the new ad and
    // then fades the same image back in
    transitionAd: function(adNum) {
        if (!adNum) { adNum = this.currentAd; }
        if (!adNum) { adNum = 0; }

        this.doFade(this.adImgId, 100,0);
        setTimeout("AdRotator.doFade(\"" + this.adImgId +"\", 0,100);", this.transitionPeriod * 100);
        setTimeout("document.getElementById(\"" + this.adHrefId + "\").href = \"" + this.ads[adNum].url + "\"", AdRotator.transitionPeriod * 100);
        setTimeout("document.getElementById(\"" + this.adImgId + "\").src = \"" + this.ads[adNum].image + "\"", AdRotator.transitionPeriod * 100);
//        setTimeout("document.getElementById(\"" + this.adImgId + "\").src = \"" + "http://newdev02.mlb.com:5090/clubs/t504/images/ads/banner300x60.gif" + "\"", AdRotator.transitionPeriod * 100);

    },

    init: function(adHrefId, adImgId, adData) {
        if (adData) {
            // load the mediawall data and preload the images
            this.ads = adData;
            this.preloadImages(this.ads);
        } else {
            return;
//            alert("No ads defined - check data file");
        }

        // ids of images to fade on transition, and elements to innerHTML new content into
        this.adHrefId = adHrefId;
        this.adImgId  = adImgId;

        if (this.ads[0].url && this.ads[0].image) {
            document.getElementById(this.adHrefId).href = this.ads[0].url;
            document.getElementById(this.adImgId).src   = this.ads[0].image;
        }

        if (this.ads.length > 1) {
            // create the first ad and start it rotating
            this.currentAd = -1; // flip add increments it, so we need to start before 0
            this.flipAd();
        }
    }

}

