﻿
var PathImg = '../../App_Themes/Wind/Images/Menu/WindPlus/Teaser/';
var ImgUnSel = 'UnselectedPageTeaser.png';
var ImgSel = 'SelectedPageTeaser.png';

var TimeoutArray = new Array();

//var Timeout;


//http://stackoverflow.com/questions/315078/how-do-you-handle-multiple-instances-of-settimeout
var Timeout = {
    _timeouts: {},
    set: function(name, func, time){
      this.clear(name);
      this._timeouts[name] = {pending: true, func: func};
      var tobj = this._timeouts[name];
     tobj.timeout = setTimeout(function()
     { /* setTimeout normally passes an accuracy report on some browsers, this just forwards that. */
       tobj.func.call(arguments);
        tobj.pending = false;
     }, time);
    },
   hasRun: function(name)
   {
        if( this._timeouts[name] )
        {
          return !this._timeouts[name].pending;
        }
       return -1; /* Whut? */
    },   runNow: function(name)
   {
      if( this._timeouts[name] && this.hasRun(name)===false )
      {
         this._timeouts[name].func(-1); /* fake time. *shrug* */
         this.clear(name);
      }
   },
    clear: function(name)
   {
     if( this._timeouts[name] && this._timeouts[name].pending )
      {
       clearTimeout(this._timeouts[name].timeout);
        this._timeouts[name].pending = false;
      }
   }
};


var TeasersString = '';var TeasersStringsAdded = 0;
_TeaserRunning = false;
function PrepareTeaser(id,index,MaxTeaser,imgId,ClearTime){
	//_id = id;_index=index;_MaxTeaser=MaxTeaser;_imgId=imgId;_ClearTime=ClearTime;
	TeasersString += 'LoadTeaser("' + id + '",1,' + MaxTeaser + ',"' + imgId  + '",' + ClearTime + ');';
	TeasersStringsAdded ++;
	//LoadTeaser(id,index,MaxTeaser,imgId,ClearTime);
}


function FireTeaser(){
	//alert('FIRE');
	if (TeasersStringsAdded<1)
		setTimeout('FireTeaser;',1000);
	else if (!_TeaserRunning){
		eval(TeasersString);
		_TeaserRunning = true
	}
	//if (!_TeaserRunning){
		//LoadTeaser(_id,1,_MaxTeaser,_imgId,_ClearTime)
		//_TeaserRunning = true;	
	//}
}

var stoppedTeasers = new Array();
function LoadTeaser(id,index,MaxTeaser,imgId,ClearTime){
    if (stoppedTeasers.join(',').indexOf(id) > -1) return
    var select = parseInt(index);    
    //alert(id+', '+index+', '+MaxTeaser+', '+imgId);
    //if (confirm('debug?')) debugger;
        
    if(index !=0){
        index = parseInt(index) - 1;
        
        if(select >= parseInt(MaxTeaser)){
            select = 0;
        }

        for (k=0;k<=MaxTeaser;k++){
            if (document.getElementById(id+k))document.getElementById(id+k).style.display = 'none';
        }        
        var TeaserNone = document.getElementById(id+index);
        var TeaserBlock = document.getElementById(id+select);
        var TeaserPageSelImg = document.getElementById(imgId+select+select);
        var TeaserPageFirstImg = document.getElementById(imgId+select+'0');

        TeaserNone.style.display  = 'none';
        TeaserBlock.style.display  = 'block';
        TeaserPageSelImg.src = PathImg+ImgSel;
        
        if (select != 0)
            TeaserPageFirstImg.src = PathImg+ImgUnSel;
    }
        select = parseInt(select) + 1;
            
        //Timeout = setTimeout("LoadTeaser('" + id + "','"+ select +"','"+ MaxTeaser +"','"+ imgId +"')", 5000);	
        
        if (stoppedTeasers.join(',').indexOf(id) == -1)
    Timeout.set(id, function(){
        LoadTeaser(id,select,MaxTeaser,imgId);
    }, 6000 );


        //eval("var Timeout_" + id + "= setTimeout(\"LoadTeaser('"+ id +"',"+"'"+ select +"',"+"'"+ MaxTeaser +"',"+"'"+ imgId +"')\", 5000)")
        if(ClearTime){
            //clearTimeout( timeout_handles[id] )
            //clearTimeout(eval("Timeout_" + id))
            //alert(eval("Timeout_" + id));
        }
        
    //alert("= setTimeout(\"LoadTeaser('"+ id +"',"+"'"+ select +"',"+"'"+ MaxTeaser +"',"+"'"+ imgId +"')\", 5000)")
          
}

