
function table_call_func( dn, func, arg1, arg2, arg3 )
{
    var o = gk_get_el(dn);
    if (!o) return false;
    o = o.gk_table_list;
    if (!o) return false;
    if ((typeof o[func])!='function') return false;
    o[func](arg1,arg2,arg3);
    return true;
}

function create_table_list(_div_name)
{
    this.main_div_name = _div_name ? _div_name : 'table-list';
    var o = gk_get_el(this.main_div_name);
    if (o) o.gk_table_list = this;
    
    this.table_columns = [];
    this.table_data = [];
    this.table_sort_by = -1;
    this.table_sort_dir = 1;
    this.table_hide_row_key = [];
    this.table_hide_row_text = [];
    this.table_hide_rows = undefined;
    this.b_number_rows = 1;

    var cookie_sort_by = 'SRT' + this.main_div_name.toUpperCase() + window.location.pathname.toUpperCase();
    if (cookie_sort_by) cookie_sort_by = cookie_sort_by.substr(0,40);
    cookie_sort_by = cookie_sort_by.replace( /\W+/g, '_' );
    if (1)
    {
	var t = cc_get( cookie_sort_by );
	if (t!='')
	{
	    this.table_sort_by = to_int(t);
	    this.table_sort_dir = this.table_sort_by<0 ? -1 : 1; 
	    this.table_sort_by = Math.abs(this.table_sort_by);
	}
    }

    this.data_compare = function( a, b )
    {
	if (a[this.table_sort_by].k>b[this.table_sort_by].k) return -this.table_sort_dir;
	if (a[this.table_sort_by].k<b[this.table_sort_by].k) return this.table_sort_dir;
	return 0;
    };
    
    this.get_data_compare = function()
    {
	var _obj=this; 
        return function(a,b) { return _obj.data_compare(a,b); }
    };

    this.render_table = function ( default_sort )
    {
	var o = gk_get_el(this.main_div_name);
        if (!o) return;
    
	if (this.table_sort_by<0 && (typeof default_sort)!='undefined')
	{
	    this.table_sort_by = default_sort;
	    this.table_sort_dir = this.table_columns[default_sort].sort_dir;
	    if (!this.table_sort_dir) this.table_sort_dir = 1;
	}
    
	if (this.table_hide_rows)
	{
	    if ((typeof this.table_hide_rows.b_hide)=='undefined')
	    {
		if (this.table_hide_rows.cookie)
		{
		    var cc = cc_get( this.table_hide_rows.cookie );
		    if (cc=='') this.table_hide_rows.b_hide = this.table_hide_rows.default_hide ? 1 : 0;
		    	   else this.table_hide_rows.b_hide = to_int(cc);
		}
		else
		    this.table_hide_rows.b_hide = this.table_hide_rows.default_hide ? 1 : 0;
	    }
	
	    if ((typeof this.table_hide_rows.update_func)=='function')
	    {
		this.table_hide_rows.update_func(this.table_hide_rows.b_hide);
    	    }
	}
    

	o.innerHTML = '';

	var all_rows = [];

//--- header -------------
	var rowspan = '';
	for( var i=0; i<this.table_columns.length; i++ )
	{
	    var col = this.table_columns[i];
	    if (col.header)
	    {
		rowspan = ' rowspan=2';
		var colspan = 1;
		if (col.header == '=')
		{
		    colspan = 0;
		}
		else
		{
		    for( var j=i+1; j<this.table_columns.length; j++ )
		    {
			if (this.table_columns[j].header != '=') break;
			colspan++;
		    }
		}
		this.table_columns[i].colspan = colspan;
	    }
	}

	var t = this.b_number_rows ? '<th'+rowspan+'>#</th>' : '';
	var t2 = '';
    
	var td_style = [];
	for( var i=0; i<this.table_columns.length; i++ )
	{
	    var col = this.table_columns[i];
	    var style = '';
	    if (col.align) style += 'text-align: '+col.align+';';
	    if (col.width) style += 'width: '+col.width+'px; min-width: '+col.width+'px;';
	    if (style) style = ' style="'+style+'"';
	    td_style[i] = style;

	    var sort_lbl = ( i==this.table_sort_by ? ( this.table_sort_dir>0 ? '&darr;' : '&uarr;' ) : '' );
	    if (sort_lbl) sort_lbl = '<span style="padding: 0px 0px 0px 3px;">'+sort_lbl+'</span>';
	
	    var col_title = col.nosort ? col.title
				       : '<A href="javascript:void(0)" onClick="table_call_func(\''+this.main_div_name+'\',\'sort_by_column\','+i+')" onMouseOver="pb_show(event,\'Click to sort the table by this column\')"><B class="sml nrml">'+col.title+'</B></A>';
	    if (col.help)
	    {
		col_title += '<img src="/img/q.png" class=img-q onMouseOver="pb_show(event,\''+col.help+'\')">';
	    }
	
	    col_title += sort_lbl;

	    style = '';
	    if (col.width) style += 'width: '+col.width+'px; min-width: '+col.width+'px;';
	    if (style) style = ' style="'+style+'"';

	    if (rowspan && col.header)
	    {
		if (col.colspan>0) t += '<TH colspan='+col.colspan+' style="text-align: center;">'+col.header+'</TH>\n';
		t2 += '<TH'+style+'>'+col_title+'</TH>\n';
	    }
	    else
	    {
		t += '<TH'+style+rowspan+'>'+col_title+'</TH>\n';
	    }
	}
	all_rows.push( '<TR>' + t + '</TR>\n' );
	if (t2) all_rows.push( '<TR>' + t2 + '</TR>\n' );
    
//--- rows ---------------

	for( var i=0; i<this.table_data.length; i++ )
	{
	    var row = this.table_data[i];
    	    for( var j=0; j<row.length; j++ )
    	    {
    		if (row[j].e!=undefined) row[j].t = unescape(row[j].e);
    		if (row[j].k==undefined) row[j].k = row[j].t;
    	    }
	}

	if (this.table_sort_by>=0) this.table_data.sort( this.get_data_compare() );

	var total_rows = 0;
	var total_hidden = 0;
	for( var i=0; i<this.table_data.length; i++ )
	{
	    var row = this.table_data[i];

	    if (this.table_hide_row_key.length>0 || this.table_hide_row_text.length>0)	// see if we need to hide this row
	    {
		var b_hide = 0;
		for( var j=0; j<row.length; j++ )
		{
		    var hide_k = this.table_hide_row_key[j];
		    if ((typeof hide_k)=='object')
		    {
			for( var ik=0; ik<hide_k.length; ik++ )
			{
			    if (hide_k[ik]==row[j].k)
			    {
				b_hide = 1;
				break;
			    }
			}
		    }
		    else
		    if ((typeof hide_k)!='undefined' && hide_k==row[j].k)
		    {
			b_hide = 1;
			break;
		    }

		    var hide_t = this.table_hide_row_text[j];
		    if ((typeof hide_t)=='object')
		    {
			for( var ik=0; ik<hide_t.length; ik++ )
			{
			    if (hide_t[ik]==row[j].t)
			    {
				b_hide = 1;
				break;
			    }
			}
		    }
		    else
		    if ((typeof hide_t)!='undefined' && hide_t==row[j].t)
		    {
			b_hide = 1;
			break;
		    }
		}
		if (b_hide)
		{
		    total_hidden++;
		    continue;
		}
	    }

	    t = this.b_number_rows ? '<td>'+(total_rows+1)+'.</td>' : '';

    	    for( var j=0; j<row.length; j++ )
	    {
		var tag = row[j].t;
		t += '<TD'+td_style[j]+'>'+tag+'</TD>';
	    }

	    all_rows.push( '<TR class='+(all_rows.length%2?'odd_list':'evn_list')+'>'+t+'</TR>' );
	    total_rows++;
	}

	var num_colspan = this.table_columns.length;
	if (this.b_number_rows) num_colspan++;
    
	if (total_rows==0)
	{
	    all_rows.push( '<TR class='+(all_rows.length%2?'odd_list':'evn_list')+'><TD colspan='+num_colspan+'>&hellip; none found &hellip;</TD></TR>' );
	}
    
	if (this.table_hide_rows && (total_hidden>0 || !this.table_hide_rows.b_hide))
        {
	    var lnk = this.table_hide_rows.b_hide ? '<span class=sml>&#9660; '+total_hidden+' '+this.table_hide_rows.items+' hidden &#9660;</span>'
						  : '<span class=sml>&#9650;</span> '+this.table_hide_rows.hide_link+' <span class=sml>&#9650;</span>';
	    all_rows.push( '<TR class='+(all_rows.length%2?'odd_list':'evn_list')+'><TD colspan='+num_colspan+'><a href="javascript:void(0)" onClick="table_trigger_hide_rows();">'+lnk+'</a></TD></TR>' );
	}
    
	o.innerHTML = '<TABLE class=list>' + all_rows.join('') + '</TABLE>';

	if ( (typeof refresh_online_status_all)=='function' ) refresh_online_status_all();
	if ( (typeof queue_update_online_status_all)=='function' ) setTimeout( 'queue_update_online_status_all()', 10 );
    };

    this.sort_by_column = function(n)
    {
	if (n<0 || n>=this.table_columns.length) return;
    
	if (this.table_sort_by==n)
	{
	    this.table_sort_dir = -this.table_sort_dir;
	}
	else
	{
	    this.table_sort_by = n;
	    this.table_sort_dir = this.table_columns[n].sort_dir;
	    if (!this.table_sort_dir) this.table_sort_dir = 1;
	}
    
	if (cookie_sort_by) 
	{
	    var cc = this.table_sort_dir<0 ? -1 : 1;
	    cc *= Math.abs(this.table_sort_by);
	    cc_set( cookie_sort_by, cc );
	}
    
	this.render_table();
    };

    this.table_trigger_hide_rows = function()
    {
        if (!this.table_hide_rows) return;

	this.table_hide_rows.b_hide = this.table_hide_rows.b_hide ? 0 : 1;
    
	if (this.table_hide_rows.cookie)
	{
	    cc_set( this.table_hide_rows.cookie, this.table_hide_rows.b_hide, 300 );	// 300 days
	}
    
	this.render_table();
    };
}

