// HO RELOADED

var menustate = 0;
var KEYCODE_ESC = 27;
var homeimage = 1;
var homeimagetimeout = 11000;
var homecurimage = 1;
var homeimagerotate;
var menutimeout;


// escape from menu
$(document).keyup(function(e) {
  if (e.keyCode == KEYCODE_ESC) {
	$("#main-nav a").removeClass("navFocusState").removeClass("subNavFocusState");
	hideMenu();	  
	$("#content-body").removeClass("ie6fix");
	} 
});

$(document).ready(function(){
	$(".filter > li > a").click(function(event) { 
		if (getUrlVars().view === "Search+results") {
			event.preventDefault();
			$(".keyword_search").append('<div id="dialog-confirm" title="Filter publications?"><p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Filtering content will cause you to lose your search results. <br /> Continue?</p></div>');
			
			$( "#dialog-confirm" ).dialog({
				resizable: false,
				height:230,
				width: 600,
				modal: true,
				buttons: {
					"Proceed": function() {
						$( this ).dialog( "close" );
						window.location = event.currentTarget.attributes[0].nodeValue;
					},
					Cancel: function() {
						$( this ).dialog( "close" );
					}
				}
			});
			
			$( "#dialog:ui-dialog" ).dialog( "destroy" );
		}
	});


	var ieVer = getInternetExplorerVersion();		
	
	var cookieValue = $.cookie('ho_fontsize');
	
	if (cookieValue != null)
	{
		$("body").css("font-size", cookieValue);
	}
	
	$("a.resize.normal").click(function() {
		$("body").css("font-size", "73%");
		$.cookie('ho_fontsize', "73%", {path:'/'});
	});
	$("a.resize.large").click(function() {
		$("body").css("font-size", "90%");
		$.cookie('ho_fontsize', "90%", {path:'/'});
	});
	$("a.resize.larger").click(function() {
		$("body").css("font-size", "120%");
		$.cookie('ho_fontsize', "120%", {path:'/'});
	});
	
	$("a.resize").attr("href", "javascript:void(0);").click(function() {
		megaDropdownOverlap();		
		dropdownRight();
		findBottomRow();
	});
	
	$("#main-search-input").val("Search this site").addClass("pre-search");
	
	$(".pre-search").click(function() {
		$(this).val("").css("color", "#000");
	});
	$(".pre-search").focus(function() {
		$(this).val("").css("color", "#000");
	});
	
	$(".search-icon").click(function() {
		$("#main-search-form").submit();
	});

	// check megadropdown overlap
	if ($("#main-header").height() > 135)
	{
		megaDropdownOverlap();
	}
	
	dropdownRight();
	findBottomRow();

	$('a[rel="external"]').attr("class", "new-window").attr("target", "_blank").append("<span class=\"off\"> (Opens in a new window)</span>");
	
	if (ishomepage && homeimagecount > 1 && disablehighlight != 'Yes')
   	{
		homeimagerotate = setTimeout('homeImageRotate()', homeimagetimeout);
	}
 
 	// faqs
	$(".inlinehelp").hide();
	
	$("#faq-showall").attr("href", "javascript:void(0);").click(function() {
		if ($(this).text() == "Show all FAQ items")
		{
			$(".inlinehelp").slideDown();
			$(this).text("Hide all FAQ items");
		}
		else
		{
			$(".inlinehelp").slideUp();
			$(this).text("Show all FAQ items");
		}
	});
	
	$(".faq-list a.helpLink").attr("href", "javascript:void(0);").click(function() {
		if ($(this).parent().next(".inlinehelp").css("display") == 'none')
		{
			$(this).parent().next(".inlinehelp").slideDown();
		}
		else
		{
			$(this).parent().next(".inlinehelp").slideUp();
		}
	});
 
	// main nav hoverisations
	$(".nav-item").hover(function() {
	  	clearTimeout(menutimeout);
		$(".subnav").css("left", "-9999px");
		$("a:not(.subnav a)", this).addClass("navFocusState");
		
		$(this).children().not(".rightdrop").css("left", "0");
		$(this).children(".rightdrop").css("right", "0").css("left", "auto");
		
		if (ieVer < 8)
		{
			$(this).children().children().children().css("left", "0");
			if (!$(this).hasClass("bottom-row"))
			{
				$(this).css("z-index", 700);				
				$(".bottom-row").css("z-index", "400");
			}
		}
	}, function() {
		$("a:not(.subnav a)", this).removeClass("navFocusState");
		menutimeout = setTimeout("hideMenu()", 400);		
	});
	
	
	$("#homepage #highlight-headlines-list a").attr("href", "javascript:void(0);").click(function() {
		if ($(this).parent().attr("class") != "highlight-active")
		{
			var listNum = $(this).attr("id").replace("highlight-headlines-item", "");
			homeNewsImage(listNum);
		}
	});
	
  // custom css expression for a case-insensitive contains()
	jQuery.expr[':'].Contains = function(a,i,m){
	return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
	};
	
	function listFilter(inputField, list) 
	{ 
		$(inputField)
			.change( function () 
				{
					var filter = $(this).val();
					if(filter) 
					{
						// this finds all links in a list that contain the input,
						// and hide the ones not containing the input while showing the ones that do
						$(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
						$(list).find("a:Contains(" + filter + ")").parent().slideDown();
					} 
					else 
					{
						$(list).find("li").slideDown();
					}
					return false;					
				})
				
			.keyup( function () 
				{
					// fire the above change event after every letter
					$(this).change();
				});
	}
	
	if ($("#filter_search0").length)
	{
		$(function () {
			listFilter($("#filter_search0"), $("#filter_list0"));
		});
	}
	if ($("#filter_search1").length)
	{
		$(function () {
			listFilter($("#filter_search1"), $("#filter_list1"));
		});
	}
	if ($("#filter_search2").length)
	{
		$(function () {
			listFilter($("#filter_search2"), $("#filter_list2"));
		});
	}
	if ($("#filter_search3").length)
	{
		$(function () {
			listFilter($("#filter_search3"), $("#filter_list3"));
		});
	}
	if ($("#filter_search4").length)
	{
		$(function () {
			listFilter($("#filter_search4"), $("#filter_list4"));
		});
	}
	if ($("#filter_search5").length)
	{
		$(function () {
			listFilter($("#filter_search5"), $("#filter_list5"));
		});
	}
	
});

function hideMenu()
{
	$(".subnav").css("left", "-9999px");
}

function homeImageRotate()
{
	// homepage rotating image.
	homecurimage = (homecurimage == homeimagecount) ? 1 : homecurimage + 1;		
	homeNewsImage(homecurimage);	
	homeimagerotate = setTimeout('homeImageRotate()', homeimagetimeout);
}

function megaDropdownOverlap()
{
		var newHeight = ($("#main-header").height() - 135) + 135;
		var newIcon = ($("#main-header").height() - 135) + 142;
		$("#main-content").css("padding-top", newHeight);
		$(".sub-level-icon").css("top", newIcon);
}

function dropdownRight()
{
	
	$(".subnav").removeClass("rightdrop").each(function() {
		var navOffset = $(this).offset();
		if (navOffset.left + (9999 + 355) > 975)
		{
			$(this).addClass("rightdrop");
		}
	});
}

function findBottomRow()
{
	// poor old ie6
	var lastoffset = 0;
	$(".nav-item").each(function() {
		var navOffset = $(this).offset();

		if (navOffset.left < lastoffset)
		{
			$(this).addClass("bottom-row");
			lastoffset = 10000;
		}
		else
		{
			lastoffset = navOffset.left;
		}
	});
}

function homeNewsImage(listNum)
{
	clearTimeout(homeimagerotate);
	
	$("#highlight-headlines-list li").removeClass("highlight-active");
	$("#highlight-headlines-item" + listNum).parent().addClass("highlight-active");

	$("#highlight-currentheadline").html($(".headline" + listNum + " h2").html());
	$("#highlight-currentcontent").html($(".headline" + listNum + " p").html());

	$(".highlightimage").fadeOut();
	$("#highlight .image" + listNum).fadeIn();
	
	homecurimage = listNum;
}


function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

//Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function dialogConfirm(term)
{
	$(".keyword_search").append('<div id="dialog-confirm" title="Search for the term?"><p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>A search will be run against all publications content for the term <b>' + term  +'</b>. The current filtered list of search results will be lost.</p></div>');
			
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	
	$( "#dialog-confirm" ).dialog({
		resizable: false,
		height:230,
		width: 600,
		modal: true,
		buttons: {
			"Proceed": function() {
				$( this ).dialog( "close" );
				$("#publictions-search-form").submit();
			},
			Cancel: function() {
				$( this ).dialog( "close" );
				$("#publictions-search-form").click(function(event) {
					event.preventDefault();
				}); 
			}
		}
	});
}

function dialogError()
{
	$(".keyword_search").append('<div id="dialog" title="Error!"><p>Please enter a search term.</p></div>');
	$(function() {
		$( "#dialog" ).dialog({
			resizable: false,
			height:230,
			width: 600,
			modal: true				
		});
	});
	return false;
}

function validatePublicationSearchTerm()
{
	  $("#publictions-search-form").click(function(event) {
		event.preventDefault();
	  });
	  
	  if ($("#publication_SearchTerm").val() != "") {
	  	if (getUrlVars().filYear !== undefined || getUrlVars().selSubject !== undefined) {
	  		return dialogConfirm($("#publication_SearchTerm").val());
	  	}
	  	else {
			$("#publictions-search-form").submit();
		}
	  }
	  else {
		dialogError();
	  }		
}





