var revisionMapURL;

var revisionMap;
var linkList = new Object();
var windowLink;
var qo;

function init(editLink,editLink2,oml){
	windowLink = window.location.href;
	var q = windowLink.substring(windowLink.indexOf("?") + 1, windowLink.length);
  	 qo = dojo.queryToObject(q);
  	if(dojo.byId("rzcontrols")){
  	if(qo.mode){
  		dojo.byId("originalMD").style.display = "inline";
  		dojo.byId("currentMD").style.display = "none";  
  		var el = dojo.byId(editLink);
  		var el2 = dojo.byId(editLink2);
  		if(el){
  			el.style.display = "none";
  			el2.style.display = "none";
  		}	
  	}else{
  		dojo.byId("originalMD").style.display = "none";
  		dojo.byId("currentMD").style.display = "inline";
// MKS 10/27/2011: comment out this if. Fixes original and current edition link problems:
// SPRS: JHES8MTLGK and JHES8MTLDG
  		// fix the original link
//  		if(qo.action){
  		//dojo.byId("view:_id1:_id72:_id80:originalModeLink").href = windowLink + "&mode=original"
//  		dojo.byId(oml).href = windowLink + "&mode=original"
//  		}
  		//var el = dojo.byId("view:_id1:_id72:editLink");
  		
  	}
  	}
	if(qo.action){
		revisionMapURL = "originalcontent?ReadViewEntries&outputformat=json";
	}else{
		revisionMapURL = "../originalcontent?ReadViewEntries&outputformat=json";
	}
 	if(qo.mode === "original"){
  			getRevisionMap();
  		}
}

function changeContentLinks(){

var oMode = dojo.byId("original_mode");
var cMode = dojo.byId("current_mode");
var currLink = windowLink.split("?openDocument&mode");
// determine if this is the original version of the article
var temp = getResTitle(currLink[0].toLowerCase());
var test = linkList[temp];
 if(test){
        	//links[i].href = "../page.xsp?documentId=" + temp + "&action=openDocument";
        	omode.href = "../page.xsp?documentId=" + temp + "&action=openDocument";
        	
        }

}
    
function buildOrigLinkList(){
	
    for(var i = 0; i < revisionMap.viewentry.length; i++){
    	linkList[revisionMap.viewentry[i].entrydata[2].text[0]] = revisionMap.viewentry[i]["@unid"];    
	}
}

function scrubTopicLinks(link){
	//var wikiDocDiv = dojo.byId('wikiDoc');
    //var links = wikiDocDiv.getElementsByTagName("a");
    //var links
    if(link){
    	links = new Array(link);
    }else{
    	links = dojo.query("#wikiDoc a")
    }
    for(var i=0; i<links.length; i++){
    	var resTitle = getResTitle(links[i]);
        //var originalRevLink = revisionMap[resTitle];            
        var originalRevLink = linkList[resTitle];
        if(originalRevLink){
        	if(link){
        		return link = "../page.xsp?documentId=" + originalRevLink + "&action=openDocument";
        	}else{
        	var test = windowLink.search("page.xsp");
        		if(test === -1){        	
        			links[i].href = "../page.xsp?documentId=" + originalRevLink + "&action=openDocument";
        		}else{
        			links[i].href = "page.xsp?documentId=" + originalRevLink + "&action=openDocument";
        		}
        	}
        }
     }
 }
 
 function changeLinks(){
 	
 	var hostName = window.location.hostname
 	 var links = dojo.query("#wikiDoc a")
    for(var i=0; i<links.length; i++){
    	var oldLink = links[i].href;
    	var test = oldLink.search("page.xsp");
    	// look for the domain of the url
    	var urlDom = oldLink.split("/");
    	if (hostName === urlDom[2]){
    		if(test === -1){
    			links[i].href = oldLink + "?OpenDocument&mode=original";
    		}else{
    			links[i].href = oldLink + "&mode=original";
    		}
    	}
    }
 }

 function getResTitle(articleLink){
 	var resTitle;
    if(articleLink){
    	//resTitle = articleLink.id;
    	if(articleLink.href){
    	    var lastSlash = articleLink.href.lastIndexOf("/");
            resTitle = articleLink.href.substring(lastSlash + 1);
        }else{
           try{
        	  var lastSlash = articleLink.lastIndexOf("/");
              resTitle = articleLink.substring(lastSlash + 1);
           }catch(err){
           }
        }    
     }
        return resTitle;
 }

function getRevisionMap(link){
	var xhrArgs = {
            url: revisionMapURL,
            handleAs: "json",
            load: function(data) {
                 revisionMap = data;
                 buildOrigLinkList();
                 if(link){
                 scrubTopicLinks(link);
                 }else{
                 scrubTopicLinks();
                 changeLinks();
                 }
              //   changeContentLinks();
            },
            error: function(error) {
                alert("unable to obtain revisionMap object: " + http_request.status);
            }
        }

        //Call the asynchronous xhrGet
        var deferred = dojo.xhrGet(xhrArgs);

}

function getTOCLink(link){
	//check to see if we need to handle this link as a category navigation item
	//or as a product documentation navigation item
	//note: this is needed since the navTree control is being used in multiple places
	// MKS 12/9/2010: If topichead, just return.
	if(link===""){
		return;
	} else {
	if(link.indexOf("lookupName") >= 0){
		//handle as a category navigation
		window.location = "xpViewCategories.xsp?" + link;
	}
	else{
		//sniff for presence of "/dx/" on the URL
		//note: category pages don't have it, article pages do
		var prefix = "";
		if(window.location.href.indexOf("/dx/") < 0){
			prefix = "dx/";
		}
		
		//determine if original mode is set
		if(qo && qo.mode && qo.mode === "original"){
			if(revisionMap){
				var newlink = scrubTopicLinks(link);
				if(newlink){
					window.location = prefix + newlink + "&mode=original";
				}
				else{
					window.location = prefix + link + "?OpenDocument&mode=original";
				}
			}
		}
		else{
			window.location = prefix + link;
		}	
	}
	}
}
    
    
  