function PreviousPage(id,index,imgId,MaxTeaser){
    //if (!_TeaserRunning) return;
    //LoadTeaser(id,index,MaxTeaser,imgId,true);
    stoppedTeasers.push(id);
    Timeout.clear(id);

    var previous = parseInt(index) - 1;
    if(previous < 0){
        UnselectedPage(id,index,parseInt(MaxTeaser) - 1,imgId);
    }
    else{
        var TeaserNone = document.getElementById(id+index);
        var TeaserBlock = document.getElementById(id+previous);
        var TeaserPageSelImg = document.getElementById(imgId+previous+previous);
        var TeaserPageFirstImg = document.getElementById(imgId+previous+'0');

        TeaserNone.style.display  = 'none';
        TeaserBlock.style.display  = 'block';
        TeaserPageSelImg.src = PathImg+ImgSel;
        if(previous > 0)
            TeaserPageFirstImg.src = PathImg+ImgUnSel;
    }
}

function UnselectedPage(id,index,sel,imgId){
    //if (!_TeaserRunning) return;
    //alert(id+', '+index+', '+sel+', '+imgId);
    //LoadTeaser(id,index,'0',imgId,true);
    stoppedTeasers.push(id);
    Timeout.clear(id);

    var TeaserNone = document.getElementById(id+index);
    var TeaserSelected = document.getElementById(id+sel);
    var TeaserPageSelImg = document.getElementById(imgId+sel+sel);
    var TeaserPageFirstImg = document.getElementById(imgId+sel+'0');
  
    TeaserNone.style.display  = 'none';
    TeaserSelected.style.display  = 'block';
    TeaserPageSelImg.src = PathImg + ImgSel;
    if(sel != 0)
        TeaserPageFirstImg.src = PathImg + ImgUnSel;
}

function NextPage(id,index,MaxTeaser,imgId){
    //if (!_TeaserRunning) return;
    //LoadTeaser(id,index,MaxTeaser,imgId,true);
    stoppedTeasers.push(id);
    Timeout.clear(id);

    var next = parseInt(index) + 1;    
    if(next >= parseInt(MaxTeaser)){
        next = 0;
    }
    //else{
        var TeaserNone = document.getElementById(id+index);
        var TeaserBlock = document.getElementById(id+next);
        var TeaserPageSelImg = document.getElementById(imgId+next+next);
        var TeaserPageFirstImg = document.getElementById(imgId+next+'0');

        TeaserNone.style.display  = 'none';
        TeaserBlock.style.display  = 'block';
        TeaserPageSelImg.src = PathImg+ImgSel;
        if (next != 0)
            TeaserPageFirstImg.src = PathImg+ImgUnSel;
            
            
   //}
        //LoadTeaser(id,next,MaxTeaser,imgId);
        //setTimeout("NextPage('" + id + "','"+ index +"','"+ MaxTeaser +"','"+ imgId +"')", 5);	
        //alert(id+', '+index+', '+MaxTeaser+', '+imgId);
   //if (Play == '1'){
        //alert(Play);
        //LoadTeaser(id,index,MaxTeaser,imgId);
   //}
   
   //Timeout = setTimeout("NextPage('" + id + "','"+ index +"','"+ MaxTeaser +"','"+ imgId +"','1')", 5000);	
   

}