var PagedTableComponent = {};
	
PagedTableComponent.Base = Class.create();
PagedTableComponent.Base.prototype = {
		 headerLinkListeners: $H(),
		 initialize: function(settings) {
		 	settings = $H(settings);
			// copy the settings into this object
			Object.extend(this, settings);
			this.baseInitialize();
		 },
		 baseInitialize: function() {
		 	this.fields = new $A(this.fields);
		 	this.dataRowStartTagGenerator.bind(this);
		 	this.dataRowEndTagGenerator.bind(this);
			this.addClickListeners();
			this.updateTotalPages();
		 },
		 showPage : function() {
		 	//var endIdx = this.startIdx+this.count;
		 	//if( this.startIdx < 0 ) this.startIdx = 0;	 	
		 	//if( this.endIdx > this.maxIdx-1 ) this.startIdx = this.maxIdx-this.count-1;		 	
		 	/*
		 	alert('<b>ptc.startIdx = '+this.startIdx+', endIdx = '+this.endIdx+
		 	      ', count = '+this.count+', maxIdx = '+this.maxIdx+',endIdx='+endIdx+
		 	      ', sortField = '+this.sortField+', sortDesc = '+this.sortDesc);
		 	*/
			moduleManager.call(null, this.componentId, 'retrievePage', { 
				requestParams: {
					startIdx: this.startIdx, 
					count: this.count, 
					sortField: this.sortField || '', 
					sortDesc: this.sortDesc, 
					vwmModuleId:this.componentId },
					validationModuleIds: new $A(),
					onComplete: this.showResults.bind(this)
				 } );
		 },
		 buildHeaderRow: function() {
			var tr = this.headerRowStartTag;
			this.fields.each( (function(fieldInfo) {
				var td = this.headerColumnStartTag;
				var sortDesc = this.sortDesc;
				var sortIcon = '';	
				if( fieldInfo.field == this.sortField ) {
					sortIcon = (sortDesc) ? this.sortedDownIcon : this.sortedUpIcon;
				}
				td += fieldInfo.label+sortIcon;
				td += td = this.headerColumnEndTag;
				tr += td;
			}).bind(this));
			tr += tr = this.headerRowEndTag;
			return tr;
		 },
		 showResults : function(resultsMap) { 	
			var results = resultsMap.dataList;
			var metaData = resultsMap.metaData;
			if( metaData == null ) {
				metaData = new $H();
			}
			if( results.length == 0 && this.noDataHtml != '' ) {
				Element.update(this.componentId+'_dataGrid', this.noDataHtml);
				return;
			}
			this.maxIdx = metaData.maxIdx;
			if( this.startIdx > this.maxIdx ) return;
			
			this.clearGrid();
			var content = this.tableStartTag;
		 	content += this.buildHeaderRow();
			var even = true;
			var rowNum = 0;
			results.each( (function(rowData) {
				rowNum++;
				var status = {};
				status.rowNum = rowNum;
				status.count = this.startIdx+rowNum;
				status.isFirstRow = (status.count == 1);				
				status.isLastRow = (status.count == results.length);								
				var tr = this.dataRowStartTagGenerator(rowData, status, metaData);
				var col = 0;
				this.fields.each( (function(fieldInfo) {
				   col++;
				   status.columnNum = col;
				   fieldDataHtml = fieldInfo.render(rowData, status, metaData);
				   if(fieldDataHtml == null) fieldDataHtml = '&nbsp;';
				   var td = this.dataColumnStartTagGenerator(rowData, status, metaData)+fieldDataHtml+this.dataColumnEndTagGenerator(rowData, status, metaData);
				   tr += td;
				}).bind(this));
				tr += this.dataRowEndTagGenerator(rowData, status, metaData);
				content += tr;
				even = !even;
			}).bind(this));
			content += this.tableEndTag;
			
			// remove the old header links
			this.removeHeaderLinkListeners();
			Element.update(this.componentId+'_dataGrid', content);
			
			this.showAppropriateButtons();
			
			setTimeout( this.setupHeaderLinks.bind(this), 10);
		 },
		 showFirstPage: function() {
		 	this.startIdx = 0;
		 	this.showPage();
		 	return false;
		 },
		 showPreviousPage: function() {
		 	this.startIdx -= this.count;
		 	this.showPage();
		 	return false;
		 },		 
		 showNextPage: function() {
		 	this.startIdx += this.count;
		 	this.showPage();
		 	return false;		 	
		 },
		 showLastPage: function() {
		 	this.startIdx = this.maxIdx-this.count;
		 	this.showPage();
		 	return false;		 	
		 },
		 sortPage: function(sortField, sortDesc) {
		 	this.sortField = sortField;
		 	this.sortDesc = sortDesc;		 	
		 	this.showPage();
		 	return false;		 	
		 },
		 setAutomaticInterval: function(interval) {
		 	this.autoInterval = interval;
		 },
		 startAutomaticIncrement: function(interval) {
			this.autoOn = true;
		 	this.setAutomaticInterval(interval);
			setTimeout(this._doNextInterval.bind(this), this.autoInterval);			
		 },
		 stopAutomaticIncrement: function() {
		 	this.autoOn = false;
		 	this.autoInterval = null;
		 },
		 _doNextInterval: function() {
		 	if( !this.autoOn ) return;
			
			var pageNum = this.calculatePageNum();
			var totalPages = this.calculateTotalPages();
			
			if( pageNum >= totalPages ) return;
			
		 	// this is private, do not call it
		 	this.showNextPage();
			if( this.autoInterval != null && this.startIdx <= this.maxIdx ) {
				setTimeout(this._doNextInterval.bind(this), this.autoInterval);
			}
		 },
		 showAppropriateButtons: function() {
		 	if(this.startIdx-this.count > 0-this.count) {
				if( this.exists('ptcFirstOn') ) {		
		 			Element.showInline(this.componentId+'_ptcFirstOn');
					Element.hide(this.componentId+'_ptcFirstOff');
				}
				if( this.exists('ptcPreviousOn') ) {								
		 			Element.showInline(this.componentId+'_ptcPreviousOn');
					Element.hide(this.componentId+'_ptcPreviousOff');
				}
		 	} else {
				if( this.exists('ptcFirstOn') ) {								
		 			Element.showInline(this.componentId+'_ptcFirstOff');
					Element.hide(this.componentId+'_ptcFirstOn');
				}
				if( this.exists('ptcPreviousOn') ) {												
			 		Element.showInline(this.componentId+'_ptcPreviousOff');
					Element.hide(this.componentId+'_ptcPreviousOn');
				}
		 	}
			if((this.startIdx+this.count) <= (this.maxIdx-1)) {
				if( this.exists('ptcNextOn') ) {																
		 			Element.showInline(this.componentId+'_ptcNextOn');
					Element.hide(this.componentId+'_ptcNextOff');
				}
				if( this.exists('ptcLastOn') ) {
		 			Element.showInline(this.componentId+'_ptcLastOn');
					Element.hide(this.componentId+'_ptcLastOff');
				}
			} else {
				if( this.exists('ptcNextOn') ) {					
		 			Element.showInline(this.componentId+'_ptcNextOff');
					Element.hide(this.componentId+'_ptcNextOn');
				}
				if( this.exists('ptcLastOn') ) {
		 			Element.showInline(this.componentId+'_ptcLastOff');
					Element.hide(this.componentId+'_ptcLastOn');
				}
			}
			this.updatePageNum();
			this.updateTotalPages();
		 },
		 updatePageNum: function() {
		 	var pageNum = this.calculatePageNum();
			if( this.exists('ptcPageNum') ) {																
				Element.update(this.componentId+'_ptcPageNum', ''+pageNum);
			}
		 },
		 calculatePageNum: function() {
		 	var pageNum = 1;
		 	var startIdx = this.startIdx;
		 	if( startIdx > 0 )  {
		 		pageNum = Math.ceil((startIdx+this.count) / this.count);	 
		 	}

			return pageNum;
		 },
		 calculateTotalPages: function() {
		 	var totalPages = 1;
			if( this.count > 0 && this.maxIdx > 0 ) {
				totalPages = Math.ceil(this.maxIdx / this.count);
			}
			return totalPages;		 	
		 },
		 updateTotalPages: function() {
		 	var totalPages = this.calculateTotalPages();
			if( this.exists('ptcTotalPages') ) {																
				Element.update(this.componentId+'_ptcTotalPages', ''+totalPages);
			}			
		 },
		 addClickListeners: function() {
		 	this.setupHeaderLinks();
			if( this.exists('ptcFirstOn') )
				Event.observe(this.componentId+'_ptcFirstOn', 'click', function(event) { 
						this.showFirstPage();
						Event.stop(event);
					}.bind(this) );
			if( this.exists('ptcPreviousOn') )				
				Event.observe(this.componentId+'_ptcPreviousOn', 'click',  function(event) { 
						this.showPreviousPage();
						Event.stop(event);
					}.bind(this) );
			if( this.exists('ptcNextOn') )								
				Event.observe(this.componentId+'_ptcNextOn', 'click',  function(event) { 
						this.showNextPage();
						Event.stop(event);
					}.bind(this) );
			if( this.exists('ptcLastOn') )												
				Event.observe(this.componentId+'_ptcLastOn', 'click',  function(event) { 
						this.showLastPage();
						Event.stop(event);
					}.bind(this) );
		 },
		 exists: function(postFix) {
 			return $(this.componentId+'_'+postFix) != null;		 	
		 },
		 setupHeaderLinks : function() {		 	
		 	// each column's header link
			this.fields.each( (function(fieldInfo) {
				var sortDesc = this.sortDesc;
				
				if(fieldInfo.field == this.sortField) {
					sortDesc = !sortDesc;
				}
				
				var elName = this.componentId+'_sortLink_'+fieldInfo.field;
				var el = $(elName);
				if( el != null ) {
					var func = function() {
						this.sortPage(fieldInfo.field,sortDesc);
					}.bind(this)
					Event.observe(el, 'click', func );
					// save so we can de-reg later to prevent mem leaks
					this.headerLinkListeners[elName] = func;  
				}
			}).bind(this));
		 },
		 removeHeaderLinkListeners : function() {
		 	this.headerLinkListeners.each( (function(entry) {
				var elName = entry.key;
				var func = entry.value;
				
				Event.stopObserving(elName, 'click', func );
			}).bind(this) );
		 },
		 clearGrid : function() {
			Element.update(this.componentId+'_dataGrid', '');
		 }
};