var page_ns_prefixes= { 
	'en':'Page',
	'fr':'Page',
	'de':'Seite',
	'it':'Pagina',
	'la':'Pagina',
	'no':'Side',
	'es':'Página',
	'pt':'Página',
	'sv':'Sida',
	'pl':'Strona',
	'hy':'Էջ',
	'ru':'Страница', 
	'hr':'Stranica',
	'hu':'Oldal',
	'ca':'Pàgina',
	'vi':'Trang',
	'sl':'Stran',
	'zh':'Page', 
	'old':'Page'
}

/* cross-domain transclusions through the API */

function iw_trans_callback(res){  
	//var reg = /<p>([\s\S]*)<\/p>([^\/]*)/;
	var target = res.requestid;
	for(var i=0; item = document.getElementsByTagName("span")[i]; i++ ) {
		if (item.className == "iwtrans" && item.title==target ) {
			var txt = res.parse.text['*'];
			item.innerHTML = txt; //.match(reg)[1];
		}
	}
}
function iw_trans() {
    var l = get_elements_by_classname( 'iwtrans', 'span' ) ; 
    for(var i=0; i < l.length ; i++ ) {
        var item = l[i];
	var m = item.title.split("|");
	lang=m[0]; title=m[1];
	if(lang=="old") { //+old+
            url = 'http://wikisource.org/w/api.php?format=json&requestid='+item.title+'&action=parse&text=\{\{:'+title+'\}\}&callback=iw_trans_callback';//+old+
        } else {//+old+
	    url = 'http://'+lang+'.wikisource.org/w/api.php?format=json&requestid='+item.title+'&action=parse&text=\{\{:'+title+'\}\}&callback=iw_trans_callback';
	}
	importScriptURI(url); //alert(url);
    }
}
addOnloadHook(iw_trans);



function iw_pages_callback(res) {
	var txt = res.parse.text['*'];
	var target = res.requestid;
        var l = document.getElementsByTagName("span");
	for(var i=0; i<l.length; i++ ) {
		var item = l[i];
		if( (item.className == "iwpages"||item.className == "iwpage") && item.title==target ) {
			item.innerHTML = txt;
		}
	}
}

function iw_pages() {
        //if(wgCanonicalNamespace != page_ns_prefixes[wgContentLanguage]) return;

        var l = get_elements_by_classname( 'iwpages', 'span' ) ; 
        for(var i=0; i < l.length ; i++ ) {
		var item = l[i]; 
		var m = item.title.split("|");
		lang=m[0]; title=m[1]; from=m[2]; to=m[3];
		if(lang=="old") { //+old+
		        url = 'http://wikisource.org/w/api.php?format=json&requestid='+item.title+'&action=parse&callback=iw_pages_callback&text=<pages index="'+title+'" from='+from+' to='+to+' />';
		} else {//+old+
			url = 'http://'+lang+'.wikisource.org/w/api.php?format=json&requestid='+item.title+'&action=parse&callback=iw_pages_callback&text=<pages index="'+title+'" from='+from+' to='+to+' />';
		}//+old+
		importScriptURI(url);
	}
	var count = 0;
        var l = get_elements_by_classname( 'iwpage', 'span' ) ; 
        for(var i=0; i < l.length ; i++ ) {
		var item = l[i]; 
		var m = item.title.split("|");
		lang = m[0]; 
		count = count + 1; 
		title = page_ns_prefixes[lang]+":"+m[1]; 
		if(m.length>2) section=m[2]; else section=false;
		if(section) title='lst\|'+title+'\|'+section; 
		if(lang=="old") { //+old+
			 url = 'http://wikisource.org/w/api.php?format=json&requestid='+item.title+'&action=parse&callback=iw_pages_callback&text=\{\{'+title+'\}\}';
		} else {//+old+
			 url = 'http://'+lang+'.wikisource.org/w/api.php?format=json&requestid='+item.title+'&action=parse&callback=iw_pages_callback&text=\{\{'+title+'\}\}';
		}//+old+
		importScriptURI(url);
	}
    cs = document.getElementById("corr-info");
    if(cs && count) {
	if(count==1) {
           if (lang=="old") lang=""; else lang=lang+".";
           cs.innerHTML = ' ' + ws_msg('iwtrans') + ' <a href="http://'+lang+'wikisource.org/w/index.php?title='+title+'">'+lang+'wikisource.org</a>.';
        } else cs.innerHTML = ws_msg('iwtrans2');
    }
}
addOnloadHook(iw_pages);