/*
* 
* cleverLinks.js
* 
* Author: Tony Knibb
* Date: 12/09/2007
* 
* Description:
* 
* 	Well, basically it just does some nice CSS trickery on <a href> for
* 	external links.
* 
* 	Adds some extra link text to inform the user that the link opens 
* 	externally or that it is a download and gives instructional info. on
* 	how to deal with a download link.
* 
* 	If this code doesn't load then links won't open in new windows!
* 
* Usage:
* 
* 	Add 'rel="external"' or 'rel="download"' to <a href> tags.
* 
* 	Also, best to add 'title=""', although if you don't the code will
* 	extract the link text and use that (max length 20 char).
* 
****************************************************************************/

/* * C O D E * B E G I N S * B E L O W * */

/*
* Add additional link functionality
***************************************/
function cleverLinks_AddFunctionality()
{
	// drop out if code is unsupported
	if (!document.getElementsByTagName) return;
	
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	/* PRINT PAGE BUTTON...
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	var addPrintButton = document.getElementById("addPrintButton");
	
	if (addPrintButton)
	{
		addPrintButton.innerHTML = '<img src="../../images/icons/printpage.gif" id="printbutton" />';
		var printbutton = document.getElementById("printbutton");
		printbutton.onmouseup = function () { window.print(); }	
	}

	/* Basic anchor tags...
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	var links = document.getElementsByTagName("a");

	for (var i = 0, link; link = links[i]; i++)
	{
		if (cleverLinks_isExternalLink(link))
		{
			// if no title exists and it isn't an anchor link, add proper title
			if ((link.title == '' || !link.title) && !link.name) link.title = "External link: "+link.innerHTML.substring(0,20);

			link.title += " (Opens in new window)";
			if (cleverLinks_isIE()) link.alt = "External link: " + link.alt + " (Opens in new window)";

			if (!cleverLinks_isBaxterLink(link))
			{
				// duplicate link to fake attribute and clear real href
				link.fakeHref = link.href;
				link.href = "#";

				// pass to popup message onMouseUp
				link.onmouseup = function () { cleverLinks_extLinkMessage(this.fakeHref); };
			} else
			{
				link.target = "_blank";
			}
		}

		if (cleverLinks_isDownloadLink(link))
		{
			// if no title exists and it isn't an anchor link, add proper title
			if ((link.title == '' || !link.title) && !link.name) link.title = "Download: "+link.innerHTML.substring(0,20);

			if (cleverLinks_isIE())
			{
				link.title += " (Right-click and select 'Save Target as...')";
				link.alt = "Download: " + link.alt+  " (Right-click and select 'Save Target as...')";
			} else
			{
				link.title += " (Right-click and select 'Save Link as...')";
			}
		}
	}

	/* Image Map area tags... (currently only run on home page)
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	var areas = document.getElementsByTagName("area");

	for (var i = 0, area; area = areas[i]; i++)
	{
		if (cleverLinks_isExternalLinkArea(area))
		{
			area.target = "_blank";
			area.title += " (Opens in new window)";
			if (cleverLinks_isIE()) area.alt = "External link: " + area.alt + " (Opens in new window)";
		}
	}

	/* UL tags... (currently only run on Travel Guide page)
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	var uls = document.getElementsByTagName("ul");

	for (var i = 0, ul; ul = uls[i]; i++)
	{
		// find if is a search list...
		var isSearchList = ( ul.id.indexOf("searchList") > -1 )? true : false;
		if (isSearchList)
		{
			var lis = ul.getElementsByTagName("li");
			for (var j = 0, li; li = lis[j]; j++)
			{
				li.className = "filter_hideMe";
			}
		}
	}

	if (isSearchList)
	{
		// add input area and instructions
		var searchWriteArea = document.getElementById("searchWriteArea");
		searchWriteArea.innerHTML = 
		'<label id="searchMessage" class="filter_hideMe" for="searchPDFs">Type a <strong>country name</strong> to filter the downloads, or type <strong>all</strong> to view all downloads.</label>' + 
		'<input type="text" id="searchPDFs" class="filter_hideMe" name="searchPDFs" value="Enter country name here..." />';

		// capture search filter parts
		var searchBox = document.getElementById("searchBox");
		var searchMessage = document.getElementById("searchMessage");
		var searchInput = document.getElementById("searchPDFs");

		// change style of search filter area
		searchBox.className = "showPretty";
		searchMessage.className = searchInput.className = "filter_showMe";

		// start the search filter system
		searchInput.startText = searchInput.value;
		searchInput.onfocus = function ()
		{
			if (this.value == this.startText)
			{
				this.oldText = this.value;
				this.value = "";
			}
		}
		searchInput.onblur = function () { if (this.value == "") this.value = this.startText; }
		searchInput.onkeyup = function () { searchTheList(this.value, lis); }
	}
}

/* * F U N C T I O N * M E T H O D S * B E L O W * */

/*
* Test for IE (any version)
***************************************/
function cleverLinks_isIE()
{
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

/*
* Test for external link
***************************************/
function cleverLinks_isExternalLink(link)
{
	return (link.getAttribute("href") && (link.getAttribute("rel") == "external" || link.getAttribute("rel") == "baxter") )? true : false;
}

/*
* Test for Baxter owned external link
***************************************/
function cleverLinks_isBaxterLink(link)
{
	return (link.getAttribute("rel") == "baxter")? true : false;
}

/*
* Test for external link (Image Map)
***************************************/
function cleverLinks_isExternalLinkArea(link)
{
	return (link.getAttribute("href") && link.className == "external")? true : false;
}

/*
* Test for download link
***************************************/
function cleverLinks_isDownloadLink(link)
{
	return (link.getAttribute("href") && link.getAttribute("rel") == "download")? true : false;
}

/*
* Show popup message for external link
***************************************/
function cleverLinks_extLinkMessage(extLink)
{
	var m = "";

	m += "This link is provided for your convenience. However, it leads to an external web site not created, ";
	m += "managed, or maintained by Baxter. Baxter is not responsible for, and does not necessarily endorse, ";
	m += "the opinions or products represented on these external pages.\r\n\n ";

	m += "Baxter Healthcare does not assume responsibility for, support or otherwise endorse any information ";
	m += "not included by us within our site or located as a result of a link to content not on our site. These "; 
	m += "links are provided as a resource for ease in finding additional information on haemophilia and on ";
	m += "travel related topics. Such links should not be construed by their inclusion in our site as being ";
	m += "supported or endorsed by Baxter Healthcare.\r\n\n";

	m += "For complete legal and copyright information click on the appropriate links at the bottom of the page.\r\n\n";

	m += "Do you wish to leave this site?";
	
	if(confirm(m)) window.open(extLink);
}

/*
* Search a download link and show matches
*******************************************/
function searchTheList(val, LIs)
{
	var val = val.toUpperCase().replace(/^\s+|\s+$/g,""); // make uppercase, and trim spaces
	var valLen = val.length;

	if (valLen > 0)
	{
		if (val == "ALL")
		{
			// show them all...
			for (var i = 0, li; li = LIs[i]; i++) { li.className = "filter_showMe"; }
		} else
		{
			for (var i = 0, li; li = LIs[i]; i++)
			{
				var theLinks = li.getElementsByTagName("a");
				for (var j = 0, thisLink; thisLink = theLinks[j]; j++)
				{
					// show matching items...
					var thisText = thisLink.innerHTML.substring(0, valLen).toUpperCase();
					if (thisText.indexOf(val) > -1)
						li.className = "filter_showMe";
					else
						li.className = "filter_hideMe";
				}
			}
		}
	} else
	{
		// hide them all...
		for (var i = 0, li; li = LIs[i]; i++) { li.className = "filter_hideMe"; }
	}
}

/* * I N I T I A T E * O N * W I N D O W * L O A D * */

window.onload = cleverLinks_AddFunctionality;
