
var LanguageSelection = {
	
	path_0: "/index.php?id=374&L=",
	path_1: "/index.php?id=374&L=",
	path_2: "/index.php?id=138&L=",
	path_3: "/index.php?id=138&L=",
	path_4: "/index.php?id=138&L=",
	path_5: "/index.php?id=138&L=",
	gotoSpecLanguageIfToLanuageNotDeOrEn: -1,
	
	languageListDiv: null,

	languages: [
		{code:"1",title:"English"}, 
		{code:"0",title:"Deutsch"},
		{code:"2",title:"Espa&ntilde;ol"},
		{code:"3",title:"T&uuml;rk&ccedil;e"},
		{code:"4",title:"&#20013;&#25991;"},
		{code:"5",title:"Italiano"},
		
		/*
		{code:"3",title:"T&uuml;rk&ccedil;e"},
		{code:"4",title:"&#20013;&#25991;"}
		*/
		
		/*
		{code:"ja",title:"&#26085;&#26412;&#35486;"},
		{code:"ko",title:"&#54620;&#44397;&#50612;"},
		{code:"ru",title:"&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;&#160;"}
		{code:"fr",title:"Fran&ccedil;ais"},
		{code:"it",title:"Italiano"},
		{code:"pt",title:"Portugu&ecirc;s"},
		{code:"nl",title:"Nederlands"},
		{code:"sv",title:"Svenska"},
		{code:"fi",title:"Suomi"}, 
		{code:"zh",title:"&#20013;&#25991;"},
		{code:"ja",title:"&#26085;&#26412;&#35486;"},
		{code:"ko",title:"&#54620;&#44397;&#50612;"},
		{code:"ru",title:"&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;&#160;"},
		{code:"pl",title:"Polski"}, 
		{code:"hu",title:"Magyar"},
		
		*/
	],		

	delayTime: 250,
	
	visible: false,
	
	delayID: null,
	
	initialize: function() {
		
		try {
		
			Event.observe($("language_link"), "click", this.showLanguages.bind(this), true);
			Event.observe($("language_link"), "mouseout", this.delayedHideLanguages.bind(this));
			
			Event.observe($("header-language-selector"), "mouseout", this.delayedHideLanguages.bind(this));
			Event.observe($("header-language-selector"), "mouseover", this.clearHideLanguages.bind(this));
						
			this.languageListDiv = $("header-language-selector");
		
			this.populateList();
	
			if ($("language-select-iframe")){
				
				Element.show(this.languageListDiv);
				
				$("language-select-iframe").style.width = this.languageListDiv.offsetWidth + "px";
				$("language-select-iframe").style.height = this.languageListDiv.offsetHeight +"px";
	
				Element.hide(this.languageListDiv);
	
			}
		} catch(e){}
	},
	
	makePositioned: function(){
		
		var leftBorder = Position.cumulativeOffset($("language_link"))[0] + $("language_link").offsetWidth;
		this.languageListDiv.style.left = (leftBorder - 168) + "px";
		this.languageListDiv.style.top  = "20px";
	
		
	},	
	
	populateList: function() {
		
		$("language-select").innerHTML = "";
		
		this.languages.each((function(language){
				
			var li 	= document.createElement("li");
			var a 	= document.createElement("a");
			
			a.innerHTML = language.title;
			a.href = "#";
			
			Event.observe(a, "click", (function(e){
				
				$("language-value").innerHTML = language.title;
				
				Event.stop(e);
				
				this.hideLanguages();
				
				if (language.code == 1)
					{
					document.location = this.path_1 + language.code;
					}
				else if (language.code == 2)
					{
					if (this.gotoSpecLanguageIfToLanuageNotDeOrEn > -1)
						{
						language.code = '&L=' + -1;
						}
					document.location = this.path_2 + language.code;
					}
				else if (language.code == 3)
					{
					if (this.gotoSpecLanguageIfToLanuageNotDeOrEn > -1)
						{
						language.code = '&L=' + -1;
						}
					document.location = this.path_3 + language.code;
					}
				else if (language.code == 4)
					{
					if (this.gotoSpecLanguageIfToLanuageNotDeOrEn > -1)
						{
						language.code = '&L=' + -1;
						}
					document.location = this.path_4 + language.code;
					}
				else if (language.code == 5)
					{
					if (this.gotoSpecLanguageIfToLanuageNotDeOrEn > -1)
						{
						language.code = '&L=' + -1;
						}
					document.location = this.path_5 + language.code;
					}
				else
					{
					document.location = this.path_0 + language.code;
					}
				
								
			}).bind(this));
			
			li.appendChild(a);
			$("language-select").appendChild(li);
			
		}).bind(this));
	},

	/*
	 * The function showLanguages computes the position of the Element e
	 * and changes the position of the Element update depending on the computed position.
	 * Then it calls the method Effect.Appear of the script.aculo.us library.
	 * The language selection is shown. 
	 *   
	 * @param Element e
	 * 
	 */
	
	showLanguages: function(e){
			
		this.clearHideLanguages();
		
		if (this.visible) {
			
			this.hideLanguages();
			
		} else {
					
			this.makePositioned();
			
			try {
								
				Effect.Appear(this.languageListDiv,{duration:0.3});
				
			} catch(e){
				
				Element.show(this.languageListDiv);
			}
			
			this.visible = true;
		}		
		
		Event.stop(e);

	},

	/*
	 * The function hideLanguages calls the method Effect.Fade of the script.aculo.us library.
	 * The language selection disappears.  
	 * 
	 */
	
	hideLanguages: function(){
		
		if (this.visible) {
			
			this.languageListDiv = document.getElementById("header-language-selector");
		
			try {	
			
				Effect.Fade(this.languageListDiv,{duration:0.45});
				
			} catch(e){
				
				Element.hide(this.languageListDiv);
			}

			this.visible = false;
		}
		
	},
	
	/*
	 * The function delayedHideLanguages calls the method hideLanguages with a delay,
	 * because the delay avoids that the language selection disappears at once when the mouse leaves link.
	 * The function is used in the onMouseOut event.  
	 * 
	 */

	delayedHideLanguages: function(e) {

		if (this.visible){

			this.clearHideLanguages();
			
			this.delayID = setTimeout(function(){
				
				this.hideLanguages();
				
			}.bind(this), this.delayTime);
		}
	},

	/*
	 * The function clearHideLanguages clears the timeout, which was set in the function delayedHideLanguages.   
	 * It is used in the onMouseOver event in the language selection layer. 
	 * 
	 */
	
	clearHideLanguages: function(){
	
		if (this.delayID !== null) {
			
			clearTimeout(this.delayID);
			
			this.delayID = null;
		}
	}                       
};

Event.observe(window, "load", function(){
	
	LanguageSelection.initialize()
});