/*
 * Navigation Object with Trigger Holder
 * jQuery 1.5.x required! 
 * 
 * @author Marcin Wr�bel
 * @company Loco Martinez
 * @date 2011-02-23 
 * @version 1.01
 * 
 * LICENSE - BSD
 *
 * Initialize with list ID then bind functions to trigger
 *
 */

function navigationObject() {}
navigationObject.prototype.init = function(container) {
	var obj = this;
	this.obj = obj;
	this.triggers = {
		click: [],
		hover: []
	};
	this.container = $(container);
	this.selectable_element = 'li';
	this.selected_class = 'selected';
	this.current_page = 1;
	this.page_counter = this.container.find(this.selectable_element).length;
	this.container.find(this.selectable_element).click(this.changePageEvent);
	this.container.find(this.selectable_element).click(function() { return false; });
	this.readCurrentPage();
}
navigationObject.prototype.bind = function(object, type) {
	if (type && type == 'hover') {
		var type = 'hover';
	} else {
		var type = 'click';
	}
	this.triggers[type].push(object);
}
navigationObject.prototype.getPage = function() {
	return this.current_page;
}
navigationObject.prototype.readCurrentPage = function() {
	var index = 1;
	var elements = this.container.find(this.selectable_element);
	for(var i = 0; i < elements.length; i++) {
		if ($(elements[i]).hasClass('selected') == true) {
			this.current_page = index;
			return true;
		}
		index++;
	}
	return false;
}
navigationObject.prototype.select = function(index) {
	this.container.find(this.selectable_element).removeClass('selected');
	$(this.container.find(this.selectable_element).get(index)).addClass('selected');
	this.current_page = index;
}
navigationObject.prototype.changePage = function(element) {
	this.container.find(this.selectable_element).removeClass('selected');
	$(element).addClass('selected');
	this.readCurrentPage();
	for(var i in this.triggers.click) {
		var object = this.triggers.click[i];
		object(this.getPage());
	}
}
navigationObject.prototype.changePageEvent = function(e) {
	/* harcoded instance of object 'cause JS haven't proper reference callbacks inside events */
	var obj = myNav;
	obj.changePage(this);
}