function ctflag(c)
{
    c = c.toLowerCase();
    if (!c || c=='zz') return '';
    return '<img src="/img/flags/'+c+'.gif" class=imgs style="width: 18px; height: 12px; padding: 0px 6px 0px 2px;">';
}

function ctpls(c)
{
    return '<a href="/search.pl?min_finished=20&max_last_online=0&country='+c+'&do=1">';
}

function uid(u,ps)
{
    var golden_pawn = ps>0 && ps<=3 ? '<a href="/premium.pl" title="premium subscriber"><img class=img-ps src="/img/pm'+ps+'.gif"></a>' : '';
    var os = '';
    if ((typeof req_online_status)=='function')
    {
	os = '<img class=img-onl'+
		( !req_online_status( u ) ? ' style="display: none;"' : '' )+
		' src="/img/online.gif" id="onlsts-'+u+'" title="player is currently online">';
    }
    return os+'<a href="/stats.pl?'+u+'" onMouseOver="gk_mnst(event,\''+u+'\')">'+u+'</a>'+golden_pawn;
}

function rcol(c)
{
    if (!c) return '';
    var c = c.toUpperCase();
    var col = 'color: #666666; background-color: #ffffff;';
    if (c!='W')
    {
	c = 'B';
	col = 'color: #aaaaaa; background-color: #000000;';
    }
    return '<div style="'+col+' width: 14px; margin: auto; text-align: center;"><b class=sml>'+c+'</b></div>';
}

