var	langSelectorArea;
var	langCollapseIcon1;
var	langCollapseIcon2;
var	langCollapseNode;
var	wipeTimer = 750;
var	expandLangDelay = 350;
var	savedHeight;
var expandLangDelayTimer;


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() {
	var pageUrl = new String(window.location);
	
	// if not tycoelectronics.com domain, don't show language selector
	var domainRE = new RegExp("://.*\.tycoelectronics\.com", "ig");
	if ( ! domainRE.test(pageUrl)) {
		
		if ( dojo.byId("TE_corpnav_header") ) {
			dojo.html.setStyle("TE_corpnav_header","padding-right","0px");
			return;
		}
		else {
			return;
		}
		
	}
		
	langCollapseNode = dojo.byId("TE_langSelector_langContent");
	langSelectorArea = dojo.byId("TE_langSelector_bottom");
	langExpandIcon = dojo.byId("TE_langSelector_expandLink");
	langCollapseIcon1 = dojo.byId("TE_langSelector_collapseLink");
	langCollapseIcon2 = dojo.byId("TE_langSelector_langContentHeader");
		
	dojo.event.connect(langSelectorArea, "onmouseover", "TE_delayedExpandLanguages");
	dojo.event.connect(langSelectorArea, "onmouseout", "TE_cancelDelayedExpandLanguages");

	var isLangIdValid = false;	
	var selectedLang = dojo.io.cookie.getCookie("language_id");
	if (selectedLang) {
		for (i = 0; i < langIds.length; i++) {
			if (langIds[i] == selectedLang) {
				isLangIdValid = true;
			}
		}
	}
	
	if ( ! isLangIdValid) {
		TE_setLanguage(1);
	}
	
	TE_showLangSelectorText();
	dojo.html.show(langSelectorArea);
	
	for (i = 0; i < langIds.length; i++) {
		if (langIds[i] != selectedLang) {
			langNode = dojo.byId("langId" + langIds[i]);
			var subLangNode = document.createElement("a");
			subLangNode.setAttribute("href", "javascript:TE_setLanguage(" + langIds[i] + ")");
			subLangNode.appendChild(langNode.firstChild);
			langNode.appendChild(subLangNode);
		} 
	}
		
	savedHeight = langCollapseNode.style.height;
}

function TE_cancelDelayedExpandLanguages() {
	clearTimeout(expandLangDelayTimer);
	expandLangDelayTimer = null;
}
	
function TE_delayedExpandLanguages() {
	expandLangDelayTimer = setTimeout(TE_expandLanguages, expandLangDelay);
}
	
function TE_expandLanguages() {
	TE_cancelDelayedExpandLanguages();
	
	dojo.event.disconnect(langSelectorArea, "onmouseover", "TE_delayedExpandLanguages");
	
	dojo.html.hide(langExpandIcon);
	dojo.html.show(langCollapseIcon1);
	closeTE_Inquira_searchselector("TE_Inquira_searchselector");
		
	dojo.lfx.toggle.wipe.show(langCollapseNode, wipeTimer, null, 
				function() {		
					langCollapseNode.style.height = savedHeight;
					dojo.event.connect(langCollapseIcon1, "onclick", "TE_collapseLanguages");
					dojo.event.connect(langCollapseIcon2, "onclick", "TE_collapseLanguages");
			});	
}
	
function TE_collapseLanguages() {
	dojo.event.disconnect(langCollapseIcon1, "onclick", "TE_collapseLanguages");
	dojo.event.disconnect(langCollapseIcon2, "onclick", "TE_collapseLanguages");
	
	dojo.html.show(langExpandIcon);
	dojo.html.hide(langCollapseIcon1);
		
	dojo.lfx.toggle.wipe.hide(langCollapseNode, wipeTimer, null,
				function() {		
					langCollapseNode.style.height = savedHeight;
					dojo.event.connect(langSelectorArea, "onmouseover", "TE_delayedExpandLanguages");
			});
}

function TE_showLangSelectorText() {
		var languagesNode = dojo.byId("TE_langSelector_languageHeading");
		var countrySitesNode = dojo.byId("TE_langSelector_countrySiteHeading");
		var worldwideNode = dojo.byId("TE_langSelector_langHeader");
		var expandLangSelImage = dojo.byId("TE_langSelector_expandImg");
		var collapseLangSelImage = dojo.byId("TE_langSelector_collapseImg");
		var closeLangSelImage = dojo.byId("TE_langSelector_closeImg");
	
		var locale = TE_getLocale();
		
		dojo.requireLocalization("te", "ewt", locale);
		var ewt = dojo.i18n.getLocalization("te", "ewt", locale);

		languagesNode.appendChild(document.createTextNode(ewt.languagesMessage));
		countrySitesNode.appendChild(document.createTextNode(ewt.countrySitesMessage));
		worldwideNode.appendChild(document.createTextNode(ewt.worldwideMessage));
		expandLangSelImage.alt = ewt.expandLangSelMessage;
		expandLangSelImage.title = ewt.expandLangSelMessage;
		collapseLangSelImage.alt = ewt.closeLangSelMessage;
		collapseLangSelImage.title = ewt.closeLangSelMessage;
		closeLangSelImage.alt = ewt.closeLangSelMessage;
		closeLangSelImage.title = ewt.closeLangSelMessage;
}

