var PaginationWidget = function(element, callback){
	
	var me = this;
	
	this.element_ = element;
	
	this.callback_ = (typeof callback === "function") ? callback : null;
	
	this.options_ = {
		page: null,
		pages: null
	};
	
	this.executing_ = false;
	
	
	// ===============
	// = Constructor =
	// ===============
	(function(){
		me.element_.find('div.pagination_button_left').bind('click', function(e){
			e.preventDefault();
			if(!me.executing_ && me.callback_) {
				page_ = me.options_.page - 1;
				page_ = page_ < me.options_.pages ? page_ : me.options_.pages - 1;
				page_ = page_ <= 0 ? 0 : page_;
				if(me.options_.page == page_) { //If already on the page switching to
					me.debug('Already on page no switching');
					return false;
				}
				me.callback_({page:page_});
			}
			return false;
		});
		me.element_.find('div.pagination_button_right').bind('click', function(e){
			e.preventDefault();
			if(!me.executing_ && me.callback_) {
				page_ = me.options_.page + 1;
				page_ = page_ < me.options_.pages ? page_ : me.options_.pages - 1;
				page_ = page_ <= 0 ? 0 : page_;
				if(me.options_.page == page_) { //If already on the page switching to
					me.debug('Already on page no switching');
					return false;
				}
				me.callback_({page:page_});
			}
			return false;
		});
	})();
	
};

PaginationWidget.prototype.setOptions = function(options, reload) {
	var me = this;
	$.extend(me.options_, options);
	if(me.options_.pages > 25) { //Only allow 25 pages (dots)
		me.options_.pages = 25;
	}
	me.updateDots(me.options_.page, me.options_.pages, reload);
}

PaginationWidget.prototype.debug = function(str) {
	try {
		console.log(str);
	}catch(e){}
}

PaginationWidget.prototype.updateDots = function(page, pages, reload) {
	
	var me = this;
	
	me.executing_ = true;
	
	var css_margin_left = 10, dots = me.element_.find('div.pagination_dots'), css_width = 16;
		
	var page_ = page;
	page_ = page_ < pages ? page_ : pages - 1;
	page_ = page_ <= 0 ? 0 : page_;
	
	if(me.options_.page == page_ && reload != true) { //If already on the page switching to
		me.debug('Already on page no switching');
		me.executing_ = false;
		return false;
	}
	
	if(pages >= 1) {
		$(me.element_).show(0);
	}else{
		$(me.element_).hide(0);
	}
	
	// me.debug(page_);
	// me.debug(pages);
	// me.debug(me.options_.page);
	
	var start_x = (parseInt(dots.css('width')) / 2) - (((css_width + css_margin_left) * pages) / 2);
	
	dots.empty();
	
	for(var i = 0; i < pages; i++) {
		var dot = $('<a class="pagination_dot" href="#page_' + i + '" id="pagination_dot_' + i + '"/>');
		if(i == page_) {
			dot.addClass('pagination_dot_filled');
		}
		dots.append(dot);
		dot.css('width', css_width + "px");
		dot.css('left', i * (parseInt(dot.css('width')) + css_margin_left) + start_x + "px");
		dot.bind('click', function(e){
			if(me.callback_) {
				me.callback_({page:parseInt($(this).attr('id').replace('pagination_dot_', ''))});
			};
			e.preventDefault();
			return false;
		});
		dot = null; //GBC
	}
		
	me.executing_ = false;
}