function rbd( bd, fb, result )
{
    var tag = '';
    if (result!=undefined) tag = ( result==0 ? 'lost' : ( result==2 ? 'won' : 'draw' ) );
    fb = fb ? 1 : 0;
    bd = to_int( bd );
    return '<a href="javascript:popa('+bd+','+fb+')"><img src="/img/newwin.gif" class=img-nw onMouseOver="gk_popbd(event,'+bd+','+fb+')">'+tag+'</a>';
}

function rbd2( bd, fb, result )
{
    var tag = '';
    if (result!=undefined) tag = ( result==0 ? '0-1' : ( result==2 ? '1-0' : '&frac12;-&frac12;' ) );
    fb = fb ? 1 : 0;
    bd = to_int( bd );
    return '<a href="javascript:popa('+bd+','+fb+')"><img src="/img/newwin.gif" class=img-nw onMouseOver="gk_popbd(event,'+bd+','+fb+')">'+tag+'</a>';
}

function rbd3( bd, fb, tag )
{
    fb = fb ? 1 : 0;
    bd = to_int( bd );
    return '<a href="javascript:popa('+bd+','+fb+')"><img src="/img/newwin.gif" class=img-nw onMouseOver="gk_popbd(event,'+bd+','+fb+')">'+tag+'</a>';
}

function rbda( bd, fb, num_moves )
{
    var tag = '';
    if (num_moves!=undefined) tag = '<span class=sml>('+num_moves+')</span>';
    fb = fb ? 1 : 0;
    bd = to_int( bd );
    return '<a href="javascript:popa('+bd+','+fb+')"><img src="/img/newwin.gif" class=img-nw onMouseOver="gk_popbd(event,'+bd+','+fb+')">'+tag+'</a>';
}

function rbdt( bd, fb, title )
{
    if (!title) var title = '???';
    var fb = fb ? 1 : 0;
    var bd = to_int( bd );
    return '<a href="javascript:popa('+bd+','+fb+')"><img src="/img/newwin.gif" class=img-nw onMouseOver="gk_popbd(event,'+bd+','+fb+')">'+title+'</a>';
}
