﻿var TE_locale;
var TE_selectedLang;
var langIds = new Array();
	langIds[0] = 1;
	langIds[1] = 2;
	langIds[2] = 3;
	langIds[3] = 4;
	langIds[4] = 5;
	langIds[5] = 6;
	langIds[6] = 8;
	langIds[7] = 9;
	langIds[8] = 10;
	langIds[9] = 14;
	langIds[10] = 15;
	langIds[11] = 16;
	langIds[12] = 17;

function TE_initLanguageSelection() {
	TE_selectedLang = dojo.cookie("language_id");
	
	// if language cookie doesn't exist, check whether language_id cookie 
	//		can be written and read
	if ( ! TE_selectedLang) {
		TE_setLanguageCookie(1);
  		TE_selectedLang = dojo.cookie("language_id");
		// cookies are not enabled or domain is not tycoelectronics.com, 
		//	so don't show language selector
		if ( ! TE_selectedLang || TE_selectedLang != 1) {
			return;
		}
 	}
	
	var isLangIdValid = false;	

	if (TE_selectedLang) {
		for (i = 0; i < langIds.length; i++) {
			if (langIds[i] == TE_selectedLang) {
				isLangIdValid = true;
				break;
			}
		}
	}
	
	if ( ! isLangIdValid) {
		TE_setLanguage(1);
		TE_selectedLang = 1;
	}
	
	TE_locale  = TE_getLocale();	// set global TE_locale based on language_id cookie
	
	var te_lang_span = dojo.query("#te-lang-sel-nav > span")[0];
	if (te_lang_span)
	{
		var parentSelNode;
		var selNode = dojo.query("#langId" + TE_selectedLang + " > a")[0];
		if (selNode) {
			te_lang_span.appendChild(document.createTextNode(selNode.firstChild.nodeValue));
		}
	}
	
	dojo.addOnLoad(function() {
		dojo.require("dijit.Dialog");
		
		var parentSelNode;
		
		dojo.requireLocalization("te", "ewt", TE_locale);
		var ewt = dojo.i18n.getLocalization("te", "ewt", TE_locale);
		
		var selNode = dojo.query("#langId" + TE_selectedLang + " > a")[0];
		if (selNode) {
			parentSelNode = selNode.parentNode;
			dojo.addClass(parentSelNode, "te-langSelected");
			parentSelNode.innerHTML = selNode.firstChild.nodeValue;
		}
		
		dojo.byId("teLangSelect").appendChild(document.createTextNode("(" + ewt.commonChange + ")"));
		
		TE_showLangSelectorText();
		
		dojo.connect(dojo.byId("teLangSelect"), "onclick", function() {			
			if (!dijit.byId("teLangSelN"))
			{
				var langSel = new dijit.Dialog({
					//title: "Choose Your Language or Country",
					title: ewt.commonChooseLang,
					style: "padding-bottom: 10px",
					id: "teLangSelN",
					autofocus:false
				}, dojo.byId("te-langsel-node"));
				
				langSel.show();	
			}
			else
			{
				dijit.byId("teLangSelN").show();
			}
		});
	});

}

function TE_setLanguageCookie(langId){
	TE_SetCookie("language_id",langId,TE_GetCookieExpiration(365),".tycoelectronics.com","/");
}

function TE_setLanguage(langId){
	TE_setLanguageCookie(langId);
	TE_locale  = TE_getLocale();	// update global TE_locale
	
	var sURL = window.location;
	sURL = TE_languageReWriteURL(sURL,langId);
	window.location.replace(sURL);
}

// Function to rewrite the URL and add or replace a
// LG parameter with the new language id
function TE_languageReWriteURL(url,id) {
	var re,str,newurl;
	// default set the return url to be the incomming url
	newurl=url;
	// create a RegExp object to handle the functionality
	// initial pattern looking for an existing LG parameter, either first or somewhere
	// on the line.  Pattern ignores case and is global (finds all occurences).
	re = new RegExp("([\?\&]LG=)([1-9][0-9]?)","ig");
	// check the URL to see if it matches
	if (re.test(url)) {
		// found a match, then we have a parameter already in place
		// lets replace it.
		// create a String object
		str = new String(url);
		// Ask the string object to do the replace for us giving it the regular expression
		// the replacement string is to return the &LG= or ?LG= ($1) followed by our new language id
		newurl = str.replace(re,"$1"+id);
	} else { // we didn't match, so no LG parameter on the URL
		var checkParam;
		// look to see if anchor fragment on the url - remove for now
		checkParam = new RegExp("([\#].*$)","ig");
		if (checkParam.test(url)) {
		    // Yes, there is a # present, so remove
			str = new String(url);
			newurl = str.replace(checkParam,"");
			// put this back in the url so we can contine to check for other parameters
			url = newurl;
		}
		
		// check if this is a catalog url with the iso language code embedded in the uri path
		var ree = new RegExp("(\/catalog\/.*\/)(en|cs|de|es|fr|hu|it|ja|ko|pl|pt|zh-hans|ru)", "ig");
		if (ree.test(url)) {
			// update the iso language code embedded in the uri path
			var stree = new String(url);
			newurl = stree.replace(ree, "$1" + TE_locale);
		} else {
			// look to see if other parameters on the string, respect them...
			checkParam = new RegExp("([\?])","ig");
			if (checkParam.test(url)) {
				newurl = url + "&LG="+id;
			} else {
				// No there wasn't a ? found on the URL so append ?LG=N
				newurl = url + "?LG="+id;
			}
		} 
	} // end of if didn't find LG paramenter
	
	// return the transformed url
	return newurl;
}

function TE_showLangSelectorText() {
	var languagesNode = dojo.byId("TE_langSelector_langHdg");
	var countrySitesNode = dojo.byId("TE_langSelector_countryHdg");
	var languageTitle = dojo.query(".te-langSelectorTitle")[0];

	dojo.requireLocalization("te", "ewt", TE_locale);
	var ewt = dojo.i18n.getLocalization("te", "ewt", TE_locale);

	languagesNode.appendChild(document.createTextNode(ewt.languagesMessage));
	countrySitesNode.appendChild(document.createTextNode(ewt.countrySitesMessage));
	languageTitle.appendChild(document.createTextNode(ewt.commonChooseLang));
}
