/**************************    HEROBOXES    *********************************/
var heronew = Class.create();
heronew.prototype = {
  initialize: function(query) {
	var container = query.container;
	this.options = Object.extend({} , arguments[0] || {}  );
    this.photos = this.options.photos || 9;
	this.onEvent = this.options.onEvent || 'mouseover';
	this.onEventOff = this.options.onEventOff || 'mouseout';
	this.onScroll = this.options.onScroll || 'click';
	this.mode = this.options.mode || "lite";
	this.effects = this.options.effects || true;
	this.fxduration = this.options.fxduration || 0.5;
	this.tabSel;
	this.tabSelText; 
	this.fx;
	this.fxfade;
	this.fxscroll;
	this.numElements;
	this.imgwidth = ( this.options.imgwidth || 100 );
	this.originalPosition;
	this.actual;
	this.posactual;
	this.waiting = false;
	this.timeoutwait;
 	this.intervalo = ( this.options.intervalo || 15000 );		
 	this.ie6 = navigator.appVersion.indexOf("MSIE 6") > 0;  	
	this.start(container);
  },
  start: function(container) {   
  	container = $(container);	
  	var elems = container.select("div.scroller");

	if (this.mode == 'news') this.imgwidth = this.ie6 ? 309 : 308;	
	else if (this.mode == 'barra' && this.imgwidth == 100) this.imgwidth = 396;		

	this.numElements = this.photos;
	this.originalPosition = 0;
	this.posactual = this.originalPosition;
	this.actual = 0;
	var prev = container.select("div.contentpager ul.pager li a.prev")[0];	
	var next = container.select("div.contentpager ul.pager li a.next")[0];
	
	var prev = (prev) ? prev : container.select("div.contentpager div.bolo a.prev")[0];
	var next = (next) ? next : container.select("div.contentpager div.bolo a.next")[0];	
	var pause = (pause) ? pause : container.select("div.contentpager div.bolo a.pause")[0];		
	
	var scroller = container.select(".scrollerhbn")[0];
	var elemsContainer = container.select(".heronew");
	elemsContainer[0].setStyle({left:this.posactual + 'px'});	
	if (prev) { 
		prev.href = "javascript:;";	
		prev.observe(this.onScroll, this.movePrev.bind(this,scroller,container,false));				
	}
	if (next) {
		next.href = "javascript:;";	
		next.observe(this.onScroll, this.moveNext.bind(this,scroller,container,false));	
	}
	if (this.mode == "barra" && pause) {
		pause.href = "javascript:;";	
		pause.observe(this.onScroll, this.movePlay.bind(this,scroller,container,pause,false));		
	}
	var i=1;
	var j=this.numElements-this.photos+1;
	if (this.numElements<this.photos){
		prev.setStyle({display:"none"});
		next.setStyle({display:"none"});
	}	
	elems.each(function(elem) {
		if (this.mode == 'lite' || this.mode == 'extendedlite'){
			if (container.select("div.sipos" + j )[0]){
				var img = container.select("div.sipos" + j + " img")[0];	
				Event.observe(img, this.onEvent, this.activate.bind(this, img), false);
				Event.observe(img, this.onEventOff, this.desactivate.bind(this, img), false);				
				var img = container.select("div.sipos" + j + "aux img")[0];	
				Event.observe(img, this.onEvent, this.activate.bind(this, img), false);
				Event.observe(img, this.onEventOff, this.desactivate.bind(this, img), false);								
				j++;
			}			
		} 
		if (this.mode == 'news' || this.mode == 'barra') {
			this.movePlay(scroller,container,pause);		
		}
    }.bind(this));
  },
  wait: function(){
	 clearTimeout(this.timeoutwaiting);
	 this.waiting = false;
  },
  moveNext: function(scroller,container,activated) { 
  	if (!this.waiting){
		if (this.mode != "news" && !activated){
			clearInterval(this.tmout);
		}		
		this.waiting = true;	
		var element = container.select('.heronew')[0];		
		var c = container.select('.pager_ico')[0];		
		if (this.mode == 'news') {
			var e = c.select(".ico"+this.actual);
			e[0].className = 'ico_inactivo ico'+this.actual;
		}		
		
		if (this.mode != 'lite') {
			this.actual = (this.actual == (this.photos - 1)) ? 0 : this.actual+1;			
			if( this.actual == 1 ){		
				this.actual = 1;
				this.posactual = 0 - this.imgwidth;
				element.setStyle({left:'0px'});		
			}
			else this.posactual = this.posactual - this.imgwidth;
		} else {
			this.posactual = this.posactual + this.imgwidth;
			if (this.mode == 'lite') {
				var hijos = element.childNodes;					
				this.actual = ( this.actual == (this.photos - 1 ) ) ? 0 : this.actual + 1;				
				for ( var cont = 0; cont < hijos.length; cont++ ){				
					var pos_x = ( cont >= this.actual ) ? cont - this.actual : ( this.photos - this.actual ) + cont ;				
					hijos[cont].setStyle({ position: 'absolute'});
					var th = this;				
					this.timeoutwait = setTimeout(function(){th.wait()},this.fxduration*1000);
					var desplazamiento = ( ( pos_x )* this.imgwidth) + pos_x;										

					hijos[cont].setStyle({ left: (desplazamiento + this.imgwidth) + 'px' });							
					this.fxscroll = new Effect.Move(hijos[cont],{x:desplazamiento,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});					
				}			
				if ( this.actual+this.photos-1 > this.photos ) {
					hijos[this.actual+this.photos-1].setStyle({ left: '0px' });							
					this.fxscroll = new Effect.Move(hijos[this.actual+this.photos-1],{x:0-this.imgwidth-1,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});							
				} else {			
					if (this.actual == 1 ) {
						hijos[9].setStyle({ left: '0px' });							
						this.fxscroll = new Effect.Move(hijos[9],{x:0-this.imgwidth-1,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});					
					}
					else if (this.actual == 0 ) {
						hijos[17].setStyle({ left: '0px' });							
						this.fxscroll = new Effect.Move(hijos[17],{x:0-this.imgwidth-1,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});							
					}
				}
			}	
		}
		
		if (this.mode == 'news') {
			var e = c.select(".ico"+this.actual);
			e[0].className = 'ico_activo ico'+this.actual;
		}		
		
		if ( this.mode != 'lite' ) {
			var th = this;
			this.timeoutwait = setTimeout(function(){th.wait()},this.fxduration*1000);
			this.fxscroll = new Effect.Move(element,{x:this.posactual,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});			
		}
	}
  },
  movePrev: function(scroller,container,activated) {   
  	if (!this.waiting){
		this.waiting = true;
		var element = container.select('.heronew')[0];
		var c = container.select('.pager_ico')[0];		
		if (this.mode == 'news') {
			var e = c.select(".ico"+this.actual);
			e[0].className = 'ico_inactivo ico'+this.actual;
		}		
		if (this.mode != 'lite') {
			this.actual = (this.actual == 0) ? (this.photos - 1) : this.actual-1;
			
			if (this.posactual==0){
				this.posactual = this.originalPosition - (this.photos*this.imgwidth);
				this.actual = this.photos-1;
				element.setStyle({left:this.posactual + 'px'});	
			}
		}
		this.posactual = this.posactual + this.imgwidth;
		if (this.mode == 'lite') {
			this.actual = (this.actual == 0) ? this.photos - 1 : this.actual - 1;
			var hijos = element.childNodes;
			for ( var cont = 0; cont < hijos.length; cont++ ){
				var pos_x = ( this.actual > cont ) ? ( this.photos - this.actual ) + cont : cont - this.actual;
				hijos[cont].setStyle({ position: 'absolute'});
				var th = this;				
				this.timeoutwait = setTimeout(function(){th.wait()},this.fxduration*1000);
				var desplazamiento = ( ( pos_x + 1 )* this.imgwidth) - this.imgwidth + pos_x;
				hijos[cont].setStyle({ left: (desplazamiento - this.imgwidth) + 'px' });							
				this.fxscroll = new Effect.Move(hijos[cont],{x:desplazamiento,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});
			}
/*			if ( this.actual+this.photos-1 > this.photos ) {
				hijos[this.actual+this.photos-1].setStyle({ left: '808px' });							
				this.fxscroll = new Effect.Move(hijos[this.actual+this.photos-1],{x:909,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});
			}*/
		}	
		if (this.mode == 'news') {			
			var e = c.select(".ico"+this.actual);
			e[0].className = 'ico_activo ico'+this.actual;
		}	
		if ( this.mode != 'lite' ) {
			var th = this;
			this.timeoutwait = setTimeout(function(){th.wait()},this.fxduration*1000);
			this.fxscroll = new Effect.Move(element,{x:this.posactual,y:0,mode:'absolute',transition: Effect.Transitions.sinoidal, duration: this.fxduration});
		}
	}
  },
  movePlay: function(scroller,container,elemplay) {
	if ( this.mode == "news" ) {
		var th = this;
		this.tmout = setInterval(function(){th.moveNext(scroller,container,true)},this.intervalo);
	}
	if ( this.mode == "barra" ) { 
		if ( elemplay ) {
			var img = elemplay.select("img")[0];				
			if (elemplay.hasClassName("pause")){
				var th = this;
				this.tmout = setInterval(function(){th.moveNext(scroller,container,true)},this.intervalo);
				elemplay.removeClassName('pause');
				elemplay.addClassName('play');			
			}else if (elemplay.hasClassName("play")){
				clearInterval(this.tmout);
				elemplay.removeClassName('play');
				elemplay.addClassName('pause');
			}
			img.src = (img.src.indexOf('pause.gif') < 0) ? img.src.replace(/play.gif/, 'pause.gif') : img.src.replace(/pause.gif/, 'play.gif');						
		} else {
			var th = this;
			this.tmout = setInterval(function(){th.moveNext(scroller,container,true)},this.intervalo);			
		}
	}	
  },
  activate: function(container){
	container.src = (container.src.indexOf('_hover.gif')<0) ? container.src.replace(/.gif/, '_hover.gif') : container.src;
  },
  desactivate: function(container){
	container.src = (container.src.indexOf('_hover.gif')<0) ? container.src : container.src.replace(/_hover.gif/, '.gif');
  }
}
/**************************    HEROBOXES    *********************************/

