/****************** Navigation App Javascript File ****************
 Author: Carl Pryke
 Date: 22/07/2011
 Version: 1.0 
 Description: This defines a navigation hierachy, generating a 
 
 Notes: This could be extended to make hard-coded arrays of navigation names and links
 to be read in instead from an XML file.
 
 Need to read in URL's for page links. 
 
 Need to have a div id for innerHTML code.

 *************************************************************/ 

function drawPosition(target, url, divides){
    var heirachyNames = url.substr(7).split("/");
    var correctedNames = url.substr(7).split("/");
    var bit = new Array();
    var prefix = new Array();
    for(var i=0; i<heirachyNames.length; i++){
        prefix = heirachyNames[i].split(".");
        heirachyNames[i] = prefix[0];
        prefix = correctedNames[i].split(".");
        correctedNames[i] = prefix[0];
        if(heirachyNames[i].charAt(heirachyNames[i].length-1) == "2"){
            bit.push("2");
            heirachyNames[i] = heirachyNames[i].substr(0, heirachyNames[i].length-1);
            correctedNames[i] = correctedNames[i].substr(0, correctedNames[i].length-1);
        }
        else{
            bit.push("");
        }
    }
    var HTMLString = "";
    HTMLString += "<div class=\"heirachyStart\">You are here: </div>";
    heirachyNames.shift();
    correctedNames.shift();
    bit.shift();
    correctedNames = convertToLocations(correctedNames);
    for(var i=0; i<heirachyNames.length; i++){
        var x = "";
        for(var j=(i+1); j<heirachyNames.length; j++){
            x += "../";
        }
        x += heirachyNames[i] + bit[i] +".html";
        if(correctedNames[i] == "Index"){
            correctedNames[i] = "Home";
        }
        HTMLString += "<div class=\"heirachyElement\"><a href=\"" + x + "\">" + correctedNames[i] + "</a></div>";
        if(divides && i<(heirachyNames.length-1)){
            HTMLString += "<div class=\"heirachyDivision\">></div>";
        }
    }
    document.getElementById(target).innerHTML = HTMLString;
}

function convertToLocations(heirachyNames){
    for(var i=0; i<heirachyNames.length; i++){
        heirachyNames[i] = capitaliseElement(heirachyNames[i]);
    }
    return heirachyNames;
}

function capitaliseElement(element){
    var words = element.split("_");
    var newName = "";
    for(var i=0; i<words.length; i++){
        var prefix = words[i].split(".");
        words[i] = prefix[0];
        words[i] = words[i].substr(0,1).toUpperCase() + words[i].substr(1);
        if(words[i] == "And"){
            words[i] = "&amp;";
        }
        newName += words[i] + " ";
    }
    return newName.substr(0, (newName.length-1));
}
