/* 
   Javascript Document - JS
   Werbemittel-Fabrik
   Malte Beckers
   Functions
*/


/* 
   Art.Nr Url transmit Function
*/
onload = function() {
	if(document.getElementById('mailformArtikel_1_Nr')){
		if(window.location.hash){
			document.getElementById('mailformArtikel_1_Nr').value = window.location.hash.substring(1);
		}
	}
}


/* 
   Slideshow Functions (for currentBar at Homepage)
*/
  var imageWidth = 136;  // Total width of every Image and Image Container
  var speed = 5;         // Speed (Speed/Step of movement per 'time')
  var time = 0;          // Speed for recursion (milliseconds)
  var pause = 5000;      // Pause between each image change per milliseconds)
 
/* Dont change anything below!!! */
  var nrChildNodes = 0;  // Grab total number of images
  var lastImage = null;  // Copy of first image
  var curImg = 1;        // Current Image Number
  var barPos = 0;        // CSS Position of Bar
  var dir = 1;           // Direction of movement (1=left, 2=right)
  var goNext = null;     // Recursion
  var moveOn = null;     // Recursion of movement

  function cBact(){document.getElementById('currentBar').style.borderColor = "#000000";}
  function cB()   {document.getElementById('currentBar').style.borderColor = "#B6B6B6";}
  
  // Onload: Start moving bar
  onload = function initSlideshow() {
	childNodes = document.getElementById('barImages').childNodes;
	for (var i in childNodes) { 
		if (childNodes[i].nodeName == "A") {
		  nrChildNodes++;
		  childNodes[i].id = "A"+nrChildNodes;
		}
	}
	// Add first Image on last position again
	document.getElementById('barImages').innerHTML += "<a href=\""+document.getElementById('A1').href+"\" id=\"A"+(nrChildNodes+1)+"\">"+document.getElementById('A1').innerHTML+"</a>";
	nrChildNodes++;
	// Set Layer Width
	document.getElementById('barImages').style.width = imageWidth * (nrChildNodes+1) + "px";
	// Set Layer Position
	document.getElementById('barImages').style.left = barPos + "px";
	// Start Movement
	moveOn = window.setTimeout("ImgNext()", pause);
  }
  
  function ImgNext(){
	  dir = 1;
	  if(curImg >= nrChildNodes){  // If right end of bar is reached
		barPos = 0;
		curImg = 1;
	  }
	  curImg++;
	  move(barPos);
	  document.getElementById('headerBarImage').href = document.getElementById('A'+curImg).href;
  }
  
  function ImgLast(){
	  dir = 2;
	  if(curImg <= 1){  // If left end of bar is reached
		barPos = - (imageWidth * (nrChildNodes-1));
		curImg = nrChildNodes;
	  }
	  curImg--;
	  move(barPos);
	  document.getElementById('headerBarImage').href = document.getElementById('A'+curImg).href;
  }
  
  function move(nPos) {
	  if(dir == 1) { // Move left (next image = from right side)
		nPos -= speed;
		if(nPos > barPos-imageWidth) {  // If final image position is NOT reached
			document.getElementById('barImages').style.left = nPos + "px";
			goNext = window.setTimeout("move("+nPos+",'next')", time);
		}
		else { // If final image position IS reached
			barPos -= imageWidth;
			document.getElementById('barImages').style.left = barPos + "px";
			window.clearTimeout(goNext);
			moveOn = window.setTimeout("ImgNext()", pause);
		}
	 }
	 if(dir == 2) { // Move right (next image = from left side)
		nPos += speed;
		if(nPos < barPos+imageWidth) {  // If final image position is NOT reached
			document.getElementById('barImages').style.left = nPos + "px";
			goNext = window.setTimeout("move("+nPos+",'next')", time);
		}
		else { // If final image position IS reached
			barPos += imageWidth;
			document.getElementById('barImages').style.left = barPos + "px";
			window.clearTimeout(goNext);
			moveOn = window.setTimeout("ImgLast()", pause);
		}
	 }
  }
		  
  function next(){
	  window.clearTimeout(goNext);
	  window.clearTimeout(moveOn);
	  ImgNext();
  }
  
  function prev(){
	  window.clearTimeout(goNext);
	  window.clearTimeout(moveOn);
	  ImgLast();
  }
