/*jslint browser: true, white: false, plusplus: false, evil: true, undef: false, bitwise: false, nomen: false */

var Grid = function(){
	this.tbody = '';
	return;
};


Grid.prototype = {
	tbody: '',
	rows:0,
	cols:0,
	cell_count:0,
	pageNr:0,
	template:[],
	data:[],
	class_odd:'row_odd',
	class_even:'row_even',
	
	init:function (tbody,rows){
		var tr, td, aRows, aCell,i;
		this.tbody = dojo.byId(tbody);
		this.rows = rows;
		this.data = [];
		this.pageNr = 0;
		this.template = [];
		
		aRows = this.tbody.getElementsByTagName("tr");
		aCell = aRows[0].getElementsByTagName("td");
		
		for(i=0; i< aCell.length; i++){
			this.template[i] = aCell[i].innerHTML;
		}
		
		this.cols = aCell.length;
		this.cell_count = this.rows * this.cols;
		
		this._clear();		
		return;
	},
	
	show:function(pageNr){
		this.pageNr = pageNr;
		this._draw();
	},
	
	_draw:function(){
		var i, j, tr, td, tbody = this.tbody, content = '';
		this._clear(tbody);

		if(this.data.length === 0) {
			return;
		}

		for(i=0; i<this.rows; i++){			
			tr = tbody.insertRow(tbody.rows.length);
			
			/* Alternate row color */
			if(i % 2 !== 0){
				tr.className = this.class_odd;
			}else{
				tr.className = this.class_even;
			}			
			
			for(j=0; j<this.cols; j++){
				content = this._fetchData(j,i);
				if(content!=='&nbsp;'){
					td = tr.insertCell(tr.cells.length);
					td.innerHTML = content;
				}
			}
		}		
		return;
	},
	
	_fetchData:function(cellNr,rowNr){
		var content,obj,key,str,offset,pointer,data;
		content = '';
		offset = (this.pageNr * this.rows);
	    pointer = rowNr + offset;
		data = this.data;
	
		if(pointer >= data.length) {
			return '&nbsp;';			
		}	
		obj = data[pointer];
		content = this.template[cellNr];
		
		for (key in obj){
			if (true){ /* If condition is here to satisfy jslint parser.  */
				str = new RegExp(str = '%' + key, "g");
				content = content.replace(str,obj[key]);
			}
		}
		
		return content;
	},
	
	_clear:function(){	
		while (this.tbody.rows.length > 0){
			this.tbody.deleteRow(0);
		}
		return;
	}
};




var Grid_Pager = function(){
	return;
};


Grid_Pager.prototype = {
	grid:'',
	pagerID:'',
	current_page:0,
	page_count:0,
	start:0,
	end:0,
	objName:'',
	init:function(pagerID,grid,objName){
		this.pagerID = pagerID;
		this.grid = grid;
		this.objName = objName;
		this.page_count = Math.ceil(this.grid.data.length/this.grid.rows);
		this._draw();
		this.setCurrentPage(this.grid.pageNr);

		return;
	},

	updateData:function(aData){
		this.grid.data = aData;
		this.page_count = Math.ceil(this.grid.data.length/this.grid.rows);
		this._draw();
		this.setCurrentPage(1);
		return;
	},

	_draw:function(){
		var i,ul = dojo.byId(this.pagerID);
		
		this._clear();

		ul.appendChild(this._new_li('li_prev','prev','<div class="li_prev">&nbsp</div>'));
		for(i=1; i<=this.page_count;i++){
			ul.appendChild(this._new_li('li_'+i,i,i));
		}
		ul.appendChild(this._new_li('li_next','next','<div class="li_next">&nbsp;</div>'));

		dojo.byId('li_prev').style.display = 'none';
		if(this.page_count <= 1){
			/* Don't show pager if there is one page or less of data.*/
			dojo.byId(this.pagerID).style.display = 'none';
		}else{
			dojo.byId(this.pagerID).style.display = 'block';
		}

		return;
	},

	_new_li:function(id,value,content){
		var li,a,fn,tmp;
		li = document.createElement("LI");
		a = document.createElement("A");
		fn = '' + this.objName + '._click("' + value + '");';
		
		/* 
		Jslint does not allow the string javascript: as part of the 
		source code. However we need this to work around an IE bug.
		
		The solution is to trick the parser by replacing the _ character
		with empty space. That way the string is still assigned to fix the
		IE Bug, and JSLint succesfully parses the source code. 
		*/
		tmp = 'java_script:';
		tmp = tmp.replace('_','');
		a.setAttribute('href',tmp + fn);
		//BREAKS INTERFUCK EXPLORER FUCK YOU MICROSOFT a.setAttribute('onclick',this.objName + '._click("' + value + '");return false;');
		a.innerHTML = '' + content;

		li.setAttribute('id',id);
		li.appendChild(a);

		return li;
	},

	_clear:function(){
		/* Most likely a bad hack. */
		var ul = dojo.byId(this.pagerID);
		ul.innerHTML = '';
		return;
	},

	setCurrentPage:function(value){
		var i,li = '';

		this.current_page = value;

		for(i=1; i<=this.page_count;i++){
			li = dojo.byId('li_'+i);

			if(i===this.current_page){
				li.className='current';
			}else{
				li.className='';
			}
		}

		if(this.page_count>1){
			if(this.current_page === 1){
				dojo.byId('li_prev').style.display = 'none';
			}else{
				dojo.byId('li_prev').style.display = 'inline';
			}

			if(this.current_page === this.page_count){
				dojo.byId('li_next').style.display = 'none';
			}else{
				dojo.byId('li_next').style.display = 'inline';
			}
		}

		this.grid.show(this.current_page-1); /* Page numbers internally start at zero.*/

		return;
	},

	_click:function(value){
		var i = this.current_page;

		if(this.current_page === value){
			 return;
		}
		if(value === 'next'){
			i++;
			if(i>this.page_count){
				 i = 1;
			}
			this.setCurrentPage(i);
			return;
		}

		if(value === 'prev'){
			i--;
			if(i<1) {
				i = this.page_count;
			}
			this.setCurrentPage(i);
			return;
		}

		this.setCurrentPage(value);
		return;
	}
};