﻿(
function()
{
	var ERA_INTERFACE_LINK = "/ERALinks/default.aspx";
	var DEFAULT_DOMAIN = "variety.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';
	}
	
	/*
		Checks to see if the request is to be in test mode. (Randomly generated links)
	*/
	function IsTestMode(current_window)
	{
	  if (current_window.era_rc != null && current_window.era_rc["Test"] != null) 
	  {
	    return current_window.era_rc["Test"];
	  } 
	  return 'false';
	}
	
	/*
		Parses the URL to retrieve the content id.
	*/
	function GetContentIdFromUrl() 
	{
		// URL of the current page.
		var reedCurrentUrl = location.href;
		
		// Regex pattern to match against.
		var reedPattern = new RegExp('(?:/article/|articleid=)(VR[0-9]+)', 'i');
		var matchResult = reedPattern.exec(reedCurrentUrl);
		if (matchResult != null)
		{
			return matchResult[1];
		}
		else
		{
			return '';
		}
	}
	
	/*	
		Gets the content id from the era_rc array if supplied else
		calls the GetReedContentIdFromUrl 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 if (IsTestMode(current_window) == 'true')
	    {
	        return '0';
	    }
	    else 
	    {
	        return GetContentIdFromUrl();
	    }
	}
		
	/* 
		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 += ("Test=" + IsTestMode(current_window));
			eraLink += ("&");
			eraLink += ("OutputType=html");
			
			if (current_window.era_rc["StyleId"] != null) {
				eraLink += ("&StyleId=" + current_window.era_rc["StyleId"]);
			}
	
			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();

})()

