﻿/*JS-VERSION:2.4.0.0*/
/*rosh:27.03.07: global variables*/
var selectionLanguage = getLanguage();
var selectionCountry	= getCountry();
/*flags*/
var ddlOnScreen = false;
var onShowLangListActive = false;
var selectionLanguage = 'EN';
var selectionCountry  = 'ROW';

function getUrlLanguage()
{
    var url = location.href.replace("#","");
    var re = /\/(EN|DE|FR|IT|ES)\//i;
    var arr = re.exec(url);
    return ((arr != null) && (arr[1] != null)) ? arr[1].toUpperCase() : '';
}

function validateUrlLanguage(language)
{
    var urlLanguage = getUrlLanguage();
    if (language && urlLanguage && (language != urlLanguage))
    {
        redirectToLanguage(language);
    }
    createCookie(cookieLanguageName,pageLanguage,365);
    createCookie("Country",pageCountry,365);
}

function redirectToLanguage(language)
{
    var url = location.href.replace("#","");
    var re = /\/(EN|DE|FR|IT|ES)\//i;
    location.href = url.replace(re, '/'+language+'/');
}

function getCookieLanguage()
{
    return readCookie(cookieLanguageName);
}

function getCookieCountry()
{
    return readCookie("Country"); 
}


function getLanguage(){
    return (typeof(pageLanguage) != 'undefined') ? pageLanguage : 'EN';
}

function getCountry(){
    return (typeof(pageCountry) != 'undefined') ? pageCountry : 'ROW';
}

/*rosh:28.05.07: maintain drop-downs on screen*/ 
function maintainDropDowns(val)
{
	if( typeof( HideDropDowns ) != 'undefined' )
	{
		if(val && ddlOnScreen)
		{
			maintainDropDowns(false);
		}
			
		HideDropDowns(val);
		ddlOnScreen = true;
	}
}

/*rosh:5.04.07: validate and set default data*/ 
function showLanguageLayer()
{
	maintainDropDowns(true);
	ddlOnScreen = true;
	if(document.getElementById('languagelayer').className == 'shown'){
		hideLanguageLayer();
	}
	else{
		document.getElementById('languagelayer').className = 'shown';
		document.getElementById('langbutton').className = "hi";
	}
	onShowLangListActive = false;
	languageActivation();
}
/*rosh:5.04.07: hide all related language selection related elements*/
function hideLanguageLayer()
{
	maintainDropDowns(false);
	hideCountryList(null, 0);
	hideLangList(null);
	document.getElementById('languagelayer').className = '';
	document.getElementById('langbutton').className = "";
}
/*rosh:5.04.07: display countries*/
function showCountryList(){
	hideLangList(null);
	if(document.getElementById('countrytitle').className == 'langlink hi'){
		hideCountryList(null);
	}
	else{
		document.getElementById('countrylist').className = 'shown';
		document.getElementById('countrytitle').className = 'langlink hi';
	}
}

/*rosh:5.04.07: hide country selection*/
function hideCountryList(elm, country, shrt, lang){
	if(elm != null){
		document.getElementById('langlist').innerHTML = document.getElementById('langlist_'+country).innerHTML;
		hideLangList(document.getElementById('langlist_'+country).firstChild.firstChild.firstChild, null);		
		document.getElementById('countrytitle').innerHTML = elm.innerHTML;
	}
	document.getElementById('countrytitle').className = 'langlink';
	document.getElementById('countrylist').className = '';
	/*set cookie values for usage after page reload*/	
	if(shrt){
		selectionCountry = shrt;
		// ROSHTMP:: createCookie("Country",selectionCountry,365);
	}	
	if(lang){
		selectionLanguage = lang;
	}
	
	onShowLangListActive = false;
	languageActivation();
}
/*rosh:5.04.07: display language*/
function showLangList(){
	hideCountryList(null, 0);
	languageActivation();
	if(document.getElementById('langtitle').className == 'langlink hi'){
		hideLangList(null);
	}
	else{
		document.getElementById('langlist').className = 'shown';
		document.getElementById('langtitle').className = 'langlink hi';
	}
}
/*rosh:5.04.07: hide country language*/
function hideLangList(elm, lang, shrt){
	languageActivation();
	if(elm != null){
		document.getElementById('langtitle').innerHTML = elm.innerHTML;
	}
	document.getElementById('langtitle').className = 'langlink';
	document.getElementById('langlist').className = '';
	
	if(shrt){
		selectionCountry = shrt;
	}	
	if(lang){
		selectionLanguage = lang;
	}
}

/*rosh:5.04.07: set language and relead page*/
function SetLanguage(){
	createCookie(cookieLanguageName,selectionLanguage,365);
	createCookie("Country",selectionCountry,365);
	hideLanguageLayer();
	var locact = location.href;
	var ahref = urlHandler(locact, selectionLanguage, selectionCountry);
	var loc = locact.indexOf("#", 0);
	if (loc > 0)
	{
		locact = locact.substring(0, loc);
	}
	if (locact == ahref)
	{
		location.reload();
	}
	else
	{
		location.href = ahref;
	}
	return false;
}

/*rosh:5.04.07: enables / disables the language */
function languageActivation()
{
	/*hack to clarify in FF and IE the correct amount of childNodes [2 -> ff; 1-> ie] */
	var count = document.getElementById('langlist').childNodes[0].childNodes.length == 0 ? 
		document.getElementById('langlist').childNodes[1].childNodes.length == 2 : 
		document.getElementById('langlist').childNodes[0].childNodes.length == 1;
	
	if(count)
	{
		document.getElementById('selectfakeLanguage').style.background = '#dfdfdf';
		document.getElementById('langtitle').onclick = function() {void(0);};
		if(document.getElementById('langtitle').style)
		{
			document.getElementById('langtitle').style.textDecoration = 'none';
			document.getElementById('langtitle').style.color = 'gray';
			document.getElementById('langtitle').style.cursor = 'default';
		}
		onShowLangListActive = false;
		/*cookie for language*/
	}
	else
	{
		if(onShowLangListActive)
		{
			document.getElementById('langtitle').setAttribute('onClick', "hideLangList(null);"); 
			/*IE6/7 ISSUE!!!*/
			document.getElementById('langtitle').onclick = function() {hideLangList(null);};
			onShowLangListActive = false;
		}
		else
		{
			document.getElementById('langtitle').setAttribute('onClick', "showLangList();"); 
			/*IE6/7 ISSUE!!!*/
			document.getElementById('langtitle').onclick = function() {showLangList();};	
			onShowLangListActive = true;
		}
		document.getElementById('langtitle').style.cursor = 'pointer';
		document.getElementById('langtitle').className = 'langlink';
		document.getElementById('selectfakeLanguage').className = 'selectfake';
		/*IE6/7 ISSUE!!!*/
		if(document.getElementById('selectfakeLanguage').style)
		{
			document.getElementById('selectfakeLanguage').style.background = "transparent url(" + serverProtocol + "://" + homeDomain + "/Style%20Library/images/language/bg_select.gif) no-repeat scroll right center";
			document.getElementById('selectfakeLanguage').style.border = "1px solid #999999";
			document.getElementById('selectfakeLanguage').style.padding = "2px 0pt 2px 5px";
		}
	}
}
