var TimerForHidingPopUpMenu = 0;
var IDofPopUpMenuNowShowing = "";
var ClientViewsAreActive = false;
var permits_subcategories = false;



function AddPageToFavorites()
{
	var theUserName = $("#username").text();
	var theHostname = location.host;
	var URL = window.location.pathname;
	var Title = document.title;
	
	if (theUserName == "")
	{
		alert("Please log in to use your lists.");
		URL = 'http://' + theHostname + '/login/' + $("#host_id").text();
		window.location = URL;
		return;
	}
	
	$.getJSON("http://" + theHostname + "/AddPageToFavorites?jsoncallback=?", {URL: URL, Title: Title, an_ajax_call_is_active: 'true'}, function(data){
		var TestForTimeout = data['Timeout'];

		if (TestForTimeout == 'Timeout')
		{
			alert("The session has timed out. Please log in again to continue.");
			window.location="/home/All/All/" + $("#host_id").text();
		}
		else
		{
			if (data['AlreadySaved'] == 'true')
			{
				$('#dialog_header').text("Already Added");
				$("#long_message").html("<p>This page has already been added to your favorites.<\p><p>Access your favorites on your profile page.<\p>");
			}
			else
			{
				$('#dialog_header').text("Added");
				$("#long_message").html("<p>This page has been added to your favorites.</p><p>Access your favorites on your profile page.<\p>");
			}
			$('#JQM_Dialog').jqm(
			{
				onHide: function(h) {
					h.o.remove(); // remove overlay
					h.w.fadeOut(444); // hide window
				}
			});

			$('#JQM_Dialog').css("top", ($(window).scrollTop() + 96) + "px");
			$('#JQM_Dialog').jqmShow();

			return;
		}
	});		
}

function KeepPopUpMenuOnScreen()
{
	clearTimeout(TimerForHidingPopUpMenu);
}

function HidePopUpMenu(thePopUpMenu)
{
	if ($(thePopUpMenu).is(":visible"))
	{
		var theID = $(thePopUpMenu).attr('id');
		theID = "#"+theID;
		TimerForHidingPopUpMenu = setTimeout('$("' + theID + '").hide();', 100);
	}
}

function ShowPopupMenu(IDofPopupMenu)
{
	if ((IDofPopUpMenuNowShowing != IDofPopupMenu) && (IDofPopUpMenuNowShowing != ""))
		$(IDofPopUpMenuNowShowing).hide();
	$(IDofPopupMenu).show();
	IDofPopUpMenuNowShowing = IDofPopupMenu;
}

function Menu2BeginHover()
{
	if (! ClientViewsAreActive)
		$(this).parent().addClass("header_2_clickMe");
		
	if (permits_subcategories)
	{
		var NameOfMenu = $(this).text();
		NameOfMenu = NameOfMenu.replace(' ', '_');
		var IDofPopupMenu = "#" + NameOfMenu + '_popup';
		KeepPopUpMenuOnScreen();
		ShowPopupMenu(IDofPopupMenu);
	}
}

function Menu2EndHover(theEvent)
{
	if (! ClientViewsAreActive)		
		$(this).parent().removeClass("header_2_clickMe");
		
	if (permits_subcategories)
	{
		var NameOfMenu = $(this).text();
		NameOfMenu = NameOfMenu.replace(' ', '_');
		var IDofPopupMenu = "#" + NameOfMenu + '_popup';
		var thePopUpMenu = $(IDofPopupMenu);
		if ($(thePopUpMenu).length == 0)
			return;

		var TopLeftOfPopUp = $(IDofPopupMenu).offset();
		var BottomOfPopUp = TopLeftOfPopUp.top + $(IDofPopupMenu).height();
		var RightOfPopUp = TopLeftOfPopUp.left + $(IDofPopupMenu).width();
		var TopOfMousePosition = theEvent.pageY;
		var LeftOfMousePosition = theEvent.pageX;

		var MouseIsHoveringOverPopUp =
			((TopOfMousePosition >= TopLeftOfPopUp.top) && (TopOfMousePosition <= BottomOfPopUp) &&
			(LeftOfMousePosition >= TopLeftOfPopUp.left) && (LeftOfMousePosition <= RightOfPopUp));

		if (! MouseIsHoveringOverPopUp)
			HidePopUpMenu(thePopUpMenu);
	}
}

