/*******************************************************
Desc: Holds small javascript helper functions that only
		appear on the homepage. (ie www.millersville.edu)
*******************************************************/
	function swap(id) //# Swap to the selected tab.
	{		
		// What fields?
		var ids = ['cultural_content','academics_content','sports_content'];

		// hide all
		for (var i=0; i<ids.length; i++)
			document.getElementById(ids[i]).style.display = 'none';

		// show selected id and change the events_bg background image to show the appropriate color
		document.getElementById(id).style.display = 'block';
	}

	// remove default text from search box
	function removeSearchText(txtBox)
	{		
		if(txtBox.value == txtBox.defaultValue)
			txtBox.value = "";
		else if(txtBox.value == "")
			txtBox.value = txtBox.defaultValue;
	}


/*******************************************************
Desc: Holds small javascript helper functions that appear
		globally. (i.e. the top site index)
*******************************************************/
	


	function switchSearchAction(newAction)
	{
		document.search.action = newAction;
	}

	function switchSearchAction2(newAction)                                                                                             
        {                                                                                                                                   
        	document.gs.action = newAction;                                                                                                 
        }  

	var timerID;

	function show(id)  
	{ 
		obj = document.getElementById(id); 
		obj.style.display = 'block'; 
		clearTimeout(timerID);  
	}

	function hide(id)  
	{ 
		obj = document.getElementById(id); 
		timerID = setTimeout("obj.style.display = 'none'", 500); 
	}

	function swapImg(obj)
	{
		var newSrc = obj.src.split(".");		// Split the full path into pieces.
		var i = newSrc.length-2		// Grab the image's path without the extension.

		//	If the over is not present, concatinate -over to the end of the image path.
		//	Else, remove -over from the image path.
		if(newSrc[i].indexOf("-over") == -1)
			newSrc[i] = newSrc[i] +  "-over";
		else
			newSrc[i] = newSrc[i].replace("-over", "");

		newSrc = newSrc.join(".");							// Join the full path back together.
		obj.src = newSrc;	// Replace the current image with the newer one.
	}

	function swapObjImg(obj)
	{
		var newSrc = obj.getElementsByTagName("img")[0].src.split(".");		// Split the full path into pieces.
		var i = newSrc.length-2		// Grab the image's path without the extension.

		//	If the over is not present, concatinate -over to the end of the image path.
		//	Else, remove -over from the image path.
		if(newSrc[i].indexOf("-over") == -1)
			newSrc[i] = newSrc[i] +  "-over";
		else
			newSrc[i] = newSrc[i].replace("-over", "");

		newSrc = newSrc.join(".");							// Join the full path back together.
		obj.getElementsByTagName("img")[0].src = newSrc;	// Replace the current image with the newer one.
	}

	var hovered = 0;

	function swapBgImg(obj)
	{
		var link = obj.getElementsByTagName("a")[0];

		if(hovered == 0)
		{
			obj.style.background = "#2f2f2f url('http://www.millersville.edu/lib/img/buttons_tabs/mn_bg-over.gif') 0px 0px";

			link.style.background = "transparent url('http://www.millersville.edu/lib/img/buttons_tabs/mn_gloss-over.gif') no-repeat 0px 0px";
			hovered = 1;
		}
		else
		{
			obj.style.backgroundImage = "url('http://www.millersville.edu/lib/img/buttons_tabs/mn_bg.gif')";
			link.style.backgroundImage = "url('http://www.millersville.edu/lib/img/buttons_tabs/mn_gloss.gif')";

			if(obj.className == 'orange')
			{
				obj.style.backgroundColor = "#c47a09";
				obj.style.backgroundPosition = "0px 0px";
			}

			else if(obj.className == 'green')
			{
				obj.style.backgroundColor = "#344c29";
				obj.style.backgroundPosition = "0px	-136px";

				link.style.backgroundPosition = "0px -136px";
			}

			else if(obj.className == 'blue')
			{
				obj.style.backgroundColor = "#015e99";
				obj.style.backgroundPosition = "0px	-272px";

				link.style.backgroundPosition = "0px -272px";
			}

			else if(obj.className == 'tan')
			{
				obj.style.backgroundColor = "#c8a24c";
				obj.style.backgroundPosition = "0px	-408px";

				link.style.backgroundPosition = "0px -408px";
			}

			else if(obj.className == 'brown')
			{
				obj.style.backgroundColor = "#815b3e";
				obj.style.backgroundPosition = "0px	-544px";

				link.style.backgroundPosition = "0px -544px";
			}

			else if(obj.className == 'purple')
			{
				obj.style.backgroundColor = "#2d0e4a";
				obj.style.backgroundPosition = "0px	-680px";

				link.style.backgroundPosition = "0px -680px";
			}

			else
			{
				obj.style.backgroundColor = "#973523";
				obj.style.backgroundPosition = "0px	-816px";

				link.style.backgroundPosition = "0px -816px";
			}

			hovered = 0;
		}
	}

	function swapBgImgReverse(obj)
	{
		var link = obj.getElementsByTagName("a")[0];

		if(hovered == 0)
		{
			obj.style.backgroundImage = "url('http://www.millersville.edu/lib/img/buttons_tabs/mn_bg.gif')";
			link.style.backgroundImage = "url('http://www.millersville.edu/lib/img/buttons_tabs/mn_gloss.gif')";

			if(obj.className == 'orange')
			{
				obj.style.backgroundColor = "#c47a09";
				obj.style.backgroundPosition = "0px 0px";
			}

			else if(obj.className == 'green')
			{
				obj.style.backgroundColor = "#344c29";
				obj.style.backgroundPosition = "0px	-136px";

				link.style.backgroundPosition = "0px -136px";
			}

			else if(obj.className == 'blue')
			{
				obj.style.backgroundColor = "#015e99";
				obj.style.backgroundPosition = "0px	-272px";

				link.style.backgroundPosition = "0px -272px";
			}

			else if(obj.className == 'tan')
			{
				obj.style.backgroundColor = "#c8a24c";
				obj.style.backgroundPosition = "0px	-408px";

				link.style.backgroundPosition = "0px -408px";
			}

			else if(obj.className == 'brown')
			{
				obj.style.backgroundColor = "#815b3e";
				obj.style.backgroundPosition = "0px	-544px";

				link.style.backgroundPosition = "0px -544px";
			}

			else if(obj.className == 'purple')
			{
				obj.style.backgroundColor = "#2d0e4a";
				obj.style.backgroundPosition = "0px	-680px";

				link.style.backgroundPosition = "0px -680px";
			}

			else
			{
				obj.style.backgroundColor = "#973523";
				obj.style.backgroundPosition = "0px	-816px";

				link.style.backgroundPosition = "0px -816px";
			}
			hovered = 1;
		}
		else
		{
			obj.style.background = "#2f2f2f url('http://www.millersville.edu/lib/img/buttons_tabs/mn_bg-over.gif') 0px 0px";

			link.style.background = "transparent url('http://www.millersville.edu/lib/img/buttons_tabs/mn_gloss-over.gif') no-repeat 0px 0px";

			hovered = 0;
		}
	}
	
	//	Used for the Majors/Minors/Programs listing
	function showHideTab(id)
	{
		var el = document.getElementById(id);
		var first = document.getElementById('first');
		var second = document.getElementById('second');

		if(el.style.display == 'none')
		{
			if(el == first)
			{
				second.style.display = 'none';
				
			}
			else if(el == second)
			{
				first.style.display = 'none';
				
			}
			swapObjImg(document.getElementById('1'));
			swapObjImg(document.getElementById('2'));
			el.style.display = 'block';
		}
	}


