﻿var KalFadeShow = function(actors,stages,name,doStatic,w,h){
    this._actors = actors;
    this._stages = stages;
    this._name = name;
    this._doStatic = doStatic;
    this._w=w;
    this._h=h
}
KalFadeShow.prototype = {
    get_actors : function(){
        return this._actors;
    },
    get_stages : function(){
        return this._stages;
    }, 
    start : function(){
        this.speed = 8000;
        try{var testanim = new Animator()}catch(er){alert("no animator object available. add the AnimationScript control");return false;}
        if(this.get_stages().length==0){alert("There were no stages found.");return false;}
        if(this.get_actors().length==0){return false;}        
        this.timers = new Array();        
        var actIndex = 0;
        for(var t =0; t<this.get_stages().length; t++){        
            this.get_stages()[t].actors=this.get_actors();
            this.get_stages()[t].currentActor0Index = actIndex;
            this.get_stages()[t].currentActor1Index = this.get_nextActorIndex(actIndex);
            if(actIndex<this.get_actors().length-1){
                actIndex++;
            }else{
                actIndex = 0;
            }            
            this.get_stages()[t].style.position="relative";  
            
            
            var div = document.createElement("div");
            div.id=this._name + "_StageScene_"+t+"_0";
            div.style.position="absolute";
            var a = document.createElement("a");
            a.id=this._name + "_StageScene_a_"+t+"_0";
            a.href = this.get_stages()[t].actors[this.get_stages()[t].currentActor0Index].LinkUrl;
            a.target = this.get_stages()[t].actors[this.get_stages()[t].currentActor0Index].LinkTarget;
            var img = new Image();
            img.style.width = this._w + "px";
            img.style.height = this._h + "px";
            img.id=this._name + "_StageScene_img_"+t+"_0";
            img.src = this.get_stages()[t].actors[this.get_stages()[t].currentActor0Index].FilePathAndName;
            img.alt = this.get_stages()[t].actors[this.get_stages()[t].currentActor0Index].Title;
            a.appendChild(img);
            div.appendChild(a);            
            this.get_stages()[t].appendChild(div);   
            
                     
            div = document.createElement("div");
            div.id=this._name + "_StageScene_"+t+"_1";
            div.style.position="absolute";
            //For Testing
            //div.style.top="100px";
            //For Testing
            a = document.createElement("a");
            a.id=this._name + "_StageScene_a_"+t+"_1";
            a.href = this.get_stages()[t].actors[this.get_stages()[t].currentActor1Index].LinkUrl;
            a.target = this.get_stages()[t].actors[this.get_stages()[t].currentActor1Index].LinkTarget;
            img = new Image();
            img.style.width = this._w + "px";
            img.style.height = this._h + "px";
            img.id=this._name + "_StageScene_img_"+t+"_1";
            img.src = this.get_stages()[t].actors[this.get_stages()[t].currentActor1Index].FilePathAndName;
            img.alt = this.get_stages()[t].actors[this.get_stages()[t].currentActor1Index].Title;
            a.appendChild(img);
            div.appendChild(a);            
            this.get_stages()[t].appendChild(div);
            
            this.get_stages()[t].anA = new Animator({duration: 1200,interval: 400,oncomplete : function(){anA=null;}}).addSubject(new NumericalStyleSubject($get(this._name + "_StageScene_"+t+"_0"), 'opacity', 1, 0));
            this.get_stages()[t].anA.seekTo(1);
        }
        this.onDeck = "0";
        
        if(this._doStatic==false){           
            this.TimerHandler = Function.createDelegate(this, this.processFade);
            this.Timer = setTimeout(this.TimerHandler,this.speed); 
        }
        
    },
    
    
    get_nextActorIndex : function(index){
        if(index < (this.get_actors().length-1)){
            index++;
        }else{
            index=0;
        }
        return index;
    },
    processFade : function(){
        for(var t =0; t<this.get_stages().length; t++){
            if(this.onDeck == "0"){
                if(t==(this.get_stages().length-1)){
                    this.crossFade($get(this._name + "_StageScene_"+t+"_1"), $get(this._name + "_StageScene_"+t+"_0"),true);
                }else{
                    this.crossFade($get(this._name + "_StageScene_"+t+"_1"), $get(this._name + "_StageScene_"+t+"_0"),false);
                }
                $get(this._name + "_StageScene_"+t+"_1").style.zIndex=1;
                $get(this._name + "_StageScene_"+t+"_0").style.zIndex=2;
            }else{
                if(t==(this.get_stages().length-1)){
                    this.crossFade($get(this._name + "_StageScene_"+t+"_0"), $get(this._name + "_StageScene_"+t+"_1"),true);
                }else{
                    this.crossFade($get(this._name + "_StageScene_"+t+"_0"), $get(this._name + "_StageScene_"+t+"_1"),false);
                }
                $get(this._name + "_StageScene_"+t+"_0").style.zIndex=1;
                $get(this._name + "_StageScene_"+t+"_1").style.zIndex=2;
            }
        } 
    },
    crossFade : function(a,b,islast){
    
        if(islast==true){
            a.changeActorsHandler = Function.createDelegate(this, this.changeActors);
            a.anA = new Animator({duration: 1200,interval: 400, onComplete: a.changeActorsHandler}).addSubject(new NumericalStyleSubject(a, 'opacity', 1, 0));
        }else{
            a.anA = new Animator({duration: 1200,interval: 400}).addSubject(new NumericalStyleSubject(a, 'opacity', 1, 0));
        }
        b.anB = new Animator({duration: 1200,interval: 400}).addSubject(new NumericalStyleSubject(b, 'opacity', 0, 1));
        a.anA.play();
        b.anB.play();
    },
    changeActors : function(){
    
        if(this.onDeck=="1"){
            this.onDeck="0";
        }else{
            this.onDeck="1";
        }
        for(var t =0; t<this.get_stages().length; t++){
            if(this.onDeck=="0"){
                
                this.get_stages()[t].currentActor0Index = this.get_nextActorIndex(this.get_stages()[t].currentActor0Index);
                
                var obj = this.get_stages()[t].actors[this.get_stages()[t].currentActor0Index];
                
                $get(this._name + "_StageScene_a_"+t+"_0").href = obj.LinkUrl;
                $get(this._name + "_StageScene_a_"+t+"_0").target = obj.LinkTarget;
                $get(this._name + "_StageScene_img_"+t+"_0").src = obj.FilePathAndName;
                $get(this._name + "_StageScene_img_"+t+"_0").alt = obj.Title;
                
            }else{
            
                this.get_stages()[t].currentActor1Index = this.get_nextActorIndex(this.get_stages()[t].currentActor1Index);
                
                var obj = this.get_stages()[t].actors[this.get_stages()[t].currentActor1Index];
                
                $get(this._name + "_StageScene_a_"+t+"_1").href = obj.LinkUrl;
                $get(this._name + "_StageScene_a_"+t+"_1").target = obj.LinkTarget;
                $get(this._name + "_StageScene_img_"+t+"_1").src = obj.FilePathAndName;
                $get(this._name + "_StageScene_img_"+t+"_1").alt = obj.Title;
                
            }
        }
        clearTimeout(this.Timer);
        this.TimerHandler = Function.createDelegate(this, this.processFade);
        this.Timer = setTimeout(this.TimerHandler,this.speed); 
    }
}
