/* TO DO LIST
(1) beautify slideshow (B1)
(2) use preloaded images (B0)
(3) notify user that image is loading (B2)
*/

//define global variables
var SLIDESHOW_TIMEOUT = 3000;
var IMAGEHOST, SUBDOMAIN, IMAGES = new Array();

addLoadEvent(preLoad);
addLoadEvent(prepareAnchors); //must be executed before addNavigation (IE conflict)
addLoadEvent(addNavigation);
addLoadEvent(excludeFolders);
addLoadEvent(loadFrame);
addLoadEvent(prepareContent);

function preLoad()
{ //assign values to globals and pre-load images
   IMAGEHOST = document.getElementById("navigationLinks").getElementsByTagName("a")[0].host.split(".");
   SUBDOMAIN = "_" + IMAGEHOST.shift();
   IMAGEHOST = "www." + IMAGEHOST.join("."); //e.g., "www.repucci.org"
   SUBDOMAIN = SUBDOMAIN.replace(/_www/,""); //e.g., "_michael"
   
   /* NEED TO FIGURE OUT AN INDEXING SYSTEM FOR SHOWIMAGE
   var images = document.getElementById("thumbnails").getElementsByTagName("img");
   for (var i=0, src; i<images.length; i++) {
      src = getElementsByClass("preview",images[i].parentNode.parentNode)[0];
      src = src?src.firstChild.nodeValue:images[i].parentNode.href;
      IMAGES[i] = new Image(); //loads image asynchronously
      IMAGES[i].src = src;
      IMAGES[i].alt = images[i].alt;
   }
   */
}

function addNavigation()
{ //add previous, next, and slideshow links
   var up = document.getElementById("navigationLinks").getElementsByTagName("a")[0];
   var images = document.getElementById("thumbnails").getElementsByTagName("img");
   
   //previous link
   var div1 = document.createTextNode("|");
   insertAfter(div1,up);
   var previous = document.createElement("a");
   previous.id = "previous";
   previous.href = "#previous";
   previous.onclick = function() {
      var currentImage = document.getElementById("pictureFrame");
      for (var i=0; i<images.length; i++)
         if (currentImage.alt==images[i].alt)
            break;
      showImage(images[(((i-1)%images.length)+images.length)%images.length].parentNode);
      return false;
   }
   previous.appendChild(document.createTextNode("previous"));
   insertAfter(previous,div1);
   
   //next link
   var div2 = document.createTextNode("|");
   insertAfter(div2,previous);
   var next = document.createElement("a");
   next.id = "next";
   next.href = "#next";
   next.onclick = function() {
      var currentImage = document.getElementById("pictureFrame");
      for (var i=0; i<images.length; i++)
         if (currentImage.alt==images[i].alt)
            break;
      showImage(images[(i+1)%images.length].parentNode);
      return false;
   }
   next.appendChild(document.createTextNode("next"));
   insertAfter(next,div2);
   
   //slideshow link
   var div3 = document.createTextNode("|");
   insertAfter(div3,next);
   var slideshow = document.createElement("a");
   slideshow.id = "slideshow";
   slideshow.href = "#slideshow";
   slideshow.onclick = function() {
      slideShow("-1");
      return false;
   }
   slideshow.appendChild(document.createTextNode("slideshow"));
   insertAfter(slideshow,div3);
}

function excludeFolders()
{ //excludes folder(s) from directory list by name
   var folderLinks = document.getElementById("folderLinks");
   if (folderLinks) {
      folderLinks = folderLinks.getElementsByTagName("a");
      var excluded = null;
      for (var i=0; i<folderLinks.length; i++)
         if (/excluded/i.test(folderLinks[i].href))
            excluded = folderLinks[i];
      if (excluded) {
         //span and exclude the link
         var span = document.createElement("span");
         span.id = "excluded";
         span.appendChild(excluded.previousSibling); //error if text node is appended second
         span.appendChild(excluded);
         document.getElementById("folderLinks").insertBefore(span,document.getElementById("folderLinks").lastChild);
         
         //add hover action
         var hotspot = document.getElementById("navigation");
         hotspot.onmouseover = function() {
            span.style.display = "inline";
         }
         hotspot.onmouseout = function() {
            span.style.display = "none";
         }
      }
   }
}

