function onLoadEvents(){
  prepareAlbumSelect();
  prepareFrame();
  prepareAlbum();
}

/**
* Function to set the album selection to show or hide albums on request
*/
function prepareAlbumSelect(){
  if (document.getElementsByTagName && document.getElementById && document.getElementById("albumSelect")){
    var nav = document.getElementById("albumSelect");
    var links = nav.getElementsByTagName("a");
    for (var i=0; i<links.length; i++ ) {
      var albumId = links[i].getAttribute("href").split("#")[1];
      if (document.getElementById(albumId)){
        document.getElementById(albumId).style.display = "none";
        links[i].destination = albumId;
        links[i].onclick = function(){
          showAlbum(this.destination);
          return false;
        }
        
        var album = document.getElementById(albumId);
        var albumTitle = album.getElementsByTagName("h3");
        for (var j=0; j<albumTitle.length; j++){
          albumTitle[j].style.display = "none";
        }
       
        var albumDesc = album.getElementsByTagName("p");
        for (var j=0; j<albumTitle.length; j++){
          albumDesc[j].style.display = "none";
        }        
      }
    }
  } else{
    return false;
  }
}

/**
* Function which determines whether or not to display each album
*/
function showAlbum(id) {
  var divs = document.getElementsByTagName("div");
  for (var i=0; i<divs.length; i++ ) {
    if (divs[i].className.indexOf("photos") != -1){
      if (divs[i].getAttribute("id") == id){
        divs[i].title = "selected";
        
        var albumTitle = document.getElementById("albumTitle");
        var selectedAlbumTitle = divs[i].getElementsByTagName("h3");
        if (albumTitle.firstChild.nodeType == 3){
            albumTitle.firstChild.nodeValue = selectedAlbumTitle[0].firstChild.nodeValue;
        }
        
        var albumDesc = document.getElementById("albumDescription");
        var selectedAlbumDesc = divs[i].getElementsByTagName("p");
        if (albumDesc.firstChild.nodeType == 3){
            albumDesc.firstChild.nodeValue = selectedAlbumDesc[0].firstChild.nodeValue;
        }
        
        var albumPhotos = divs[i].getElementsByTagName("a");
        var source = albumPhotos[0].getAttribute("href");
        var frame = document.getElementById("photoFrame");
        frame.setAttribute("src", source);
    
        var photoTitle = albumPhotos[0].getAttribute("title");
        var photoText = document.getElementById("photoText");
        if (photoText.firstChild.nodeType == 3){
          photoText.firstChild.nodeValue = photoTitle;
        }
        
        var albumFrame = document.getElementById("albumFrame");
        var allLinks = albumFrame.getElementsByTagName("a");
        if (allLinks.length == 2){
            var previousPhoto = allLinks[0];
            var nextPhoto = allLinks[1];
        }
 
        previousPhoto.href = albumPhotos[0].getAttribute("href");
        previousPhoto.title = albumPhotos[0].getAttribute("title");
        previousPhoto.id = albumPhotos[0].getAttribute("id");
        if (previousPhoto.firstChild.nodeType == 3){
          previousPhoto.firstChild.nodeValue = "Previous Photo";
        }
  
        nextPhoto.href = albumPhotos[1].getAttribute("href");
        nextPhoto.title = albumPhotos[1].getAttribute("title");
        nextPhoto.id = albumPhotos[1].getAttribute("id");        
        if (nextPhoto.firstChild.nodeType == 3){
          nextPhoto.firstChild.nodeValue = "Next Photo";
        }

        divs[i].style.display = "block";
        
      } else{
        divs[i].title = "unSelected";
        divs[i].style.display = "none";
      }
    }
  }
}

