/* Language switching for Basic Bilingual for Wordpress
 * by Tim Isenheim - www.freshlabs.de
 * version: 0.3a
 */


// GLOBAL VARIABLES + CONFIGURATION - edit to your needs

// available languages. Must be the same as in the BasicBilingual configuration
var lang1 = 'de';
var lang2 = 'en';

// styles for specific language,
// LANGUAGE AS SUFFIX IS NECCESSARY
var icon_de = "url(http://www.yourdomain.com/wp-content/plugins/langswitcher/langball_de.gif)";
var icon_en = "url(http://www.yourdomain.com/wp-content/plugins/langswitcher/langball_en.gif)";
var linktext_de = "Diesen Artikel in deutscher Sprache lesen";
var linktext_en = "This article is available in english";

var content_pre = 'post-';        // Prefix for content-div
var replacement_pre = 'lang-ex';  // Prefix for other-language-div
var story_pre = 'story-';         // Prefix for storycontent

/** =========================================================================*/
/** NO EDITING NECESSARY BELOW THIS POINT, at least you should know what you're doing **/

/* initializing function - provides a link for switching the language */
function offerSwitching(the_id, new_language){
  var old_language = (new_language == lang2) ? lang1 : lang2;
  var swc = "langswitch-" + the_id;
	var switchContainer = document.getElementById(swc);
	var toggle = switchContainer.appendChild(document.createElement('a'));

	toggle.href = '#' + story_pre + the_id;   // link to the storycontent, so the post is focused
	toggle.lang = new_language;
	toggle.className = 'langswitch';
	toggle.onclick = function(){
	  replaceContent(toggle, content_pre, replacement_pre, the_id, new_language, old_language);
	}

  toggle.style.backgroundImage = eval('icon_'+toggle.lang);
  toggle.style.backgroundRepeat = "no-repeat";
  toggle.style.paddingLeft = "20px";
  toggle.appendChild(document.createTextNode( getLangSelectionText(new_language) ));

}

/*
 * Replace CONTENT with REPLACEMENT and change LINK
 * also works backwards due to the 'lang' attribute
 */
function replaceContent(link, content_pre, replacement_pre, id, new_language, old_language){
  var postdiv = document.getElementById(content_pre + id);
  var newpost = document.getElementById(replacement_pre + id);
  
  // switching from original to other language
  if(link.lang == new_language ){
	  postdiv.style.display = "none";
	  newpost.style.display = "block";
	  link.style.backgroundImage = eval('icon_'+old_language);
	  link.style.backgroundRepeat = "no-repeat";
	  link.style.paddingLeft = "20px";
	  link.firstChild.nodeValue = getLangSelectionText(old_language);
	  link.lang = old_language;
  }
  // switching from the other language back to the original
  else{
	  postdiv.style.display = "block";
	  newpost.style.display = "none";
	  link.style.backgroundImage = eval('icon_'+new_language);
	  link.style.backgroundRepeat = "no-repeat";
	  link.style.paddingLeft = "20px";
	  link.firstChild.nodeValue = getLangSelectionText(new_language);
	  link.lang = new_language;
	}
}

/* returns specific language selection text */
function getLangSelectionText(lang){
	var linktext = eval("linktext_"+lang);
  return linktext;
}