/**********************************************************************
 * Creador: Fernando Soto 						*
 * Programa: Carrousel.jsp					*
 * Fecha de Creación: 05/09/2008					*
 * Libre de uso, siempre y cuando aparescan los créditos del creados,  									*
 					*
 *********************************************************************/
PeriodicalExecuter.prototype.registerCallback = function() {
    this.intervalID = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
}

PeriodicalExecuter.prototype.stop = function() {
    clearInterval(this.intervalID);
}



var TvCarrousel = Class.create({
							   
							   
   options: {
		SlideClass:'divTv',
		ButtonClass:'btDivTv',
		width: '637',
		height : '237',
		noLinks : false,
		timer : 7
	},
							   
  initialize: function(options) {
	
	Object.extend(this.options,options || {});
	
	this.width   = this.options.width;
	this.height  = this.options.height;
	this.timer   = this.options.timer;
	this.noLinks = this.options.noLinks;
	
	
		this.index = 0;
		this.oring = 1;
		
		this.getGlobals();
		this.setSlideClass();
		this.setButtonClass();
		this.setDescClass();
		this.createObjects();
	
	
  },
  
  getGlobals : function(){
		
		this.maxMove = -80 ;
		
		if(navigator.appName == 'Microsoft Internet Explorer'){
		
			this.Awidth = this.options.width + 'px';
			
		}else{
			
			this.Awidth = (this.options.width -10) + 'px';
		
		}
	  
  },
  
  setSlideClass : function(clase){
	  
	   this.objs = $$( this.options.SlideClass ); 
	  
  },
  
   setButtonClass : function(){
	  
	  this.Bts  = $$( this.options.ButtonClass );
	  
  },
  
   setDescClass : function(){
	
	
		this.Desc = $$( this.options.DesClass );
	  
  },
  
  createObjects : function(){
	  
	  
	 this.objs.each(function(it,idx){
							 
		it.id 			= 'slideCont'+idx;
		it.style.width  = this.options.width + 'px';
		it.style.height = this.options.height + 'px';
		it.className    = 'edivTv';
		it.absolutize();
		it.hide();
		
		
		var bt = this.Bts[idx];		
		
		bt.id  = 'BtSlide_'+idx; 
		bt.setStyle({cursor:'pointer'});
		
		this.slideLinks(bt,idx);
		
		
		Desc  	  	  = Object.extend(this.Desc[idx]);	
		Desc.id   	  = 'DescSlide'+idx;
		Desc.setStyle({ 
			
			width:this.Awidth, 
			height:'60px', 
			position:'absolute', 
			left:'0', 
			bottom:'-1'
			
		});			
		

								  
	}.bind(this));
	 

	 this.next(0);
	 this.initApi();
  },
  
  initApi : function(){
	
	this.Hnext = this.next.bindAsEventListener(this);
	this.PE = new PeriodicalExecuter(this.Hnext,this.timer);
	

	  
  },
  
  next : function(idx){

	if(typeof(idx) == 'object'){
		
		this.tmp = this.index;
		this.tmp++;
	
	}else{
		
		this.tmp   = idx;	

		if(this.PE)
		this.PE.stop();
		
	}
	
	
	
	
	this.nidx = this.tmp >= this.objs.length ? 0 : this.tmp ;

		
		this.changeAparenceTab();
		this.changePicture();
		this.showDescription();
		
		
	
	
	this.index = this.nidx == 0 ? 0 : this.nidx;
	this.oring = null;
	
  },
  
  
 slideLinks : function(bt,idx){
	
	btChild = bt.childElements();
		 
			 if(this.noLinks){
			 
			 btChild.each(function(header,m){
				
				 if ('H3' == header.tagName){
						
						 links = header.childElements();					
						 links.each(function(a,n){
									
								 if ('A' == a.tagName){
										
										a.id = 'link_'+idx;
										this.setNext(a);
									 
								 }
									
									
						}.bind(this));
						
				 }
			
			}.bind(this));
		 
		 }
	 
 },
  
  changePicture :function (){
	  
		Effect.Appear(this.objs[this.nidx].id,{duration:0.8});
		this.hideSlide();
  },
  
  
  hideSlide : function(){
	  
	  if(this.index > 0 && this.oring != 1){
		  
			Effect.Fade(this.objs[this.index].id,{duration:0.8});		
			
		}
		
		new Effect.Move(this.Desc[this.index], { x:0, y:80 , mode: 'relative' });
		
  },
  
  
  showDescription : function(){
	  
	this.DesDiv = this.Desc[this.nidx];	
	this.DesDiv.style.top = this.height;	
	new Effect.Move(this.DesDiv, { x:0, y:this.maxMove , mode: 'relative' });
	  
  },
  
  
  
  changeAparenceTab:function(){
	  
	  this.Bts[this.nidx].addClassName('activeNote');
	  this.Bts[this.index].removeClassName('activeNote');
	  
	 // new Effect.Highlight(this.parentNode, { startcolor: '#ffff99', endcolor: '#fffffff' })
	  
	  
  },
  
  setNext : function(obj){
	  
	this.HnextExe = this.nextExecute.bindAsEventListener(this);	
	
	obj.href = "javascript:;";
	Event.observe(obj,'click',this.HnextExe);
	
  },
  
  
  nextExecute : function(event){
	  
	   		element = Event.element(event);			
			
			var data = (element.id).split('_');
			
			
			
			this.Desc[data[1]].style.top = '243px';
			this.next(data[1]);
			this.initApi();
			
			
  }
  
  
});



