/*
Name: Translate.js

Background:
This script is intended to be used to overcome a limitation in the Google translation process. The process takes a page url parameter, performs a machine translation
on the page, then returns the output to the browser, effectively serving the host site content from the Google domain. During this process, all links in the translated 
page are converted so they they point to the google translation service with the original destination as a parameter. In this manner, the navigation will continue to 
function on a site while allowing each page to be translated as requested. 

A problem arises, however when navigation is accomplished via javascript. In this case, the urls may be relative and after translation if one of these urls is followed, a 404 
will be generated because that page does not exist relative  to the Google translation service. This script solves that problem by examining the current page to
determine if it has been translated by Google. If it has been translated and the intended destination is relative, the script will prepend the destination with the appropriate
Google address and pass the destination as a fully qualified domain, thereby creating the same effect as the link conversion performed by Google.

This script is specifically designed to be used with the ComponentArt Menu navigation. To use this script, follow these steps:

	1) Change the value of the domain variable in the script below
	2) Place this script on the site and set a script tag src attribute to it's location
	3) Add the following element to the ComponentArt Menu control:

		<ClientEvents>
		<ItemSelect EventHandler="Translate" />
		</ClientEvents>

*/

	var domain = "http://covingtonpolice.preview.vc3.com";
	var baseUrl = "http://" + domain;




function checkTrans()
{ 
	if (location.href.indexOf("&u")==-1)
	{
		parent.location="http://google.com/translate?langpair=en%7C"+document.getElementById('selector').value+"&u="+location.href; 
	}
	else
	{
		str_array=location.href.split("&u="); str_array[1];
		parent.location="http://google.com/translate?langpair=en%7C"+document.getElementById('selector').value+"&u="+str_array[1]; 
	} 
} 







	// Translate is the entry point into the process that determines the google translation link
	function Translate(sender, eventArgs)
	{
		url = eventArgs.get_item().get_navigateUrl();

		if (document.domain != domain && document.domain != "www."+domain)
			HandleTranslation(url);
		else
			window.location = url;
	}

	//HandleTranslation decides if the parent page has been translated and if it has,
	//sets the location of the page to the translation service, passing in the url as a parameter
	function HandleTranslation(url)
	{ 
		if (url.indexOf(baseUrl) < 0 && url.indexOf("http:") < 0)
		{
			url = baseUrl + url;
		}
		else
		{
			if (url.indexOf("http:") >=0)
			{
				parent.location = url;
				return;
			}
		}                                              

		var currentUrl = window.location.toString();

		request = currentUrl.split("?");
		
		if (request[1] != null)
		{
			querystring = request[1].split("&");
			
			if (querystring.length > 0)
			{
				for (var index = 0; index < querystring.length; index++)
				{
					lang = querystring[index].split("=");
					
					if (lang[0] == "langpair")
					{
						parent.location = "http://google.com/translate?langpair="+ lang[1] +"&u="+url; 
						break;
					}
				}
			}
			else
			{
				parent.location ="http://google.com/translate?langpair=en%7C"+document.getElementById('selector').value+"&u="+url; 
			} 
		}
		else
		{
			parent.location ="http://google.com/translate?langpair=en%7C"+document.getElementById('selector').value+"&u="+url; 
		} 
	} 
