/********************************************************************
*  programmed by H2G Internetagentur, CH-Aarau, www.h2g.ch, march06 *
********************************************************************/

// Set end position for scrolling (div width minus 3x260 (=780))
var maxpos;

// Set Speed (higher = slower)
var speed = 15;
// Set pixel per step (must be a factor of 260)
var pixel = 4;
// Set First image in sight
var pos = 2;
// Set Number of Images
var anzImg;

// create obj variable
function init(totalwidth,totalimg){
	
	obj = document.getElementById("contpic");
	obj.style.left = "0px";

	maxpos = totalwidth - 780;
	maxpos = maxpos * -1;
	anzImg = totalimg;

	endImg = pos + 1;

	// Button right >>
	if (endImg == anzImg) {
		document.getElementById('buttonright').style.visibility = 'hidden';
	}

}



// Function to move the div to the right
function slideRight(goalpos, left, right){
	if(parseInt(obj.style.left) < goalpos){
		// Move div to the right
		obj.style.left = parseInt(obj.style.left) + pixel + "px";
		setTimeout("slideRight("+goalpos+","+left+","+right+")",speed);
	} else if (parseInt(obj.style.left) == goalpos) {
		// At the End of the movement: Hide and show buttons
		stopSlide();
	}
}
	
// Function to move the div to the left
function slideLeft(goalpos, left, right){	
	if(parseInt(obj.style.left) > goalpos){
		// Move div to the left
		obj.style.left = parseInt(obj.style.left) - pixel + "px";
		setTimeout("slideLeft("+goalpos+","+left+","+right+")",speed);
	} else if (parseInt(obj.style.left) == goalpos) {
		// At the End of the movement: Hide and show buttons
		stopSlide();
	}
}

// Function for Enable and Disable the Scrollbuttons
function stopSlide() {

	var next;
	var last;
	var self;
	var next_width;
	var last_width;

	endImg = pos + 1;

	currentpos = parseInt(obj.style.left);

	// Disable Buttons on start- and endposition

	// Button right >>
	if (endImg == anzImg) {
		document.getElementById('buttonright').style.visibility = 'hidden';
	} else if (currentpos > maxpos) {
		document.getElementById('buttonright').style.visibility = 'visible';
	}
		
	// Button << left
	if (currentpos == 0) {
		document.getElementById('buttonleft').style.visibility = 'hidden';
	} else if (currentpos < 0) {
		document.getElementById('buttonleft').style.visibility = 'visible';
	}


	
	// Enable the Button Links
	last = pos - 1;
	next = pos + 1;
	self = pos;

	// Get width of self and next image
	next_width = document.getElementsByTagName("img")[next].width;
	self_width = document.getElementsByTagName("img")[self].width;

	document.getElementById('lnkleft').href = "javascript:startSlide('right',"+next_width+","+self_width+");";

	if (endImg < anzImg)
	{
		document.getElementById('lnkright').href = "javascript:startSlide('left',"+next_width+","+self_width+");";
	}
	
}
	
// Initialize Scrolling
function startSlide(direction,left,right) {

	if (left == '' && right == '')
	{
		left = document.getElementsByTagName("img")[3].width;
		right = document.getElementsByTagName("img")[1].width;
	}

	var currentpos;
	var goal;

	// Calculate Goal Position
	currentpos = parseInt(obj.style.left);

	// Disable the Button Links
	document.getElementById('lnkleft').href = "javascript:void(0);";
	document.getElementById('lnkright').href = "javascript:void(0);";
		
	// Start Sliding
	if (direction == "left") {
		// Left
		goalpos = currentpos - left;
		slideLeft(goalpos, left, right);
		pos++;
	} else if (direction == "right" && currentpos < 0) {
		// Right
		goalpos = currentpos + right;
		slideRight(goalpos, left, right);
		pos--;
	}
}
