//javascript file for forecast

var INIT_SORT_COL_ID="assets_amt";//initial sorting order column
var prevColumn = INIT_SORT_COL_ID;
var currentColumn = INIT_SORT_COL_ID;
var is_ascend;
var full_list_val='f';
var full_list_html;
var ANNOUNCEMENTS_HTML="Complete List of Foundation Announcements";  
var imgId=INIT_SORT_COL_ID+"ImgId";


function load()
{
			loadPanel = new YAHOO.widget.Panel("loadPanelId");
			loadPanel =
				new YAHOO.widget.Panel("waitLoadPanel",  
				{ 	width:"240px", 
					fixedcenter:true, 
					underlay:"none",
					close:false, 
					draggable:false, 
					zindex:4,
					visible:false
				} 
			);
			
			loadPanel.setHeader("Loading, please wait...");
			loadPanel.setBody('<img src="http://l.yimg.com/a/i/us/per/gr/gp/rel_interstitial_loading.gif" />');
			loadPanel.render(document.body);
			
			// Show the Panel
			loadPanel.show();
	
			new Ajax.Updater('foundations', 'forecastController.php',
			{	method:'get',
				onSuccess: function(transport){var response = transport.responseText || "no response text";},
				onFailure: function(){alert('Something went wrong...')},
				onComplete: function()
				{
					document.getElementById(INIT_SORT_COL_ID+"ImgId").style.display="inline"; 
					document.getElementById(INIT_SORT_COL_ID).style.background="lightgrey";
					
					//remove panel's markup/placeholder as opposed to visiblity only. doing so ensures no trailing whitespace.
					document.getElementById("waitLoadPanel").style.display="none";
					document.getElementById("full_list_link_top").style.display="inline";
					document.getElementById("full_list_link_bot").style.display="inline";
					document.getElementById("announcements").style.display="inline";
				}
			});
			
			sortPanel = new YAHOO.widget.Panel("sortPanelId");
			sortPanel =
				new YAHOO.widget.Panel("waitSort",  
				{ width:"240px", 
				fixedcenter:true, 
				close:false, 
				draggable:false, 
				zindex:4,
				visible:false
				} 
			);
			
			sortPanel.setHeader("Sorting, please wait...");
			sortPanel.setBody('<img src="http://l.yimg.com/a/i/us/per/gr/gp/rel_interstitial_loading.gif" />');
			sortPanel.render(document.body);
}

function changeCursor()
{
	document.getElementById("giving").style.cursor="crosshair";
}

function sortColumns(column)
{
	// Show the Panel
	sortPanel.show();
	
	prevColumn=currentColumn;
	currentColumn = column;
	
	imgId=column+"ImgId";
			
			//condition should also check for a null/empty value
			if(currentColumn!=prevColumn)
			{
				document.getElementById(prevColumn+"ImgId").style.display="none";
				document.getElementById(prevColumn).style.background="white";
				
				
				if(currentColumn=="sort_name" || currentColumn=="gm_type" || currentColumn=="ec_ind" || currentColumn=="new_init")
					document.getElementById("is_ascend").value = 't';
				else
					document.getElementById("is_ascend").value = 'f';
			}
			
			
			if(document.getElementById("is_ascend").value=='t')
			{
				document.getElementById("is_ascend").value = 'f';
				is_ascend = 't';
			}
			else
			{
				document.getElementById("is_ascend").value = 't';
				is_ascend = 'f';
			}
		
			new Ajax.Updater('foundations', 'forecastController.php?column='+column+'&is_ascend='+is_ascend+'&full_list='+full_list_val,
			{	method:'get',
				onSuccess: function(transport){var response = transport.responseText || "no response text";},
				onFailure: function(){alert('Something went wrong...')},
				onComplete: function(){sortPanel.hide(); switchDirection(column);}
			});
			
}

function switchDirection(column)
{
		if(is_ascend == 't')
		{
			document.getElementById(imgId).src="images/arrow_asc.gif";
			document.getElementById(column).style.background="silver";
		}
		else
		{
			document.getElementById(imgId).src="images/arrow_desc.gif";
			document.getElementById(column).style.background="lightgrey";
		}
			
		document.getElementById(imgId).style.display="inline";	
}

function switchViewList()
{	
	window.scrollTo(0,0);//reset scroll back to top to allow for proper display/positioning of loadPanel

	//reset current columns
	document.getElementById(currentColumn+"ImgId").style.display="none";
	document.getElementById(currentColumn).style.background="white";
	
	//show load panel. used in place of "show()". corrects visibility/markup issues 
	document.getElementById("waitLoadPanel").style.display="inline-block";
	
	
	if(document.getElementById("full_list").value=='t')//displaying full view
	{
		full_list_val='f';
		document.getElementById("full_list").value='f';//switch to top 100 view
		full_list_html = "View complete list of foundation announcements&raquo";
		ANNOUNCEMENTS_HTML = "Announcements by Foundations Ranked Within 100 Largest by Total Assets";
	}
	else
	{
		full_list_val='t';
		document.getElementById("full_list").value='t';//switch to full view
		full_list_html = "View announcements by 100 largest foundations&raquo";
		ANNOUNCEMENTS_HTML = "Complete List of Foundation Announcements";
	}
		
			new Ajax.Updater('foundations', 'forecastController.php?full_list='+full_list_val,
			{	method:'get',
				onSuccess: function(transport){var response = transport.responseText || "no response text";},
				onFailure: function(){alert('Something went wrong...')},
				onComplete: function()
				{	
					//remove panel's markup/placeholder as opposed to visiblity only. doing so ensures no trailing whitespace.
					document.getElementById("waitLoadPanel").style.display="none";
					imgId = INIT_SORT_COL_ID+"ImgId"
					currentColumn = INIT_SORT_COL_ID;
					document.getElementById(imgId).style.display="inline"; 
					document.getElementById(INIT_SORT_COL_ID).style.background="lightgrey"; 
					document.getElementById(imgId).src="images/arrow_desc.gif";
					document.getElementById("full_list_link_top").innerHTML=full_list_html; 
					document.getElementById("full_list_link_bot").innerHTML=full_list_html;
					document.getElementById("announcements").innerHTML=ANNOUNCEMENTS_HTML;
				}
			});
}

