/* 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.5b r13
 * modified: 22-05-2006
 * License: http://creativecommons.org/licenses/by-nc-sa/2.0/de/
 */

// URL to your WordPress-Installation (without trailing slash)
var siteURL = "http://www.yourdomain.com/wordpress";

var LanguageSwitcher = {
	otherlanguageURL : siteURL + "/wp-content/plugins/langswitcher/otherlanguage.php",
	
	// Your two languages defined in BasicBilingual config
	languages : ['de','en'],
	
	// Text that is linked to the other language article
	linktext : [
			"Diesen Artikel in deutscher Sprache lesen.",
			"Read this article in english."
			],

// NO EDITING NECESSARY BELOW THIS LINE #########################
	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")){
			this.active = (new_language == this.languages[1]) ? 0 : 1;
			this.switchContainer = document.getElementById("langswitcher");
			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 = 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 == new_language ){
			original_content = $(this.contentDiv).innerHTML; // temp-save content
			// do Ajax request
			pars = "p=" + entry_id + "&lang=" + new_language;
			LoadingMessage.append(this.contentDiv);
			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();
		}
	}
}

/*-------------------------*/
/* Object: Loading Message */
/*-------------------------*/

var LoadingMessage = {
	imageURL : siteURL + '/wp-content/plugins/langswitcher/images/indicator_black.gif',
	waitImg : null,
	containerId : "loading-message",
	loadTextId : "loading-text",
	waitImgId : "loading-image",
	waitImgWidth : 32,
	waitImgHeight : 32,
	
	init : function(){
		this.waitImg = document.createElement('img');
		this.waitImg.setAttribute('src', this.imageURL);
		this.waitImg.setAttribute('height', this.waitImgHeight);
		this.waitImg.setAttribute('width', this.waitImgWidth);
		this.waitImg.setAttribute('alt','loading...');
		this.waitImg.id = this.waitImgId;
		this.waitImg.style.border = '0';
		this.waitImg.style.backgroundColor = 'transparent';
		this.waitImg.style.margin = '0';
		this.waitImg.style.padding = '0';
	},
	
	append : function(where){
		var parent = document.getElementById(where);
		if(!$(this.containerId)){
			var loadbox = document.createElement('div');
			var loadtext = document.createElement('div')
			loadbox.id = this.containerId;
			loadtext.id = this.loadTextId;
			txt = document.createTextNode(" please wait");
			loadtext.appendChild(this.waitImg);
			loadtext.appendChild(txt);
			loadbox.appendChild(loadtext);
			parent.appendChild(loadbox);
		}
	},
	
	remove : function(){
		removeElementById(this.containerId);
	}
} // .LoadingMessage ---------------------


/* 
 * removeElementById()        
 * Author: Tim Isenheim      
 */

function removeElementById(theId){

	var deleted = false;
	
	if(document.getElementById(theId)){
		// acquire parent node
		var parent = document.getElementById(theId).parentNode;
		var children = parent.childNodes;
		
		// browse child nodes until id-item is found
		for(var i=0; i<children.length;i++){
		  if(children[i].id == theId){
				deleted = parent.removeChild(children[i]);  // remove it
			}
		}
	}
	return deleted;
}
