// ----------------------------------------------------------------
// LOOP - horizontale Bildschleife
// Copyright by Marcus Ollas. All Rights Reserved.
// ----------------------------------------------------------------
var LOOP = {
	// ------------------------------------------------------------
	loop : null,
	inner : null,
	status : null,
	pictures : 0,
	loaded : 0,
	fadeIn : 0,
	dir : 0,
	toDir : 0,
	tref : null,
	delay : 25,
	step : 1,
	toStep : 1,
	maxStep : 40,
	center2 : 0,
	// ------------------------------------------------------------
	getPosition : function(e) {
		var left = 0;
		var top  = 0;
	
		while (e.offsetParent) {
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}
		left += e.offsetLeft;
		top  += e.offsetTop;
	
		return {y:top,x:left};
	},
	// ------------------------------------------------------------
	startFading : function() {
		LOOP.fadeIn = 0;
		LOOP.fade();
	},
	fade : function() {
		if (el = document.getElementById('Moving' + LOOP.fadeIn))
			el.style.visibility = 'visible';

		if (LOOP.fadeIn++ <= LOOP.pictures)
			window.setTimeout('LOOP.fade()',30);
		else {
			try {
				if (QUOTES) {
					QUOTES.init('Zitate');
					QUOTES.start();	
				}
			}
			catch(e) {};
		}
	},
	// ------------------------------------------------------------
	startMoving : function(which) {
		LOOP.toDir = which;
		LOOP.move();
	},
	stopMoving : function() {
		if (LOOP.tRef) {
			window.clearTimeout(LOOP.tRef);
			LOOP.tRef = null;
		}
	},
	// ------------------------------------------------------------
	move : function() {
		if (LOOP.inner) {
			var newleft = (LOOP.inner.offsetLeft + (LOOP.dir * LOOP.step));
	
			// nach rechts
			if (LOOP.center2 - Math.abs(newleft) > LOOP.inner.lastChild.offsetWidth) {
				var w = LOOP.inner.lastChild.offsetWidth;			
				var last = LOOP.inner.lastChild;
				var first = LOOP.inner.firstChild;
				LOOP.inner.insertBefore(last,first);
				newleft -= w;
			}
			// nach links
			else if (Math.abs(newleft) - LOOP.center2 > LOOP.inner.firstChild.offsetWidth) {
				var w = LOOP.inner.firstChild.offsetWidth;
				var last = LOOP.inner.lastChild;
				var first = LOOP.inner.firstChild;
				LOOP.inner.appendChild(first);
				newleft += w;
			}
			// Richtungs?nderung?
			if (LOOP.toDir != LOOP.dir) {
				// Langsam?
				if (LOOP.step <= 1) {
					LOOP.dir = LOOP.toDir;
					LOOP.step = 1;
				}
				// Erst Geschwindigkeit drosseln!
				else
					LOOP.step-- ;
			}
			else {
				if (LOOP.toStep < LOOP.step)
					LOOP.step-- ;
				else if (LOOP.toStep > LOOP.step)
					LOOP.step++ ;
			}
			LOOP.inner.style.left = newleft;
			LOOP.tRef = window.setTimeout("LOOP.move()",LOOP.delay);
		}
	},
	// ------------------------------------------------------------
	imageLoaded : function(ref) {
		LOOP.loaded++ ;
	
		if (LOOP.status == null)
			LOOP.status = document.getElementById("LoopStatus");
	
		if (LOOP.pictures) {
			var rest = Math.max(0,(LOOP.pictures - LOOP.loaded));
			var msg = 'Bilddaten werden vorbereitet: ' + rest;
			if (LOOP.status)
				LOOP.status.innerHTML = msg;
			else
				window.status = msg;

			if (!rest)
				window.setTimeout("LOOP.clearStatus()",1000);
		}
	},
	clearStatus : function() {
		if (LOOP.status)
			LOOP.status.innerHTML = '';
		else
			window.status = window.defaultStatus;
	},
	// ------------------------------------------------------------
	init : function(loopId) {
		LOOP.loop = document.getElementById(loopId); //"Loop");
		LOOP.inner = (LOOP.loop ? LOOP.loop.firstChild : null); //document.getElementById("Inner");

		if (LOOP.loop && LOOP.inner && LOOP.loop.offsetWidth < LOOP.inner.offsetWidth) {
			// Eventhandler
			LOOP.loop.onmousemove = function(event) {
				var ev = event || window.event;
				w = this.offsetWidth;
				w2 = w/2;
				w4 = w/4;
				w8 = w/8;
				var lPos = LOOP.getPosition(this);
				var x = ev.clientX - lPos.x;
				m = w2;
	
				// von 3/8tel bis 5/8tel sind die Ruhezone!
				if (x > w2-w8 && x < w2+w8)
					LOOP.toStep = 0;
				// von 2/8tel bis 3/8tel langsam/links
				else if (x > w4 && x <= w2-w8) {
					LOOP.toDir = 1;
					LOOP.toStep = 1;
				}
				// von 5/8tel bis 6/8tel langsam/rechts
				else if (x > w2+w8 && x <= w2 + w4) {
					LOOP.toDir = -1;
					LOOP.toStep = 1;
				}
				else {
					LOOP.toDir = (x < w2 ? 1 : -1);
					LOOP.toStep = LOOP.maxStep;
				}
			}
			LOOP.loop.onmouseout = function(event) {
				// ignore childnode caused mouseout-Events! 
				if (!event || !event.relatedTarget || !event.relatedTarget.parentNode || event.relatedTarget.parentNode != LOOP.inner)
					LOOP.toStep = 1;
			}
			// den inneren Bereich zuf?llig einstellen!
			LOOP.inner.style.left = -Math.min(LOOP.inner.offsetWidth - LOOP.inner.lastChild.offsetWidth,
			                         Math.max(LOOP.inner.firstChild.offsetWidth,parseInt(Math.random()*LOOP.inner.offsetWidth)));
			LOOP.center2 = (LOOP.inner.offsetWidth - LOOP.loop.offsetWidth) / 2;
			LOOP.inner.style.visibility = 'visible';
		
			LOOP.startFading();
			LOOP.startMoving(-1);
		}
	}
};
// ----------------------------------------------------------------
// END OF loop.js
// ----------------------------------------------------------------
