// JavaScript Document
Choquin.Control.CarouselController = Choquin.Control.Base.extend({
	constructor: function(element,carousel,options){
		this.carousel = carousel;
		this.base(element,options);
	},
	
	create: function(){
		var ul = new Element('ul');
		this.carousel.getPageCount().times(function(index){
			var li = new Element('li');
			li.update(index+1);
			ul.insert(li);
		});
		this.element.insert(ul);
		this.buttonsContainer = ul;
	},
	
	buttons: function(){
		return this.buttonsContainer.childElements();
	}
});

Choquin.Control.Carousel = Choquin.Control.Base.extend({
	constructor:function(element,options){
		this.base(element,options);
		this._page = 0;
		this.gotoPage(0);
	},
	
	create: function(){
		this._content = $(this.element).down();
		this._content.appendChild($(this._content.childElements()[0]).clone(true));
		this._controllerElement = null;
		
		this.observe('change', function(evt){
			if(this._selectedControllerItem) this._selectedControllerItem.removeClassName('selected');
			if(this._controllerElement){
				this._selectedControllerItem = this._controllerElement.childElements()[evt.page];
				this._selectedControllerItem.addClassName('selected');
			}
			//console.debug('change',evt);
		}.bind(this));
	},
	
	assignController: function(element){
		//asigna controles a los child elements de element. Preferentemente element es un UL y los child son LI que pueden o no tener <a> 
		var controller = this;
		$(element).childElements().each(function(child,index){
			child.getPageElement = function(){
				return controller.getPageElement(index);
			}

			if(!child.select('a').size()){
				var oldContent = child.innerHTML;
				var a;
				child.update(a = new Element('a',{href:''}).update(oldContent));
				child = a;
			}
			child.observe('click',function(evt){
				controller.gotoPage(index);
				controller.resetTimer();
				evt.stop();
			});
			
			//console.debug(a);
		});
		
		this._controllerElement = element;
	},
	
	createController: function(element){
		//crea nuevos controles dentro del contenedor element
		this.controller = new Choquin.Control.CarouselController(element,this);
		var ul = this.controller.buttonsContainer;
		/*
		var ul = new Element('ul');
		this.getPageCount().times(function(index){
			var li = new Element('li');
			li.update(index+1);
			ul.insert(li);
		});
		if (element) $(element).insert(ul);
		*/
		
		this.assignController(ul);
		return ul;
	},
	
	gotoPage: function(page){
		page = [0,[page,this.getPageCount()].min()].max();
		var element = this.element;
		if(this._tween) this._tween.cancel();
		this._dimensions = element.getClientDimensions();
		var scrollTop = this._dimensions.height*page;
		var scrollLeft = this._dimensions.width*page;
		var options = {
			duration:this.options.duration||1,
			afterFinish: this._onTweenEnd.bind(this)
		};
		this._tween = new Effect.Tween(element,this._page,page,options,function(value){
			this._scrollToPage(value);
		}.bind(this));
		
	},
	
	_scrollToPage: function(value){
			this.element.scrollTop = this._dimensions.height*value;
			this.element.scrollLeft = this._dimensions.width*value;
			this._page = value;
	},
	
	_onTweenEnd: function(effect){
		if(this.getPage()==this.getPageCount()){
			this._scrollToPage(0);
			this._page = 0;
		}
		
		var evt = new Choquin.Event('change',this);
		evt.page = this.getPage();
		this.fire(evt);
	},
	
	getPage: function(){
		return Math.round(this._page);
	},
	
	getPageElement: function(pageNumber){
		pageNumber = pageNumber || this.getPage();
		return this._content.childElements()[pageNumber];
	},
	
	nextPage: function(){
		var nextPage = this.getPage()+1;
		if(nextPage>this.getPageCount()) nextPage = 0;
		this.gotoPage(nextPage);
	},
	
	previousPage: function(){
		var nextPage = this.getPage()-1;
		if(nextPage<0) {
			this._scrollToPage(this.getPageCount());
			nextPage = this.getPageCount()-1;
		}
		this.gotoPage(nextPage);
	},

	
	getPageCount: function(){
		return this._content.childElements().size() - 1; //-1 porque el último es igual que el primero
	},
	
	start: function(interval){
		this._playing = true;
		this._start_interval = interval || this._start_interval || 5;
		if(this._periodicalExecuter) this._periodicalExecuter.stop();
		this._periodicalExecuter = new PeriodicalExecuter(function(pe){
			this.nextPage();
		}.bind(this),this._start_interval);
	},
	
	stop: function(){
		this._playing = false;
		if(this._periodicalExecuter) this._periodicalExecuter.stop();
	},
	
	restart: function(){
		this.stop();
		this.start();
	},
	
	resetTimer: function(){
		if(this._playing){
			this.stop();
			this.start();
		}
	}
});


/*Choquin.Control.Carousel.Event = Choquin.Event.extend({
	constructor: function(
});*/
