/**
 * @author Matthew Foster
 * @date   October 10th 2007
 */
	var PageController = Class.create();
	Object.extend(Object.extend(PageController.prototype, EventDispatcher.prototype),
					{
						
						initialize : function(container, page){
						
							this.setPage(page);
							this.createListener();
							this.buildInterface(container);
							
						
						},
						buildInterface : function(container){
							
							if($(container).tagName.toLowerCase() != "tr")
								throw { message : "Container for PageController must be a TABLE ROW element, element sent's tag name : " + container.tagName };
							
							this.container = $(container);
							this.next = $C("td", { className : "next" });
							this.previous = $C("td", { className : "previous" });
							
							this.next.observe("click", this.nextHandle);
							this.previous.observe("click", this.prevHandle);
							
							this.container.appendChild(this.previous);
							this.container.appendChild(this.next);
						
							
						},
						setPage : function(page){
							
							if(!(page instanceof PaginationIteration))
								throw { message : "Page object sent to PageController isn't of type Pagination, unacceptable" };
							
							this.page = page;						
						
						},
						createListener : function(){
							
							this.serviceHandle = this.handleService.bind(this);
							this.nextHandle = this.handleNext.bindAsEventListener(this);
							this.prevHandle = this.handlePrev.bindAsEventListener(this);
							this.specificHandle = this.handleSpecific.bindAsEventListener(this);
							this.gadgetHandle = this.rebuild.bind(this);
						
						},
						buildItemSet : function(){
							
							var collection = $R(0, this.page.getLength()-1).collect(this.buildItem.bind(this));
							
							this.next.parentNode.removeChild(this.next);
							
							collection.each(this.attachItem.bind(this));
							
							this.container.appendChild(this.next);
							
							var firstItem = collection.first().down("a");
							firstItem.addClassName("active");
							this.previousActive = firstItem;
							
						},
						placeNext : function(){
							
							if(this.page.hasNext())
								this.next.addClassName("active");
							else
								this.next.removeClassName("active");
						},
						placePrev : function(){
							
							if(this.page.hasPrev())
								this.previous.addClassName("active");
							else
								this.previous.removeClassName("active");
								
						},
						buildItem : function(num){
							var container = $C("td");
							
							var ele = $C("a", { href: "javascript:undefined", innerHTML : (num+1) });
							
							ele.setAttribute("number", num);
							ele.observe("click", this.specificHandle);
							
							container.appendChild(ele);
							
							return container;
											
						},
						rebuild : function(xml){
							
							this.refreshContainer();
													
							this.page.setDataSet($A(xml.getElementsByTagName("item")));
							
							this.placePrev();
							this.buildItemSet();
							this.placeNext();
							
							this.dispatchEvent("change", this.page.current());
													
						},
						update : function(page){
							
							this.activateItem();
							this.placePrev();
							this.placeNext();					
							
							this.dispatchEvent("change", page);
							
						},
						activateItem : function(){
							
							var ele = this.container.down("a[number="+this.page.getIndex() +"]");
							
							try{
								ele.addClassName("active");
							}
							catch(e){
								//shh
							}
							try{								
								this.previousActive.removeClassName("active");
							}
							catch(e){
								//shh
							}
							
							this.previousActive = ele;
							
						},
						handleService : function(eAja){
							
							this.rebuild(eAja.responseXML);						
						
						},
						handleSpecific : function(e){
							
							var index = parseInt(Event.element(e).getAttribute("number"));
							if(!this.page.setIndex(index))
								console.log("index out of bounds");
							else
								this.update(this.page.current());		
							
						
						},
						handleNext : function(e){
														
							this.update(this.page.next());
						
						},
						handlePrev : function(e){
							
							this.update(this.page.prev());
						
						},
						refreshContainer : function(){
							
							var tmp = this.container.cloneNode(false);
							
							this.container.parentNode.replaceChild(tmp, this.container);
							
							this.container = tmp;
							
							this.container.appendChild(this.previous);
							this.container.appendChild(this.next);				
						
						},
						attachItem : function(obj){
								
							this.container.appendChild(obj);
						
						}		
					
					
					}
				);
	