// Function for making a DIV visible with a warning message
// designed to alert the user that not all content is translated 
	
function TE_showLanguageDisclaimer(langId){
   if(langId == "1" ) {
	   
		TE_setLanguage(langId);

   }
   else {
		dojo.byId("TE_changeLanguageDiv").style.display = "inline";
		//xGetElementById("TE_changeLanguageDiv").style.height = "40";
		//xGetElementById("TE_changeLanguageDiv").style.width = "100%";

		dojo.byId("TE_changeLanguageFormId").languageurl.value = 'language.asp?ID=' + langId;
		dojo.byId("TE_changeLanguageFormId").action = 'javascript:TE_setLanguage('+ langId+');';
		dojo.byId("TE_language_info").innerHTML = TE_getLanguageNoticeText(langId);
		dojo.byId("TE_blankChangeLanguageDivButton").value = TE_getLanguageContinue(langId);
   }
	return false;
}

function TE_setLanguage(langId){
	TE_SetCookie("language_id",langId,TE_GetCookieExpiration(365),".tycoelectronics.com","/");
	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;
		}
		// look to see if other parameters on the string, respect them...
		checkParam = new RegExp("([\?])","ig");
		if (checkParam.test(url))
		{
		    // Yes, there is a ? already present, so append &LG=N
			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 for getting back a language specific text
// designed to alert the user 
// not used.
function TE_getLanguageNotice(langId){
	// Note
    var noticeText = new Array(15);
	noticeText[0] = "Not Used";
	noticeText[1] = "Notice"; // English - not used
	noticeText[2] = "Notice"; // French - not used
	noticeText[3] = "Anmerkung"; // German 
	noticeText[4] = "Nota"; // Italian
	noticeText[5] = "Nota"; // Spanish
	noticeText[6] = "ノート"; // Japanese
	noticeText[7] = "通知"; // Trad Chinese
	noticeText[8] = " "; // - not used
	noticeText[9] = "说明"; // Simple Chinese 
	noticeText[10] = "Nota"; // Portugues
	noticeText[11] = "Not Used";
	noticeText[12] = "Not Used";
	noticeText[13] = "Not Used";
	noticeText[14] = "извещение"; //Russian

	
	return noticeText[langId];
}

// Function for getting back a language specific text
// designed to label the button that continues the user 
// in the process of changing languages.
//
function TE_getLanguageContinue(langId){
	// Continue
    var noticeText = new Array(15);
	noticeText[0] = "Not Used"; // not used
	noticeText[1] = "Continue"; // English - not used
	noticeText[2] = "Continue"; // French - not used
	noticeText[3] = "Fahren Sie fort"; // German 
	noticeText[4] = "Continui"; // Italian
	noticeText[5] = "Continúe "; // Spanish
	noticeText[6] = "続ける"; // Japanese
	noticeText[7] = "繼續"; // Traditional Chinese
	noticeText[8] = " "; // - not used
	noticeText[9] = "继续"; // simplified Chinese
	noticeText[10] = "Continue"; // Portugues
	noticeText[11] = "Not Used"; // not used
	noticeText[12] = "Not Used"; // not used
	noticeText[13] = "Not Used"; // not used
	noticeText[14] = "Продолжить"; // not used
	
	return noticeText[langId];
}
// Function for getting back a language specific text
// designed to inform the user that not all the pages are translated
function TE_getLanguageNoticeText(langId){
	// NOT ALL CONTENT WILL APPEAR IN THE LANGUAGE SELECTED
    var noticeText = new Array(15);
	noticeText[0] = "Not Used"; // not used
	noticeText[1] = "Not all content will appear in the language selected"; // English - not used
	noticeText[2] = "Not Used"; // French - not used
	noticeText[3] = "Nicht aller Inhalt erscheint in der vorgewählten Sprache "; // German 
	noticeText[4] = "Non tutto il soddisfare comparirà nella lingua selezionata "; // Italian
	noticeText[5] = "No todo el contenido aparecerá en la lengua seleccionada "; // Spanish
	noticeText[6] = "全ての内容が選択した言語で表示されるわけではありません"; // Japanese
	noticeText[7] = "沒有所有內容將出現在語言被選擇"; // Traditional Chinese
	noticeText[8] = "Notice "; // - not used
	noticeText[9] = "并不是所有的内容将出现在选定的语言"; // Simplified Chinese
	noticeText[10] = "Nem todo o conte&uacute;do aparecer&aacute; no idioma selecionado"; // Portuguese
	noticeText[11] = "Not Used"; // not used
	noticeText[12] = "Not Used"; // not used
	noticeText[13] = "Not Used"; // not used
	noticeText[14] = "Не все содержимое появится на выбранном языке"; // not used
	
	return noticeText[langId];
}
