var gLinks;
var activePic;

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareGallery() {
	
	activePic = 0;
	
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("small_thumbs")) return false;
  
  var gallery = document.getElementById("small_thumbs");
  var links = gallery.getElementsByTagName("a");
  //set to the global var for kicks
  gLinks = links;

  for ( var i=0; i < links.length; i++) {

    links[i].onclick = function() {
  	  return showPic(this);
	}
	   links[i].onkeypress = links[i].onclick;
	
  }
  

 
}


function showPic(whichpic) {
	

  if (!document.getElementById("placeholder")) return true;
  var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
	activePic = whichpic.getAttribute("id"); 	


 
  return false;
}

function setButtons () {
	//next button
	var next = document.getElementById("next");
   	next.onclick = function() { 
  		
   			nextPic = parseInt(activePic);
				if (activePic >= gLinks.length) {
					nextPic = 0;	
					}
					
					
   	return showPic(gLinks[nextPic]);
	return activePic ++;
		alert("active pic is" + activePic);	
	}
	
	//previous button
	var prev = document.getElementById("prev");
	 	prev.onclick = function() { 
	
  			lastPic = parseInt(activePic) - 2;
				if (lastPic < 0 ) {
					lastPic = gLinks.length -1;	
					}
									
    return showPic(gLinks[lastPic]);
	return activePic= activePic - 1;

	}

}

addLoadEvent(setButtons);
addLoadEvent(prepareGallery);
