﻿var isIE;
function preset(divs){
    var l = divs.length;
    for(var i=0;i<l;i++){
        if(!isIE){
            document.getElementById(divs[i]).style.opacity = 0;
        }else{
            document.getElementById(divs[i]).filters.alpha.opacity = 0;
        }
    }
}
function SimpleRotator(holderName, images, slideTime, transitionTime){
    this.currentIndex = 0;
    this.holder = document.getElementById(holderName);
    this.images = images;
    this.slideTime = slideTime * 1000;
    this.transitionTime = transitionTime * 1000;
    this.timer;
    
    this.imgLoader;
    this.currentLoadIndex = 0;
    
    this.init = function(){
        this.currentIndex = 0;
        this.currentLoad = 0;
        
        if(!isIE){
            this.holder.style.opacity = 0;
        }else{
            this.holder.filters.alpha.opacity = 0;
        }
        
        this.imgLoader = new ImageLoader();
        this.imgLoader.setImage(this.images[this.currentLoadIndex], {scope:this, func:"onImgLoadComplete"});
    }
    
    this.onImgLoadComplete = function(target){
        if(this.currentLoadIndex == this.currentIndex){
            this.showCurrent();
            this.start();
        }
        
        this.currentLoadIndex++;
        if(this.currentLoadIndex < this.images.length) this.imgLoader.setImage(this.images[this.currentLoadIndex], {scope:this, func:"onImgLoadComplete"});
    }
    
    this.start = function(){
        if(this.images.length > 1){
            var thisObj = this;
            this.timer = setInterval(function() { thisObj.slideAction(); },this.slideTime + this.transitionTime);
        }
    }
    
    this.slideAction = function(){
        this.incrementIndex();
        this.hideCurrent();
    }
    
    this.hideCurrent = function(){
        new Tween(this.holder, this.transitionTime, {opacity:0}, {delay:100, scope:this, completeListener:"showCurrent"});
    }
    
    this.showCurrent = function(){
        this.holder.innerHTML = "<img src='"+this.images[this.currentIndex]+"' />";
        new Tween(this.holder, this.transitionTime, {opacity:1}, {delay:100});
    }
    
    this.incrementIndex = function(){
        this.currentIndex++;
        if(this.currentIndex >= this.images.length) this.currentIndex = 0;
    }
    
    this.init();
}
function CrossFadeRotator(holder1, holder2, images, slideTime, transitionTime){
    this.currentIndex = 0;
    this.holder1 = document.getElementById(holder1);
    this.holder2 = document.getElementById(holder2);
    this.top;
    this.bottom;
    
    this.images = images;
    this.slideTime = slideTime * 1000;
    this.transitionTime = transitionTime * 1000;
    this.timer;
    
    this.imgLoader;
    this.currentLoadIndex = 0;
    
    this.init = function(){
        if(this.images.length > 1){
            this.currentIndex = 0;
            this.currentLoad = 0;    
            
            this.setLayers(this.holder1, this.holder2);
            this.top = this.holder1;
            this.bottom = this.holder2;
        
            this.imgLoader = new ImageLoader();
            this.imgLoader.setImage(this.images[this.currentLoadIndex], {scope:this, func:"onImgLoadComplete"});
        }
    }
    
    this.onImgLoadComplete = function(){
        if(this.currentLoadIndex == this.currentIndex){ this.populateCurrent(); new Tween(this.top, 500, {opacity:1}, {delay:100}); }
        if(this.currentLoadIndex == this.currentIndex + 1) this.start();
        
        this.currentLoadIndex++;
        if(this.currentLoadIndex < this.images.length) this.imgLoader.setImage(this.images[this.currentLoadIndex], {scope:this, func:"onImgLoadComplete"});
    }
    
    this.start = function(){
        if(this.images.length > 1){
            var thisObj = this;
            this.timer = setInterval(function() { thisObj.slideAction(); },this.slideTime + this.transitionTime);
        }
    }
    
    this.slideAction = function(){
        this.populateCurrent();    
        this.showCurrent();
        this.incrementIndex();
    }
    
    this.showCurrent = function(){
        if(!isIE){
            this.top.style.opacity = 1;
            this.bottom.style.opacity = 0;
        }else{
            this.top.filters.alpha.opacity = 100;
            this.bottom.filters.alpha.opacity = 0;
        }
        
        new Tween(this.top, this.transitionTime, {opacity:0}, {delay:100});
        new Tween(this.bottom, this.transitionTime, {opacity:1}, {delay:100});
    }
    
    this.populateCurrent = function(){
        var nextIndex = this.currentIndex + 1;
        if(nextIndex >= this.images.length) nextIndex = 0;
        
        this.top.innerHTML = "<img src='"+this.images[this.currentIndex]+"' />";
        this.bottom.innerHTML = "<img src='"+this.images[nextIndex]+"' />";
    }
    
    this.incrementIndex = function(){
        this.currentIndex++;
        if(this.currentIndex >= this.images.length) this.currentIndex = 0;
    }
    
    this.setLayers = function(top, bottom){
        top.style.zIndex = 1001;
        bottom.style.zIndex = 1000;
    }
    
    this.init();
}
