var current_section = 0;

function moveSlideshow(elementID,final_x,final_y,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_x <= -elem.max_x) {
        final_x = -elem.max_x;
    }
    if (final_x > 0) {
	    final_x = 0;
    }
    
    // animation bit (taken from the book DOM Scripting by Jeremy Keith)
    if (xpos < final_x) {
        var dist = Math.ceil((final_x - xpos)/10);
        xpos = xpos + dist;
    }
    if (xpos > final_x) {
        var dist = Math.ceil((xpos - final_x)/10);
        xpos = xpos - dist;
    }
  
    // again, restrict showing white area
    if (xpos <= -elem.max_x) {
	    xpos = -elem.max_x;
    }
    if (xpos > 0) {
	    xpos = 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_x+","+final_y+","+interval+")";
    elem.movement = setTimeout(repeat,interval);
}

function prepareSlideshow( ul_id, w, h ) {
	var slideshow_set = document.getElementById(ul_id);
	
	slideshow_set.style.top = 0+"px";
	slideshow_set.style.left = 1020+"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_x = (li.length-1) * w;
	slideshow_set.max_y = li.length * h;
	
	// need the width of the gallery so that they do not scroll vertical
	var width = li.length * w;
	slideshow_set.style.width = width + "px";
	
	//create scroll links
	/*
	var left_link = document.getElementById('link1');
	var mid_link = document.getElementById('link2');
	var right_link = document.getElementById('link3');
	

  	left_link.onmouseover = function() {
		var slideshow_set = document.getElementById(ul_id);
		var x = parseInt(slideshow_set.style.left);
		if (x % w == 0) {
			moveSlideshow(ul_id,-1020,0,20);
		}
		return false;
	}	

  	mid_link.onmouseover = function() {
		var slideshow_set = document.getElementById(ul_id);
		var x = parseInt(slideshow_set.style.left);
		if (x % w == 0) {
			moveSlideshow(ul_id,-2040,0,20);
		}
		return false;
	}
	
  	right_link.onmouseover = function() {
		var slideshow_set = document.getElementById(ul_id);
		var x = parseInt(slideshow_set.style.left);
		if (x % w == 0) {
			moveSlideshow(ul_id,-3060,0,20);
		}
		return false;
	}
	*/
	var slideshow_set = document.getElementById(ul_id);
	var x = parseInt(slideshow_set.style.left);
	if (x % w == 0) {
		moveSlideshow(ul_id,-1020,0,20);
		current_section++;
		setTimeout("startSlideShow('"+ul_id+"')",8000);
	}
	//setTimeout("moveSlideshow('"+ul_id+"',0,0,20);",100);
}


function startSlideShow(ul_id) {
	current_section++;
	if ( current_section <= 3 ) {
		var final_x = current_section*-1020;
		moveSlideshow(ul_id,final_x,0,20);
		setTimeout("startSlideShow('"+ul_id+"')",8000);
	} else {
		current_section = 0;
		prepareSlideshow( ul_id, 1020, 250 );
	}
}
