var MenuTimer = 0;
var CurrentBLN_Item = 0;
var CSSLoadTimeout;
var IS_PRODUCTION = (location.host !== 'localhost:8888');
var theHostname;
var InfoDivsHaveBeenCalled = false;
var MAXLENGTHFORURL = 384;
if (IS_PRODUCTION)
	theHostname = 'generalwebapps.com';
else
	theHostname = 'localhost:8888';

//http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (value == "")
		value = "no_cookie_value";
	var expires;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function ShowDescription(theBLNItemTitle, hloc, vloc)
{
	var theDescription = jQuery(theBLNItemTitle).parent().parent().find('.BLN_Item_description').text();

	jQuery("#ToolTip").text(theDescription);
	jQuery("#ToolTip").css("top", (vloc + 16) + "px");
	jQuery("#ToolTip").css("left", (hloc + 16) + "px");
	jQuery("#ToolTip").show();
	
}

function ClearHint(theTextInputField)
{
	var theHint = jQuery(theTextInputField).attr("title");
	var theCurrentText = jQuery(theTextInputField).val();

	if (theHint == theCurrentText)
	{
		jQuery(theTextInputField).removeClass('form_field_hint');
		jQuery(theTextInputField).val('');
	}

	jQuery("#ToolTip").text(jQuery(theTextInputField).attr("title"));
	
	var theLocation = jQuery(theTextInputField).parent().offset();
	var theHLoc;
	if (jQuery(theTextInputField).hasClass('top_of_page'))
		theHLoc = theLocation.left;
	else
		theHLoc = theLocation.left + jQuery(theTextInputField).width() - jQuery("#ToolTip").width();

	jQuery("#ToolTip").css("top", (theLocation.top - jQuery("#ToolTip").height() - 18) + "px");
	jQuery("#ToolTip").css("left", (theHLoc) + "px");
	jQuery("#ToolTip").show();
}

function ShowHint(theTextInputField, force)
{
	var theHint = jQuery(theTextInputField).attr("title");
	var theCurrentText = jQuery(theTextInputField).val();

	if ((theCurrentText == '') || (force))
	{
		jQuery(theTextInputField).addClass('form_field_hint');
		jQuery(theTextInputField).val(theHint);
	}
	else if (theCurrentText != theHint)
	{
		jQuery(theTextInputField).removeClass('form_field_hint');
	}

	jQuery("#ToolTip").hide();
}

function InitHints()
{
	jQuery(".apply_hint_js").live("focus", function(){
		ClearHint(this);
	});

	jQuery(".apply_hint_js").live("blur", function(){
		ShowHint(this, false);
	});

//	jQuery(".apply_hint_js").each(function (i) {
//		ShowHint(this);
//	});
}


// http://javascript.internet.com
//Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(theString){
	theString = theString.replace(/&(lt|gt);/g, function (strMatch, p1){
			return (p1 == "lt")? "<" : ">";
		});
	theString = theString.replace(/<\/?[^>]+(>|jQuery)/g, "");
	theString = theString.replace(/</, "");
	return theString;
}

// http://phpjs.org/functions/in_array:432
function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array
     var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}
function AdminMenuClicked(theAdminMenuItem)
{
//	var theHostname = location.host;
	var theLinkID = CurrentBLN_Item;
	var IDofLinkSubmitter = jQuery("#id_of_link_submitter_" + theLinkID).text();
	var Client_URL = WeAreShowingHotLinksUnderneathBlogPosts() ? jQuery("#Client_URL").text() : "";
	var theURL = Client_URL + "http://" + theHostname + "/client/admin_center/"+ theLinkID  + "/" + IDofLinkSubmitter + "/" + jQuery("#host_id").text();

	window.location= theURL;

//	var theAdminItem = jQuery(theAdminMenuItem).text();
//   jQuery.getJSON("http://" + theHostname + "/client/AdminItemClicked?jsoncallback=?", {theLinkID: theLinkID, theAdminItem: theAdminItem, 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="http://" + theHostname + "/login/" + jQuery("#host_id").text();
//	}
//	else
//	{
//	};
}

function KeepPopupMenuOnScreen()
{
	clearTimeout(MenuTimer);
}

function HidePopupMenu(theMenuID)
{
	if (jQuery(theMenuID).is(":visible"))
	{
		MenuTimer = setTimeout('jQuery("' + theMenuID + '").hide();', 450);
	}
}

function HandleClickOnBLN_ItemTitle(theTitle)
{
	var theLinkID = jQuery(theTitle).parent().parent().parent().attr('id');
	theLinkID = theLinkID.replace(/BLN_Item_/,"");
	var HostID = jQuery("#host_id").text();
	var Client_URL = WeAreShowingHotLinksUnderneathBlogPosts() ? jQuery("#Client_URL").text() : "";

	jQuery.getJSON("http://" + theHostname + "/amp/IncrementSourceArticleViews?jsoncallback=?", {theLinkID: theLinkID}, function(data){
	});

	return true;

//	var S = Client_URL + "http://" + theHostname + "/amp/comments/" + theLinkID + '/' + jQuery("#host_id").text();
//	window.location = S;
//
//	return false;
}

