(
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 = "ddimagazine.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 GetVnuContentIdFromUrl() 
	{
		// VNU content id parameter (must include '=')
		var vnuContentIdParam = 'vnu_content_id=';
		// VNU content id parameter value to be returned.
		var vnuContentIdParamValue = 0;
		// URL of the current page.
		var vnuCurrentUrl = location.href;
		// Index of the content id parameter.
		var vnuContentIdParamValueIndex = 0;
		
		if (vnuCurrentUrl.indexOf(vnuContentIdParam) > 0) 
		{
			vnuContentIdParamValueIndex = vnuCurrentUrl.indexOf(vnuContentIdParam) + vnuContentIdParam.length;
			if (vnuContentIdParamValueIndex > 0) 
			{
				if (vnuCurrentUrl.substring(vnuContentIdParamValueIndex) > 0)
				{
					vnuContentIdParamValue = vnuCurrentUrl.substring(vnuContentIdParamValueIndex);
				}
			}
		}
		return vnuContentIdParamValue; 
	}
	
	/*	
		Gets the content id from the era_rc array if supplied else
		calls the GetVnuContentId 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 GetVnuContentIdFromUrl();
		}
	}
		
	/* 
			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();

})()

