    function getTextNodes(topnode)
    {
	if (3==topnode.nodeType) 
	{
		return [topnode];
	}
	var ret=[];
	if ('iframe'!=topnode.nodeName.toLowerCase() && 'a'!=topnode.nodeName.toLowerCase())
	{
	    for(var i=0;topnode.childNodes.length>i;i++)
		ret=ret.concat(getTextNodes(topnode.childNodes[i]));
	}
	return ret;
    }

    (function($) {
	$.fn.replacelink = function(hash) {
	     // Get all text nodes:
/*	    var $textNodes = this
		     .find(":not(.al_facebook_comments) *")
		     .andSelf()
		     .filter(":not(.al_facebook_comments)")
//		     .css('border','1px solid silver')
		     .contents()
		     .filter(function() {
			try {
			 return this.nodeType === 3 &&
			     !$(this).parent("a").length;
			}
			catch (e)
			{
				return false;
			}		
		     });
*/	
//	    $textNodes=$();
	    var $textNodes=$();
	    this.filter(function() { 
		var l=getTextNodes(this);
		$textNodes=$textNodes.add(l);
            });

	    var tree={};
	    for (k in hash)
	    {
		var x=tree;
		for(i=0;i<k.length;i++)
		{
		    var c=k.charAt(i).toLowerCase();
		    if (!x[c]) x[c]={};
		    x=x[c];
		}
		x["_value"]=hash[k];
	    }
//	    $textNodes.css('border','1px solid cyan');

	    // Iterate through the text nodes, replacing the content
	    // with the link:
	    $textNodes.each(function(index, element) {
		var contents = $(element).text();
		var newtext="";
		var i=0;
		var x=tree;
		var start=0;
		var lastfound=null;
		var lastlink=null;
		var substr="";
		while (i<contents.length)
		{
		    var c=contents.charAt(i).toLowerCase();
		    if (x[c]) {
			if (x[c] && x[c]["_value"] && 
			  ( i-start>=4 || 
			    (
				(0==start || contents.charAt(start-1).match(/[ .,;'"!?<>()-]/) ) &&	
				(i+1==contents.length || contents.charAt(i+1).match(/[ .,;'"!?<>()-]/) )  
			    ) 
			) ) {
			    lastfound=i;
			    lastlink=x[c]["_value"];
			}
			x=x[c];
			substr+=c;
			i++;
		    }
		    else if (lastfound){
			newtext+="<a class=\"wikilink\" href=\""+lastlink+"\">"+contents.substr(start,lastfound-start+1)+"</a>";
			start=lastfound+1;
			lastlink=null;
			lastfound=null;
			x=tree;
			substr="";
			i=start;
		    }
		    else {	
			newtext+=contents.substr(start,1);
			start+=1;
			x=tree;
			substr="";
			i=start;
		    }
		}
		if (lastfound){
		    newtext+="<a class=\"wikilink\" href=\""+lastlink+"\">"+contents.substr(start,lastfound-start+1)+"</a>";
		    start=lastfound+1;
		    lastlink=null;
		    lastfound=null;
		    x=tree;
		    substr="";
		    i=start;
		}
		else if (start!=i)
		{
		    newtext+=contents.substr(start,i-start);
		}
		$(element).replaceWith(newtext);
	    });
	};
    })(jQuery);


    jQuery(document).ready(function($) {
//	if (window.location.href.match(/akkumulator/i))
        $.ajax({
            url: 'http://www.szakalmetal.eu/index.php?option=com_content&view=category&id=86&limit=0',
            success : function(xhr) {
                var wikilinks={};
		$(xhr).find("table.category td a").each( function(idx) {
		    wikilinks[$(this).text().replace(/^\s+|\s+$/,'')]=$(this).attr("href");
		});
		$('div#page').replacelink(wikilinks);
            }
        });
        $.ajax({
            url: 'http://www.szakalmetal.eu/index.php?option=com_content&view=category&id=87&limit=0',
            success : function(xhr) {
                var wikilinks={};
		$(xhr).find("table.category td a").each( function(idx) {
		    wikilinks[$(this).text().replace(/^\s+|\s+$/,'')]=$(this).attr("href");
		});
		$('div#page').replacelink(wikilinks);
            }
        });

    });





