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

	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("covers")) return false;


	var gallery = document.getElementById("covers");
	var links = gallery.getElementsByTagName("a");

			for ( var i=0; i < links.length; i++) {

				links[i].onclick = function() {

		      	return showPic(this);

		    	}
		  	} // end for loop
} // end function


function showPic (whichpic) {

	if (!document.getElementById) return false;
	
	var picId = document.getElementById('slideimage');
	var pHolder = document.getElementById('placeholder');
	
	if (picId.parentNode) {
		
		picId.parentNode.removeChild(picId);
		
	}
	
	pHolder.className = whichpic.className;
	
	var newPic = document.createElement('img');
	newPic.id = "slideimage";
	newPic.src= whichpic.href;
	newPic.alt = whichpic.title;
	
	pHolder.appendChild(newPic);
	

	var gallery = document.getElementById("covers");
	var links = gallery.getElementsByTagName('a') ;
	var classes = links.className;

		for ( var i=0; i < links.length; i++) {

			if (links[i] == whichpic) {
				
				links[i].className = links[i].className.replace(/\b ?current\b/, '');
				links[i].className += ' current';

		    }else{
					links[i].className = links[i].className.replace(/\b ?current\b/, '');
			}



} // end for loop

return false;

} // end function


function showPage(element) {
	if (!document.getElementById) return false;
	
	var p1 = document.getElementById('p1');
	var page1 = document.getElementById('page1');
		
	var p2 = document.getElementById('p2');
	var page2 = document.getElementById('page2');
	
	var p3 = document.getElementById('p3');
	var page3 = document.getElementById('page3');
	
	var paginate = document.getElementById('paginate');
	var secondaryContent = document.getElementById('secondaryContent');
	var primaryContent = document.getElementById('primaryContent');
	
	/* select the first link in the page1 id and make the link a current class */
	var links = page1.getElementsByTagName('a');
	var link = links[0];
	link.className = 'current';
	
	/* setup a normal page view remove full class and show hidden elements */
	primaryContent.className = primaryContent.className.replace(/\b ?full\b/, '');
	secondaryContent.style.display = 'block';
	paginate.style.display = 'block';
	
	/* hide page 2 & 3 as we want pagination in place */
	if (page2 != null) {
		page2.style.display = 'none';
	}
	if (page3 != null) {
		page3.style.display = 'none';
	}
	//page3.style.display = 'none';
	
	p1.onclick = function() {
	
		p1.className = p1.className.replace(/\b ?current\b/, '');
		p2.className = p2.className.replace(/\b ?current\b/, '');
		p3.className = p3.className.replace(/\b ?current\b/, '');
		p1.className += ' current';
		
		
  		page1.style.display = 'block';
		if (page2) {
		page2.style.display = 'none';
		}
		if (page3) {
		page3.style.display = 'none';
		}
	}
	
	p2.onclick = function() {
	
		
		p2.className = p2.className.replace(/\b ?current\b/, '');
		p3.className = p3.className.replace(/\b ?current\b/, '');
		p1.className = p1.className.replace(/\b ?current\b/, '');
		p2.className += ' current';
		
		if (page2) {
  		page2.style.display = 'block';
		}
		if (page3) {
		page3.style.display = 'none';
		}
		page1.style.display = 'none';

	}
	
	p3.onclick = function() {
	
		
		p3.className = p3.className.replace(/\b ?current\b/, '');
		p1.className = p1.className.replace(/\b ?current\b/, '');
		p2.className = p2.className.replace(/\b ?current\b/, '');
		p3.className += ' current';
		
  		if (page3) {
		page3.style.display = 'block';
		}
		page1.style.display = 'none';
		
		if (page2) {
		page2.style.display = 'none';
		}

	}

}


addLoadEvent(prepareGallery);
addLoadEvent(showPage);