function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function insertBefore(newElement,targetElement) {
  var parent = targetElement.parentNode;
  parent.insertBefore(newElement,targetElement);  
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}

/* elements which we need globally */
 theCount = 0; 
 theState = 'play';
 firstThumb = 0; 
 thumbNumber = 3; 


function showPic(whichpic) {

//  if (!document.getElementById("placeholder")) return true;
  
  var galleryContent = document.getElementById("galleryContent");   
  
  /* we delete the children */
  if (document.getElementById("galleryLink")) { 
    var galleryLinkOld = document.getElementById("galleryLink"); 
    galleryContent.removeChild(galleryLinkOld); 
  }
  if (document.getElementById("placeholder")) { 
    var placeholderOld = document.getElementById("placeholder");   
    galleryContent.removeChild(placeholderOld); 
  }
  
  /* we check if there's a link specified*/
   if (whichpic.getElementsByTagName("span").length > 0) {
    var span = whichpic.getElementsByTagName("span");
  if (span[0].firstChild.nodeType == 3) {
    var imgLink = span[0].firstChild.nodeValue;
   
  /* if there's a link we create the element */
  var galleryLink = document.createElement("a");
  galleryLink.setAttribute("href",imgLink);
  galleryLink.setAttribute("id","galleryLink");  
  galleryContent.appendChild(galleryLink);
   }
  } 
  
  /* we create the image element */
  var source = whichpic.getAttribute("href");
  var name = whichpic.getAttribute("id"); 
  var name = parseInt(name);  
  var alt = whichpic.getAttribute("name");
  var placeholder = document.createElement("img");
  placeholder.setAttribute("id","placeholder");   
  placeholder.setAttribute("src",source);
//  placeholder.setAttribute("name",name);
  placeholder.setAttribute("alt",alt);

 /* if a link is specified we put the image into the link */
  if (document.getElementById("galleryLink")) {
  galleryLink.appendChild(placeholder);
  } else {
  galleryContent.appendChild(placeholder);
  }
 
 if (name == links.length-1)  {
 theCount = 0
 } else {
 theCount = name+1; 
 }  
 
  return false;
}


/* function preparePlaceholder() {
  if (!document.createElement) return false;
  if (!document.createTextNode) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("minigallery")) return false;
  var placeholder = document.createElement("img");
  placeholder.setAttribute("id","placeholder");
  placeholder.setAttribute("src","images/placeholder.gif");
  placeholder.setAttribute("alt","my image gallery");
  var gallerytext = document.createElement("p");
  gallerytext.setAttribute("id","gallerytext");
  var desctext = document.createTextNode("Choose an image");
  gallerytext.appendChild(desctext);
  var gallery = document.getElementById("minigallery");
  insertBefore(placeholder,gallery);  
  insertAfter(gallerytext,placeholder);
} */




function loopGallery () {
 if ((theState != 'play')) return false;
 showPic(links[theCount]); 
 setTimeout("loopGallery()",5500);  
}


function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("minigallery")) return false;
  thisGallery = document.getElementById("minigallery");
  links = thisGallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
  // if (i == 0) { showPic(links[i]);  }  
   links[i].onclick = function() {
      stopLoop();  
      return showPic(this);
    }
  }
  
  if (links.length > thumbNumber) {
      document.getElementById("thumbsNext").disabled = "";  
      document.getElementById("thumbsNextImg").setAttribute("src","images/icon_next.gif"); 
   var thumbLimit = thumbNumber;           
   } else { 
   var thumbLimit = links.length;  
    }
  
  for ( var i=0; i < thumbLimit; i++) {
  links[i].parentNode.style.display = "block";   
  } 
  
  /* Let's set the mouse-over setting for the main image */
   var galleryContent = document.getElementById("galleryContent");
   galleryContent.onmouseover = function() {
      stopLoop();  
    } 
  
}

function stopLoop() {
theState = 'stop';
}

function playLoop() {
theState = 'play';

loopGallery(); 
}


function previousPic() {
theState = 'stop';

/* the count always indexes the NEXT pic so to go back we need to deduct -2 */
   if (links.length == 1) {
   return false; 
   } else if (theCount == 1) {
    theCount = links.length-1;
  } else if (theCount == 0) {
    theCount = links.length-2;  
  } else {
    theCount = theCount-2;
  }

showPic(links[theCount]);
}


function nextPic() {
 theState = 'stop';

/* the Count indexes the NEXT pic so we just keep moving forward */
showPic(links[theCount]);

}


function nextThumb() {
 if ((firstThumb+thumbNumber) > links.length-1) {
   return false; 
 } else {
   links[firstThumb].parentNode.style.display = "none"; 
   links[firstThumb+thumbNumber].parentNode.style.display = "block"; 
   firstThumb++;

   if ((firstThumb+thumbNumber) > links.length-1) {
      document.getElementById("thumbsNext").disabled = "disabled";
      document.getElementById("thumbsNextImg").setAttribute("src","images/icon_next_disabled.gif"); 
   }
      document.getElementById("thumbsPrevious").disabled = "";
      document.getElementById("thumbsPreviousImg").setAttribute("src","images/icon_previous.gif");      
 }
}


function previousThumb() {
 if (firstThumb == 0) {
   return false; 
 } else {
   links[firstThumb+thumbNumber-1].parentNode.style.display = "none";  
   links[firstThumb-1].parentNode.style.display = "block";  
   firstThumb--; 

   if (firstThumb == 0) {
      document.getElementById("thumbsPrevious").disabled = "disabled";
      document.getElementById("thumbsPreviousImg").setAttribute("src","images/icon_previous_disabled.gif");       
   } 
      document.getElementById("thumbsNext").disabled = "";  
       document.getElementById("thumbsNextImg").setAttribute("src","images/icon_next.gif");      
 }
}

// addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);
addLoadEvent(loopGallery);

