//
// (C) Trurl McByte
//

    var shtable=null;

//
// 
//

    function ghdb_table_init(url,fdata,method,holder,holder_status) {
	var data = null;
//	if(status_holder) var holder_status = document.getElementById(status_holder);
	var x = gh_db_getAtrax(null,null,ghdb_GetData,holder_status,holder);
	if(method=='GET') {
	    x.xmlHttp.open("GET",url + '/index.js' + '?' + fdata,true);
	    x.xmlHttp.send(null);
	} else {
	    x.xmlHttp.open("POST",url + '/index.js',true);
	    x.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    x.xmlHttp.send(fdata);
	}

	function ghdb_GetData(event,handler,holder) {
		var idata = eval( handler.responseText.toString() );
		shtable = new ghdb_Table(holder,idata,url);
	}
	return false;
    }

    var ghdb_Table = function (holder,data,url) {
	this.cursor=0;
	this.url=url;
	this.fields = new Array();
	this.holder = holder;
	this.holder.innerHTML='';
	this.table = document.createElement('table');
	this.table.border=1;
	this.table.width='100%';
	this.data = data;
	this.cols_order = new Array();
	this.col_names = new Array();
	this.pageRows = 20;
	this.pageCols = 0;
	this.currow=null; // internal use
	this.holder.appendChild(this.table);
	this.tbody=document.createElement('tbody');
	this.table.appendChild(this.tbody);
	this.parseHeader();
	this.makeHeader();
	this.printRows();
    }

    ghdb_Table.prototype.showNext = function (event,move) {
	for(var i=this.tbody.childNodes.length; i > 1 ; i--) {
	    this.tbody.removeChild(this.tbody.lastChild);
	}
	if(move != 0) this.cursor += move * this.pageRows;
	this.printRows();
	return false;
    }

    ghdb_Table.prototype.buildNav = function () {
	var self=this;
	var navrow=document.createElement('tr');
	var navbar=document.createElement('td');
	navbar.colSpan=this.pageCols;
	var prev = document.createElement('div');
	prev.style.styleFloat='left';
	prev.style.cssFloat='left';
	prev.style.width='33%';
	prev.style.textAlign='left';
	navbar.appendChild(prev);
	if(this.cursor > 1) {
	    prev.style.cursor='pointer';
	    prev.innerHTML = ' &lt; Prev ';
	    prev.onclick=function(e){ return self.showNext(e, -1); };
	} else prev.innerHTML='&nbsp;';
	var pager = document.createElement('div');
	pager.innerHTML=' &nbsp; ( Page ' + String(Math.ceil((this.cursor+1) / this.pageRows)) + 
	    ' of '+ String(Math.ceil((this.data.length)/this.pageRows))  +' ) &nbsp; ';
	pager.style.width='33%';
	pager.style.styleFloat='left';
	pager.style.cssFloat='left';
	pager.style.textAlign='center';
	navbar.appendChild(pager);

	var next = document.createElement('div');
	next.style.styleFloat='left';
	next.style.cssFloat='left';
	next.style.width='33%';
	next.style.textAlign='right';
	navbar.appendChild(next);
	if(this.data.length > this.pageRows+this.cursor) {
	    next.innerHTML = ' Next &gt; ';
	    next.onclick=function(e){ return self.showNext(e, 1); };
	    next.style.cursor='pointer';
	} else next.innerHTML='&nbsp;';
	navrow.appendChild(navbar);
	return navrow;
    }

    ghdb_Table.prototype.parseHeader = function () {
	var self=this;
	for (var i=0, l=this.data[0].length; l>i; i++) {
	    if(this.data[0][i].fname!=undefined ) {
		var key=this.data[0][i].fname;
		this.fields[i]=this.data[0][i];
		this.col_names[i]=this.data[0][i].label!=undefined?this.data[0][i].label:key;
	    } else {
		this.fields[i] = new Object();
		this.fields[i].fname = this.data[0][i];
		this.col_names[i] = this.data[0][i];
	    }

	}
    }

    ghdb_Table.prototype.makeHeader = function () {
	var self=this;
	this.header=document.createElement('tr');
	this.pageCols=0;
	for (var i=0, l=this.data[0].length; l>i; i++) {
	    if(this.fields[i].format!=undefined && this.fields[i].format==0) continue;
	    var col=document.createElement('th');
	    col.innerHTML=this.col_names[i];
	    this.cols_order[i]=0;
	    if((this.fields[i].sort==undefined || this.fields[i].sort!=0) && this.data.length > 2) {
		col.style.cursor='n-resize';
		eval('col.onclick = function(e) { return self.doSort(e,"'+i+'"); };');
	    }
	    this.header.appendChild(col);
	    this.pageCols++;
	}
	this.tbody.appendChild(this.header);
	this.data.splice(0,1);

    }

    ghdb_Table.prototype.doSort = function (e, ikey) {
	var self=this;
	if(this.fields[ikey].sort!=undefined) eval('var sfunc=self.sort' + this.fields[ikey].sort);
	else var sfunc=self.sortString;
	if (!e) var e = window.event;
	if (e.target) var targ = e.target;
        else if (e.srcElement) var targ = e.srcElement;
	var key=this.col_names[ikey];
	if(this.cols_order[ikey]==-1) {
	    this.data.sort( function(a,b) { return sfunc(a[ikey],b[ikey],1); } );
	    this.cols_order[ikey]=1;
	    targ.innerHTML = key + '&nbsp;v';
	    targ.style.cursor='n-resize';
	} else {
	    this.data.sort( function(a,b) { return sfunc(a[ikey],b[ikey],-1); } );
	    this.cols_order[ikey]=-1;
	    targ.innerHTML = key + '&nbsp;^';
	    targ.style.cursor='s-resize';
	}
	this.showNext(e, 0);
	return false;
    }

    ghdb_Table.prototype.sortString = function(a,b,order) { if(a==b) return 0; if(a > b) return -1 * order; return order; }
    ghdb_Table.prototype.sortHString = function(a,b,order) {
	var xa = a.replace(/(<([^>]+)>)/ig,"");
	var xb = b.replace(/(<([^>]+)>)/ig,"");
	if(xa==xb) return 0; if(xa > xb) return -1 * order; return order; 
    }
    ghdb_Table.prototype.sortInt = function(a,b,order) { if(parseInt(a,10)== parseInt(b,10)) return 0; if(parseInt(a,10) > parseInt(b,10)) return -1 * order; return order; }
    ghdb_Table.prototype.sortFloat = function(a,b,order) { if(parseFloat(a) == parseFloat(b)) return 0; if(parseFloat(a) > parseFloat(b)) return -1 * order; return order; }


    ghdb_Table.prototype.printRows = function () {
	if(this.pageRows<this.data.length) this.tbody.appendChild(this.buildNav());
	for (var i=this.cursor; this.data.length > i && this.pageRows+this.cursor > i; i++) {
	    var row=document.createElement('tr');
	    for (var j=0, l=this.data[i].length; l>j; j++) {
		if(this.fields[j].format!=undefined && this.fields[j].format==0) continue;
		var col=document.createElement('td');
		col.innerHTML=this.formatCell(j,this.data[i][j],i);
		row.appendChild(col);
	    }
	    this.tbody.appendChild(row);
	}
	if(this.pageRows<this.data.length) this.tbody.appendChild(this.buildNav());
    }

    ghdb_Table.prototype.formatCell = function (key,val,row) {
	var self=this;
	if(this.fields[key].format!=undefined)  eval('var pfunc=self.format' + this.fields[key].format);
	else var pfunc=self.formatAsIs;
	if( this.fields[key].fname == 'name'
	    || this.fields[key].fname=='main_name'
	    || this.fields[key].fname=='game_title')
	    return '<a class="trow" href="'+this.url.replace('/ghdb/','/db/')+'/'+this.data[row][0]+'.html" onmouseover="gh_db_showtip(this,event,6,500,0,-1,30);" target="_blank">'+val+'</a>';
	if(this.fields[key].fname == 'icon') return this.formatIcon(val);
	
	if(val==null) return '&nbsp;';

	return pfunc(val);
    }

    ghdb_Table.prototype.formatAsIs = function (val) { return val; }
    ghdb_Table.prototype.formatNope = function (val) { return '&nbsp;'; }
    ghdb_Table.prototype.formatIcon = function (val) { return '<img src="'+val+'" style="border:0;width:32px;height:32px;">'; }
    ghdb_Table.prototype.formatImg = function (val) { return '<img src="'+val+'" style="border:0;">'; }


