/*	Javascript Source File: tabscripts.js

	Author:	Garrison Hansen, Natural Science Division Webster, Fall 2007
	
	Purpose:This javascript file is designed for use with a faculty web page
			template. It contains code that doesn't need to be modified with each page.
*/
var width;	//holds the old window values loaded from a cookie
var height;	//or the current window size if no cookie is found

/*	function: 	load
	parameters:	none
	returns: 	nothing
	
	purpose:	called when page is loaded. Clicks on the default tab if the page hasn't
				been opened yet
*/
function load()
{
	/*	This checks a saved cookie and will keep the window from reverting 
		to the default tab when resized (a bug due to the 'write_banner()' command)
		It will 'click' on the default tab if no info is found (when page first loaded) 
		and when the page is reloaded with the same window dimensions
	*/
	width=document.body.clientWidth;
	height=document.body.clientHeight;
	
	var currentTab=readCookie();
	if (currentTab==undefined)
		currentTab=defaultTab;
	if (!didSizeChange())
		currentTab=defaultTab;
	tabClick(currentTab);
}

/*	function: 	tabClick
	parameters:	bodyId (a tab id)
	returns: 	nothing
	
	purpose:	called when a tab is clicked on, and when the page is loaded. Calls the other
				functions that will change the visibility of the different tabs
*/				
function tabClick(bodyId)
{
	visibleByIdTab(bodyId);
	setTabImg(bodyId+"T");
	//	Saves the current tab and window dimensions in a cookie
	setCookie(bodyId);
	//	Resets the location of the copyright
	fixLocation();
}

/*	function: 	setDisplay
	parameters:	bodyId (a tab id) , display (block or none) 
	returns: 	nothing
	
	purpose:	hides or displays a tab's body information
*/
function setDisplay(id, display) 
{
	document.getElementById(id).style.display = display;
}

/*	function: 	setCookie
	parameters:	id (current tab)
	returns: 	nothing
	
	purpose:	called from tabClick(). Saves current tab, and window dimensions into a cookie.
				(the cookie is deleted when the brower is closed)
*/
function setCookie(id)
{
	var cookieInfo="current_tab="+escape(id);
	document.cookie=cookieInfo;
	cookieInfo="window_width="+escape(document.body.clientWidth);
	document.cookie=cookieInfo;
	cookieInfo="window_height="+escape(document.body.clientHeight);
	document.cookie=cookieInfo;
}

/*	function: 	readCookie
	parameters:	none
	returns: 	currentTab or undefined (if unable to read currentTab)
	
	purpose:	called when page is loaded. Attempts to read the currentTab, old width, and old
				height from a cookie. Stores old window dimensions into 'width' and 'height'
				
*/
function readCookie()
{
	var currentInfo, oWidth, oHeight;
	var cookieInfo=document.cookie;
	cookieInfo=unescape(cookieInfo);
	//	Parses all the different cookies that may be in the web page
	cookieInfo=cookieInfo.split(";");
	oWidth=cookieInfo[1];
	if (oWidth!=undefined)
	{
		oWidth=oWidth.split("=");
		width=oWidth[1];
	}				
	oHeight=cookieInfo[2];
	if (oHeight!=undefined)
	{
		oHeight=oHeight.split("=");
		height=oHeight[1];
	}
	currentInfo=cookieInfo[0];
	currentInfo=currentInfo.split("=");
	if(currentInfo[0]=="current_tab")
		return currentInfo[1];
	else
		return undefined;
}

/*	function: 	didSizeChange
	parameters:	none
	returns: 	true or false
	
	purpose:	Called with load(). Checks to see if window size has changed from old window
				dimensions stored in the cookie				
*/
function didSizeChange()
{
	var same=true;
	var x=document.body.clientWidth;
	var y=document.body.clientHeight;
	//if either of these isn't true then 'same' is changed to false
	same*=(x==width);
	same*=(y==height);
	return !same;
}

/*	function: 	fixLocation
	parameters:	none
	returns: 	nothing
	
	purpose:	Called with tabClick(). Makes sure that the copyright information is at the bottom
				of the page, or directly under the tab body if that is taller than the page.
*/
function fixLocation()
{
	//var mainH=document.getElementById("main").clientHeight;
	//mainH+=document.getElementById("tabsec").clientHeight;
	//var winH=document.body.clientHeight;
	//document.getElementById("copyright").style.top=winH-mainH-90-25;
	//if (winH-mainH-90-25<0)
		//document.getElementById("copyright").style.top=1;
}

