function doPagination(){
	var hidden = document.getElementById("secondary");
	//if( hidden ){
		hidden.style.visibility = "visible";
	//}
	var elem = document.getElementsByTagName("body")[0];
	var s  =  elem.className.indexOf("pageProject");
	if( s !== -1 ){
		return pagination.init("projectsList");
	}
}



pagination = {

	next: null,		/* HTMLAnchorElement */	
	prev: null,		/* HTMLAnchorElement */	
	para: null,		/* HTMLParagraphElement */	
	count: null,	/* String, pagination count in the form of XX to XX of XXX */
	start: 1,		/* Number, pointer to currently shown page */		
	limit: 10,		/* Number, limit of items per page */			
	pages: null,	/* Array, list of paginated pages */	
	id: null,
	txtDesc: "Knight Architects' bridge projects include:",
	txtAltdesc: "Martin Knight's past bridge projects include:",
	moniker: null,

	init: function( id ){
		var links = this.getLinks( id );
		var elem = this.getClass( "col1" )[0];
		var hint = this.getClass( "hint", elem, "p" )[0];
		
		this.moniker = document.getElementById("moniker");
		this.id = id;
		this.pages = this.paginate( links );
		this.para = this.build();

		if( hint ){
			elem.insertBefore( this.para, hint );
		}else{
			elem.appendChild( this.para );	
		}
		this.start = this.setStart();
		this.show();
	},
	
	setStart: function(){
		var body = document.getElementsByTagName( "body" )[0];

		for (var i = 0; i < this.pages.length; i++ ){
			for(var j = 0; j < this.pages[i].length; j++ ){
				var link = this.pages[i][j].firstChild;
				
				if( link.id.indexOf("navProject") !== -1 ){
					var strg = link.id.split("nav")[1];
					var s = body.id.indexOf( strg );	
				}else{
					var s = link.id.indexOf( body.id );	
				}
						
				if( s !== -1 ){
					return i + 1;
				}
			}
		}
	},
	
	paginate: function( links ){
		var elems = [];

		for ( var i = 0, j = 0; i < links.length; i++ ){
			if( (i % this.limit) === 0 ){
				j = i / this.limit;
				elems[j] = [];
			}
			elems[j].push(links[i]);
		}
		return elems;
	},
	
	build: function(){
		var para, attrs = { "id": "pagination" };
		para = this.buildDom( "p", attrs );
		para.appendChild( this.setCount() );
		para.appendChild( this.setPrev() );
		para.appendChild( this.setNext() );
		return para;
	},

	show: function(){
		if( this.hideAll() !== true ){
			setTimeout( "this.show()", 50 );
		}else{
			this.each( this.pages[this.start-1], function( key, value ){
				value.style.display = "block";
			});
			this.setPagination();
		}
	},

	hide: function( elems ){
		this.each( elems, function( key, value ){
			if( value.style.display !== "none" ){
				value.style.display = "none";
			}
		});
	},

	hideAll: function(){
		this.each( this.pages, function( key, value ){
			pagination.hide( value );
		});
		return true;
	},

	handleEvent: function( obj ){
		return function( e ){
			var ev = e.target || e.srcElement;
			var s = ev.id.indexOf("next");		
			pagination.start = s !== -1 ? 
				pagination.start + 1 : pagination.start - 1;
			pagination.show();
		};
	},

	each: function( obj, fn, args ) {
		if ( obj.length == undefined )
			for ( var i in obj )
				fn.apply( obj[i], args || [i, obj[i]] );
		else
			for ( var i = 0, ol = obj.length; i < ol; i++ )
				if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break;
					return obj;
	},

	setPagination: function(){
		//**Hack!** to do, remove none DOM method innerHTML;
		this.moniker.innerHTML = this.txtAltdesc;

		this.setCount();
		if( this.start == 1 ){
			this.moniker.innerHTML = this.txtDesc;
			if( this.pages.length > 0 ){
				this.next.style.display = 'inline';
				this.prev.style.display = 'none';
			}		
		}else if( this.start === this.pages.length ){
			this.next.style.display = 'none';
			this.prev.style.display = 'inline';
		}else{
			this.next.style.display = 'inline';
			this.prev.style.display = 'inline';
		}
	},
	
	setCount: function(){
		var txt, node, elems;
		elems = this.getLinks( this.id );
		txt = (((this.start-1)*this.limit)+1) + " - " + 
				(this.start*this.limit) + " of " + elems.length;

		if( ! this.count ){
			this.count = this.buildDom( "strong" );
		}else{
			//**Hack!** to do, remove innerHTML;
			this.count.innerHTML = "";
		}
	
		node = document.createTextNode( txt );
		this.count.appendChild( node );
		return this.count;	
	},
	
	getLinks: function( id ){
		var list = document.getElementById( id );
		return list.getElementsByTagName( "li" );
	},
	
	setNext: function(){	
		if( ! this.next ){
			var attrs, txt;
			
			attrs = {	
				"id": "next", 
				"href": "#"
			};
			
			this.next = this.buildDom( "a", attrs );
			addEvent( this.next, "click", pagination.handleEvent() );
			txt = document.createTextNode( "Next \u00BB" );
			this.next.appendChild( txt );
		}
		//alert(this.next.firstChild.nodeValue);
		return this.next
	},
	
	setPrev: function(){		
		if( ! this.prev ){
			var attrs, txt;
			
			attrs = {	
				"id": "prev", 
				"href": "#"
			};
			
			this.prev = this.buildDom( "a", attrs );
			addEvent( this.prev, "click", pagination.handleEvent() );
			txt = document.createTextNode( "\u00AB Previous" );
			this.prev.appendChild( txt );
		}
		
		return this.prev
	},

	buildDom: function( name, attrs ){
		var elem = document.createElement( name );
		if( attrs ){
			for( var attr in attrs ){
				elem[attr] = attrs[attr];
			};
		}
		return elem;
	},

	getClass: function( searchClass, node, tag ) {
		var classElements = new Array();
		if ( node == null ){
			node = document;
		}
		if ( tag == null ){
			tag = '*';
		}
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
};

addEvent( window, "load", doPagination );
