/* Language switching for Basic Bilingual with Ajax
 * replaces the default language PHP-Paramter-Link and loads the other content with Ajax
 * by Tim Isenheim - www.freshlabs.de
 * version: 0.6b r18
 * modified: 27-02-2008
 */


var LanguageSwitcher = {
	otherlanguageURL : "http://www.freshlabs.de/journal/wp-content/plugins/langswitcher/otherlanguage.php",
	
	languages : ["en","de"],
			
	linktext : ["Read this article in english.","Diesen Artikel in deutscher Sprache lesen."],
	

	active : -1, // 0:Language 1, 1:Language 2, -1:init
	toggleLink : null,
	original_content : null,  // original content HTML
	switchContainer : null,
	contentDiv : String,
	
	getInactive : function(){
		var temp;
		if(this.active == 1) temp=0;
		else temp=1;
		return temp;
	},
	
	/* initializing function - provides a link for switching the language */
	init : function(contentDiv, new_language){
		if(document.getElementById && document.getElementsByTagName && document.getElementById("langswitcher-"+contentDiv)){
			this.active = (new_language == this.languages[1]) ? 0 : 1;
			this.switchContainer = document.getElementById("langswitcher-"+contentDiv);
			this.contentDiv = contentDiv;
			
			this.toggleLink = this.switchContainer.getElementsByTagName('a').item(0);
		
			this.toggleLink.href = 'javascript:void(0)';
			
			var the_full_id = this.toggleLink.id;
			var the_id = the_full_id.substr(the_full_id.lastIndexOf("-")+1);
		
			this.toggleLink.onclick = function(){
			  LanguageSwitcher.replaceContent(the_id, new_language);
			}
		}
	},
	
	run : function() {
		this.active = (this.active == 1) ? 0 : 1;
		this.switchContainer.className = "langswitcher " + this.languages[this.getInactive()];
		this.toggleLink.firstChild.nodeValue = this.linktext[this.getInactive()];
		this.toggleLink.lang = this.languages[this.getInactive()];
	},
	
	/*
	 * Replace CONTENT with REPLACEMENT and change LINK
	 */
	replaceContent : function(entry_id, new_language){
		// switching from original to other language
		if(this.switchContainer.className == "langswitcher " + new_language ){
			original_content = $(this.contentDiv).innerHTML; // temp-save content
			// do Ajax request
			pars = "p=" + entry_id + "&lang=" + new_language;
			//LoadingMessage.append(document.getElementsByTagName("body").item(0));
			var myAjax = new Ajax.Updater(this.contentDiv, this.otherlanguageURL, {method: 'get', parameters: pars, onComplete: function(){ /*LoadingMessage.remove();*/ /*new Effect.Highlight($("itemcontent"));*/ } });
			
			this.run();
		}
		// switching from the other language back to the original
		else{
			$(this.contentDiv).innerHTML = original_content; // restore content
			/*new Effect.Highlight($("itemcontent"));*/
			this.run();
		}
	}
}