function loadFrame()
{ //add a "picture frame" to the DOM in which images are displayed and a place for image notes
   var pictureLink = document.createElement("a");
   pictureLink.id = "pictureLink";
   
   var pictureFrame = document.createElement("img");
   pictureFrame.id = "pictureFrame";
   pictureLink.appendChild(pictureFrame);
   
   var imageNotes = document.createElement("div");
   imageNotes.id = "imageNotes";
   
   var thumbnails = document.getElementById("thumbnails");
   thumbnails.parentNode.insertBefore(pictureLink,thumbnails);
   thumbnails.parentNode.insertBefore(imageNotes,thumbnails);
   
   showImage(document.getElementById("thumbnails").getElementsByTagName("a")[0]);
}

function prepareContent()
{ //adjusts content notice and thumbnail links
   document.getElementById("javascriptNotice").style.display = "none";
   
   var thumbLinks = document.getElementById("thumbnails").getElementsByTagName("a");
   for (var i=0; i<thumbLinks.length; i++)
      thumbLinks[i].onclick = function() {
         showImage(this);
         return false;
      }
}

function showImage(imageLink)
{ //shows selected image with notes
   var src = getElementsByClass("preview",imageLink.parentNode)[0];
   src = src?src.firstChild.nodeValue:imageLink.href;
   var pictureFrame = document.getElementById("pictureFrame");
   pictureFrame.src = src; //loads image asynchronously
   pictureFrame.alt = imageLink.firstChild.alt;
   
   document.getElementById("pictureLink").href = imageLink.href;
   var imageNotes = document.getElementById("imageNotes");
   var newNotes = imageLink.nextSibling.cloneNode(true);
   newNotes.id = "imageNotes";
   imageNotes.parentNode.replaceChild(newNotes,imageNotes);
   imageLink.blur();
}

function slideShow(i)
{ //runs a slideshow of the available images
   var slideshow = document.getElementById("slideshow");
   if (i==-1) { //start or abort
      if (slideshow.ID) {
         setTimeout("slideShow('-2')",0);
      } else {
         /* IMPROVE SLIDESHOW VIA CROSS-BROWSER FIXES, MAKE STOP BUTTON/LINK, POSSIBLY WINDOW.OPEN, USER-DEFINED INTERVAL
         if (window.navigator.appName=="Netscape")
         { //IE uses padding to calculate image width and height and thus poorly renders the pop-out slideshow
            var pictureFrame = document.getElementById("pictureFrame");
            pictureFrame.style.display = "block";
            pictureFrame.style.position = "fixed";
            pictureFrame.style.top = "0px";
            pictureFrame.style.left = "0px";
            pictureFrame.style.padding = "130px 100% 100% 200px";
            pictureFrame.style.background = "#999999";
            pictureFrame.style.zIndex = "1";
            slideshow.style.zIndex = "2";
         }
         */
         
         slideshow.firstChild.nodeValue = "stop";
         slideshow.ID = setTimeout("slideShow(0)",SLIDESHOW_TIMEOUT);
      }
   } else if (i==-2) { //terminate
      /*
      var pictureFrame = document.getElementById("pictureFrame");
      pictureFrame.style.display = "inline";
      pictureFrame.style.position = "static";
      pictureFrame.style.top = "0px";
      pictureFrame.style.left = "0px";
      pictureFrame.style.padding = "10px";
      pictureFrame.style.background = "";
      */
      
      slideshow.firstChild.nodeValue = "slideshow";
      clearTimeout(slideshow.ID);
      slideshow.ID = null;
   } else {
      var images = document.getElementById("thumbnails").getElementsByTagName("img");
      if (i<images.length) {
         showImage(images[i].parentNode);
         slideshow.ID = setTimeout("slideShow(" + (i+1) + ")",SLIDESHOW_TIMEOUT);
      } else {
         setTimeout("slideShow('-2')",0);
      }
   }
}
