function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function moveSlideshow(elementID,final_y,final_x,interval) {
    if (!document.getElementById) return false;
    
    // if the element does not exist we have nothing to do
    if (!document.getElementById(elementID)) return false;
    var elem = document.getElementById(elementID);
    
    // the slideshow events stack up and the animation is not smooth anymore
    if (elem.movement) {
        clearTimeout(elem.movement);
    }
    
    // current slideshow position
    var xpos = parseInt(elem.style.left);
    var ypos = parseInt(elem.style.top);
    if (xpos == final_x && ypos == final_y) {
        return true;
    }
    
    // restrict moving to white area
    if (final_y <= -elem.max_y) {
        final_y = -elem.max_y;
    }
    if (final_y > 0) {
	    final_y = 0;
    }
    
    // animation bit (taken from the book DOM Scripting by Jeremy Keith)
    if (ypos < final_y) {
        var dist = Math.ceil((final_y - ypos)/10);
        ypos = ypos + dist;
    }
    if (ypos > final_y) {
        var dist = Math.ceil((ypos - final_y)/10);
        ypos = ypos - dist;
    }
  
    // again, restrict showing white area
    if (ypos <= -elem.max_y) {
	    ypos = -elem.max_y;
    }
    if (ypos > 0) {
	    ypos = 0;
    }
    
    // fix the elements position
    elem.style.left = xpos + "px";
    elem.style.top = ypos + "px";
    
    // and set up the event again after an interval
    var repeat = "moveSlideshow('"+elementID+"',"+final_y+","+final_x+","+interval+")";
    elem.movement = setTimeout(repeat,interval);
}

function prepareSlideshow() {
	// first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
	// Make sure the elements exist
  	if (!document.getElementById("slideshow")) return false;
  	var slideshow = document.getElementById("slideshow");
  	var wrapper = document.getElementById("slideshow_wrapper");
  	wrapper.style.overflow = "hidden";
  	
  	// prepare the navigation bit we will be using
  	// left
  	var navigation = document.createElement("ul");
  	navigation.setAttribute("id", "navigation");
  	var li = document.createElement("li");
  	var scroll_up = document.createElement("a");
  	scroll_up.setAttribute("id", "scroll_up");
  	scroll_up.href ="#";
  	var text = document.createTextNode("");
  	scroll_up.appendChild(text);
  	li.appendChild(scroll_up);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
  	
  	//right
  	var li = document.createElement("li");
  	var scroll_down = document.createElement("a");
  	scroll_down.setAttribute("id", "scroll_down");
  	scroll_down.href ="#";
  	var text = document.createTextNode("");
  	scroll_down.appendChild(text);
  	li.appendChild(scroll_down);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
	
	var slideshow_set = document.getElementById("slideshow_set");
	slideshow_set.style.top = 0+"px";
	slideshow_set.style.left = 0+"px";
	
	// to get the max y position of the gallery image track we need to count all
	// the li items and multiply that number with 130
	var li = slideshow_set.getElementsByTagName("li");
	slideshow_set.max_y = (li.length-1) *220;
	slideshow_set.max_x = li.length * 220;
	
	// need the width of the gallery so that they do not scroll vertical
	var width = li.length * 220;
	slideshow_set.style.max_y = width + "px";
	
	// Attach onmouseover event for left
  	scroll_up.onclick = function() {
		// get the current position of the gallery element
		var slideshow_set = document.getElementById("slideshow_set");
		var y = parseInt(slideshow_set.style.top);
		if (y % 220== 0) {
    		moveSlideshow("slideshow_set",y+220,0,13);
		}
		return false;
	}
	
	// Attach onmouseover event for right
  	scroll_down.onclick = function() {
		// get the current position of the gallery element
		var slideshow_set = document.getElementById("slideshow_set");
		var y = parseInt(slideshow_set.style.top);
    	if (y % 220 == 0) {
    		moveSlideshow("slideshow_set",y-220,0,13);
		}
		return false;
	}
}
addLoadEvent(prepareSlideshow);