$(function() {
	
	/********** SELECT COUNTRY **********/
	
	var ctryBox 	= $("#utilCountry");
	var ctryFirst 	= $("#utilCountry li.current");
	var ctryOptions = $("#utilCountry ul");
	var ctryItem 	= $("#utilCountry ul li");
	var hash		= window.location.hash.substr(1).replace("/","~");
	
	function onCountry()	{ ctryBox.addClass("hover"); }
	function offCountry()	{ ctryBox.removeClass("hover"); }
	function closeCountry()	{ ctryOptions.slideUp('fast',offCountry); }
	
	//Auto close if menu abandoned
	ctryBox.hoverIntent({ over: onCountry, timeout: 400, out: closeCountry });
	
	//Highlight initial item upon hovering
	ctryBox.hover(onCountry, function(){ 
		if(ctryOptions.is(':hidden')) offCountry(); 
	});
	
	//Open menu upon clicking initial item
	ctryFirst.click(function(){
		onCountry(); //force highlight in case menu was closing
		ctryOptions.is(':hidden') ? ctryOptions.slideDown('fast') : ctryOptions.slideUp('fast');
	});
	
	//Close menu upon clicking item from drop down
	ctryItem.click(closeCountry);
	
	//ctryFirst.click(); //for debugging
	
	
	/********** LOGIN FORM FIELDS **********/
	
	function checkFormField(item, checkString, passCheck) {
		if(passCheck) {
			item.focusin(function(){ 
				item.hide();
	            checkString.show();
	            checkString.focus();
				checkString.addClass('on');
			});
		} else {
			//upon entering an input field
			item.focusin(function() {
			 	item.val(item.val() == checkString ? "" : item.val()); 
				if(item.val() != checkString) item.addClass('on');
			});
			
			//upon leaving input field, reset
			item.focusout(function() { 
				item.val(item.val() == "" ? checkString : item.val()); 
				if(item.val() == checkString) item.removeClass('on');
			});
		}
	}
	
	checkFormField($("#loginBarUser"), "Email", false);
	checkFormField($("#loginBarPassHolder"), $("#loginBarPass"), true);
	
	/********** SCROLL TO TOP **********/
	$('#toTop').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
	});
	
	/********** ALTERNATING TABLE ROW COLORS **********/
	$(".grid table.zebra").each(function(){
		$(this).find("tr:not('.staticRow'):even").addClass('alt');
	});
	
	$("#mainPane ul, #bioMain ul, #bioMain ol").each(function(){
		$(this).find("li:even").addClass('alt');
	});
	
	/********** TABS **********/
	
	function loadTab(item) {
		$(".tabs li").removeClass('active');
		item.addClass('active');

		if(!$("#tabContent").is(':empty')) {
			if($("."+item.attr('id')).is(':hidden')) {	
				$(".tab").hide();
				$("."+item.attr('id')).show();
			}
		}
		
		hash = item.attr("title");
		location.hash = hash;
	}
	
	//load tab upon clicking
	$(".tabs li").click(function(){
		loadTab($(this));
	});

	if($(".tabs").length)
	{
		if(hash) {
			var hashes = hash.split("~");
			var found = 0;
			$(".tabs li").each(function(){
				if(hashes[0] == $(this).attr("title")) {
					$(this).click();
					found++;
				}
			});
			if(!found) loadTab($(".tabs li:first"));
		} else loadTab($(".tabs li:first"));
	}
	
	/********** TOOLTIPS **********/
	$(".tip").tooltip({ effect: 'slide'});

	/********** OVERLAYS **********/	
	$(".overlayLink").overlay({
		onBeforeLoad: openOverlay,
		onLoad: initOverlay,
		onClose: closeOverlay
	});
	
	$("#modalPane").height($(document).height()+100);
	
	function openOverlay(){
		$("#modalPane").show();
	}
	
	function initOverlay(){
		if($(".timeField").length) {
			$(".timeField").timePicker({
			  startTime: "07:00",
			  endTime: new Date(0, 0, 0, 23, 00, 0),
			  show24Hours: false,
			  separator: ':',
			  step: 15});
		}
	}
	
	function closeOverlay(){
		$("#modalPane").hide();
	}
});	
	
function notify(message){
	$('#status').html(message);
	$("#status").show();
	setTimeout(function() { $('#status').fadeOut(1000); }, 3500);
}

