/* author : ThomasV */

function dict_entry_callback(res) {
	var res2 = eval("0,"+res);
	var txt = res2.parse.text['*'];
	var item = document.getElementById("dict_entry");
	if(item) { item.innerHTML = txt;
            self.pagenum_ml = get_elements_by_classname( 'pagenum', 'span' );
            for(var i=self.pagenum_ml.length-1; i>=0; i--) {

        var a = self.pagenum_ml[i];
        var num = decodeURI( a.id ); //.replace(/\./g,'%')
        if(num==".CE.9E") num="Ξ";
        var page = a.title;
        pagetitle = decodeURI( page ); //.replace('?','%3F').replace(/\.2/g,'%2').replace("%2F","/")
        pagekey = pagetitle.replace(/ /g,"_");
        var page_url = wgArticlePath.replace("$1", pagekey  );
        var ll = a.parentNode.nextSibling;
        if(ll && ll.tagName=="A" && ll.className=="new") class_str=" class=\"new\" "; else class_str="";
        var link_str = "<a href=\""+page_url+"\"" + class_str + " title=\""+pagetitle+"\">"+num+"</a>"
        a.innerHTML = "&#x0020;<span class=\"pagenumber noprint\" style=\"color:#666666; display:inline; margin:0px; padding:0px;\">[<b>"+link_str+"</b>]</span>&#x0020;";

	    }
        }
}
 

function show_dict_entry(index,from,to,title) {
	var item = document.getElementById("dict_entry");
	if(!item) return;
	item.innerHTML="…";
	var fs="fromsection=\""+title+"\"";
	var ts="tosection=\""+title+"\"";
	var url = wgServer+wgScriptPath+'/api.php?format=json&action=parse&text=<pages index="'+index+'" from='+from+' to='+to+' '+fs+' '+ts+' /> <references/>';
	var api=sajax_init_object();
	api.open("GET",url,true);
	api.onreadystatechange=function(){ /*if(api.readyState!=4)return; if(api.status==200)*/ dict_entry_callback(api.responseText);};
	api.send(null);
	location.href = document.URL.split('#')[0] + '#'+ escape(encodeURI( title.replace(/ /g,'_')).replace(/%/g,'.') ).replace(/%/g,'.');
}

 
function dl_links() {
	var mlist = document.getElementById("dynamic_links");
	if(!mlist) return;
	var index = mlist.title;
	var url_title = document.URL.split('#')[1]; 
	var elements = mlist.getElementsByTagName("a");
	for(var i=0; i<elements.length; i++ ) {
                var item = elements.item(i);
		if(item.title == "DL") {
			var title = item.innerHTML;
			var dl_ref = item.href.split('#')[1];
			var m = dl_ref.split(":");
			var m_from = parseInt(m[0]); 
			var m_to = parseInt(m[1]);
			item.href = "javascript:show_dict_entry(\""+index+"\","+m_from+","+m_to+",\""+title+"\");";
			item.title = title;
			if(url_title == escape(encodeURI( title.replace(/ /g,'_')).replace(/%/g,'.') ).replace(/%/g,'.')) show_dict_entry(index,m_from,m_to,title);
		}
	}
}
addOnloadHook(dl_links);