(
function()
{
	// E|R|A related items interface link.
	var ERA_INTERFACE_LINK = "/EN/interface/getrelateditems.aspx";
	// Default E|R|A client domain.
	var DEFAULT_DOMAIN = "channelweb.firstlightera.com";
	
	/*
		Gets url of the E|R|A related items interface.
	*/
	function GetEraDomain(current_window) 
	{
		if (current_window.era_rc != null && current_window.era_rc["ERADomain"] != null) 
		{
				return "http://" + (current_window.era_rc["ERADomain"]) + ERA_INTERFACE_LINK;;

		} 
		
		return "http://" + DEFAULT_DOMAIN + ERA_INTERFACE_LINK;
	}
	
	/*
		Gets height from the era_rc array. 
	*/
	function GetEraBlockHeight(current_window) 
	{
		if (current_window.era_rc != null && current_window.era_rc["Height"] != null) 
		{
				return GetStringWithQuotes(current_window.era_rc["Height"]);
		} 
		
		return GetStringWithQuotes(500);
	}
	
	/*
		Gets width from the era_rc array. 
	*/
	function GetEraBlockWidth(current_window) 
	{
		if (current_window.era_rc != null && current_window.era_rc["Width"] != null) 
		{
				return GetStringWithQuotes(current_window.era_rc["Width"]);
		} 
		
		return GetStringWithQuotes(500);
	}
	
	/*
		Gets height from the era_rc array. 
	*/
	function GetEraMaxItems(current_window) 
	{
		if (current_window.era_rc != null && current_window.era_rc["MaxRelatedItems"]  != null) 
		{
				return current_window.era_rc["MaxRelatedItems"];
		} 
		
		return 10;
	}
	
	/*	
		Gets sort type from the era_rc array. 
	*/
	function GetEraSortType(current_window) 
	{
		if (current_window.era_rc != null && current_window.era_rc["SortBy"] != null) 
		{
				return current_window.era_rc["SortBy"];
		} 
		
		return 'Rank';
	}
	
	/*	
		Gets content type from the era_rc array. 
	*/
	function GetContentType(current_window) 
	{
		if (current_window.era_rc != null && current_window.era_rc["ContentType"] != null) 
		{
				return current_window.era_rc["ContentType"];
		} 
		
		return 'SSMicrosites';
	}
	
	/*
		Parses the URL to retrieve the content id.
	*/
	function GetChannelWebContentIdFromUrl() 
	{	
		var contentIdPattern = new RegExp('[0-9]+$', 'i');
		var matchResult = contentIdPattern.exec(StandardizeUrl(location.href));
		if (matchResult != null)
		{
			return matchResult[0];
		} 
		return '';
	}
	
	/*
		Checks the url and returns the appropriate left hand side section of the url.
	*/
	function StandardizeUrl(url)
	{
		if (url.indexOf(';') > 0)
		{
			return url.substring(0, url.indexOf(';'));
		}
		else if (url.indexOf('?') > 0)
		{
			return url.substring(0, url.indexOf('?'));
		}	
		else
		{
			return url;
		}
	}
	
	/*	
		Gets the content id from the era_rc array if supplied else
		calls the GetChannelWebContentIdFromUrl method to retrieve the content id 
		from the URL.
	*/
	function GetContentId(current_window) 
	{
		if (current_window.era_rc["ContentId"] != null) 
		{
			return (current_window.era_rc["ContentId"]);
		} 
		else 
		{
			//Get the content id from the URL.
			return GetChannelWebContentIdFromUrl();
		}
	}
		
	/* 
			Prints the related links IFrame to the document. 
	*/
	function DisplayEraFrame(eraLink, current_document, current_window)
	{
		current_document.write('<iframe name="era_relatedLinks" width='+GetEraBlockWidth(current_window)+" height="+GetEraBlockHeight(current_window)+' frameborder=0 src='+ eraLink +' marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no">');
		current_document.write("</iframe>");
	}

	/* 
			Adds double quotes to the provided string. 
	*/
	function GetStringWithQuotes(checkString)
	{
		return checkString!=null ?'"'+ checkString + '"':'""'
	}
	
	/* 
		 Generates the related links request link using the 
		 link parameters. 
	*/
	function CreateEraLink(current_window, current_document)
	{
		var eraLink= GetEraDomain(current_window) + "?";
		if (current_window.era_rc != null) 
		{
			eraLink += ("ContentId=" + (GetContentId(current_window)));
			eraLink += ("&");
			eraLink += ("numrequests=1");
			eraLink += ("&");
			eraLink += ("req1=" + GetContentType(current_window) + "||");
			eraLink += (GetEraMaxItems(current_window)+ "|");
			eraLink += ("SortBy:" + GetEraSortType(current_window));
			eraLink += ("&");
			eraLink += ("OutputType=html");
	
			DisplayEraFrame(eraLink, current_document, current_window);
			current_window.era_rc = null;
		}
	}
	
	/* 
		 Calls functions to print the related links frame. 
	*/
	function EraMain()
	{
		var current_window = window;
		var current_document = document;
		
		CreateEraLink(current_window, current_document);
	}
	
	EraMain();

})()


