fw.Libs.load([
	'http://images.freewebs.com/JS/mootools.js'
]);

var fwPaging = new Class({
	type: 'paging',
	initialize: function(containerID, options){
		this.containerID = containerID;
		this.options = {
			onClick: '',
			startPage: 1,
			write: true,
			next: 'next',
			nextOff: '',
			prev: 'prev',
			prevOff: '',
			spacer: '&hellip;',
			threshold: 10,
			pageCount: 0,
			breaks: 1,
			bleed: 2,
			pageStyle: 'margin:0px 3px 0px 3px;',
			selectedPageStyle: 'font-size:1.4em; font-weight:bold;',
			navStyle: '',
			containerStyle: ''
		}
		Object.extend(this.options, options || {});
		this.HTML = '<div id="'+this.containerID+'" style="'+(this.options.containerStyle)+'"></div>'
		if(this.options.write)
			this.write();
	},
	write: function(){
		document.write(this.HTML);
		this.activate();
	},
	activate: function(){
		this.container = $(this.containerID);
		this.page = this.options.startPage;
		this.writePages();
	},
	setPage: function(page){
		if(page == this.page) return;
		this.page = page;
		if(this.options.onClick)
			this.options.onClick(page);
	},
	setPageCount: function(pageCount){
		this.options.pageCount = pageCount;
	},
	writePages: function(){
		this.removeChildren(this.container);
		
		//write previous button
		if(this.page > 1){
			var prev = document.createElement('a');
			if(this.options.navStyle) prev.style.cssText = this.options.navStyle;
			prev.href = "#";
			prev.onclick = function(){ this.setPage(this.page - 1); return false; }.bind(this);
			prev.innerHTML = this.options.prev;
		}
		else{
			var prev = document.createElement('span');
			prev.innerHTML = this.options.prevOff;
		}
		this.container.appendChild(prev);
		
		//write page numbers
		var bleed = this.options.bleed;
		var page = this.page;
		var pageCount = this.options.pageCount;
		var spacing = Math.ceil(pageCount/this.options.breaks/5)*5;
		var preCount = 5;
		var lastNum = 0;
		
		if(pageCount >1)
		for(var i=1; i<=pageCount; i++){
			if(pageCount > this.options.threshold){
				if((i>page+bleed || i<page-bleed) && i>preCount && i%spacing && i!=pageCount)
					continue;
				if(lastNum+1!=i)
					new Element('span').setHTML(this.options.spacer).injectInside(this.container);
					//this.container.appendChild(document.createTextNode(this.options.spacer));
			}
			lastNum = i;
			if(this.page==i)
				var pEl = document.createElement('span');
			else{
				var pEl = document.createElement('a');
				pEl.href = "#";
				pEl.onclick = function(){ this.CommentBook.setPage(this.i); return false; };	
				pEl.CommentBook = this;
				pEl.i = i;
			}
			pEl.style.cssText = this.options.pageStyle + (this.page==i ? this.options.selectedPageStyle : '');	
			pEl.appendChild(document.createTextNode(i));
			this.container.appendChild(pEl);
		}
		
		//write next button
		if(this.page < pageCount){
			var next = document.createElement('a');
			if(this.options.navStyle) next.style.cssText = this.options.navStyle;
			next.href = "#";
			next.onclick = function(){ this.setPage(this.page - 0 + 1); return false; }.bind(this);
			next.innerHTML = this.options.next;
		}
		else{
			var next = document.createElement('span');
			next.innerHTML = this.options.nextOff;
		}
		this.container.appendChild(next);
	},
	removeChildren: function(node){
		$A(node.childNodes).each(function(n){node.removeChild(n)});
	}
});