galeria=function(aElemList,achildrenType,aRotacion,aBtnAnterior,aBtnSiguiente,aPaginador,aoffsetPaginado,aoffsetPaging){
	this._elementoListable=aElemList;
	this._childrenType=achildrenType;
	this._rotacion=aRotacion;
	this._rotando=true;
	this._hWdRotate=null;
	this._BtnAnterior=aBtnAnterior;
	this._BtnSiguiente=aBtnSiguiente;
	this._Paginador=aPaginador;	
	this._contentScrolled=null;	
	this._elemCurrent=0;
	this._widthContent=0;
	this._offsetPaginado=aoffsetPaginado;
	this._ElementPaging;
	this._offsetPaging=aoffsetPaging;
	this._Elem4page;
			this._pagess=1;
	
}
galeria.prototype= {
	 initialize: function() {
		this._ElementPaging=0;
		
	 	_self=this;
	 	this._contentScrolled=new Array();
	 	this._widthContent=this._elementoListable.find("img.imagenBanner").attr("width");

		this._Elem4page = Math.round($(".PieSecciones").css("width").replace("px","")/_self._Paginador.find("a").css("width").replace("px",""),0);		
		if ($(document).width()<=1024)
		{
			$(".PieSecciones").find("div").css("width", _self._Elem4page * _self._Paginador.find("a").css("width").replace("px","") +"px" );
		}
		$(".PieSecciones").find("div").find("div").css("width","10000px")
		this._pagess=Math.round(_self._elementoListable.children(_self._childrenType).size()/this._Elem4page )
	 	$.each(_self._elementoListable.children(_self._childrenType), function(i, Row) {
	 			 _self._contentScrolled.push(Row);
	 	 });
		
		$(this._contentScrolled[this._elemCurrent]).css("left","0px")
 		
	 	this._BtnAnterior.click(function(){
	 		_self._rotando=false;
	 		if(_self._ElementPaging>0){
	 		_self._ElementPaging--;
	 		}
	 		else{
	 		_self._ElementPaging=_self._pagess-1;
	 		}
			var newValue=_self._ElementPaging*_self._Elem4page ;
			/*if (newValue==0)
				newValue++*/
			_self._Paginador.find("div").css("left",0+(newValue)*(_self._Paginador.find("a").css("width").replace("px",""))*(-1)+"px")
			});
		this._BtnSiguiente.click(function(){
			_self._rotando=false;

			if(_self._ElementPaging<_self._pagess-1){
	 		_self._ElementPaging++;
	 		}
	 		else{
	 		_self._ElementPaging=0;
	 		}
			var newValue=_self._ElementPaging*_self._Elem4page ;

			/*if (newValue==0)
				newValue++*/
			_self._Paginador.find("div").css("left",0+(newValue)*(_self._Paginador.find("a").css("width").replace("px",""))*(-1)+"px")
//	 		_self.goto(newValue);
//			_self.selPager(newValue);
	 		//_self.avanza();
	 	});
	 	
	 	if(this._rotacion.attr("value")!="0"){

			_self._elementoListable.hover(
			      function () {
			     _self._rotando=false;
			     //_self.rota(10000000000)
       			      }, 
			      function () {
			      //rota(_self._rotacion.attr("value"));
 				_self._rotando=true;

			      }
			    );
			//_self._Paginador.bind("mouseout",function(){setTimeout(function(){galery._rotando=true;rota();}, 8000);});
		_self.rota(_self._rotacion.attr("value"));
	 	}
	 	
	 }
	 ,
	 rota:function(aTime){

		if(!this._rotando){ 
			setTimeout(function(){rota();}, aTime);
			return;
		}

		 setTimeout(function(){avanza();rota();}, aTime);

	 },
	initializePager:function(){
		_self=this;
		_self._Paginador.find("a").bind("mouseover",function(){
			try{
			//	$($(this).find('img')[0]).trigger("mouseover");
			}
			catch(e)
			{
			}

		});
		_self._Paginador.find("a").bind("mouseout",function(){
		try{
			//$($(this).find('img')[0]).trigger("mouseout");
			}
			catch(e)
			{
			}
		});		 
		_self._Paginador.find("a").click(function(){
			var posicion;
			posicion=Array.indexOf($(".PieSecciones").find("div").find("a"),this);
			
			//if(_self._elemCurrent!=0){
			_self.goto(posicion+1+_self._offsetPaginado);
			_self.selPager(posicion+1);
			//}
			//else{
			//_self.goto(posicion+1);

//			_self.selPager(0);

			//}
			
		});

	},
	avanza: function() {
		_self=this;

		oldID=this._elemCurrent
		if (this._elemCurrent<this._contentScrolled.length-1){
			this._elemCurrent++;
		}
		else{
			this._elemCurrent=0;
		}	
		if($.browser.msie)
		$(_self._contentScrolled[oldID]).find("span,img,a").fadeOut(100)
		
		$(_self._contentScrolled[oldID]).fadeOut("100",function(){
			$(_self._contentScrolled[oldID]).css("left",_self._widthContent*(-1) +"px");
			$(_self._contentScrolled[_self._elemCurrent]).css("left","0px");
	
			$(_self._contentScrolled[_self._elemCurrent]).fadeIn("100")
			if($.browser.msie)
			$(_self._contentScrolled[_self._elemCurrent]).find("span,img,a").fadeIn("100")
		})

		if(this._Paginador !=null)
									
			this.selPager (this._elemCurrent)
		

	},
	atras : function(){
		
			
		_self=this;
		oldID=this._elemCurrent
		if (this._elemCurrent>0){
			this._elemCurrent--;
		}
		else{
			this._elemCurrent=this._contentScrolled.length-1;
		}
		if($.browser.msie)
		$(_self._contentScrolled[oldID]).find("span,img,a").fadeOut("100")

		$(_self._contentScrolled[oldID]).fadeOut("100",function(){
			$(_self._contentScrolled[oldID]).css("left",_self._widthContent +"px");
			$(_self._contentScrolled[_self._elemCurrent]).css("left","0px");
	

			$(_self._contentScrolled[_self._elemCurrent]).fadeIn("100")
			if($.browser.msie)
			$(_self._contentScrolled[_self._elemCurrent]).find("span,img,a").fadeIn("100")
			
		})



		
		if(this._Paginador !=null)
		this.selPager (this._elemCurrent)
		
	},
	goto : function(aImg){
		_self._rotando=false;

		oldID=this._elemCurrent
		this._elemCurrent=aImg-1;

		if($.browser.msie)		
		$(_self._contentScrolled[oldID]).find("*").fadeOut("100")

		$(_self._contentScrolled[oldID]).fadeOut("100",function(){
			$(_self._contentScrolled[oldID]).css("left",_self._widthContent*(-1) +"px");
			$(_self._contentScrolled[_self._elemCurrent]).css("left","0px");
	

			$(_self._contentScrolled[_self._elemCurrent]).fadeIn("100")
			if($.browser.msie)			
			$(_self._contentScrolled[_self._elemCurrent]).find("*").fadeIn("100")
			
		})


	},
	selPager : function(aInt){
		_self=this;
			
		var event = jQuery.Event("mouseout");

		$.each(_self._Paginador.find("a").find('img'), function(i2, Rows) {
			$(Rows).trigger("mouseout")
		});
		

		_self._Paginador.find("a").removeClass("current");
				
		if(this._elemCurrent==0){_self._Paginador.find("div").css("left","0px");return;}
		_self._Paginador.find("a:eq("+ (aInt -1) +")").addClass("current");
		
		ancho=_self._Paginador.find("a").css("width").replace("px","")
		
		if(aInt*(ancho)>$(".PieSecciones").find("div").width()){
			_self._Paginador.find("div").css("left",0+(_self._Elem4page)*(_self._Paginador.find("a").css("width").replace("px",""))*(-1)+"px")
		}else{
			_self._Paginador.find("div").css("left","0px")//+(aInt-4)*(_self._Paginador.find("a").css("width").replace("px",""))*(-1)+"px")
		}
		var event = jQuery.Event("mouseover");
		 _self._Paginador.find("a:eq("+ (aInt -1) +")").find('img').trigger(event);


	}
	,
	crearPaginador : function(){
		
		_self=this;
		_self._Paginador.empty();
		$("<ul></ul>").appendTo(_self._Paginador.selector);
		$.each(_self._elementoListable.children(_self._childrenType), function(i2, Rows) {
	 		$("<li><a title='" + (i2 + 1 ) + "'>" + i2 + "</a></li>").appendTo($(_self._Paginador.selector).children("ul"));
	 	 });
	 	 _self.selPager (2);
		_self._Paginador.find("a").click(function(){
			_self.goto(this.title);
			_self.selPager(this.title);
			
		});
	}
}
var galery;
$(document).ready(function(){
	
	galery=new galeria($(".ScrolledImg"),"div",$("input[id$='bnrRotado']"),$(".antSec"),$(".segSec"),$(".PieSecciones").find("div"),1,5)
	galery.initialize();
	
	galery.initializePager();
	//galery.crearPaginador ();
});

function avanza(){
	if(!galery._rotando){return;}
	galery.avanza();
	galery._rotando=true;	

}

function rota(){
	galery.rota($("input[id$='bnrRotado']").attr("value"));

}