﻿//DECLARE THE VARIABLES
    var fileext = ".aspx";
    var numnavItems = 5;
    var sPage = "default";
    var sSubfolder = 0;



//This function runs each time the page is loaded
function loadPage() {
    // 1 - Set the tab in the menu to be selected
    setActiveTabItem()
}

//This function allows li items to behave as links and change the page
function selectPage(sSubfolder, sPage) {   
    
    var sPath = window.location.pathname; //The full path
    var sLocation1 = sPath.substring(0, sPath.lastIndexOf('/')); //Cut the filename off the end
    var sLocation2 = sLocation1.substring(0, sLocation1.lastIndexOf('/')); //Cut the subfolder off the end
    var sChildfolder = sLocation1.substring(sLocation1.lastIndexOf('/') + 1); //Get the subfolder name
    
    //Check if the target page is in a subfolder
    if (sSubfolder == '0') {
        if (sChildfolder == "cp") {  //NEED AN IF TO CATER FOR ALL POSSIBLE SUBFOLDERS
            //Go back to the root and then open the page - check for Buffalypso for local viewing
            if (sLocation1 == "/Buffalypso") {
                window.location = sChildfolder + '/' + sPage + fileext;
            } else {
                window.location = sLocation2 + '/' + sPage + fileext;
            }
        } else {
            //If not a subfolder, simply go to the page
            window.location = sPage + fileext;
        }
     } else {
        //Check if the target subfolder is the same as the current folder
        if (sChildfolder == sSubfolder) {
            //If it is the same, just go to the file(reload it)
            window.location = sPage + fileext;
        } else {
            //If its different, go to the new subfolder and open the page - check for Buffalypso for local viewing
            if (sLocation1 == "/Buffalypso") {
                window.location = sSubfolder + '/' + sPage + fileext;
            } else {
                window.location = sLocation1 + '/' + sSubfolder + '/' + sPage + fileext;
            }
        }
     }
}

// This function checks for the current page and sets the class for the menu item
function setActiveTabItem() {
    //Get the filename of the current page
    var sPath = window.location.pathname;
    var sFile = sPath.substring(sPath.lastIndexOf('/') + 1);
    //Capture subpages by checking for a hyphen
    if (sFile.lastIndexOf('-') > 0) {
    var sSubFile = sFile.substring((sFile.lastIndexOf('-')), 0);
    sPage = sSubFile;
    } else {
    sPage = sFile.substring(0, sFile.lastIndexOf(fileext));
    }
    if (sPage == "") {
        sPage = "default";
    }
    //Set the tab once the page type has been defined
    setActiveTab(sPage);
}

//This function uses the page name to clear all selections and then set the class of the curent page to active
function setActiveTab(sPage) {
     //Set the current page tab to be active
        var currTabElem = document.getElementById(sPage);
        currTabElem.setAttribute("class", "active");
        currTabElem.setAttribute("className", "active");

    return;
} 


//This function changes the style of the menu item when it is hovered.
function menuHover(id) {
    var sPath = window.location.pathname; //The full path
    var sLocation1 = sPath.substring(0, sPath.lastIndexOf('/')); //Cut the filename off the end
    var sLocation2 = sLocation1.substring(0, sLocation1.lastIndexOf('/')); //Cut the subfolder off the end
    var sChildfolder = sLocation1.substring(sLocation1.lastIndexOf('/') + 1); //Get the subfolder name
    
    if (sChildfolder == "cp")
    {
        document.getElementById(id).style.background = 'url(../images/nav-hover.gif)';
    } else {
        document.getElementById(id).style.background = 'url(images/nav-hover.gif)';
    }
    
    document.getElementById(id).style.borderLeft='1px solid #FFFFFF';
    document.getElementById(id).style.width='98px';
    document.getElementById(id).style.borderRight='1px solid #FFFFFF'; 
    document.getElementById(id).style.cursor='hand';
}

//This function changes the style of the menu item ack to its normal state once the hover is over.
function menuHoverOut(id) {
    var sPath = window.location.pathname; //The full path
    var sLocation1 = sPath.substring(0, sPath.lastIndexOf('/')); //Cut the filename off the end
    var sLocation2 = sLocation1.substring(0, sLocation1.lastIndexOf('/')); //Cut the subfolder off the end
    var sChildfolder = sLocation1.substring(sLocation1.lastIndexOf('/') + 1); //Get the subfolder name
    
    
    if (document.getElementById(id).className == "active") {
    //Set it back to the active style
        if (sChildfolder == "cp")
        {
            document.getElementById(id).style.background = 'url(../images/nav-active.gif)';
        } else {
            document.getElementById(id).style.background = 'url(images/nav-active.gif)';
        }
        document.getElementById(id).style.borderLeft='1px solid #FFFFFF'; 
        document.getElementById(id).style.width='98px'; 
        document.getElementById(id).style.borderRight='1px solid #FFFFFF'; 
        document.getElementById(id).style.cursor='pointer';
    }else{
    //Set it back to the normal style
        if (sChildfolder == "cp")
        {
            document.getElementById(id).style.background = 'url(../images/nav-background.gif)';
        } else {
            document.getElementById(id).style.background = 'url(images/nav-background.gif)';
        }
        document.getElementById(id).style.borderLeft='0px'; 
        document.getElementById(id).style.width='100px'; 
        document.getElementById(id).style.borderRight='0px'; 
        document.getElementById(id).style.cursor='pointer';
    }
}



//NOTHING BELOW THIS LINE IS REQUIRED



//Function to load different Pages
var xmlhttp;
var pageurl = "";
var loadingPage = '<div align="center" style="padding-top:100px;"><img src="images/loading.gif" /></div>';


function loadPageContent(url)
{
//start the loading page
//document.getElementById('mainPageContent').innerHTML = loadingPage;
//set the menu item to be selected
setActiveTab(url);

//Define the page to be loaded
pageurl = pageitem[url] + fileext;

xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET",pageurl,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('mainPageContent').innerHTML=xmlhttp.responseText;
    
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}

function changeLogo(logofile) {
//look for the logo tag and then apply a new image
    var logoImage = document.getElementById("logoimage");
    logoImage.setAttribute("src", logofile);
    
  return;
}

function menuHoverOver(id) {

}

function menuHoverOver(id) {

}