function Menu3BeginHover()
{
	if ($(this).attr("id") == "mark_favorite")
		return;
	if (! ClientViewsAreActive)
		$(this).parent().addClass("header_2_clickMe");
}

function Menu3EndHover()
{
	if (! ClientViewsAreActive)
		$(this).parent().removeClass("header_2_clickMe");
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function ThirdPartyCookiesAreSupported()
{
	var ThirdPartyCookiesAreSupportedHere = true;
	var HostID = $("#host_id").text();
	if (HostID == "host_id_0")
		return ThirdPartyCookiesAreSupportedHere;

	//document.cookie = "TemporaryTestCookie=yes;";
	Set_Cookie("TemporaryTestCookie", "yes", "5", "/", ""); //this cookie will self-destruct in 5 seconds
	if(document.cookie.indexOf("TemporaryTestCookie=") == -1) {
		$("body").empty();
		var S = '<div class="no_links_yet"><p>Please make sure that third-party cookies are enabled. If you are using Safari, please go to "Preferences->Security" and set "Accept Cookies" to "Always."</p> <p>If you are using FireFox, please go to "Preferences->Privacy". In the "Firefox will:" dropdown menu, select "Use custom settings for history." Check the checkboxes for "Accept cookies from sites" and "Accept third-party cookies".</p></div>';
		$("body").css("padding", "10px");
		$("body").css("maxWidth", "560px");
		$("body").css("backgroundColor", "white");
		$("body").append(S);
		$(".no_links_yet").css("maxWidth", "510px");
		return ThirdPartyCookiesAreSupportedHere = false;
	}

	return ThirdPartyCookiesAreSupportedHere;
}


$(document).ready(function()
{
	if (! ThirdPartyCookiesAreSupported())
		return;
	
	var config = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 20, // number = milliseconds for onMouseOver polling interval    
		over: Menu2BeginHover, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: Menu2EndHover // function = onMouseOut callback (REQUIRED)    
	};

	$(".header_2_menu ul li a").hoverIntent(config);
	
	config = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 20, // number = milliseconds for onMouseOver polling interval    
		over: Menu3BeginHover, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out:  Menu3EndHover// function = onMouseOut callback (REQUIRED)    
	};
	
	$(".header_3_menu li a").hoverIntent(config);

	//position all menu 2 popups
	$(".header_2_menu ul li a").each(function (i) 
		{
			var NameOfMenu = $(this).text();
			NameOfMenu = NameOfMenu.replace(' ', '_');
			var IDofPopupMenu = "#" + NameOfMenu + '_popup';
			var thePopUpMenu = $(IDofPopupMenu);
			var thePosition = $(this).position();
			var theTop = thePosition.top + $(this).height();
			var theLeft = thePosition.left;
			$(thePopUpMenu).css("top", (theTop) + "px");
			$(thePopUpMenu).css("left", theLeft + "px");
		}
	);		
	
	$('.popUpMenu').hover(
		function(){
			KeepPopUpMenuOnScreen(this);
		}, 
		
		function(){
			HidePopUpMenu(this);
		}
	);

	$('#mark_favorite').hover(
		function(){
			$(this).css("opacity", "0.50");
		},

		function(){
			if (! $(this).hasClass("current_sub_category"))
				$(this).css("opacity", "1.0");
		}
	);

	$('#mark_favorite').click(
		function(){
			AddPageToFavorites();
			return false;
		}
	);	
	
	$('.popUpMenuItem').hover(
		function(){
			$(this).css("font-weight", "bold");
		}, 
		
		function(){
			if (! $(this).hasClass("current_sub_category"))
				$(this).css("font-weight", "normal");
		}
	);	

	$('.popUpMenuItem').click(
		function(){
			var theHref = $(this).children("a").attr('href');
			var theHostname = location.host;
			var S = "http://" + theHostname + theHref;
			if (! ClientViewsAreActive)
				window.location = S;
			return false;				
		}
	);

	var S = $("#permits_subcategories").text();
	if (S == "1")
		permits_subcategories = true;

	$(window).scroll(function()
	{
		//http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=303
		if ($("#JQM_Dialog").is(":visible"))
	  		$('#JQM_Dialog').animate({top:($(window).scrollTop()+96)+"px" },{queue: false, duration: 350});
	});
	
//	on Mac FF, on older Macs, the popup menus disappear behind google flash ads. 
//	this fixes that. 
	$("embed").attr("wmode", "opaque").wrap('<div>');
		
});  