/*-------------------------------------
 * Image scrollbar using scriptaculous
 * Author: Martin Nonnenmacher (2009)
 *-------------------------------------*/

var direction = -1;
var imagesLoaded = false;
var jumpOffset = 63;
var lastDirection = -1;
var offsetLeft = 0;
var offsetTop = 0;
var pictureCount = 0;
var pictureWidth = 0;
var pos = 0;
var scrollMarginRight = -68;
var speed = 10;
var startOffset = -148;
var totalPictureWidth = 0;

//alert("appCodeName="+navigator.appCodeName+" appName="+navigator.appName+" appVersion="+navigator.appVersion+" userAgent="+navigator.userAgent);

if (navigator.appName.indexOf("Opera") != -1) {
	startOffset = -148;
	jumpOffset = 63;
	scrollMarginRight = -67;
} else if (navigator.appName.indexOf("Explorer") != -1) {
	startOffset = -148;
	jumpOffset = 63;
	scrollMarginRight = -63;
} else if (navigator.appVersion.indexOf("Apple") != -1) {
	startOffset = -148;
	jumpOffset = 63;
	scrollMarginRight = -67;
}

function getPictureWidth(element) {
	var children = element.childNodes;
	var width = 0;
	for (var i = 0; i < children.length; i++) {
		if (children[i].nodeName == "SPAN") {
			width += children[i].firstChild.firstChild.width;
		}
	}
	return width;
}

function getPictureCount(element) {
	var children = element.childNodes;
	var count = 0;
	for (var i = 0; i < children.length; i++) {
		if (children[i].nodeName == "SPAN") {
			count ++;
		}
	}
	return count;
}

function startMove(element) {
	imagesLoaded = allImagesLoaded();
	if (imagesLoaded) {
		pictureWidth = getPictureWidth($('scroll1'));
		pictureCount = getPictureCount($('scroll1'));
		totalPictureWidth = pictureWidth + 6 * pictureCount;
		$('inner_container').style.width = 2 * totalPictureWidth + 5 * pictureCount;
		$('scroll2').innerHTML = $('scroll1').innerHTML;
		$('scroll1').style.width = totalPictureWidth;
		$('scroll2').style.width = totalPictureWidth;
		$('scroll1').style.marginRight = scrollMarginRight;
		$('scroll2').style.marginRight = scrollMarginRight;
		new Effect.Move(element, {x: startOffset, y: 0, duration: 0, mode: 'relative', transition:Effect.Transitions.linear, queue: { position: 'end', scope: 'scroll' }})
		moveEffect(element);
	} else {
		setTimeout(function() { startMove(element); }, 50);
	}
}

function moveEffect(element){
	var curSpeed = speed;
	var curDir = direction;
	if (curDir == 1 && pos + curSpeed > 0) {
		new Effect.Move(element, {x: -totalPictureWidth + jumpOffset, y: 0, duration: 0, mode: 'relative', transition: Effect.Transitions.linear, queue: { position: 'end', scope: 'scroll' }})
		pos += -totalPictureWidth + jumpOffset;
		setTimeout(function() { moveEffect(element); }, 100);
	} else if (curDir == -1 && pos - curSpeed < -totalPictureWidth) {
		new Effect.Move(element, {x: totalPictureWidth - jumpOffset, y: 0, duration: 0, mode: 'relative', transition: Effect.Transitions.linear, queue: { position: 'end', scope: 'scroll' }})
		pos += totalPictureWidth - jumpOffset;
		setTimeout(function() { moveEffect(element); }, 100);
	} else {
		if (curDir != 0) {
			new Effect.Move(element, {x: curDir * curSpeed, y: 0, duration: 0.25, mode: 'relative', transition: Effect.Transitions.linear, queue: { position: 'end', scope: 'scroll' }})
			pos += curDir * curSpeed;
		}
		setTimeout(function() { moveEffect(element); }, 250);
	}
	//$('debug').innerHTML = 'pos = ' + pos;
}

function stop() {
	if (direction != 0)
		lastDirection = direction;
	direction = 0;
}

function left() {
	if (direction != 0)
		lastDirection = direction;
	direction = -1;
}

function right() {
	if (direction != 0)
		lastDirection = direction;
	direction = 1;
}

function last() {
	direction = lastDirection;
}

function allImagesLoaded() {
	var images = document.images;
	for (var i = 0; i < images.length; i++) {
		if(images[i].complete == false) {
			return false;
		}
	}
	return true;
}

function setInfo(info) {
	$('beschreibung').innerHTML = info;
}

function resetInfo() {
	$('beschreibung').innerHTML = '<br/><br/><br/>';
}

function getcords(e) {
	//$('debug').innerHTML = 'waitForClose='+waitForClose+' jkpopwin='+(typeof jkpopwin == 'undefined' ? typeof jkpopwin : jkpopwin.closed ? 'closed' : 'open');
	if (waitForClose && (typeof jkpopwin == "undefined" || jkpopwin.closed)) {
		waitForClose = false;
		last();
	}

	now = new Date();
	if (now.getTime() < lastPopupTime + 2000)
		return;

	mouseX = Event.pointerX(e);
	mouseY = Event.pointerY(e);
	if (offsetLeft == 0) {
		var el = $('scroll_container');
		while (el != null) {
			offsetLeft += el.offsetLeft;
			offsetTop += el.offsetTop;
			el = el.offsetParent;
		}
	}
	var mouseRelX = mouseX - offsetLeft;
	var mouseRelY = mouseY - offsetTop;
	if (mouseRelX < 0 || mouseRelX > $('scroll_container').offsetWidth || mouseRelY < 0 || mouseRelY > $('scroll_container').offsetHeight) {
		mouseRelX = -1;
		mouseRelY = -1;
	}

	if (mouseRelX != -1 && mouseRelY != -1) {
		var part = Math.round(mouseRelX / $('scroll_container').offsetWidth * 80) - 40;
		speed = Math.abs(part);
		if (part < 0)
			right();
		else if(part > 0)
			left();
		else
			stop();
	}

	//$('debug').innerHTML = 'mouseX:' + mouseX + ' mouseY:' + mouseY + ' offsetLeft:' + offsetLeft + ' offsetTop:' + offsetTop + ' mouseRelX:' + mouseRelX + ' mouseRelY:' + mouseRelY + ' speed:' + speed;
}