function StoreNameOfNewList(Name, Description)
{
	if (Name == "")
		return;

//	var theHostname = location.host;

	jQuery.getJSON("http://" + theHostname + "/amp/StoreNameOfNewList?jsoncallback=?", {Name: Name, Action: 'create', theListID: 0, Description: Description, an_ajax_call_is_active: 'true'}, function(data){

		var TestForTimeout = data['Timeout'];

		if (TestForTimeout == 'Timeout')
		{
			AjaxLogIn();
			return;
		}
		else
		{
			var result = data['result'];
			if (result == 'Success')
			{
				jQuery('#dialog_header').text("Success!");
				jQuery("#long_message").html('<p>Your new list, "' + Name + '," has been created.<\p>');
				jQuery("#curated_list_menu").hide();
				var theLastMenuItem = jQuery(".curated_list_item:last");
				if (jQuery(theLastMenuItem).text() == "What is a List?")
				{
					jQuery(theLastMenuItem).remove();
				}

				var theHTML =  '<div class="curated_list_item sortable">' +
							'<input class="cl_checkbox" type="checkbox" name="' + Name + '" value="' + Name + '">' +
							Name + '</div>';
				jQuery("#curated_list_menu").prepend(theHTML);

				jQuery(".sortable").tsort({order:"asc", place: "end"});
			}
			else
			{
				jQuery('#dialog_header').text("List already exists");
				jQuery("#long_message").html('<p>A list named, "' + Name + '," already exists in your profile.<\p>');

			}
			jQuery('#JQM_Dialog').jqm(
			{
				onHide: function(h) {
					h.o.remove(); // remove overlay
					h.w.fadeOut(444); // hide window
				}
			});

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

function EnforceCharacterLimits(theInputField, MaxLength)
{
	var theText = jQuery(theInputField).val();
	theText = removeHTMLTags(theText);

	var theLength = theText.length;

	if (theLength > MaxLength)
	{
		theText = theText.slice(0, MaxLength);
		jQuery(theInputField).val(theText);
		theLength = MaxLength;
	}

	var CharsRemaining = MaxLength - theLength;
	var theCharsRemainingField = "#" + jQuery(theInputField).attr('name') + '_CharsRemaining';
	jQuery("#" + jQuery(theInputField).attr('name') + '_CharsRemaining').text(CharsRemaining);
}

function GetNameOfList()
{
//	var theHostname = location.host;
	var theUserName = jQuery("#username").text();
	if (theUserName == "")
	{
		AjaxLogIn();
		return false;
	}

	var Name = "";
	var Description = "";
	jQuery('#dialog_header').text("Create a New List");
	jQuery("#long_message").html('' +
				'<div class="clear-all"></div>' +
				'<div class="jqmLabel">Name:</div> <div class="jqmCharsRemaining" id="Name_CharsRemaining"></div>' +
				'<div class="clear-all"></div>' +
				'<p><input name="Name" id="jqmListName" class="alphaNumericOnly" type="text" SIZE=50, MAXLENGTH=60></p>' +
				'<div class="jqmLabel">Description:</div>  <div class="jqmCharsRemaining" id="Description_CharsRemaining"></div>' +
				'<div class="clear-all"></div>' +
				'<p><textarea name="Description" id="jqmListDescription" ROWS=8, COLS=43></textarea></p>' +
				'<p><input type="submit" id="submit_list_name" name="Submit" value="Submit" /></p>'
	);

	jQuery("#jqmListDescription").keyup(
		function(event) {
			EnforceCharacterLimits(this, 350);
		}
	);

	jQuery("#jqmListName").keyup(
		function(event) {
			EnforceCharacterLimits(this, 60);
		}
	);

	jQuery("#submit_list_name").click(
		function(e){
			var Name = jQuery("#jqmListName").val();
			Name = removeHTMLTags(Name);

			var Description = jQuery("#jqmListDescription").val();
			Description = removeHTMLTags(Description);

			StoreNameOfNewList(Name, Description);

			jQuery('#JQM_Dialog').jqmHide();
		});

	jQuery('#JQM_Dialog').css("top", (jQuery(window).scrollTop() + 96) + "px");
	jQuery('#JQM_Dialog').css("left", ((jQuery(window).width() - 400)/2) + "px");

	jQuery('#JQM_Dialog').jqm(
	{
		onHide: function(h) {
			h.o.remove(); // remove overlay
			h.w.hide(); // hide window
			return false;
		}
	});

	jQuery('#JQM_Dialog').css("top", (jQuery(window).scrollTop() + 96) + "px");
	jQuery('#JQM_Dialog').jqmShow();
	jQuery('.alphaNumericOnly').alphanumeric({allow:" -"});

	return false;
}

function PerformAjaxLogin()
{
	var HostID = jQuery("#host_id").text();
	var username = jQuery("#login_username").val();
	var password = jQuery("#password").val();
	var IsChecked = jQuery("#remember_me").attr("checked");
	
	var remember_me = 'false';
	if (IsChecked)
		remember_me = 'remember_me';
	jQuery(".log_me_in").addClass("js_working");

	jQuery.getJSON("http://" + theHostname + "/PerformAjaxLogin?jsoncallback=?", {LoginMethod: 'WordPress', HostID: HostID, username: username, password: password, remember_me: remember_me, an_ajax_call_is_active: 'true'}, function(data){
		jQuery(".log_me_in").removeClass("js_working");
		var result = data['result'];
		if (result == 'Success')
		{
			jQuery('#dialog_header').text("Login Complete");
			jQuery("#long_message").html('<p>You are logged in to Hot Links.<\p>' + 
				'<p><input type="submit" class="jqmClose" name="Submit" value="Okay" /></p>'
			);
			
			jQuery(".apply_hint_js").attr('disabled', false);
			jQuery(".gwa_sn_submit_button").attr('disabled', false);
			jQuery("#gwa_sn_category_id_top_of_page").attr('disabled', false);

			jQuery(".log_me_in").replaceWith('<a href="#" class="log_me_out">Logout of Hot Links</a>' );
			jQuery("#username").text(username);

			jQuery('#JQM_Dialog').jqm(
			{
				onHide: function(h) {
					h.o.remove(); // remove overlay
					h.w.fadeOut(444); // hide window
				}
			});

			jQuery('#JQM_Dialog').css("top", (jQuery(window).scrollTop() + 96) + "px");
			jQuery('#JQM_Dialog').jqmShow();
		}
		else
		{
			RedirectUserToLoginPage('Welcome! Since this is your first time logging into Hot Links, we need to bring up the full login form.')
		}

	});
}

function AjaxLogIn()
{
	jQuery('#dialog_header').text("Log In");
	jQuery("#long_message").html('' +
		'  <h1>Login with WordPress Username</h1>' +'' +
		'  <label for="username">User name</label><br>' +'' +
		'  <input type="text" value="" name="username" id="login_username" class="header_login_form_field"><br>' +'' +
		'  <br>' +'' +
		'  <label for="password">Password</label><br>' +'' +
		'  <input type="password" value="" name="password" id="password" class="header_login_form_field"><br>' +'' +
		'  <br>' +'' +
		'  <p>Remember Me <input type="checkbox" id="remember_me" value="remember_me" name="remember_me"></p><input id="SubmitForAjaxLogIn" type="submit" value="Login">' +''
	);

	jQuery('#JQM_Dialog').css("top", (jQuery(window).scrollTop() + 96) + "px");
	jQuery('#JQM_Dialog').css("left", ((jQuery(window).width() - 400)/2) + "px");

	jQuery('#JQM_Dialog').jqm(
	{
		onHide: function(h) {
			h.o.remove(); // remove overlay
			h.w.hide(); // hide window
			return false;
		}
	});

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

	return false;
}



function WeAreShowingHotLinksUnderneathBlogPosts()
{
	return jQuery(".gwa_sn_blog_post_toggle_sn").length > 0;
}

function ShowListDefinition()
{
	jQuery('#dialog_header').text("What is a List?");
	jQuery("#long_message").html("<p></p><p>Add links to a list you create. Then share your list with friends.<\p>");
	jQuery('#JQM_Dialog').jqm(
	{
		onHide: function(h) {
			h.o.remove(); // remove overlay
			h.w.fadeOut(444); // hide window
		}
	});

	jQuery('#JQM_Dialog').css("top", (jQuery(window).scrollTop() + 96) + "px");
	jQuery('#JQM_Dialog').css("left", ((jQuery(window).width() - 400)/2) + "px");


	jQuery('#JQM_Dialog').jqmShow();

	return;
}

function AjaxLogout()
{
//	var theHostname = location.host;
	var HostID = jQuery("#host_id").text();

	jQuery(".log_me_out").addClass("js_working");

	jQuery.getJSON("http://" + theHostname + "/client/AjaxLogout?jsoncallback=?", {HostID: HostID}, function(data){
		jQuery(".log_me_out").removeClass("js_working");
		jQuery(".apply_hint_js").attr('disabled', true);
		jQuery(".gwa_sn_submit_button").attr('disabled', true);
		jQuery(".log_me_out").replaceWith('<a href="#" class="log_me_in">Login to Hot Links</a>' );
		jQuery("#username").text("");
	});
}

function RedirectUserToLoginPage(Message)
{
	if (Message !== '')
	{
		if (! confirm(Message))
			return;
	}

	if (WeAreShowingHotLinksUnderneathBlogPosts())
	{
		var CurrentURL = window.location;
		CurrentURL = CurrentURL.href;
//		var theHostname = location.host;

		jQuery.getJSON("http://" + theHostname + "/client/StoreCurrentURL?jsoncallback=?", {CurrentURL: CurrentURL}, function(data){
				var Client_URL = WeAreShowingHotLinksUnderneathBlogPosts() ? jQuery("#Client_URL").text() : "";
				var LoginURL = Client_URL + 'http://' + theHostname + '/login/WordPress/' + jQuery("#host_id").text();
				window.location = LoginURL;
		});
	}
	else
	{
		var URL = 'http://' + theHostname + '/login/' + jQuery("#host_id").text();
		window.location = URL;
	}
}


function CuratedListButtonClicked(theCheckBox)
{
	if (! jQuery(theCheckBox).hasClass("cl_checkbox"))
		theCheckBox = jQuery(theCheckBox).find(".cl_checkbox");
//	var theHostname = location.host;
	var theLinkID = CurrentBLN_Item;
	var theUserName = jQuery("#username").text();

	var theListName = jQuery(theCheckBox).parent().text();

	var WeAreOnLinkPreviewPage = jQuery("#link_preview_header").length > 0;

	if (theUserName == "")
	{
		AjaxLogIn();
		return;
	}

	//are we toggling on or off?
	var IsChecked = jQuery(theCheckBox).attr("checked");

	jQuery.getJSON("http://" + theHostname + "/amp/AddItemToCuratedList?jsoncallback=?", {theLinkID: theLinkID, theList: theListName, IsChecked: IsChecked, an_ajax_call_is_active: 'true'}, function(data){

		var TestForTimeout = data['Timeout'];

		if (TestForTimeout == 'Timeout')
		{
			AjaxLogIn();
			return;
		}
		else
		{
			var result = data['result'];
			if (result == 'Success')
			{
				//udpate contents of .curated_lists_that_include_this_link
				var theCheckBoxes = jQuery("#curated_list_menu").find('input');
				var Curated_Lists = new Array();
				var Counter = 0;
				jQuery(theCheckBoxes).each(function(i){
					if (jQuery(this).attr('checked'))
						{
							Curated_Lists[Counter] = jQuery(this).attr('name');
							Counter++;
						}
					}
				);
				Curated_Lists = JSON.encode(Curated_Lists);
				jQuery("#BLN_Item_" + CurrentBLN_Item).find(".curated_lists_that_include_this_link").text(Curated_Lists);
			}
		}

		var tempTimer = setTimeout('jQuery("#JQM_Dialog").hide();', 450);

	});
}


function VoteButtonClicked(theAmpButton, AmpOrMute)
{
	if (jQuery(theAmpButton).hasClass('preview'))
		return false;
		
	var theLinkID = jQuery(theAmpButton).attr('id');
	var theHostID = jQuery("#host_id").text();
	theHostID = theHostID.replace(/host_id_/, "");

	if (jQuery("#username").text() == "")
	{
		AjaxLogIn();
		return;
	}

	var theMuteItem = '';
	if (AmpOrMute == 'Mute')
	{
		theMuteItem = jQuery(theAmpButton).text();
		theLinkID = CurrentBLN_Item;
		theAmpButton = jQuery("#mute" + CurrentBLN_Item);
	}
	else
		theLinkID = theLinkID.replace(/amp_it_button/,"");

	   jQuery.getJSON("http://" + theHostname + "/amp/VoteButtonClicked?jsoncallback=?", {theLinkID: theLinkID, AmpOrMute: AmpOrMute, theMuteItem: theMuteItem, theHostID: theHostID, an_ajax_call_is_active: 'true'}, function(data){

		var TestForTimeout = data['Timeout'];

		if (TestForTimeout == 'Timeout')
		{
			AjaxLogIn();
			return;
		}
		else
		{
			var ThisUserHasAlreadyVotedForThisItem  = (data['ThisUserHasAlreadyVotedForThisItem'] == 'true');
			if (ThisUserHasAlreadyVotedForThisItem)
				alert("You have already voted on this item.");
			else
			{
				var theText = '';
				var theUpdatedScore = data['theUpdatedScore'];
				//fade out updated score
				var theAmpCount = jQuery("#amp_count" + theLinkID);
				if (AmpOrMute == 'Mute')
					jQuery("#mute_menu").hide();

				//fade out amp it text
				jQuery(theAmpCount).animate({
				        opacity: 0.0
				      }, 500,
				      function()
				      {
							//fade in new score
							jQuery(theAmpCount).text(theUpdatedScore);
							//fade in new button text
							jQuery(theAmpCount).animate({
									  opacity: 1.0
									}, 500);
				      }
				  );

				//ditto amp it button
				jQuery(theAmpButton).animate({
				        opacity: 0.0
				      }, 500,
				      function()
				      {
							if (jQuery(theAmpButton).hasClass("amp_it_button"))
							{
								//i.e. if it is the Amp Button and not the Mute Button
								jQuery(theAmpButton).removeClass('amp_it_button');
								jQuery(theAmpButton).addClass('amped_button');
							}
							//fade in new label
							(AmpOrMute == 'Amp') ? theText = 'Amped!' : theText = 'Muted!';
							jQuery(theAmpButton).text(theText);
							//fade in new button text
							jQuery(theAmpButton).animate({
									  opacity: 1.0
									}, 500);
				      }
				  );

				  var theLabel = jQuery(theAmpCount).next();
				  (theUpdatedScore == 0) ? theText = 'decibel' : theText = 'decibels';
				  if (jQuery(theLabel).text() == 'decibel')
				  {
				  	jQuery(theLabel).text(theText);
				  }


			}
		}
	});
}

function UpdateCheckBoxesInCuratedListMenu(Curated_Lists)
{
	var theCheckBoxes = jQuery("#curated_list_menu").find('input');
	jQuery(theCheckBoxes).attr("checked", false);
	if (Curated_Lists == "")
		return;
	Curated_Lists = JSON.decode(Curated_Lists);
	var NeedsToBeChecked = false;
	jQuery(theCheckBoxes).each(function(i){
		NeedsToBeChecked = in_array(jQuery(this).attr('name'), Curated_Lists);
		jQuery(this).attr("checked", NeedsToBeChecked);
		}
	);
}

function ShowPopUpMenu(theButton, theMenu)
{
	jQuery(theButton).addClass("info2button_hover");

	var theLocation = jQuery(theButton).position();
	var TopPositionForMenu = theLocation.top + 18;
	var WeAreOnLinkPreviewPage = jQuery("#link_preview_header").length > 0;

	if ((! WeAreOnLinkPreviewPage) && (! WeAreShowingHotLinksUnderneathBlogPosts()))
	{
		var MenuHeight =  jQuery(theMenu).outerHeight();
		var BottomOfWrap = jQuery("#wrap").position();
		BottomOfWrap = BottomOfWrap.top + jQuery("#wrap").height();
		if ((TopPositionForMenu + MenuHeight) > BottomOfWrap)
		{
			TopPositionForMenu = TopPositionForMenu - MenuHeight - jQuery(this).outerHeight() - 6;
		}
	}

	jQuery(theMenu).css("top", (TopPositionForMenu) + "px");
	jQuery(theMenu).css("left", theLocation.left + "px");
	jQuery(theMenu).fadeIn("fast");
}



/* http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(theString){
	theString = theString.replace(/&(lt|gt);/g, function (strMatch, p1){
			return (p1 == "lt")? "<" : ">";
		});
	theString = theString.replace(/<\/?[^>]+(>|jQuery)/g, "");
	theString = theString.replace(/</, "");
	return theString;
}

function ShowCharsRemaining(theInputObject)
{
	if (jQuery(theInputObject).length == 0)
		return;

	var theTitle = jQuery(theInputObject).attr("title");
	var theCharsID = theTitle + "-chars";
	var theText = jQuery(theInputObject).val();
	theText = removeHTMLTags(theText);
	var theLength = theText.length;
	var MaxLength = 0;
	var theBlogPostID;

	switch (theTitle)
	{
		case 'Link Title':
			theBlogPostID = jQuery(theInputObject).attr("id");
			theBlogPostID = theBlogPostID.replace(/gwa_sn_title_id_/, "");
			MaxLength = 128;
			break;
		case 'Description':
			theBlogPostID = jQuery(theInputObject).attr("id");
			theBlogPostID = theBlogPostID.replace(/gwa_sn_description_id_/, "");
			MaxLength = 350;
			break;
	}

	if (theLength > MaxLength)
	{
		theText = theText.slice(0, MaxLength);
		jQuery(theInputObject).val(theText);
		theLength = MaxLength;
	}

	var CharsRemaining = MaxLength - theLength;

	jQuery("#chars_remaining_" + theBlogPostID).html("&nbsp;&nbsp;Remaining: " + CharsRemaining);
}


//FUNCTIONS FOR HOT LINKS ON BLOG PAGES
function SetCookieThatShowsWhichPostsHaveHotLinksOpen()
{
	var HostID = jQuery(".gwa_sn_blog_post_toggle_sn").attr('rel');
	HostID = HostID.replace(/host_id_/,"");
	var S;
	var IDsOfPostsWithOpenHotLinks = jQuery.map( jQuery(".gwa_sn_blog_post_toggle_open"), function (theElement, i) {
			S = jQuery(theElement).attr('id');
			S = S.replace(/gwa_sn_post_id_/,"");
			return S + ',';
		});
	S = IDsOfPostsWithOpenHotLinks.toString()
	createCookie("GWA_HL_IDsOfPostsWithOpenHotLinks_" + HostID, S);
}

function UseCookieThatShowsWhichPostsHaveHotLinksOpen()
{
	if (jQuery(".gwa_sn_blog_post_toggle_sn").first().css('display') == 'none')
		return;
		
	var HostID = jQuery(".gwa_sn_blog_post_toggle_sn").attr('rel');
	HostID = HostID.replace(/host_id_/,"");
	var IDsOfPostsWithOpenHotLinks = readCookie("GWA_HL_IDsOfPostsWithOpenHotLinks_" + HostID);
	if (IDsOfPostsWithOpenHotLinks == null)
		return;
	var ArrayOFIDs = IDsOfPostsWithOpenHotLinks.split(",");
	for (key in ArrayOFIDs) {
			var theBlogPostID = ArrayOFIDs[key];
			if (theBlogPostID !== '')
			{
				var IDofElement = '.gwa_sn_post_id_' + theBlogPostID;
				ShowHotLinks(jQuery("#sn_post_id_" + theBlogPostID), true);
			}
		}
}

function SetCookieThatShowsWhetherHotLinksAtTopOfPageAreOpen(ToggleIsOpen)
{
	var HostID = jQuery(".gwa_sn_blog_post_toggle_sn").attr('rel');
	HostID = HostID.replace(/host_id_/,"");
	var StringFormOfToggleIsOpen = ToggleIsOpen ? 'true' : 'false';
	createCookie("GWA_HL_HotLinksAtTopOfPage_" + HostID, StringFormOfToggleIsOpen);
}

function UseCookieThatShowsWhetherHotLinksAtTopOfPageAreOpen()
{
	var HostID = jQuery(".gwa_sn_blog_post_toggle_sn").attr('rel');
	HostID = HostID.replace(/host_id_/,"");
	var StringFormOfToggleIsOpen = readCookie("GWA_HL_HotLinksAtTopOfPage_" + HostID);
	if (StringFormOfToggleIsOpen == null)
		return;
	var ToggleIsOpen = StringFormOfToggleIsOpen == 'true';
	if (ToggleIsOpen)
		ShowHotLinks(jQuery("#link_for_gwa_hl_top_of_page_discloser"), false);
}

function AddHotLinkCountToThisText(theText, theCount)
{
	var Result = '';
	
	var PositionOfOpenParen = theText.indexOf('(');
	if (PositionOfOpenParen > 0)
	{
		theText = theText.substr(0,PositionOfOpenParen - 1);
	}

	Result = theText + " (" + theCount + ")";
	return Result;
}

function ShowNumberOfHotLinks()
{
	if (!WeAreShowingHotLinksUnderneathBlogPosts())
		return;

	var theLinks = jQuery(".gwa_sn_blog_post_toggle_sn");
	var theHostID = jQuery(theLinks).first().attr("rel");
	theHostID = theHostID.replace(/host_id_/,"");
	var theBlogPostIDs = new Array();
	var Counter = 0;

	jQuery(theLinks).each(function(i){
			var blog_post_id = jQuery(this).attr("id");
			blog_post_id = blog_post_id.replace(/sn_post_id_/,"");
			theBlogPostIDs[Counter] = blog_post_id;
			Counter++;
		}
	);

	var serializedBlogPostIDs = theBlogPostIDs.join();
//	var theHostname = location.host;


	jQuery.getJSON("http://" + theHostname + "/client/ShowNumberOfHotLinks?jsoncallback=?", {serializedBlogPostIDs: serializedBlogPostIDs, theHostID: theHostID}, function(data){

		var Past24HoursCount = data['Past24HoursCount'];
		var BlogPostIDsWithSNCounts = data['BlogPostIDsWithSNCounts'];
		var ArrayLength =BlogPostIDsWithSNCounts.length;
		var theBlogPostID;
		var theSNCount;
		var theText;

		UpdateCountOfHotLinksAtTopOfPage(Past24HoursCount);
		
		for ( var i=0, len=ArrayLength; i<len; ++i ){
			theBlogPostID = BlogPostIDsWithSNCounts[i]['theBlogPostID'];
			theSNCount = BlogPostIDsWithSNCounts[i]['SNCount'];
			theText = jQuery("#sn_post_id_" + theBlogPostID).text();
			theText = AddHotLinkCountToThisText(theText, theSNCount);
			jQuery("#sn_post_id_" + theBlogPostID).text(theText);
		}

	});
}

function RefreshHotLinksAtTopOfPage(theButton)
{
	var theButtonName = jQuery(theButton).val();
	var theSort = jQuery("#gwa_top_of_page_sort_dropdown").val();
	var theCategory = jQuery("#gwa_top_of_page_category_dropdown").val();
	var thePage = jQuery("#gwa_top_of_page_pager_dropdown").val();
	
	var theHostID = jQuery("#host_id").text();
	theHostID = theHostID.replace(/host_id_/, "");

	jQuery("#link_for_gwa_hl_top_of_page_discloser").addClass("js_working");

	jQuery.getJSON("http://" + theHostname + "/client/RefreshHotLinksAtTopOfPage?jsoncallback=?", {theHostID: theHostID, theSort: theSort, thePage: thePage, theCategory: theCategory, theButtonName: theButtonName}, function(data){
		jQuery("#link_for_gwa_hl_top_of_page_discloser").removeClass("js_working");

		var Past24HoursCount = data['Past24HoursCount'];
		UpdateCountOfHotLinksAtTopOfPage(Past24HoursCount);

		var theHTML = data['html'];
		jQuery("#gwa_links_at_top_of_page_wrapper").replaceWith(theHTML);
	});
}

function UpdateCountOfHotLinksAtTopOfPage(Past24HoursCount)
{
	var theText = jQuery("#link_for_gwa_hl_top_of_page_discloser").text();
	theText = AddHotLinkCountToThisText(theText, Past24HoursCount);
	jQuery("#link_for_gwa_hl_top_of_page_discloser").text(theText);
}

function ExtractDomainNameFromURL(theURL)
{
	if ((typeof(theURL) == "undefined") || (theURL == "") || (theURL.match(/:\/\/(.[^/]+)/) == null))
	{
		return "";
	}
	
	var theDomain = theURL.match(/:\/\/(.[^/]+)/)[1];
	theDomain = theDomain.replace(/www./, "");	
	
	return theDomain;
}

function GetInputForHotLink(theButton)
{
	if (jQuery(theButton).attr('id') == 'gwa_hl_form_submit_at_top_of_page')
	{
		var theTitle = jQuery("#gwa_sn_title_id_top_of_page").val();
		var theURL = jQuery("#gwa_sn_url_id_top_of_page").val();
		var theDescription = jQuery("#gwa_sn_description_id_top_of_page").val();
		var theCategory = jQuery("#gwa_sn_category_id_top_of_page").val();
	}
	else
	{
		var theBlogPostID = jQuery(theButton).parent().attr('id');
		theBlogPostID = theBlogPostID.replace(/Get_New_SN_Post_/,"");
		var theTitle = jQuery("#gwa_sn_title_id_" + theBlogPostID).val();
		var theURL = jQuery("#gwa_sn_url_id_" + theBlogPostID).val();
		var theDescription = jQuery("#gwa_sn_description_id_" + theBlogPostID).val();
		var theCategory = jQuery("#gwa_sn_category_id_" + theBlogPostID).val();
	}
	
	var ErrorMsg = '';
	if (theURL.length > MAXLENGTHFORURL)
		ErrorMsg = 'The URL is too long. The maximum length is ' + MAXLENGTHFORURL + '.';
	else
	if (theTitle == 'Link Title')
		ErrorMsg = 'Please enter a title.';
	else
	if (theURL == 'Link URL')
		ErrorMsg = 'Please enter a URL.';
	else
	if (theDescription == 'Description')
		ErrorMsg = 'Please enter a description.';
	else
	if (theCategory == 0)
		ErrorMsg = 'Please select a category.';
	if (ErrorMsg !== '')
	{
		alert(ErrorMsg);
		return false;
	}
	
	return theTitle + '|-|' + theURL + '|-|' + theDescription + '|-|' + theCategory;
}


function PreviewHotLink(theButton)
{
	var theButtonName = jQuery(theButton).val();
	if (theButtonName != 'Submit')
	{
		if (jQuery(theButton).attr('id') == 'gwa_hl_form_submit_at_top_of_page')
			HandleFormSubmitButtonsAtTopOfPage(theButton);
		else
			HandleFormSubmitButtonsUnderBlogPosts(theButton);
		return false;
	}
	
	var S = GetInputForHotLink(theButton);
	if (S == false)
		return;
		
	var theInput = S.split('|-|');
	var theTitle = theInput[0];
	var theURL = theInput[1];
	var theDescription = theInput[2];
	var theCategory = theInput[3];
	
	jQuery('#preview_title').text(theTitle);
	jQuery('#preview_title').attr('title', theURL);
	jQuery('#preview_description').text(theDescription);
	jQuery('#preview_domain').html(ExtractDomainNameFromURL(theURL) + ' &mdash; ');
	jQuery('#preview_submitted_by').html('Submitted by <a href="#">' + jQuery('#username').text() + '.</a>');

	jQuery('#JQM_Preview').jqm(
		{
			onHide: function(h) {
				h.o.remove(); // remove overlay
				h.w.hide(); // hide window
				return false;
			}
		});

	jQuery('#Preview_Confirm_Button').unbind(); //unbind all previous click events
	jQuery('#Preview_Confirm_Button').bind('click', {ButtonUsedByPreview: theButton}, function(event) {
		jQuery('#JQM_Preview').jqmHide();
		if (jQuery(event.data.ButtonUsedByPreview).attr('id') == 'gwa_hl_form_submit_at_top_of_page')
			HandleFormSubmitButtonsAtTopOfPage(event.data.ButtonUsedByPreview);
		else
			HandleFormSubmitButtonsUnderBlogPosts(event.data.ButtonUsedByPreview);
		return false;
	});
	
	jQuery('#JQM_Preview').css("top", (jQuery(window).scrollTop() + 96) + "px");
	jQuery('#JQM_Preview').jqmShow();
	jQuery('.white_button').blur();
}

function HandleFormSubmitButtonsAtTopOfPage(theButton)
{
	var theButtonName = jQuery(theButton).val();
	var theBlogPostID = 0;
	var theElement = jQuery(".gwa_sn_blog_post_toggle_sn").first();
	//if we're on a blog page, attach this new link to the most recent post
	if (jQuery(theElement).length > 0)
	{
		theBlogPostID = theElement.attr('id');
		theBlogPostID = theBlogPostID.replace(/sn_post_id_/,"");
	}

	var id_of_most_recent_link = 0;
	var theHostID = jQuery("#host_id").text();

	switch (theButtonName)
	{
		case 'Submit':
			var S = GetInputForHotLink(theButton);
			if (S == false)
				return;
				
			var theInput = S.split('|-|');
			var theTitle = theInput[0];
			var theURL = theInput[1];
			var theDescription = theInput[2];
			var theCategory = theInput[3];
			break;
	}

	jQuery(".log_me_out").addClass("js_working");

	jQuery.getJSON("http://" + theHostname + "/HandleFormSubmitButtonsUnderBlogPosts?jsoncallback=?", {title: theTitle, url: theURL, description: theDescription, category_name: theCategory, theBlogPostID: theBlogPostID, theHostID: theHostID, id_of_most_recent_link: id_of_most_recent_link, theButtonName: theButtonName}, function(data){
		jQuery(".log_me_out").removeClass("js_working");

		var Success = data['success'];
		if (Success == 'false')
		{
			alert(data['ErrorMsg']);
			return;
		}

		RefreshHotLinksAtTopOfPage(jQuery("#refresh_links_at_top_of_page"));
		jQuery("#show_submit_form_at_top_of_page").val('Submit a Link');
	});
}

function HandleFormSubmitButtonsUnderBlogPosts(theButton)
{
	var theButtonName = jQuery(theButton).val();
	var theBlogPostID = jQuery(theButton).parent().attr('id');
	theBlogPostID = theBlogPostID.replace(/Get_New_SN_Post_/,"");
	var id_of_most_recent_link = jQuery("#gwa_sn_post_id_" + theBlogPostID).find(".id_of_most_recent_link").text();
	var theHostID = jQuery("#host_id").text();

	switch (theButtonName)
	{
		case 'Hide':
			var LinkThatIsClickedToDiscloseHotNews = jQuery("#sn_post_id_" + theBlogPostID);
			var CurrentContents = jQuery(LinkThatIsClickedToDiscloseHotNews).text();
			CurrentContents = CurrentContents.replace(/▼/,"►");
			jQuery("#gwa_sn_post_id_" + theBlogPostID).slideUp("fast");
			jQuery(LinkThatIsClickedToDiscloseHotNews).text(CurrentContents);
			return;
			break;
		case 'Submit':
			var S = GetInputForHotLink(theButton);
			if (S == false)
				return;
				
			var theInput = S.split('|-|');
			var theTitle = theInput[0];
			var theURL = theInput[1];
			var theDescription = theInput[2];
			var theCategory = theInput[3];
			break;
		case 'Refresh':
			break;
	}

	jQuery(".log_me_out").addClass("js_working");

	jQuery.getJSON("http://" + theHostname + "/HandleFormSubmitButtonsUnderBlogPosts?jsoncallback=?", {title: theTitle, url: theURL, description: theDescription, category_name: theCategory, theBlogPostID: theBlogPostID, theHostID: theHostID, id_of_most_recent_link: id_of_most_recent_link, theButtonName: theButtonName}, function(data){
		jQuery(".log_me_out").removeClass("js_working");

		var Success = data['success'];
		if (Success == 'false')
		{
			alert(data['ErrorMsg']);
			return;
		}

		var theSNCount = data['TotalLinks'];
		id_of_most_recent_link = data['id_of_most_recent_link'];
		var theText = jQuery("#sn_post_id_" + theBlogPostID).text();
		theText = AddHotLinkCountToThisText(theText, theSNCount);
		jQuery("#sn_post_id_" + theBlogPostID).text(theText);

		var UpdateInfo = data['UpdateInfo'];
		var ArrayLength =UpdateInfo.length;
		var theLinkID;
		var time_since_posted;
		var theWhenPostedElement;
		for ( var i=0, len=ArrayLength; i<len; ++i ){
			theLinkID = UpdateInfo[i]['link_id'];
			time_since_posted = 'Posted ' + UpdateInfo[i]['time_since_posted'] + ' ago.';
			theWhenPostedElement = jQuery("#gwa_sn_post_id_" + theBlogPostID).find("#BLN_Item_" + theLinkID).find(".when_posted");
			jQuery(theWhenPostedElement).text(time_since_posted);
		}

		jQuery("#gwa_sn_post_id_" + theBlogPostID).find(".id_of_most_recent_link").text(id_of_most_recent_link);

		var Past24HoursCount = data['Past24HoursCount'];
		UpdateCountOfHotLinksAtTopOfPage(Past24HoursCount);
		
		var theHTML = data['html'];
		var theFormElement = "#Get_New_SN_Post_" + theBlogPostID;
		jQuery(theFormElement).before(theHTML);
		jQuery(".apply_hint_js").each(function (i) {
			ShowHint(this, true);
		});
	});
}

function LoadInfoDivsDelayedObserver()
{
	//can't load the info divs until after the css has loaded
	var CSShasBeenLoaded = jQuery(".test_to_see_if_css_has_loaded").css('position') == 'fixed';
	if (! CSShasBeenLoaded)
	{
		var timer = setTimeout("LoadInfoDivsDelayedObserver();", 125);
		return;
	}
	
	if (jQuery("#gwa_hl_info").length != 0)
		return;
	
	if (InfoDivsHaveBeenCalled)
		return;
		
	GetInfoDivs();

	InfoDivsHaveBeenCalled = true;
}

// delayed observer code by VR */
function CSSLoadDelayedObserver(blog_post_id, host_id, IDofLinkThatIsClickedToDiscloseHotNews, StringFormOfThisLinkIsForHotLinksUnderABlogPost)
{
	var ThisLinkIsForHotLinksUnderABlogPost = (StringFormOfThisLinkIsForHotLinksUnderABlogPost == 'true') ? true : false;
	var CSShasBeenLoaded = jQuery(".test_to_see_if_css_has_loaded").css('position') == 'fixed';
	if (CSShasBeenLoaded)
	{
		GetHotLinks(blog_post_id, host_id, jQuery("#" + IDofLinkThatIsClickedToDiscloseHotNews), ThisLinkIsForHotLinksUnderABlogPost);
		clearTimeout(jQuery("#" + IDofLinkThatIsClickedToDiscloseHotNews).data('timer'));
		return;
	}

	var theDate = new Date();
	var theTime = theDate.valueOf();

	var theDelay = 250;
	var timeOfLastKeyPress = jQuery("#" + IDofLinkThatIsClickedToDiscloseHotNews).data('timeOfLastKeyPress');
	var theDifference = theTime - timeOfLastKeyPress;

	if ((theDifference > theDelay) && (CSShasBeenLoaded))
	{
		GetHotLinks(blog_post_id, host_id, jQuery("#" + IDofLinkThatIsClickedToDiscloseHotNews), ThisLinkIsForHotLinksUnderABlogPost);
	}
	else
	{
		jQuery("#" + IDofLinkThatIsClickedToDiscloseHotNews).data('timeOfLastKeyPress', theTime);
		clearTimeout(jQuery("#" + IDofLinkThatIsClickedToDiscloseHotNews).data('timer'));
		var theJS = "CSSLoadDelayedObserver(" + blog_post_id + ", " + host_id + ", '" + IDofLinkThatIsClickedToDiscloseHotNews + "', '" + StringFormOfThisLinkIsForHotLinksUnderABlogPost + "');"
		var timer = setTimeout(theJS, theDelay);
		jQuery("#" + IDofLinkThatIsClickedToDiscloseHotNews).data('timer', timer);
	}
}

function GetHotLinks(theBlogPostID, theHostID, LinkThatIsClickedToDiscloseHotNews, ThisLinkIsForHotLinksUnderABlogPost)
{
//	var theHostname = location.host;

	jQuery(LinkThatIsClickedToDiscloseHotNews).addClass("js_working");
	var id_of_most_recent_link = 0;

	jQuery.getJSON("http://" + theHostname + "/client/GetHotLinks?jsoncallback=?", {theBlogPostID: theBlogPostID, theHostID: theHostID, id_of_most_recent_link: id_of_most_recent_link, ThisLinkIsForHotLinksUnderABlogPost: ThisLinkIsForHotLinksUnderABlogPost}, function(data){

		jQuery(LinkThatIsClickedToDiscloseHotNews).removeClass("js_working");

		var theHTML = data['html'];
		var theElementWithHotLinksHTML = ThisLinkIsForHotLinksUnderABlogPost ? "#gwa_sn_post_id_" + theBlogPostID : "#gwa_hl_top_of_page";

		jQuery(theElementWithHotLinksHTML).html(theHTML);

		jQuery(".apply_hint_js").each(function (i) {
			ShowHint(this, false);
		});

		jQuery(theElementWithHotLinksHTML).slideDown("fast");

		var ClasstoAddToShowToggleIsOpen = ThisLinkIsForHotLinksUnderABlogPost ? "gwa_sn_blog_post_toggle_open" : "gwa_sn_top_of_page_toggle_open";
		jQuery(theElementWithHotLinksHTML).addClass(ClasstoAddToShowToggleIsOpen);

		if (ThisLinkIsForHotLinksUnderABlogPost)
			SetCookieThatShowsWhichPostsHaveHotLinksOpen();
		else
			SetCookieThatShowsWhetherHotLinksAtTopOfPageAreOpen(true);
	});
}


function ShowHotLinks(LinkThatIsClickedToDiscloseHotNews, ThisLinkIsForHotLinksUnderABlogPost)
{
	if (jQuery(LinkThatIsClickedToDiscloseHotNews).length == 0)
		return;
	var host_id = jQuery(".gwa_sn_blog_post_toggle_sn").attr("rel");
	if (host_id == null)
		return;
	host_id = host_id.replace(/host_id_/,"");

	if (jQuery(".gwa_sn_blog_post_toggle_sn").first().css('display') == 'none')
		return;

	var blog_post_id = '0';
	if (ThisLinkIsForHotLinksUnderABlogPost)
	{
		blog_post_id = jQuery(LinkThatIsClickedToDiscloseHotNews).attr("id");
		blog_post_id = blog_post_id.replace(/sn_post_id_/,"");
		var theElementWithHotLinksHTML = jQuery("#gwa_sn_post_id_" + blog_post_id);
	}
	else
	{
		var theElementWithHotLinksHTML = jQuery("#gwa_hl_top_of_page");
	}

	var CurrentContents = jQuery(LinkThatIsClickedToDiscloseHotNews).text();

	var isVisible = jQuery(theElementWithHotLinksHTML).is(':visible');

	//have to do this now, because calling it marks theElementWithHotLinksHTML as being visible
	//bring in style sheet now that user is loading Hot Links
	if (jQuery("#gwa_sn_css").hasClass("needs_update"))
	{
		jQuery("#gwa_sn_css").attr("href", "http://" + theHostname + "/dynamic_css/external_site_stylesheet/" + host_id);
		isVisible = false;
		jQuery("#gwa_sn_css").removeClass("needs_update");
	}

	if (isVisible)
	{
		jQuery(theElementWithHotLinksHTML).slideUp("fast");
		CurrentContents = CurrentContents.replace(/▼ Hide/,"► Show");
		CurrentContents = CurrentContents.replace(/▼/,"►");
		jQuery(theElementWithHotLinksHTML).removeClass("gwa_sn_blog_post_toggle_open");
		if (ThisLinkIsForHotLinksUnderABlogPost)
			SetCookieThatShowsWhichPostsHaveHotLinksOpen();
		else
			SetCookieThatShowsWhetherHotLinksAtTopOfPageAreOpen(false);
	}
	else
	{
		CurrentContents = CurrentContents.replace(/► Show/,"▼ Hide");
		CurrentContents = CurrentContents.replace(/►/,"▼");
		//make sure the css has finished loading
		var StringFormOfThisLinkIsForHotLinksUnderABlogPost = ThisLinkIsForHotLinksUnderABlogPost ? 'true' : 'false';
		LoadInfoDivsDelayedObserver();
		CSSLoadDelayedObserver(blog_post_id, host_id, jQuery(LinkThatIsClickedToDiscloseHotNews).attr('id'), StringFormOfThisLinkIsForHotLinksUnderABlogPost);
	}

	jQuery(LinkThatIsClickedToDiscloseHotNews).text(CurrentContents);
}

function GetInfoDivs()
{
//	var theHostname = location.host;
	var HostID = jQuery(".gwa_sn_blog_post_toggle_sn").attr("rel");
	HostID = HostID.replace(/host_id_/,"");

	jQuery.getJSON("http://" + theHostname + "/client/GetInfoDivs?jsoncallback=?", {HostID: HostID}, function(data){
		var theHTML = data['theHTML'];
		jQuery("#link_for_gwa_hl_top_of_page_discloser").before(theHTML);

	});
}



jQuery(document).ready(function()
{
	if (WeAreShowingHotLinksUnderneathBlogPosts())
	{
		//framebuster in case we are loaded inside a GWA SN iframe
		//This frame buster code follows the approach of the Frame Buster plugin for WordPress by Denis de Bernardy
//			Plugin Name: Frame Buster
//			Plugin URI: http://www.semiologic.com/software/frame-buster/
//			Description: Thwarts any attempt to load your site in a frame.
//			Version: 5.0
//			Author: Denis de Bernardy
//			Author URI: http://www.getsemiologic.com

		try {
			var parent_location = new String(parent.location);
			var top_location = new String(top.location);
			var cur_location = new String(document.location);
			parent_location = parent_location.toLowerCase();
			top_location = top_location.toLowerCase();
			cur_location = cur_location.toLowerCase();

			if ( top_location != cur_location && parent_location.indexOf(location.host) != 0 )
				top.location.href = document.location.href;
		} catch ( err ) {
			top.location.href = document.location.href;
		}

		ShowNumberOfHotLinks();
		UseCookieThatShowsWhetherHotLinksAtTopOfPageAreOpen();
		var WeAreOnAPageWithASingleBlogPost = jQuery(".gwa_sn_blog_post_toggle_sn").length == 1;
		var WeAreOnAPageWithGWAiFrame = jQuery("#SocialNews").length != 0;
		if (WeAreOnAPageWithGWAiFrame)
			jQuery(".gwa_sn_blog_post_toggle_sn").hide();

		if ((WeAreOnAPageWithASingleBlogPost) && (!WeAreOnAPageWithGWAiFrame))
			ShowHotLinks(jQuery(".gwa_sn_blog_post_toggle_sn").first(), true);
		else
			UseCookieThatShowsWhichPostsHaveHotLinksOpen();
		InitHints();
	}

	jQuery("#link_for_gwa_hl_top_of_page_discloser").click(
		function(){
				ShowHotLinks(this, false);
				return false;
			}
	);

	jQuery(".gwa_sn_blog_post_toggle_sn").click(
		function(){
				ShowHotLinks(this, true);
				return false;
			}
	);


	jQuery(".amp_it_button").live("mouseover", function(){
			jQuery(this).addClass("amp_it_button_highlighted");
	});

	jQuery(".amp_it_button").live("mouseout", function(){
			jQuery(this).removeClass("amp_it_button_highlighted");
	});

	jQuery(".amp_it_button").live("click", function(){
			VoteButtonClicked(this, 'Amp')
	});



	jQuery(".gwa_hl_form_button").live("click", function(){
			if (jQuery(this).attr('id') == 'refresh_links_at_top_of_page')
				RefreshHotLinksAtTopOfPage(this)
			else
// 				if (jQuery(this).attr('id') == 'gwa_hl_form_submit_at_top_of_page')
// 					HandleFormSubmitButtonsAtTopOfPage(this);
// 				else
// 					HandleFormSubmitButtonsUnderBlogPosts(this);
				PreviewHotLink(this);
			return false;
	});

	jQuery("#show_submit_form_at_top_of_page").live("click", function(){
			var ButtonName;
			if (jQuery("#Get_New_SN_Post_top_of_page").is(":visible"))
				{
					(jQuery("#Get_New_SN_Post_top_of_page").slideUp("fast"));
					ButtonName = "Submit a Link";
				}
			else
				{
					(jQuery("#Get_New_SN_Post_top_of_page").slideDown("fast"));
					//popup should already be enabled if user is logged in - but reports
					//are that in some cases it is not
					jQuery("#gwa_sn_category_id_top_of_page").attr('disabled', false);
					jQuery(".apply_hint_js").each(function (i) {
						ShowHint(this, true);
					});
					ButtonName = "Hide Entry Form";
				}
			jQuery("#show_submit_form_at_top_of_page").val(ButtonName);
		}
	);


	jQuery(".mute").live("mouseover", function(){
			jQuery(".popUpMenu").hide();
			jQuery("#at15s").hide();  //add this popup
			CurrentBLN_Item = jQuery(this).attr('id');
			CurrentBLN_Item = CurrentBLN_Item.replace(/mute/,"");
			ShowPopUpMenu(this, jQuery("#mute_menu"));
	});

	jQuery(".mute").live("mouseout", function(){
			jQuery(this).removeClass("info2button_hover");
			HidePopupMenu("#mute_menu");
	});

	jQuery(".mute").live("click", function(){
			VoteButtonClicked(this, 'Mute')
	});




	jQuery(".mute_menu_item").live("mouseover", function(){
			jQuery(this).css("font-weight", "bold");
	});

	jQuery(".mute_menu_item").live("mouseout", function(){
			jQuery(this).css("font-weight", "normal");
	});

	jQuery(".mute_menu_item").live("click", function(){
			VoteButtonClicked(this, 'Mute')
	});



	jQuery(".admin").live("mouseover", function(){
			jQuery(".popUpMenu").hide();
			jQuery("#at15s").hide();  //add this popup
			CurrentBLN_Item = jQuery(this).attr('id');
			CurrentBLN_Item = CurrentBLN_Item.replace(/admin/,"");
			ShowPopUpMenu(this, jQuery("#admin_menu"));
	});

	jQuery(".admin").live("mouseout", function(){
			jQuery(this).removeClass("info2button_hover");
			HidePopupMenu("#admin_menu");
	});



	jQuery(".admin_menu_item").live("mouseover", function(){
			jQuery(this).css("font-weight", "bold");
	});

	jQuery(".admin_menu_item").live("mouseout", function(){
			jQuery(this).css("font-weight", "normal");
	});

	jQuery(".admin_menu_item").live("click", function(){
			AdminMenuClicked(this)
	});



	jQuery(".curated_list").live("mouseover", function(){
			jQuery(".popUpMenu").hide();
			jQuery("#at15s").hide();  //add this popup
			var BLN_Item = jQuery(this).attr('id');
			BLN_Item = BLN_Item.replace(/curated_list/,"");

			CurrentBLN_Item = BLN_Item;
			var Curated_Lists = jQuery(this).parent().find(".curated_lists_that_include_this_link").text();
			UpdateCheckBoxesInCuratedListMenu(Curated_Lists);
			ShowPopUpMenu(this, jQuery("#curated_list_menu"));
	});

	jQuery(".curated_list").live("mouseout", function(){
			jQuery(this).removeClass("info2button_hover");
			HidePopupMenu("#curated_list_menu");
	});



	jQuery(".popUpMenu").live("mouseover", function(){
			KeepPopupMenuOnScreen();
	});

	jQuery(".popUpMenu").live("mouseout", function(){
			HidePopupMenu("#" + jQuery(this).attr("id"));
	});



	jQuery("#new_curated_list").live("click", function(){
			jQuery("#curated_list_menu").hide();
			GetNameOfList();
			return false;
	});

	jQuery("#what_is_a_list").live("click", function(){
			jQuery("#curated_list_menu").hide();
			ShowListDefinition();
			return false;
	});




	jQuery(".curated_list_item").live("mouseover", function(){
			jQuery(this).css("font-weight", "bold");
	});

	jQuery(".curated_list_item").live("mouseout", function(){
			jQuery(this).css("font-weight", "normal");
	});

	jQuery(".curated_list_item").live("click", function(){
			CuratedListButtonClicked(this);
	});


	jQuery("a.Title").live("mouseover", function(e){
		ShowDescription(this, e.pageX, e.pageY);
	});

	jQuery("a.Title").live("mouseout", function(){
			jQuery("#ToolTip").hide();
	});

	jQuery("a.Title").live("click", function(){
			return HandleClickOnBLN_ItemTitle(this);
	});


	jQuery("#SubmitForAjaxLogIn").live("click", function(){
			PerformAjaxLogin();
			jQuery('#JQM_Dialog').jqmHide();
	});

	jQuery(".log_me_in").live("click", function(){
			AjaxLogIn("");
			return false;
	});

	jQuery(".log_me_out").live("click", function(){
			AjaxLogout();
			return false;
	});

	jQuery(".update-preview").live("keyup", function(){
			ShowCharsRemaining(this);
			return true;
	});


	jQuery(".gwa_top_of_page_dropdown").live("change", function(){
			RefreshHotLinksAtTopOfPage();
			return true;
	});




	jQuery(window).scroll(function()
	{
		//http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=303
		if (jQuery("#JQM_Dialog").is(":visible"))
	  		jQuery('#JQM_Dialog').animate({top:(jQuery(window).scrollTop()+96)+"px"},{queue: false, duration: 350});
	  		
		if (jQuery("#JQM_Preview").is(":visible"))
	  		jQuery('#JQM_Preview').animate({top:(jQuery(window).scrollTop()+96)+"px"},{queue: false, duration: 350});	  		
	});



});  