// This file is a mess.  Please clean me up!  -mark - 5/17/07


var oldheights = new Array(); // when the widgets are maximized, this remembers the original size
var oldwidths = new Array();
var ismaximized = new Array(); // a flag to indicate a widget IS maximized
var xchange = 2; // nmber of pixels smaller
var ychange = 22; // number of pixels smaller the _interior is from the main object

/**
 * toggles the visibility of whatever ID you send it
 * this function handles the "windowshade" function of output
 *
 * onCLick="toggle_visibility(this)";
 *
 * 
 * @param string $nr the name of the ID or whatever
**/
function toggle_visibility(nr)
{
		if (document.getElementById(nr + '_interior').style.display == 'none')
		{
			$("#"+nr+"_interior").slideDown("fast");
			$("#"+nr+"_subheading").hide();
			$("#"+nr+"_footer").hide();
			$("#"+nr+"_resize").fadeIn("slow");
			$("#"+nr+"_max").fadeIn("slow");
			$("#"+nr+"_filter").fadeIn("slow");
		}
		else
		{
			$("#"+nr+"_interior").slideUp("fast");
			$("#"+nr+"_subheading").hide();
			$("#"+nr+"_footer").hide();
			$("#"+nr+"_resize").fadeOut("slow");
			$("#"+nr+"_max").fadeOut("slow");
			$("#"+nr+"_filter").fadeOut("slow");
		}
}

// this function handles the maximize function
// It could probably use a bit of a rewrite.  This was one of my earliest JS functions, and it's also
// recently been upgraded to use jquery, which may offer an even better way to do this.
function maximize(nr)
{
	var nri = nr+'_interior';
	var width; var height;
	var el =document.getElementById(nr);
	var eli =document.getElementById(nri);
	if (el)
	{
		if (ismaximized[nr] == true)
		{
			$('#'+nr).width(oldwidths[nr]);
			$('#'+nr).height(oldheights[nr]);
			$('#'+nri).width(oldwidths[nri]);
			$('#'+nri).height(oldheights[nri]);
			el.style.position='static';
			ismaximized[nr]=false;
		}
		else
		{
			var deltax = $('#'+nr).width()-$('#'+nri).width();
			var deltay = $('#'+nr).height()-$('#'+nri).height();
			oldwidths[nr]=$('#'+nr).width();
			oldheights[nr]=$('#'+nr).height();
			oldwidths[nri]=$('#'+nri).width();
			oldheights[nri]=$('#'+nri).height();
			$('#'+nr).width((parseInt(window.innerWidth)-17)+'px');
			$('#'+nr).height((parseInt(window.innerHeight)-2)+'px');
			$('#'+nri).width((parseInt($('#'+nr).width()) - xchange)+'px');
			$('#'+nri).height((parseInt($('#'+nr).height()) - ychange)+'px');
			el.style.position = 'fixed';
			el.style.left=0;
			el.style.top=0;
			ismaximized[nr]=true;
		}
		makeRequest(nr, '&width='+parseInt($('#'+nr).width())+'&height='+parseInt($('#'+nr).height()),true); //<-- TODO: true should be the ajaxy flag
	}
	
}
// handles the displaying of the row in outputExpandyTable()
function toggle_row_visibility(nr, rowcount)
{
	if (document.getElementById(nr+'_row_'+rowcount).style.display == 'none')
	{
		$('#'+nr+'_expandy_closed_'+rowcount).hide();
		$('#'+nr+'_expandy_open_'+rowcount).fadeIn();
		$('#'+nr+'_row_'+rowcount).slideDown("fast");
	}
	else
	{
		$('#'+nr+'_expandy_open_'+rowcount).hide();
		$('#'+nr+'_expandy_closed_'+rowcount).fadeIn();
		$('#'+nr+'_row_'+rowcount).slideUp("fast");
	}
}
// this function handles the stuff for outputTab
var last_id;
function show_tab(id)
{ 
	if (id != last_id)
	{
		if (last_id)
			$('#'+last_id).hide();
		$('#'+id).fadeIn("normal");
	}
	last_id = id;
}



var widgets = new Array();  // hold a list of Outputs

// this is supposed to do some javascript whenever a window is resized, but i think that whole feature is being depricated.
function HandleResize()
{
//	alert(document.getElementById(id).style.Width);
//	makeRequest(id, '&width='+document.getElementById(id).style.offsetWidth+'&height='+document.getElementById(id).style.height);
//	makeRequest(id, '&width=500&height=500');

}

// supposed to redraw all the windows when the page loads.
function HandlePageload()
{
	HandleResize();
}

var i; // a dumb variable to track the instances of graphs.  Poorly named!  


var timeoflastkeypress = 0;
// this is used by the $o->Filter feature of output.php.  It also includes a 500ms timeout to prevent
// lots of awkward race conditions.  If 500ms have passed since the last keypress - do the request.  
// That's the theory anyways, it's not quite perfect.
// 
// id = id of the output
// val = what to search for
// trues = start at 1, and used internally
function doFilter(id, val, tries)
{
	var timeout = 500;
	var date = new Date();
	if (tries==1)
		timeoflastkeypress=date.getTime();
	if (date.getTime()-timeoflastkeypress>timeout/2)
		makeRequest(id, '&_filter='+val,false);
	else if (tries>0)
		setTimeout('doFilter("'+id+'", "'+val+'", '+(tries-1)+')', timeout);
}
