/**
 *
 * Pagination a class that manages the page content.
 * Created ON 03/09/2010
 *
 * @package mootools-extends
 * @version 0.2
 * @author �ngel Luis Quesada Nieto http://alquesada.com
 * @author Jose Villalobos Cort�s http://josevillalobos.es
 * @author Victor Ant�n Sanchez
 * @copyright Copyright (c) 2010 �ngel Luis Quesada Nieto. (http://alquesada.com)
 * @license http://www.gnu.org/licenses/lgpl.html GNU LESSER GENERAL PUBLIC LICENSE
 * @todo change check Page via DOM by check through the array
 *
 * LICENSE
 * This source file is subject to the GNU LESSER GENERAL PUBLIC LICENSE that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/lgpl.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to angel.quesada@kubide.es so we can send you a copy immediately.
 *
 */

var Pagination = new Class({

	Implements: [Options, Events],

	options: {
		aPage:0,
		tPages:false,
		pages:[],
		nPage: "li",
		parent:"#pagination",
		hash:"pagination-",
		loop:true,
		counter:false,
		delay:10*1000,
		auto:{
			delay: 2,
			step: 1,
			start: false,
			play: "play",
			stop: "stop",
			toggle : "toggle"
		},
		fx:"move", // fade = fadeIn fadeOut: move = moveLeft moveRight
		fxOptions: {},
		controls:[],
		disableClass: "disable"
	},

	initialize: function(options) {
		this.setOptions(options);
		this._checkPages();
		this._setAPage();
		this._hasEmpty();
		this._auto();
	},

/** PRIVATE **/
	_goto: function(i) {
		if (this.options.aPage === i)
			return true;
		if (!this._hasEmpty(i))
			return false;
		this._showPage(i)
		this.options.aPage = i;
		this._checkControls();
		this._setAPage();
	},

	_canMove: function(i){

		if ( (i == this.options.auto.play && !this.options.auto.start)
			|| (i == this.options.auto.stop && this.options.auto.start) )
			return true;

		if ( (i == this.options.auto.play && this.options.auto.start)
			|| (i == this.options.auto.stop && !this.options.auto.start) )
			return false;
		if (i == "last")
			return (this.options.tPages) ? this.options.tPages-1 : false;
		if (i == "first")
			return 0;
		var j = this.options.aPage + i.toInt();
		if (this.options.loop && this.options.tPages)
		{
			if (this.options.tPages && j < 0)
				j = this.options.tPages+j;
			if (this.options.tPages && j >= this.options.tPages)
				j = j-this.options.tPages;
		}else{
			if (j < 0)
				j = false; //j = 0; //j = false
			if (this.options.tPages && j >= this.options.tPages)
				j = false; //this.options.tPages-1;
		}
		return j;
	},

	_hasEmpty: function(i) {
		var start = 0;
		var end = 0;
		if (!$chk(i))
			end = i = this.options.aPage;
		else{
			start = this.options.aPage <= i ? this.options.aPage : i;
			end = this.options.aPage >= i ? this.options.aPage : i;
		}
		var parent = document.getElement(this.options.parent);
		while (end >= start)
		{
			if (!$chk(this.options.pages[start]) )
			{
				var delay = (start != i);
				this.options.pages[start] = {}
				this.options.pages[start].element = parent.hasChild(this.options.hash+start) ?
					parent.hasChild(this.options.hash+start) :
					this._fillPage(start, delay);

			}else if( (start == i ) && !$chk(parent.getElement("#"+this.options.hash+start).get("html")) )
			{
				$clear(this.options.pages[start].delay);
				this._getContent(i);
			}

			start++;
		}

		return true;
	},

	_fillPage: function(i, delay) {
		var nElement = new Element(this.options.nPage, {
		    //'class': this.options.'class',
		    'id': this.options.hash+i
		});
		document.getElement(this.options.parent).adopt(nElement)
		if (delay)
		{
			var delayID = this._getContent.delay(this.options.delay, this, i);
			this.options.pages[i].delay = delayID;
		}
		else
			this._getContent(i);
		return nElement;
	},

	_showPage: function(i) {
		switch (this.options.fx){
			case "move" :
				this._showPageFxMove(i);
				break;
			case "fade" :
				this._showPageFxFade(i);
				break;
			default:
				this._showPageExtra(i);
		};
	},

	_showPageFxMove: function(i) {
		var parent = document.getElement(this.options.parent);
		var positionX = -1*i*this.options.fxOptions.width;
		parent.tween('margin-left', positionX);
	},

	_showPageFxFade: function(i) {
		document.id(this.options.hash+this.options.aPage).fade('out');
		document.id(this.options.hash+i).fade('in');
	},

	_changeControlStatus: function(i) {
		if (!$chk(i))
			i = this.options.controls.length-1;
		this.options.controls[i].element.removeEvents('click');
		if (this.options.controls[i].status == "enable")
		{
			this.options.controls[i].status = "disable";
			this.options.controls[i].element.addEvent("click", function(e){
				e.stop();
			}.bind(this));
			this.options.controls[i].element.addClass(this.options.disableClass);

		}else{
			this.options.controls[i].status = "enable";
			this.options.controls[i].element.addEvent("click", function(e){
				e.stop();
				if (this.options.controls[i].units == this.options.auto.play
					|| this.options.controls[i].units == this.options.auto.stop
					|| this.options.controls[i].units == this.options.auto.toggle )
					this._auto(this.options.controls[i].units, this.options.controls[i].element);
				else
					this.move(this.options.controls[i].units);
			}.bind(this));
			this.options.controls[i].element.removeClass(this.options.disableClass);
		}
	},

	_checkControls: function()
	{
		this.options.controls.each(function(control, index){

			if ((control.status != "enable" && (this._canMove(control.units) !== false)) // Debemos activar
				|| ((control.status == "enable" || !$chk(control.status)) && (this._canMove(control.units) === false)) // Debemos desactivar
				)
				this._changeControlStatus(index)
		}.bind(this));
	},

	_checkPages: function() {

		switch (this.options.fx){
			case "move" :
				this._checkPagesFxMove();
				break;
			case "fade" :
				this._checkPagesFxFade();
				break;
			default:
				this._checkPagesExtra();
		};
	},

	_checkPagesFxMove: function()
	{
		
		var parent = document.getElement(this.options.parent);
		var tPages = this.options.tPages ? this.options.tPages+1 : 100;
		parent.setStyle("width", this.options.fxOptions.width*tPages);
		parent.set('tween', this.options.fxOptions.tween);
		this._showPageFxMove(this.options.aPage);
	},

	_checkPagesFxFade: function()
	{
		var parent = document.getElement(this.options.parent);
		parent.getChildren().each(function (children)
		{
			//Chequear la otra opci�n
			if (children.id.test(this.options.hash) && !children.id.test(this.options.hash+this.options.aPage))
				children.fade('out');

		}.bind(this));

	},

	_addControl: function(element, units) {
		if (!$chk(units))
			units = element.rel ? element.rel : 1;

		this.options.controls.push({"element":element, "units":units});
		this._changeControlStatus();
	},

	_setAPage: function(){
		if (this.options.counter && document.getElement(this.options.counter))
			document.getElement(this.options.counter).set("html", this.options.aPage+1);
	},

	_auto: function(status, element) {
		if (!$chk(status))
			status = this.options.auto.start;

		if (status === this.options.auto.toggle)
		{
			status = this.options.auto.start ? false : true;
		}
		
		if (status === true || status === this.options.auto.play)
		{
			this.options.auto.id = this.move.periodical(this.options.auto.delay*1000,this, this.options.auto.step);
			this.options.auto.start = true;
			//TODO arreglar
			if ($chk(element)){
				element.removeClass("play");
				element.addClass("pause");
			}
			
		}else{
			//TODO arreglarç
			if ($chk(element)){
				element.addClass("play");
				element.removeClass("pause");
			}

			$clear(this.options.auto.id);
			this.options.auto.start = false;
		}

		this._checkControls();
	},

/* MUST OVERRIDDEN */
	_checkPagesExtra: function() {
		alert ("This method should be overridden");
	},

	_getContent: function(i) {
		alert ("This method getContent should be overridden");
	},

	_showPageExtra: function(i) {
		alert ("This method showPageExtra should be overridden");
	},

/* PUBLIC */
	addControl: function(element, units) {
		this._addControl(element, units)
		this._checkControls();
	},

	addControls: function (elements)
	{
		elements.each(function(e){
			this._addControl(e.element, e.units);
		}.bind(this));

		this._checkControls();
	},

	move: function(i){
		var j = this._canMove(i);
		if (j !== false)
			this._goto(j);
		else
			return false;
	}
});

/*var relPagination = new Class({
    Extends: Pagination,

	_getContent: function(i) {
		domElement = document.id(this.options.hash+i)
		domElement.set("html", "Content -> "+i);
	}

});*/

