// function for Paginator constructor
function Paginator(klass, entries, options) {
	this.initialize(klass, entries, options);
}

// prototype for paginator instances
Paginator.prototype = {
	// initialize paginator
	initialize: function(klass, entries, options) {
		this.entries			= entries;								// entry data
		this.count        = entries.length;					
		this.per_page     = options.per_page;
		this.page_fn			= options.page_fn;				// function used to dipslay pages from data
		this.selector     = '.' + klass;						// used to select paginator container
	  this.current_page = 1;
		this.last_page    = Math.ceil(this.count / this.per_page);		
		this.set_initial_display();
	},
	
	// set default display
	set_initial_display: function() {
		var count  	 = this.count,
				per_page = this.per_page;
						
		if ( count > per_page ) {
			var front_anchors    = "<a style='display: none;' href='#' title='first page'><<</a> <a style='display: none;' href='#' title='previous'><</a>",
					back_anchors     = "<a href='#' title='next'>></a> <a href='#' title='last page'>>></a>",
					display_text     = " <span>1 - " + per_page + " of " + count + "</span> ",
					pagination       = jq(front_anchors + display_text + back_anchors),
					pagination_block = jq(this.selector);
					
			pagination_block.html(pagination);
			pagination_block.children('a').bind('click', { paginator: this }, this.update);
		} else
			jq(this.selector).empty();
		
		this.page_fn();
		this.show_page();
	},
			
	// update paginator, page data
	update: function(e) {
		var paginator = e.data.paginator;
		
	  // update pagination links
		switch ( jq(this).text() ) {
	    case '<<':
				paginator.current_page = 1;
		    paginator.on_first_page();
	      break;
			case '<':
				if ( --paginator.current_page == 1 )
		    	paginator.on_first_page();
				else
					paginator.on_center_page();
				break;
			case '>':
				if ( ++paginator.current_page == paginator.last_page ) 
					paginator.on_last_page();
				else
					paginator.on_center_page();
				break;
			case '>>':
				paginator.current_page = paginator.last_page;
				paginator.on_last_page();
				break;
	  }
	
		// show page block or request new page data
		paginator.show_page();
		
		// set paginator display text
		paginator.set_display_text();
		
		return false;
	},
	
	// show / hide front links
	front_links: function(method) {
		var paginate_links = jq(this.selector).children('a:lt(2), a:eq(4), a:eq(5)');
		this.links(paginate_links, method);
	},
	
	// show / hide back links
	back_links: function(method) {
		var paginate_links = jq(this.selector).children('a:gt(5), a:eq(2), a:eq(3)');
		this.links(paginate_links, method);
	},
	
	// show / hide all links
	all_links: function(method) {
		var paginate_links = jq(this.selector).children('a');
		this.links(paginate_links, method);
	},
	
	// run show / hide method
	links: function(paginate_links, method) {
	  if ( method == 'show' )
			paginate_links.show();
		else if ( method == 'hide' )
			paginate_links.hide();
	},
	
	// first page
	on_first_page: function() {
    this.front_links('hide');
		this.back_links('show');
	},
	
	// center pages
	on_center_page: function() {
		this.all_links('show');
	},
	
	// last page
	on_last_page: function() {
		this.front_links('show');
		this.back_links('hide');
	},
	
	// set paginator display text
	set_display_text: function() {
	  var current_page = this.current_page,
				per_page     = this.per_page,
				count        = this.count,
				display      = jq(this.selector).children('span'),
				from         = per_page * (current_page - 1) + 1,
				to           = per_page * current_page,
				text;
		
		if ( to > count )
			to = count;
		
		if ( from == to )
			text = from + " of " + count;
		else
			text = from + " - " + to + " of " + count;
		
		display.html(text);
	},
	
	show_page: function() {
		var visible_page = jq('#cs_results ul:visible'),
				current_page = jq(this.current_page_id());
				
		visible_page.hide();
		current_page.show();
	},
	
	current_page_id: function() {
		switch ( this.current_page ) {
	    case 1:
				return '#first_page';
			case 2:
				return '#second_page';
			case 3:
				return '#third_page';
			case 4:
				return '#fourth_page';
			case 5:
				return '#fifth_page';
	  }
	}
}
