// JavaScript Document


function getClientHeight()   
{   
    var clientHeight=0;   
    if(document.body.clientHeight&&document.documentElement.clientHeight)   
    {   
        var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight) ?
			document.body.clientHeight:document.documentElement.clientHeight;           
    }   
    else  
    {   
        var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight) ?
			document.body.clientHeight:document.documentElement.clientHeight;       
    }   
    return clientHeight;   
} 

function remove_last_str( str, count )
{
	return str.substr( 0, str.length - count );
}

function get_file_title( fn )
{
	var idx_start = fn.lastIndexOf('\\');
	idx_start = ( idx_end < 0 ) ? 0 : idx_start + 1;
	
	var idx_end = fn.lastIndexOf('.');
	idx_end = ( idx_end < 0 ) ? fn.length : idx_end;

	return fn.substr( idx_start, idx_end - idx_start );	
}

function show_history( paths )
{
	if( paths == "" )
		return;

	var fn = paths.split('|');
	var table_history = document.getElementById("table_history");

	for( i = 0; i < fn.length; i++ )
	{
		var filetitle = get_file_title( fn[i] );
		var filepath  = fn[i].replace( /\\/g, '\\\\' );
		var row = table_history.insertRow();
		row.height = "32px";
		var cell = row.insertCell();
		cell.innerHTML = "<a href=\"javascript:void(0);\" onclick=\"c_openfile( '" + filepath + "' );\">" + filetitle + "</a>";
	}
}


////////////////////////////////////////////////////////////////////////////

function on_size()
{
	var ybtm = remove_last_str( document.getElementById("div_bottom").style.height, 2 );
	var ytop = remove_last_str( document.getElementById("div_top").style.height, 2 );
	var cy = getClientHeight();
	document.getElementById("div_middle").style.height = (cy - ybtm - ytop - 10) + "px";
	document.getElementById("div_bottom").style.display = "";
}

function on_load()
{
	on_size();
	var his = c_query_history();
	show_history( his );
}


////////////////////////////////////////////////////////////////////////////

function c_openfiledlg()
{
	window.external.callcfunc( 'openfiledlg' );
}

function c_openfile( fn )
{
	window.external.callcfunc( 'openfile', fn );
}

function c_query_history()
{
	return window.external.callcfunc( 'queryhistory' );
}

function c_clear_history()
{
	var table_history = document.getElementById("table_history");
	var pchildren = table_history.childNodes;
	for( i = 0; i < pchildren.length; i++ )
		table_history.removeChild( pchildren[i] );

	window.external.callcfunc( 'clearhistory' );
}