/*
* Prototype Pagination Class:
*
*
*
*
*
*/

var Pages = Class.create({
	initialize: function(contentID, backendScript, entriesPerPage) {
		this.display = $(contentID);
		this.script = backendScript;
		this.params = new Object;
		this.params.perPage = entriesPerPage;

		if (this.script.indexOf("?") > -1) {
			var query = this.script.split("?");
			var items = query[1].split("&");
			for (i=0;i<items.length;i++) {
				var pair = items[i].split("=");
				this.params[pair[0]] = pair[1];
			}
		}
		
		this.currentPage = 1;	
		this.getResults();
		
	},
	update: function() {
		this.getResults();
	},
	nextPage: function() {
		this.currentPage++;
		this.getResults();
	},
	prevPage: function() {
		this.currentPage--;
		this.getResults();
	},
	gotoPage: function(e,page) {
		this.currentPage = page;
		this.getResults();
	},
	startup: function() {
		sIFR.replaceElement(named({sSelector:"b.rate", sFlashSrc:"js/sifr/sweetpea.swf", sColor:"#000000", sWmode: "transparent"}));
		if ($('nextPage')) {
			Event.observe($('nextPage'),'click',this.nextPage.bindAsEventListener(this) );
		}
		if ($('prevPage')) {
			Event.observe($('prevPage'),'click',this.prevPage.bindAsEventListener(this) );
		}
		if ($('viewAll')) {
			$('viewAll').show();
		}
		$$('div.pages a[rel]').each(function(item) {
			Event.observe(item,'click',this.gotoPage.bindAsEventListener(this,item.rel) );
		}.bind(this));
		new Effect.Opacity(this.display,{from: 0.25, to: 1, duration:0.3});
		$('inspireLoading').setStyle({backgroundPosition: '-1000px'});
	},
	loading: function() {
		this.display.setOpacity(0.25);
		//new Effect.Opacity(this.display,{from: 1.0, to: 0.25, duration:0.3});
		$('inspireLoading').setStyle({backgroundPosition: 'center'});
		if ($('viewAll')) {
			$('viewAll').hide();
		}
	},
	getResults: function() {
		this.params.currentPage = this.currentPage;
		var call=new Ajax.Updater(this.display, this.script, {method: 'post',
																	parameters: this.params,
																	onCreate: this.loading.bind(this), 
																	onComplete: this.startup.bind(this),
																	evalScripts:true
																});
		
	}
});