/*******************************************************
Desc: Scaling functions used to resize the page contents.
*******************************************************/
	var defaultSize = 62.5;

	/*************************************
	 * Func: createCookie
	 * Desc: create new cookie
	 * Cite: http://www.quirksmode.org/js/cookies.html
	 * @param {Object} obj
	 */

	function createCookie(name,value,days)
	{
		if (days) 
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else 
		{
			var expires = "";	
		}
		
		document.cookie = name+"="+value+expires+"; path=/";
	}


	/*************************************
	 * Func: readCookie
	 * Desc: reasds cookie data
	 * Cite: http://www.quirksmode.org/js/cookies.html
	 * @param {Object} obj
	 */

	function readCookie(name)
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		
		for(var i=0;i < ca.length;i++) 
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) 
					return c.substring(nameEQ.length,c.length);
		}
		
		return null;
	}



	/*************************************
	 * Func: getFontSize
	 * Desc: returns fontSize w/o unit
	 * @param {Object} obj
	 */

	function getFontSize(obj)
	{
		size = obj.style.fontSize ? obj.style.fontSize : defaultSize + "%";
		return parseFloat(size.replace("%", ""));
	}


	/*************************************
	 * Func: setFontSize
	 * Desc: sets fontSize of obj
	 * @param {Object} obj
	 */

	function setFontSize(obj, size, unit)
	{
		obj.style.fontSize = size + unit;
		return size;
	}


	/*************************************
	 * Func: turnLinkOn
	 * Desc: Sets link to active state
	 * @param {Object} id
	 */

	function turnLinkOn(id)
	{
		document.getElementById(id).style.color  = "#ddd";
		document.getElementById(id).style.cursor = "pointer";
	}


	/*************************************
	 * Func: turnLinkOff
	 * Desc: Sets link to inactive state
	 * @param {Object} id
	 */

	function turnLinkOff(id)
	{
		document.getElementById(id).style.color  = "#8f8f8f";
		document.getElementById(id).style.cursor = "default";
	}


	/*************************************
	 * Func: setLinkState
	 * Desc: Changes links to proper color and changes to proper cursor
	 * @param {Object} newSize
	 */

	function setLinkState(newSize)
	{
		if(newSize > 90)
		{
			turnLinkOff("grow");
			turnLinkOn("shrink");
		}
		else if(newSize < 63.0)
		{
			turnLinkOn("grow");
			turnLinkOff("shrink");
		}
		else 
		{
			turnLinkOn("grow");
			turnLinkOn("shrink");
		}
	}


	/*************************************
	 * Func: fontSizer
	 * Desc: Changes fontSize of body element 
	 * 	@param {Object} op 
	 */

	function fontSizer(op)
	{
		body = document.getElementsByTagName("body")[0];
		step = 10;
		size = getFontSize(body);
		
		if((size < 90 && op == "+" ) || (size > 63 && op == "-"))	
		{
			newSize = eval(size + op + step);
			setFontSize(body, newSize , "%");
			
			createCookie("muTextSize", newSize, 7);
			setLinkState(newSize);
		}
	}


	/*************************************
	 * Func: window.onload
	 * Desc: Changes fontSize of body element to what is set in the cookie
	 */

	window.onload = function ()
	{
		cookieSize = readCookie("muTextSize");
		body = document.getElementsByTagName("body")[0];
		
		if(cookieSize)
		{
			setFontSize(body, cookieSize, "%");
			setLinkState(cookieSize);
		}
	}
