/*
 * Function to get a cookie
 *
 * param	String 	Name of cookie
 *
 */
function getCookie( name )
{
	var cname	= name + "=";
	var dc		= document.cookie;
	
	if( dc.length > 0 )
	{
		begin = dc.indexOf(cname);
		if( begin != -1 )
		{
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if( end == -1 )
			{
				end = dc.length;
			}
			return unescape(dc.substring(begin, end));
		}
	}

	return null;
}

/*
 * Function to set a cookie
 *
 * param	String 	Name of cookie
 * param	String 	Value
 * param	String 	Expiry date
 * param	String 	Path
 * param	String 	Domain
 * param	String 	Secure
 *
 */
function setCookie( name, value, expires, path, domain, secure )
{
	document.cookie = name + "=" + escape(value) +
	((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
	((path == null) ? "" : "; path=" + path) +
	((domain == null) ? "" : "; domain=" + domain) +
	((secure == null) ? "" : "; secure");
}

/*
 * Function to delete a cookie
 *
 * param	String 	Name of cookie
 * param	String 	Path
 * param	String 	Domain
 *
 */
function delCookie( name, path, domain )
{

	if( getCookie(name) )
	{
		document.cookie = name + "=" +
		((path == null) ? "" : "; path=" + path) +
		((domain == null) ? "" : "; domain=" + domain) +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/*
 * Function to change the size of the font on the page
 *
 * param	String 	Font Size
 * param	Integer	Incremental change (eg: -1 or 1)
 *
 */
var sizeChange = "";

function changeFontsize( increment )
{

	// Declare variables
	var elementTagArray;
	var currentFontSize;

	// Is this the first call?
	if( null == getCookie('fontSize') )
	{
		sizeChange = increment;
	}
	else
	{
		sizeChange = parseFloat(getCookie('fontSize')) + increment;
	}

	sizeChange = parseFloat(sizeChange);

	if( sizeChange > -0.4 && sizeChange < 0.6 )
	{

		if( document.getElementsByTagName )
		{
			var tagArray = new Array ("h1", "h2", "h3", "h4", "p", "li");

			// Iterate through elements in tag Array
			for( j=0; j < tagArray.length; j++ )
			{

				// Get all elements in body that match the current tag we are searching for
				elementTagArray	=  document.getElementsByTagName(tagArray[j]);

				var elementTag;

				for( i=0; i<elementTagArray.length; i++ )
				{
					elementTag = elementTagArray[i];

					// Skip tag resize if class matches one of the below:
					if( "textsize" == elementTag.getAttribute("class") || "textsize" == elementTag.className ||
						"print" == elementTag.getAttribute("class") || "print" == elementTag.className ||
						"search" == elementTag.getAttribute("class") || "search" == elementTag.className )
					{
						continue;
					}

					fontIncrement		= parseFloat(increment);

					switch( tagArray[j] )
					{
						case "h1":
							newFontSize = 2 + (sizeChange * 3);
							break;

						case "h2":
							newFontSize = 1.2 + sizeChange;
							break;

						case "h3":
							newFontSize = 2 + (sizeChange * 2);
							break;

						case "h4":
							newFontSize = 1.2 + sizeChange;
							break;

						default:
							newFontSize = 1 + sizeChange;

					}

					elementTag.style.fontSize = newFontSize + "em";

				}
			}
		}

		setCookie('fontSize', sizeChange);

	}

}


/*
 * Function generate the mini scroll area in teh banner section or on the job pages
 *
 *
 */
function register_mini_scroll()
{
	// First calculate some key values

	var items = $('#scroll_pane ul').children("li.summary");
	var count_items = items.length;

	var ref_item = items[0];

	// Add up the withd of the item
	var step_width = parseInt($(ref_item).css('width').replace(/px/, ''),10);
	step_width += parseInt($(ref_item).css('padding-right').replace(/px/, ''),10);
	step_width += parseInt($(ref_item).css('padding-left').replace(/px/, ''),10);
	// and add seperator
	step_width += parseInt(1);

	var scroll_pane_width = step_width * count_items;

	// As we show 2 entries, calculate the max left (ngative) offset
	var max_offset = (count_items - 2) * step_width * (-1);

	// set the scroll pane inner part to thsi width
	$("#scroll_pane ul").css('width',scroll_pane_width);

	$("img.scroll_arrow").click(function(){
		var arrow = $(this).attr("id");

		var old_left = $("div#scroll_pane ul").css("left").replace(/px/,'');
		var new_left;

		if( arrow == "next" )
		{
			new_left = parseInt(old_left) - step_width;
			if (new_left < max_offset )
			{
				new_left = max_offset;
			}
		}
		else
		{
			new_left = parseInt(old_left) + step_width;
			if (new_left > 0 )
			{
				new_left = 0;
			}
		}

		$("div#scroll_pane ul").animate({
				left: new_left +"px"
			});
	});

}

/**
 * ------------------------------------
 * Onload Functions
 * ------------------------------------
 *
 */

/*
 * Nice helper javascript to enable you to make some javascript commands any where
 * in your script and call them once the page has loaded. To use, simply call
 * onLoad.push() to add a function as an event:
 *
 * Pass in a function to be run
 * onLoad.push(my_onload_function);
 *
 * Note that onLoad.push() is called with the function itself, not the result of
 * calling the function!
 *
 * You also pass a string to be evaluated if your function requires parameters:
 * onLoad.push("function_call('one', 2)");
 *
 * but consider using an anonymous function instead, as this will give you more
 * immediate feedback on syntax errors:
 * onLoad.push(function() { function_call('one', 2); });
 *
*/

function OnloadRunner()
{
	this.push = function( item )
	{
		var fun = this.wrapFunction(item);

		if( typeof window.addEventListener != 'undefined' )
		{
			// standards compliant browsers
			window.addEventListener('load', fun, false);
		}
		else if( typeof document.addEventListener != 'undefined' )
		{
			// opera 7
			document.addEventListener('load', fun, false);
		}
		else if( typeof window.attachEvent != 'undefined' )
		{
			// win/ie
			window.attachEvent('onload', fun);
		}
		else
		{
			//.. mac/ie5 and anything else that gets this far
			if( typeof window.onload == 'function' )
			{
				var existing = window.onload;

				window.onload = function()
				{
					existing();

					fun();
				}
			}
			else
			{
				window.onload = fun;
			}
		}
	}

	/*
	 * Wrap a function (or string to be evaluated) in a try/catch.
	 */
	this.wrapFunction = function( func_or_string )
	{
		return function()
		{
			try
			{
				if( typeof func_or_string == 'function' )
				{
					func_or_string();
				}
				else
				{
					eval(func_or_string);
				}
			}
			catch( ex )
			{
				alert("nload Failed: " + func_or_string + "\nError Message:" + ex.message + "\non line " + ex.lineNumber + " in file " + ex.fileName);
			}
		}
	}
}

var onLoad = new OnloadRunner();




/*
 * Document load
 *
 */

$(document).ready(function(){
	// Get fontSize from cookie, if set then resize page
	//changeFontsize(0)
	
	$("input.text").hint();
	
	// Add PDF icons for pdf links
	$("a.pdf").prepend('<span class="pdf"></span>');
});




/**
 * Add classes for zebra-striping tables
 **/
function zebraStripe(selector)
{
	$(selector+":odd").addClass("odd");
}

/**
 * ------------------------------------
 * Service Center Locator
 * ------------------------------------
 *
 */

/*
 * Function opens a URL passed from flash file
 *
 */

function openMapWindow(url, title)
{
    var windowParams = 'left=0,top=0,screenX=0,screenY=0,width=600,height=530,toolbars=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=yes,menubar=no,copyhistory=no';
    window.open(url, title, windowParams);
}
