/*
    This function writes a div on the documets and then populates it
    with the content it receives as argument. It is used for the Java Script
    Callback method
*/
var ERA_RC = window.ERA_RC||{};
ERA_RC.resource = function()
{

    var $={};
    return {
        init:function(elementID){
            document.write("<div id='ERA_RC'></div>");
            $.placeHolder=document.getElementById(elementID);
        },getContent:function(content){
            $.placeHolder.innerHTML=content.value;
        }
    };
}();
ERA_RC.resource.init("ERA_RC");

(
function()
{
	// E|R|A related items interface link.
	var ERA_INTERFACE_LINK = "/ERALinks/Default.aspx";
	// Default E|R|A client domain.
	var DEFAULT_DOMAIN = "roadtransport.firstlightera.com";
	// CSS link - If you don't want this link to be created, set DEFAULT_CSS_URL = ""
	var DEFAULT_CSS_URL = "Css/HtmlRelatedLinks.css";


	/*
	    This function writes the CSS link to the document. It is primarily
	    used for related links JavaScript output. If you don't want this link
	    to be created, set DEFAULT_CSS_URL = ""
	*/
	function WriteCSS(current_window, current_document)
	{
	        if (DEFAULT_CSS_URL != "")
	        {
			    current_document.write("<link href='" + DEFAULT_CSS_URL + "' type='text/css' rel='stylesheet' />");
			}
	}

	/*
		This function can be modified to incorporate custom content ID retrieval.
	*/
	function GetCustomContentID()
		{
			// URL of the current page.
			var reedCurrentUrl = location.href;

			// Regex pattern to match against.
			var reedPattern = new RegExp('/Articles/.+/([0-9]{6})', 'i');
			var matchResult = reedPattern.exec(reedCurrentUrl);
			if (matchResult != null)
			{
				return matchResult[1];
			}
			else
			{
				return '';
			}
	}

    /*
		Gets UseIFrame status from era_rc
	*/
	function GetUseIFrame(current_window)
	{
		if (current_window.era_rc != null && current_window.era_rc["UseIFrame"]  != null)
		{
				return current_window.era_rc["UseIFrame"];
		}

		return "yes";
	}

	/*
		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';
	}



	/*
		Gets the content id from the era_rc array if supplied else
		calls the GetCustomContentID 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 GetCustomContentID();
	    }
	}

	/*
			Gets the stylesheet from the era_rc array if supplied
		*/
		function GetStylesheet(current_window)
		{
			if (current_window.era_rc["OverrideCss"] != null)
			{
				return (current_window.era_rc["OverrideCss"]);
			}
	}

		/*
			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';
	}

	/*
		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>");
	}

	/*
		Prints the related links JavaScript to the document.
	*/
	function DisplayEraJavaScript(eraLink, current_document, current_window)
	{
	    current_document.write("<script type='text/javascript' src="+ eraLink +"></script>");
	}

	/*
			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));

			if (IsTestMode(current_window)) {
				eraLink += ("&Test=" + IsTestMode(current_window));
			}

			if (current_window.era_rc["StyleId"] != null) {
				eraLink += ("&StyleId=" + current_window.era_rc["StyleId"]);
			}

			if (GetStylesheet(current_window) != null)
			{
				eraLink += ("&OverrideCss=" + GetStylesheet(current_window));
			}

	        // if UseIFrame property is set to no, write javascript, else use iframe
	        if (GetUseIFrame(current_window).toLowerCase() == "no")
	        {
	            eraLink += ("&");
			    eraLink += ("OutputType=javascript");
			    DisplayEraJavaScript(eraLink, current_document, current_window);
		    }
		    else
		    {
                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();

})()

