/*
Author: Michiel de Roo
released under cc-by-sa 3.0, 2012
 
==Example configuration==
 
var interwikiTools = {}; 
interwikiTools.auto = true;
interwikiTools.sort = true;
interwikiTools.toplinks = ;
interwikiTools.translateLink = true;
interwikiTools.userConfig = true;
 
// load language file
mw.loader.load('//nl.wikipedia.org/w/index.php?title=Gebruiker:Michielderoo/interwiki-translation-nl.js&action=raw&ctype=text/javascript');
 
*/
 
// initialize default configuration if none
if(typeof interwikiTools=='undefined') { 
 
	// init object
	var interwikiTools = {}; 
}
 
if(!interwikiTools.userConfig) {
	interwikiTools.auto = true;
	interwikiTools.sort = true;
	interwikiTools.translateLink = true;
}
 
// these interwiki's are put on top of the list
if(!interwikiTools.toplinks) interwikiTools.toplinks = ;
 
// set default language to english, if no language is set.
if(!interwikiTools.translation) { mw.loader.load('//nl.wikipedia.org/w/index.php?title=Gebruiker:Dinosaur918/interwiki-translation-nl.js&action=raw&ctype=text/javascript'); } 
 
// some private variables, don't touch these
interwikiTools.original = ;
interwikiTools.originalTitle = ;
interwikiTools.originalLang = ;
interwikiTools.translated = false;
 
addLoadEvent(initInterwikiTools);
 
getWPLanguageCode();
 
/* Function to translate the interwiki links in the sidebar to another language.
*/ 
function startInterwikiTools() {
 	
	// switch used to create a toggle effect
	if(interwikiTools.translated) return resetInterwikiTools();
	
	// initalize the array to order the items
	var ordered = ;
	var toplinks = ;
	var bottomlinks = ;
	
	// create a list of interwikilink classes with their corresponding translation
	var translation = ;
	for(var key in interwikiTools.translation) {
	  translation = interwikiTools.translation;
	}
	
	// index the toplinks by their short language code (e.g. en, de ,fr)
	var toplinksIndex = ;
	for(var key in interwikiTools.toplinks) {
		toplinksIndex] = 1;
	}
	
	// get the main container from sidebar
	var container = document.getElementById("p-lang");
	if (!container) return;
 
	// get the childs from the container and start to loop
	var childs = container.getElementsByTagName("li");
	var cnt = 0; var start = interwikiTools.translateLink?1:0; var cnt_bottom = 0;
	for (i=start;i<childs.length;i++) {
		
		// get the first class of the element
		var matches = childs.getAttribute('class').match(/interwiki-*/);
		if(matches && matches.length>0) iwclass = matches;
		else iwclass="";
		
		// get the anchor element
		var anchor = childs.getElementsByTagName("a").item(0);
		if(iwclass.substring(0,10)=='interwiki-') {	
			if(translation) {
	
				// swap link title with the translation
				anchor.innerHTML = translation;
				anchor.lang = "en";
				
				// store the element in either the toplinks array or the array to be ordered
				if(toplinksIndex) {
					toplinks = {title: translation, content: childs};
				}
				else {
					ordered = {title: anchor.innerHTML, content: childs};
				}
			}
			else {
				ordered = {title: anchor.innerHTML, content: childs};
			}
		}
		else {
			bottomlinks = {title: anchor.innerHTML, content: childs};
		}
	}
	
	// sort the array using locale compare function
	if(interwikiTools.sort) ordered.sort(function(a,b) { return a.title.localeCompare(b.title); });
	
	// get ul element so we can remove the old items and put the sorted items back
	var ul = container.getElementsByTagName("ul");
	if (!ul) return; if (ul.length != 1) return; ul = ul;
	
	var child;
	// remove all items
	while (child = ul.firstChild) {
	  ul.removeChild(child);
	}
	
	// append ordered items
	addTranslateLink(container);
	for(key in interwikiTools.toplinks) {
		if(toplinks]) 
			ul.appendChild(toplinks].content);
	}
	
	for (i=0;i<ordered.length;i++) {
	  ul.appendChild(ordered.content);
	}

	for (i=0;i<bottomlinks.length;i++) {
	  ul.appendChild(bottomlinks.content);
	}

	// toggle translated switch
	interwikiTools.translated = !interwikiTools.translated;
	
}

function initInterwikiTools() {
	// get the main container from sidebar
	var container = document.getElementById("p-lang");
	if (!container) return;
	
	// store the original items
	childs = container.getElementsByTagName("li");
	for (i=0;i<childs.length;i++) {
		interwikiTools.originalTitle = childs.getElementsByTagName("a").item(0).innerHTML;
		interwikiTools.originalLang = childs.getElementsByTagName("a").item(0).lang;
		interwikiTools.original = childs;
	}
	
	addTranslateLink(container);	
	
	if(interwikiTools.auto) startInterwikiTools();
}

function resetInterwikiTools() {

	// get the main container from sidebar
	var container = document.getElementById("p-lang");
	if (!container) return;

	var ul = container.getElementsByTagName("ul");
	if (!ul) return; if (ul.length != 1) return;
	ul = ul;
	
	var child;
	// remove all items
	while (child = ul.firstChild) {
	  ul.removeChild(child);
	}

	// append ordered items
	addTranslateLink(container);	
	for (i=0;i<interwikiTools.original.length;i++) {
		interwikiTools.original.getElementsByTagName("a").item(0).innerHTML = interwikiTools.originalTitle;
		interwikiTools.original.getElementsByTagName("a").item(0).lang = interwikiTools.originalLang;
		ul.appendChild(interwikiTools.original);
	}
	interwikiTools.translated = !interwikiTools.translated;
	
}

function addTranslateLink(container) {
	
	if(!interwikiTools.translateLink) return;
	
	newNode = document.createElement("li");
	newNode.className = "interwiki-translate";
	newNode.innerHTML = "<a href='' onClick='startInterwikiTools(); return false;'>" + interwikiTools.translation + "</a>";

	var ul = container.getElementsByTagName("ul");
	if (ul) {
		ul = ul;
		ul.insertBefore(newNode,ul.firstChild);
	}
}


// code by ExpressingIT
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
       window.onload = func
    } else {
       window.onload = function() {
           if (oldonload) {
                  oldonload()
          }
          func()
       }
   }
}

// parse the language code from the domain.
function getWPLanguageCode() {
	url = window.location.hostname;
	return url.match(/(.+)/);
}