/*************************************************
 * Helper Functions
**************************************************/	
	
	/*************************************************
	 * Global Variables
	**************************************************/

		/* Used in buildSearchNav, runSearch */	
		var query = '';
		var interval = null;
		
	/*************************************************
	 * Function: initSearchNav
	 * by: Ryan Griffith
	 * Description: Creates the effects used for the top site index hover effects
			as well as the ajax search suggestions.
	**************************************************/
		
		function initSearchNav() {			
			$('#search-mu-keyword')
				.focus(function() {
					if(this.value == this.defaultValue)
						this.value = '';
					else if(this.value != '' && this.value != this.defaultValue)
						$('#search-suggested').fadeIn('fast');
				})
		
				.blur(function() {
					if(this.value == '')
						this.value = 'Type search here';
					
					clearInterval(interval);
					$('#search-suggested').fadeOut('fast');
				})
				
				.keyup(function(event) {
					if(this.value != '' && (event.keyCode == 9 || event.keyCode > 45))
					{
						interval = setInterval('updateQuery()',1200);
					}
					else
					{
						clearInterval(interval);
						if(this.value == '')
							$('#search-suggested').fadeOut('fast');
					}
				})
				
				.attr({autoComplete: 'off'});
			
			$('#banner-siteindex')
				.hover(
					function() { $(this).css('display', 'block'); },
					function() { $(this).fadeOut('medium'); }
				);
			
			$('#topSiteIndexLink')
				.mouseover(function() {
					if($('#search-suggested').css('display') == 'block')
						$('#search-suggested').fadeOut('fast');
												
					$('#banner-siteindex').fadeIn('fast');
				});
				
			$('#search-mu-radio-pages').click(function() {
				$('#search-mu').attr('action', 'http://www.millersville.edu/searchresults.php');
			});
			
			$('#search-mu-radio-people').click(function() {
				$('#search-mu').attr('action', 'http://www.millersville.edu/directory/search.php');
			});
		};
	
	
	/*************************************************
	 * Function: initClickableHeadings
	 * by: Ryan Griffith
	 * Description: Adds a collapse/expand effect to content headings. This
	 		includes sidebar and main content modules.
	**************************************************/
	
	function initClickableHeadings()
	{
		$('.content-module h1, .sidebar-module h1').css({'cursor':'pointer'}).toggle(
			function() { $(this).next('.module-content').slideUp(); },
			function() { $(this).next('.module-content').slideDown(); }
		);
	}
	
	
	/*************************************************
	 * Function: updateQuery
	 * by:	Ryan Griffith
	 * Description: Runs the ajax search at specific interval (see initSearchNav()). 
	**************************************************/
		function updateQuery()
		{	
			var keyword = $('#search-mu-keyword');
			
			clearInterval(interval);
			
			if(keyword.attr('value') != query)
			{
				query = (keyword.attr('value') != keyword.attr('defaultValue') && keyword.attr('value') != '') ?keyword.attr('value') : '';
				
				if(query != '')
				{
					// Show the preloader.
					$('#search-suggested').fadeIn('fast');
					$('#search-suggested-output').html('<p style="text-align:center"><img src="/lib/v2/img/common/loader.gif" alt="Searching..." /></p>');
					
					// Run the AJAX and update the results container.
					runSearch();		
				}
			}
		}	
		
	
	/*************************************************
	 * Function: runSearch
	 * by:	Ryan Griffith
	 * Description: Performs the google and people search, then adds the results to the 'searchResults' container.
	**************************************************/
		
		function runSearch()
		{
			$.ajax({
				type: "POST",
				url: "/lib/v2/inc/ajaxSearch.php",
				data: 'query='+query,
				success: function(html){				
					$('#search-suggested-output').html(html);					
				}
			});
		}
		
	
	/*******************************************************
	Desc: Holds small javascript helper functions that appear
			globally. (i.e. the top site index)
	*******************************************************/
	
		function switchSearchAction(newAction)
		{
			document.search.action = newAction;
		} 
		
		//	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 > 80)
		{
			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 < 80 && op == "+" ) || (size > 63 && op == "-"))	
		{
			newSize = eval(size + op + step);
			setFontSize(body, newSize , "%");
			
			createCookie("muTextSize", newSize, 7);
			setLinkState(newSize);
		}
	}


	/*************************************
	 * Func: initFontsize
	 * Desc: Changes fontSize of body element to what is set in the cookie
	 */

	function initFontSize()
	{
		cookieSize = readCookie("muTextSize");
		body = document.getElementsByTagName("body")[0];
		
		if(cookieSize)
		{
			setFontSize(body, cookieSize, "%");
			setLinkState(cookieSize);
		}
	}
	
	
	
// Initialization
initSearchNav();	
	