/**
* Function to set up the photo frame to display the jpg
*/
function prepareFrame() {
  if (document.createElement || document.createTextNode){

    //Create the photo album
    var divAlbum = document.createElement("div");
    divAlbum.setAttribute("id", "albumFrame");
    
    //Create the Album Title and description
    var albumTitle = document.createElement("h2");
    albumTitle.setAttribute("id", "albumTitle");
    var albumTitleText = document.createTextNode("Please select an album from the list above");
    albumTitle.appendChild(albumTitleText);
    
    var albumDescription = document.createElement("p");
    albumDescription.setAttribute("id", "albumDescription");
    var albumDescriptionText = document.createTextNode("Album Description");
    albumDescription.appendChild(albumDescriptionText);
    
    //append the title to the album
    divAlbum.appendChild(albumTitle);
    divAlbum.appendChild(albumDescription);

    //create the photo frame
    var divFrame = document.createElement("div");
    divFrame.setAttribute("id", "photoDiv");
    
    //create the previous link
    var previousPhoto = document.createElement("a");
    previousPhoto.setAttribute("id", "previousPhoto");
    previousPhoto.setAttribute("name", "previousPhoto");
    var previousPhotoText = document.createTextNode("");
    previousPhoto.appendChild(previousPhotoText);
    previousPhoto.onclick = function() {
      return display(this);
    }

    //create the next link
    var nextPhoto = document.createElement("a");
    nextPhoto.setAttribute("id", "nextPhoto");
    nextPhoto.setAttribute("name", "nextPhoto");
    var nextPhotoText = document.createTextNode("")
    nextPhoto.appendChild(nextPhotoText);
    nextPhoto.onclick = function() {
      return display(this);
    }
    
    //append the forward and back buttons to the frame
    divFrame.appendChild(previousPhoto);
    divFrame.appendChild(nextPhoto);

    //create the jpg Holder and photo text
    var frame = document.createElement("img");
    frame.setAttribute("id","photoFrame");
    frame.setAttribute("src","../image/information/blank.gif");
    frame.setAttribute("alt","photo Album");
    
    var photoText = document.createElement("p");
    photoText.setAttribute("id", "photoText");
    var photoDefaultText = document.createTextNode("Photo Description");//MRB Text to be removed
    photoText.appendChild(photoDefaultText);
    
    //append photo text and jpg to the frame
    divFrame.appendChild(photoText);
    divFrame.appendChild(frame);
    
    //append the frame to the album
    divAlbum.appendChild(divFrame);

    var albumSelect = document.getElementById("albumSelect");
    insertAfter(divAlbum, albumSelect)
  }
}

/**
* Function to create element after chosen element - this is not standard so have to import this utility function
*/
function insertAfter(newElement,targetElement) { 
  var parent = targetElement.parentNode; 
  if (parent.lastChild == targetElement) { 
    parent.appendChild(newElement); 
  } else { 
    parent.insertBefore(newElement,targetElement.nextSibling); 
  } 
}

/**
* Function that replaces the anchor link and gets it to display in the photo album (created earlier)
*/
function prepareAlbum() {
  if (document.getElementsByTagName || document.getElementById || document.getElementById("photoAlbum")){
    var divs = document.getElementsByTagName("div");
    for (var i=0; i<divs.length; i++ ) {
      if (divs[i].className.indexOf("photos") != -1){
        var links = divs[i].getElementsByTagName("a");
        for ( var j=0; j < links.length; j++) {
          links[j].onclick = function() {
            return display(this);
          }
        }
      }
    }
  } else {
    return false;
  }
}

function display(photo) {
  if (document.getElementById){
    var source = photo.getAttribute("href");
    var frame = document.getElementById("photoFrame");
    frame.setAttribute("src", source);
    
    var photoTitle = photo.getAttribute("title");
    var photoText = document.getElementById("photoText");
    if (photoText.firstChild.nodeType == 3){
      photoText.firstChild.nodeValue = photoTitle;
    }

    var albumFrame = document.getElementById("albumFrame");
    var allLinks = albumFrame.getElementsByTagName("a");
    
    if (allLinks.length == 2){
        var previousPhoto = allLinks[0];
        var nextPhoto = allLinks[1];
    }
           
    var divs = document.getElementsByTagName("div");
    for (var i=0; i<divs.length; i++ ) {
      if (divs[i].getAttribute("title") == "selected"){
        var links = divs[i].getElementsByTagName("a");
        for ( var j=0; j < links.length; j++) {
          if (photo.getAttribute("id") == links[j].getAttribute("id")){
            if(j==0){
              var prevPhotoHref = links[0];
              var nextPhotoHref = links[j+1];
            } else if(j==links.length-1){
              var prevPhotoHref = links[j-1]
              var nextPhotoHref = links[j];
            } else{
              var prevPhotoHref = links[j-1];
              var nextPhotoHref = links[j+1];
            }
          }
        }
      } 
    }

    previousPhoto.href = prevPhotoHref.getAttribute("href");
    previousPhoto.title = prevPhotoHref.getAttribute("title");
    previousPhoto.id = prevPhotoHref.getAttribute("id");

    nextPhoto.href = nextPhotoHref.getAttribute("href");
    nextPhoto.title = nextPhotoHref.getAttribute("title");
    nextPhoto.id = nextPhotoHref.getAttribute("id");

    return false;
  } else {
    return true;
  }
}

window.onload = onLoadEvents;