// wikilinkfix.js created by Tom O'Dea

function getElementsByClassName( strClassName, obj ) {
    var ar = arguments[2] || new Array();
    var re = new RegExp("\\b" + strClassName + "\\b", "g");

    if ( re.test(obj.className) ) {
        ar.push( obj );
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i], ar );
    
    return ar;
}


function getwikibox() {
	//ms-wikicontent is the standard wiki content container
    var aryClassElements = getElementsByClassName('ms-wikicontent', document.body );
    for ( var i = 0; i < aryClassElements.length; i++ ) {
		fixlinks();
    }
}


function fixlinks() {
if (!document.getElementsByTagName) return;
 
var wikipage = window.location.pathname; //current path & page
var badanchor1 = "EditForm";
var badanchor2 = "CreateWebPage";

 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   var chklink = anchor.getAttribute("href");
   
   if (anchor.getAttribute("href"))
   		{
		// check for the bad anchors and replace with normal path & page 
		var pganchor = chklink.substr(chklink.lastIndexOf("#"));
	   		 if (chklink.indexOf(badanchor1) != -1)
			 { 
				 anchor.href = wikipage + pganchor;
			 }
			 
			 if (chklink.indexOf(badanchor2) != -1)
			 {
				 anchor.href = wikipage + pganchor;
			 }
		}
 }
}


function runOnLoad(){
	getwikibox();
}
_spBodyOnLoadFunctionNames.push("runOnLoad");