$(document).ready(function() {
	$("#mainMenu ul:first > li").each(
		function(i, elm){
				
			$(elm).hover(
				function () {
					$("#mainMenu ul li div:first").each(
						function(i, elm){
							jQuery(this).css("display", "none");
						}
					)

					$(this).children().filter("div:first").fadeIn("fast");
				},

				function () {
					$(this).children().filter("div:first").css("display", "none");
				}
			)
		}
	)
	
	// Setup Any Roll Overs Which May Be Applicable
	$('.imgHover').imgHover();

	// Setup Cookie Events For Masthead Menu Options
	$('a[name]').click(function(){	
		$.cookie('submenu', $(this).attr('name'), { path:$(this).attr('href')});
	});
	
	// Older Tab Control Support
	$("#menuTechBulletins").click(function(){
		$.cookie('resourcesTab', 'tabTechBulletins');
	});

	$("#menuWhitePapers").click(function(){ 
		$.cookie('resourcesTab', 'tabWhitePapers');
	});
	
	$("#menuUnderHour").click(function(){
		$.cookie('resourcesTab', 'tabUnderHour');
	});
	
	// Setup Site Search
	$('#btnSiteSearch').click(function(){
		document.location = "/search/index.aspx?q=" + document.getElementById("txtSiteSearch").value;
	});
	
	if($('#subMenu').length > 0){
		$.getScript('/js/jquery/jquery.submenu-display.js', function(data, status){		
			if(status === "success"){
				$("#subMenu").SubMenuDisplay();
			}		   
		});
	}
	
	if($("#resources").length > 0){
		$.getScript('/js/jquery/jquery.resource-display.js', function(data, status){
			if(status === "success"){
				$("#resources").ResourceDisplay();
			}
		});
	}

	// Add Short Cut Icon
	$("head").append("<link rel=\"shortcut icon\" href=\"/img/lv.ico\" />");
	
	// Hack to Re-draw page in IE
	if ( $.browser.msie ) {
		$('#pageWraper').addClass('');
	}
	
});	

$.preLoadImg = function() {
	var imgCache = [];
	var argsLen = arguments.length;
	for (var i = argsLen; i--;) {
		var domImg = document.createElement('img');
			domImg.src = arguments[i];
			imgCache.push(domImg);
	}
}

$.fn.imgHover = function() {
	 this.each(function(p_index, p_elm) {
	 	var file = p_elm.src.split('/').pop();

		var fileRollOver = p_elm.src.substring(0, p_elm.src.lastIndexOf('/'));
			fileRollOver += '/' + file.substring(0, file.lastIndexOf('.'));
			fileRollOver += '-over' + file.substring(file.lastIndexOf('.'), file.length);
			
		var defaultFile = p_elm.src;
		
		$.preLoadImg(fileRollOver);

	 	$(p_elm).hover(
	 		function (){
	 			this.src = fileRollOver;
	 		},
	 		function (){
	 			this.src = defaultFile;
	 		}
	 	);
	 });
	 return this;
};

$.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
			options.expires = -1;
		}

		var date = new Date();
		if (typeof options.expires == 'number' && options.expires != -1) {
			date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		}else{

			date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
		}

		var expires = '; expires=' + date.toUTCString();

		// NOTE Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = "; path=/"; // Force All Cookies in Root
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';

		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};
