/**
 * Manages pagination links
 * 
 * @author Angelo Selvini <angelo@exmachina.ch>
 *
 * TODO: create/manage list of pages
 *
 */


if(!ch){
	var ch = {};
}
if(!ch.exmachina){
	ch.exmachina = {};
}
if(!ch.exmachina.bravofly){
	ch.exmachina.bravofly = {};
}
if(!ch.exmachina.bravofly.ui){
	ch.exmachina.bravofly.ui = {};
}

ch.exmachina.bravofly.ui.Pager = function(/* object */ ctorArgs){

	if (ctorArgs)
		this.name  = ctorArgs["name"] || "pagerWidget";

	this.dom  = null;
	this.tmpl = null;

	this.params  = null;
	this.options = null;

	this.ui = {
		prev: null,
		next: null,
		list: null,	// not impl yet
		total: null,
		current: null
	};

};

ch.exmachina.bravofly.ui.Pager.prototype = {

	modal: false,
	dom  : null,
	tmpl : null,
	type : "ch.exmachina.bravofly.ui.Pager",

	params : null,
	options: null,

	callback  : null,

	ui: {
		prev: null,
		next: null,
		list: null,	// not impl yet
		total: null,
		current: null
	},

	markup: ''+
		'<div class="vg_pager">'+
			'<div class="vg_prev">precedente</div>'+
			'<div class="vg_page"><span class="vg_current">#{current}</span>&nbsp;di&nbsp;<span class="vg_total">#{total}</span></div>'+
			'<div class="vg_next">successiva</div>'+
		'</div>',

	setDom: function(/* object */ d){
		if (!d) return;

		//	summary: change current dom
		//	description:
		//		change the dom attached and created if any
		//		with the new one created
		var s=this;
		s.unbindButton("next");
		s.unbindButton("prev");

		if (s.dom)
			s.dom.parentNode.removeChild( s.dom );

		s.dom = d;
		s._readDom();
	},

	setMarkup: function(/* object */ m){
		var d = document.createElement("div");
		d.innerHTML = m;

		// Fix markup eventually
		this.markup = d.innerHTML;
	},

	setOptions: function(/* object */ o){
		this.options = o;
	},

	setParams: function(/* object */ p){
		this.params = p;
	},

	getNode: function(){
		return this.dom;
	},

	getValue: function(){
		var v = 0,
			p = this.params
		;
		if (p && !isNaN(p.current))
			v = p.current;

		return v
	},
	setValue: function(v){
		var p = this.params
		;
		p.current = v;
		this.update();
	},

	// DOM
	create: function(){
		// summary: create template object
		this.tmpl = new Template(this.markup)
		this.tmpl.evaluate( this.params );
	},

	draw: function(){
		// summary: evaluate template with parameters

		var d = document.createElement("div");
		d.innerHTML = this.tmpl.evaluate( this.params );

		this.dom = d.removeChild( d.firstChild );

		d = null;

		this._readDom();

		return this.dom;
	},

	update: function(){
		var p = this.params;
		if (!p || isNaN(p.current) || isNaN(p.total)){
			return;
		}

		if (p.current == 0) { p.current = 1; }
		if (p.total == 0)   { p.total = 1;   }

		this._manageEvents();
		this._manageValues();
	},

	_readDom: function(){
		var u = this.ui,
			t = this.dom
		;

		u.next    = ( $(t).select(".vg_next") )[0];
		u.prev    = ( $(t).select(".vg_prev") )[0];
		u.list    = ( $(t).select(".vg_list") )[0];
		u.total   = ( $(t).select(".vg_total") )[0];
		u.current = ( $(t).select(".vg_current") )[0];
	},

	_manageValues: function(){
		var p = this.params,
			u = this.ui
		;

		u.current.innerHTML = p.current;
		u.total.innerHTML = p.total;
	},

	_manageEvents: function(){
		var p = this.params;

		// Bind buttons
		if (p.current == p.total)
			this.unbindButton("next");
		else
			this.bindButton("next");


		if (p.current == 1)
			this.unbindButton("prev");
		else
			this.bindButton("prev");
	},

	bindButton: function(bn, st){
		if (!/^(next|prev)$/.test(bn))
			return;

		var d = $( this.ui[ bn ] ),
			en = "click",
			ef = (bn == "next" ? this.clickNext : this.clickPrev)
		;

		if (!d)
			return;

		if (st === false){
			d.stopObserving(en, ef);
			d.removeClassName("vg_pager_btn_active");
		} else {
			d.stopObserving(en);//, ef);
			d.observe(en, ef.bindAsEventListener(this) );
			d.addClassName("vg_pager_btn_active");
		}
	},

	unbindButton: function(bn){
		this.bindButton(bn, false);
	},

	clickNext: function(evt){
		this.setValue( this.getValue()+1 );
		this.handleEvents( evt );
	},
	clickPrev: function(evt){
		this.setValue( this.getValue()-1 );
		this.handleEvents( evt );
	},

	handleEvents: function(evt){
		//console.debug("evt: %o:%o", evt, this.callback);

		var c = this.callback;

		if (c){
			c( evt );
		}
	},

	setCallback: function(/* function */ f){
		if (! (f instanceof Function))
			return;

		this.callback = f;
	},

	getType: function(){
		return this.type;
	},

	destroy: function(){
		this.tmpl = null;
		this.dom  = null;
	}
	
};
