// JavaScript Document

// This function controls all the others when the page Loads
//=====================================================================

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
};



// This function highlights the page for Positional Awareness 
//============================================================

function highlightPage() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("horNav")) return false;
  var nav = document.getElementById("horNav");
  var links = nav.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    var currenturl = window.location.href;
    if (currenturl.indexOf(linkurl) != -1) {
      links[i].className = "mark";
      var linktext = links[i].lastChild.nodeValue.toLowerCase();
      document.body.setAttribute("id",linktext);
    }
  }
}

function showPic(whichpic) {
  if (!document.getElementById("placeholder")) return false;
  var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  if (!document.getElementById("describe")) return false;
  if (whichpic.getAttribute("title")) {
    var text = whichpic.getAttribute("title");
  } else {
    var text = "";
  }
  var description = document.getElementById("describe");
  if (description.firstChild.nodeType == 3) {
    description.firstChild.nodeValue = text;
  }
  return false;
}

function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("imagegallery")) return false;
  var gallery = document.getElementById("imagegallery");
  var links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
    links[i].onclick = function() {
      return showPic(this);
	}
    links[i].onkeypress = links[i].onclick;
  }
}


//General Purpose function for inserting mark up on the fly
//==============================================================

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}


addLoadListener(highlightPage);
addLoadListener(prepareGallery);
//addLoadListener(display);



//function showPic(whichpic) {
//  if (!document.getElementById("placeholder")) return false;
//  var source = whichpic.getAttribute("href");
//  var placeholder = document.getElementById("placeholder");
//  placeholder.setAttribute("src",source);
//  if (!document.getElementById("description")) return false;
//  if (whichpic.getAttribute("title")) {
//    var text = whichpic.getAttribute("title");
//  } else {
//    var text = "";
//  }
//  var description = document.getElementById("description");
//  if (description.firstChild.nodeType == 3) {
//    description.firstChild.nodeValue = text;
//  }
//  return false;
//}
//
//function prepareGallery() {
//  if (!document.getElementsByTagName) return false;
//  if (!document.getElementById) return false;
//  if (!document.getElementById("imagegallery")) return false;
//  var gallery = document.getElementById("imagegallery");
//  var links = gallery.getElementsByTagName("a");
//  for ( var i=0; i < links.length; i++) {
//    links[i].onclick = function() {
//      return showPic(this);
//	}
//    links[i].onkeypress = links[i].onclick;
//  }
//}

//function prepareGallery() {
//  if (!document.getElementsByTagName) return false;
//  if (!document.getElementById) return false;
//  if (!document.getElementById("imagegallery")) return false;
//  var gallery = document.getElementById("imagegallery");
//  var links = gallery.getElementsByTagName("a");
//  for ( var i=0; i < links.length; i++) {
//    links[i].onclick = function() {
//      alert(showPic(this)); //make sure you're getting a 'false'
//      return false; //stop the default link behavior
//    };
//    links[i].onkeypress = links[i].onclick;
//  }
//}



//function addLoadEvent(func) {
//  var oldonload = window.onload;
//  if (typeof window.onload != 'function') {
//    window.onload = func;
//  } else {
//    window.onload = function() {
//      oldonload();
//      func();
//    }
//  }
//}
//
//addLoadEvent(prepareGallery);
