/**
CARRUSEL
**/
var ACarrusel= new Class({
	Implements : [Options,Events],
	contenedor:"",
	options:{
		anchoItem:"",
		altoItem:"",
		ajustar_ancho: 300, // por implementar para ajustar el ancho del contenedor run-in-time
		ver:2,
		avanze:2,
		anterior:"",
		siguiente:"",
		duracion:0.5,
		auto:"",
		anchoTotal:"",
		transicion:"linear",
		ruedita:"",
		controles:"si",
		modo:"horizontal",
		onForward:$empty,
		onBack:$empty,
		onMove:$empty,
		useKeys:false,
		circular:false,
		regresaAlPrimero:false,
		ofssetX:0,
		ofssetY:0,
		_wrap:''
	},
	anchoTotal:"",
	timer:"",
	hijos:"",
	_actual:'',
	moveTo:$empty,
	anchoCarrusel:'',
	numeroItems:'',
	initialize:function (contenedor,options){
		this.setOptions(options);
		this.contenedor=$(contenedor);
		padre=this.contenedor.getParent().getCoordinates();
		var warp= new Element("div",{
			styles:{
				"overflow":"hidden",
				"position":"relative",
				"width":padre.width
			}
		});
		this._wrap=warp;
		if(this.options.ruedita!=""){
			this.contenedor.addEvents({
				"mousewheel":function(e){
					var e = new Event(e).stop();
					if ( e.wheel > 0  ) {
						this.mover("adelante");
					}else{
						this.mover("atras");
					}
				}.bind(this)
			});
		}
		if(this.options.useKeys){
			switch(this.options.modo){
				case "horizontal":
					this.contenedor
					.addEvent("keydown",function(e){
						e.stop();
						alert("asas");
						switch(e.key){
							case "left":this.mover("atras")	;break;
							case "right":this.mover("adelante");break;
							default:break;
						}
					}.bind(this));
				break;
				case "vertical":
					this.contenedor
					.addEvent("keydown",function(e){
						e.stop();
						alert("asas");
						switch(e.key){
							case "up":this.mover("atras");break;
							case "down":this.mover("adelante");break;
							default:break;
						}
					}.bind(this));
				break;
			}
		}
		this.contenedor.getParent().grab(warp);
		warp.adopt(this.contenedor);
		var ancho=this.contenedor.getParent().getCoordinates();	
		if(this.options.anchoItem=="" && this.options.altoItem==""){
			this.options.anchoItem= (ancho.width.toInt()/this.options.ver);
			this.options.altoItem= (ancho.height.toInt()/this.options.ver);
		}
		var nodos=this.contenedor.getChildren();
		this.hijos=nodos;
		this.anchoTotal=this.options.anchoItem * nodos.length;		
		this.contenedor	.setStyles({
			"overflow":"hidden",
			"position":"relative",
			"list-style":"none",
			"margin":0,
			"padding":0,
			"width":parseInt(this.anchoTotal)+ parseInt(this.options.ofssetX)+300
			});		
		this.contenedor.getChildren();
		$items=this.contenedor.getChildren();
		this.numeroItems=$items.length;								
		switch(this.options.modo){
			case "horizontal":
				$items.setStyles({"float":"left","height":this.options.altoItem,"width":this.options.anchoItem});
			break;
			case "vertical":
				this.contenedor.setStyles({height:this.options.altoItem * nodos.length,"width":this._wrap.getStyle("width")});
				$items.setStyles({"height":this.options.altoItem,"width":this.options.anchoItem});
			break;
		}		
		$items.each(function(e,k){
			e.addEvents({
				"mouseenter":function(){
					if(this.options.auto!=""){
						this.detenerAutomovible();
					}
				}.bind(this),
				"mouseleave":function(){
					if(this.options.auto!=""){
						this.automovible();
					}
				}.bind(this)
			});
		}.bind(this));		
		if(this.options.controles=="si"){		
			$(this.options.anterior).setStyle("cursor","pointer");
			$(this.options.siguiente).setStyle("cursor","pointer");			
			$(this.options.siguiente)			.addEvent("click",function(){
				this.mover("adelante");
			}.bind(this));			
			$(this.options.anterior).			addEvent("click",function(){				this.mover("atras");
			}.bind(this));
		}		
		if(this.options.auto!=""){
			this.automovible();
		}		
		switch(this.options.modo){
			case "horizontal":
				this._actual=this.contenedor.getStyle("margin-left").toInt();
			break;
			case "vertical":
				this._actual=this.contenedor.getStyle("margin-top").toInt();
			break;
		}				if(this.options.circular){					this.options.avanze=1;			this.contenedor.getLast("li").inject(this.contenedor,"top");						switch(this.options.modo){				case "horizontal":					this.contenedor.setStyle("margin-left",-this.options.anchoItem);					this._actual=this.contenedor.getStyle("margin-left").toInt();				break;				case "vertical":					this.contenedor.setStyle("margin-top",-this.options.anchoItem);					this._actual=this.contenedor.getStyle("margin-top").toInt();				break;			}				}				
	},
	automovible:function(){
		this.timer=this.mover.periodical(this.options.auto * 1000,this,["adelante"]);
	},
	detenerAutomovible:function(){
		$clear(this.timer);
	},
	/*onForward:function(){},
	onBack:function(){},
	*/
	mover:function(direccion){
			if(direccion=="adelante"){
				this.contenedor.set("morph",{
					duration:this.options.duracion * 1000,wait:true,transition:this.options.transicion,
					onStart:function(){
						switch(this.options.modo){
							case "horizontal":
								itemactual= - (this.contenedor.getStyle("margin-left").toInt());//this.anchoTotal
								aver=(itemactual/this.options.anchoItem)+1;
							break;
							case "vertical":
								itemactual= - (this.contenedor.getStyle("margin-top").toInt());//this.anchoTotal
								aver=(itemactual/this.options.anchoItem)+1;
							break;
						}
						if(!this.options.circular){
							if(aver>(this.hijos.length-this.options.avanze)){
								this.contenedor.get("morph").cancel();
								if(this.options.regresaAlPrimero){
									this.moverAlPrimero();
								}
								return false;
							}						}
						//this.fireEvent("forward");
					}.bind(this),
					onComplete:function(){
						switch(this.options.modo){
							case "horizontal":
								this._actual=this.contenedor.getStyle("margin-left").toInt();
							break;
							case "vertical":
								this._actual=this.contenedor.getStyle("margin-top").toInt();
							break;
						}						if(this.options.circular){
							this.contenedor.getFirst("li").inject(this.contenedor);							switch(this.options.modo){								case "horizontal":									this.contenedor.setStyle("margin-left",-this.options.anchoItem);									this._actual=this.contenedor.getStyle("margin-left").toInt();								break;								case "vertical":									this.contenedor.setStyle("margin-top",-this.options.anchoItem);									this._actual=this.contenedor.getStyle("margin-top").toInt();								break;							}						}
						numeroIndex=(this.anchoTotal/this.numeroItems);
						index=Math.abs(this._actual/(numeroIndex));
						this.fireEvent("move",index);
					}.bind(this)
				});
					switch(this.options.modo){
						case "horizontal":
							this.contenedor.morph({
								"margin-left":  (this._actual - this.options.anchoItem * this.options.avanze)
							});
						break;
						case "vertical":
							this.contenedor.morph({
								"margin-top":  (this._actual - this.options.altoItem * this.options.avanze)
							});
						break;
					}
			}else if (direccion="atras"){				
				this.contenedor.set("morph",{duration:this.options.duracion * 1000,wait:false,transition:this.options.transicion,
					onStart:function(){
						switch(this.options.modo){
							case "horizontal":
								itemactual=(this.contenedor.getStyle("margin-left").toInt());//this.anchoTotal
							break;
							case "vertical":
								itemactual=(this.contenedor.getStyle("margin-top").toInt());//this.anchoTotal
							break;
						}						if(!this.options.circular){
							if(itemactual==0){								this.contenedor.get("morph").cancel();								return false;							}						}
						//this.fireEvent("back");
					}.bind(this),
					onComplete:function(){
						switch(this.options.modo){
							case "horizontal":
								this._actual=this.contenedor.getStyle("margin-left").toInt();
							break;
							case "vertical":
								this._actual=this.contenedor.getStyle("margin-top").toInt();
							break;
						}						if(this.options.circular){							this.contenedor.getLast("li").inject(this.contenedor,"top");							this.contenedor.setStyle("margin-left",-this.options.anchoItem);							this._actual=this.contenedor.getStyle("margin-left").toInt();													}
						numeroIndex=(this.anchoTotal/this.numeroItems);
						index=Math.abs(this._actual/(numeroIndex));
						this.fireEvent("move",index);
					}.bind(this)
				});
				switch(this.options.modo){
					case "horizontal":
						this.contenedor.morph({
							"margin-left":  (this._actual + this.options.anchoItem * this.options.avanze)
						});
					break;
					case "vertical":
						this.contenedor.morph({
							"margin-top":  (this._actual + this.options.altoItem * this.options.avanze)
						});
					break;
				}
			}
	},
	moverAlPrimero:function(index){
		this.contenedor.set("morph",{duration:this.options.duracion * 1000,wait:true,transition:this.options.transicion});
			switch(this.options.modo){
				case "horizontal":
					this.contenedor.morph({
						"margin-left":  0
					});
				break;
				case "vertical":
					this.contenedor.morph({
						"margin-top":  0
					});
				break;
			}
		this._actual=0;
	},
	moveTo:function(index){
		this.contenedor.set("morph",{
			onComplete:function(){
				this._actual=this.contenedor.getStyle("margin-left").toInt();
			}.bind(this)
		});
		numeroIndex=(this.anchoTotal/this.numeroItems);
		switch(this.options.modo){
			case "horizontal":
				this.contenedor.morph({
					"margin-left":  -(numeroIndex)*index
				});
			break;			
			case "vertical":
				this.contenedor.morph({
					"margin-top":  -(numeroIndex)*index
				});
			break;
		}
	}
});
