/*****TIMEINC Utilities*****/

/* TIMEINC namespace initializer */
if (typeof TIMEINC != 'object' || TIMEINC == null)
{
	window.TIMEINC = {};
}
		
/* Stops the default action of an event, such as following a link or submitting a form */
TIMEINC.stopDefaultAction = function (event)
{
	event.returnValue = false;
	if (typeof event.preventDefault != 'undefined')
	{
		event.preventDefault ();
	}
};

/* Gets the parameter value from the current page's url, stripping the anchor reference if it exists
   Sample url: http://somehost.com?state=NY&zip=10020#goToThisAnchor
   Default usage:
   TIMEINC.getUrlParameter ('zip') returns '10020'
   Alternate usage:
   TIMEINC.getUrlParameter ('zip', 'state=NJ&zip=07306#anchor') returns '07306' */
TIMEINC.getUrlParameter = function (key, alternateQueryString)
{
	key = key.replace (/[\[]/, '\\\[').replace (/[\]]/, '\\\]');
	var regex = new RegExp ('[\\?&]' + key + '=([^&#]*)');
	var result = regex.exec (typeof alternateQueryString == 'string' && alternateQueryString.length > 0 ? alternateQueryString : location.href);
	return result == null ? '' : result [1];
};

/* Replaces all hexadecimal HTML entities, such as '&#x60;', with their single character versions, e.g., '`'
   This method is currently used to retrieve John Neilson's RSS Fetch (reader) hexadecimally-encrypted JSON text correctly */
TIMEINC.hexToChar = function (text)
{
	return text.replace (/&#.*?;/g, function (matchedInstance)
	{
		return String.fromCharCode (parseInt (matchedInstance.replace (/&#x/, '').replace (/;/, ''), 16));
	});
};

/* Make the current page the homepage automatically if the browser is Internet Explorer; otherwise, the link should point to a page that gives instructions on how to do this task manually for other browsers
   Parameters:
   jQueryAnchorSelector (required): jQuery selector with which to retrieve the "Make this my homepage" anchor (<a>)
   homepageOrAlternateUrl (optional): If this parameter is specified, the homepage will be set to its value (e.g., 'http://www.timeinc.com') */
TIMEINC.makeThisMyHomepage = function (jQueryAnchorSelector, homepageOrAlternateUrl)
{
	jQuery (document).ready (function ()
	{
		var performAction = function (event)
		{
			var anchor = jqAnchor.get (0);
			
			/* Omniture tracking for all browsers - will fail silently if Omniture is not enabled */
			try { s_gi (s_account).tl (anchor, 'o', 'MakeThisMyHomePage: Clicks'); }
			catch (error) {}
			
			/* If the browser is Internet Explorer */
			if (/MSIE/.test (navigator.userAgent))
			{
				anchor.style.behavior = 'url(#default#homepage)';
				anchor.sethomepage (typeof homepageOrAlternateUrl == 'string' && homepageOrAlternateUrl.length > 0 ? homepageOrAlternateUrl : location.href);
				TIMEINC.stopDefaultAction (event);
			}
		};
		var jqAnchor = jQuery (jQueryAnchorSelector);
		jqAnchor.click (performAction);
	});
}