﻿var Languages =[	{code:"en", lang:"&nbsp; English",		text:"&nbsp; Language"},
					{code:"de", lang:"&nbsp; Deutsch",		text:"&nbsp; Sprache"},
					{code:"jp", lang:"&nbsp; 日本語",		text:"&nbsp; 言語"}
				];

// Get the current language code
var CurrentLangCode = null;
var pathArray = window.location.pathname.split( '/' );
for(var i=0; i < pathArray.length; i++){
	if( (i > 0) && (pathArray[i-1] == 'content')){
		CurrentLangCode = pathArray[i];
	}
}


// Generate alternative pages:
function GenAltLink(LangCode){
	pathArray = window.location.pathname.split( '/' );
	altlink = "";
	for(var i=0;i<pathArray.length;i++){
		if( pathArray[i] != ""){
			altlink += "/";
			if( (i > 0) && (pathArray[i-1] == 'content')){
				altlink += LangCode;
			}else{
				altlink += pathArray[i];
			}
		}
	}
	return altlink;
}

function BuildLanguageMenu(){
	var Menu = document.getElementById('LangSelect');
	if(Menu == null){ return null;}

	var html = "";
	html +='<div class="skinnedSelect">';
	var LangPos = 0;

	for(var i=0; i < Languages.length;i++){
		if( Languages[i].code == CurrentLangCode ){
			LangPos = i;
		}
	}
	html +='<div class="text">'+Languages[LangPos].text+'</div>';
	html +='<div id="list">';
	for(var i=0; i < Languages.length;i++){
		html +='<a href="javascript:LanguageChange(\''+Languages[i].code+'\')">'+Languages[i].lang+'</a>';
	}
	html +='</div>';

	Menu.innerHTML += html;
}

function LanguageChange(LangCode){
	var altlink = GenAltLink(LangCode);

	if( CurrentLangCode == null){
		return "";
	}


	// store the language selection
	createCookie("lang",LangCode,7);
	
	// change viewed page
	window.location.href = altlink;
}


// Cookie Functions
// http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	//alert("Set "+name+": "+value);
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}



function init(){
	var selectedLang = readCookie('lang');
	
	// If cookie set change language
/*	if(selectedLang != null){
		// check if we already are on the correct page
		if( CurrentLangCode != selectedLang){
			LanguageChange(selectedLang);
		}
	}*/
	
	BuildLanguageMenu();
}


//document.onload = init();

// DOM tree must be loaded before running the function
var alreadyrunflag=0 //flag to indicate whether target function has already been run
if (document.addEventListener)
  document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; init();}, false)
else if (document.all && !window.opera){
  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
  var contentloadtag=document.getElementById("contentloadtag")
  contentloadtag.onreadystatechange=function(){
    if (this.readyState=="complete"){
      alreadyrunflag=1;
      init();
    }
  }
}


window.onload=function(){
  setTimeout("if (!alreadyrunflag){init();}", 0)
}

