/* Scrollt eine Leiste von Bilder */

var IE5 = false;

// Init-Vars
var scrollStep = 1;
var scrollTime = 100;
var scrollFrom = 20;
var scrollTo = 680;
var imgWidth = 110;			// globale Breite für alle Bilder. TODO: Evtl. später pro Bild

// Runtime-Vars
var imgPool = new Array()
var imgPoolCount = 0;
var canScroll = 1;
var scrollActive = 1;
var nullClip = null;		// speichert den exakten String für die Nullgröße
var scrollInterval = null;

function initImgScroll(step, stepTime, from, to, gblWidth) {
	scrollStep = step;
	scrollTime = stepTime;
	scrollFrom = from;
	scrollTo = to;
	imgWidth = gblWidth;
	
	NS = (navigator.appName=='Netscape');
	IE5 = (navigator.appVersion.indexOf("MSIE 5.0")>0);
	
	canScroll = DOM;// && !NS;
}

function startImgScroll() {
	if (!canScroll) return;
	if (scrollInterval!=null) return;
	
	scrollInterval = window.setInterval("scrollPool()", scrollTime);
}

function stopImgScroll() {
	if (!canScroll) return;
	if (scrollInterval==null) return;
	
	window.clearInterval(scrollInterval);
	scrollInterval = null;
}

function canImgScroll() {
	return canScroll;
}

function addImg(id, top, width, height, text) {
	if (!canScroll) return;

	width = imgWidth;		// TODO: später entfernen
	var left = scrollFrom + imgWidth*imgPoolCount;		// TODO: anpassen auf variable Bildbreite
	var visLeft = (left>scrollTo ? scrollTo : left);

    var div = createElem("div", new Array("id", "style"), new Array(id, "{position:absolute; left:" + visLeft + "; top:" + top + "; width:" + width + "px; }"));
    div.appendChild(text);
    getElem("id", "addImages", null).appendChild(div);

	// zum späteren Scrollen merken
	var img = new ScrollImage(id, left, width, height);
	imgPool[imgPoolCount++] = img;

    // endImg
	img.elem = getElem("id", img.id, null);
	showImg(img);
}

function beginImg(id, top, width, height) {
	if (!canScroll) return;
	
	width = imgWidth;		// TODO: später entfernen
	var left = scrollFrom + imgWidth*imgPoolCount;		// TODO: anpassen auf variable Bildbreite
	var visLeft = (left>scrollTo ? scrollTo : left);
	
	document.writeln("<div id=\"" + id + "\" style=\"{position:absolute; left:" + visLeft + "; top:" + top + "; width:" + width + "px; }\"><div align=\"center\">");
	//border:1px solid;
	
	// zum späteren Scrollen merken
	var img = new ScrollImage(id, left, width, height);
	imgPool[imgPoolCount++] = img;
}

function endImg() {
	if (!canScroll) return;
	
	document.writeln("</div></div>");
	var img = imgPool[imgPoolCount-1];
	img.elem = getElem("id", img.id, null);
	showImg(img);
}

function scrollPool() {
	if (!canScroll) return;
	if (!scrollActive) return;

	for (var i=0; i<imgPool.length; ++i) {
		moveImg(imgPool[i], -scrollStep);	
	}
}

function ScrollImage(id, leftPos, width, height) {
	this.id = id;
	this.leftPos = leftPos;
	this.width = width
	this.height = height;
	this.elem = null;
}

function showImg(img) {
	var elem = img.elem;
	if (elem==null) return;
	var clip = elem.style.clip;
	var left = img.leftPos;
	
	// hide image
	if ((left>scrollTo) && (clip!=nullClip)) {
		elem.style.clip = "rect(0px 0px 0px 0px)";
		if (nullClip==null) nullClip = elem.style.clip;
	}
	
	// show image
	if ((left<=scrollTo) && (clip==nullClip))
		elem.style.clip = "rect(0px " + img.width + "px " + img.height + "px 0px)";
}

function moveImg(img, step) {
	img.leftPos += step;
	
	// links raus? dann ans Ende
	if (img.leftPos<=scrollFrom) img.leftPos = scrollFrom + img.width*imgPoolCount;
	
	// in Scrollbereich? dann anzeigen : sonst Bild an den Ende des Scrollbereiches
	if (img.leftPos<=scrollTo) img.elem.style.left = img.leftPos;
	
	// je nachdem, ob Bild in Scrollbereich oder nicht, dieses anzeigen
  showImg(img);
}

