//Add OnLoad EventListener to Window
if (typeof window.addEventListener != "undefined") {
	window.addEventListener( "load", vScrollerInit, false );
} else if (typeof window.attachEvent != "undefined" ) {
	window.attachEvent( "onload", vScrollerInit);
}

function vScrollerInit() {
	if (document.getElementById("profiAngebote")) {
		var topAngeboteScoller = new vScroller(4, 8, 2000, "topAngebote");
		var profiAngeboteScoller = new vScroller(4, 8, 2000, "apHits");
		var profiAngeboteScoller = new vScroller(5, 8, 2000, "profiAngebote");
		var monatsHitsScoller = new vScroller(4, 8, 2000, "monatsHits");
	
		document.getElementById("profiAngebote").style.display = "none";
		document.getElementById("monatsHits").style.display = "none";
		document.getElementById("apHits").style.display = "none";
	}
}

function vScroller(visibleElements, speed, pauseTime, rootDiv) {
	//User Defined
	this.vScrollerVisible = visibleElements;
	this.vScrollerSpeed = speed;
	this.vScrollerPauseTime = pauseTime;
	this.vScrollerDiv = rootDiv;
	
	//Global Vars
	this.vScrollerPosition = 0;
	this.vScrollerEndPosition = 0;

	this.vScrollerStepSize = 0;
	this.vScrollerStep = 0;
	this.vScrollerStepCurrent = 0;
	this.vScrollerStepCount = 0;

	this.vScrollerPause = 0;

	this.vScrollerArea;
	this.vScrollerMover;
	this.vScrollerObjects;
	
	if (document.getElementById(this.vScrollerDiv)) {
		//Format Area
		this.vScrollerArea = document.getElementById(this.vScrollerDiv);
		this.vScrollerArea.style.cssText = this.vScrollerArea.style.cssText + "; overflow:hidden; ";
		
		var delegate = this;
		this.vScrollerArea.onmouseover = function() {delegate.vScrollerPause=2;}
		this.vScrollerArea.onmouseout = function() {delegate.vScrollerPause=0;}
		
		//Format Objects
		this.vScrollerObjects = getChilds(this.vScrollerArea, "*");
		
		if (this.vScrollerObjects.length > this.vScrollerVisible) {
			//Create Mover
			var newWidth = (this.vScrollerObjects.length+this.vScrollerVisible)*this.vScrollerObjects[0].offsetWidth*2;
			var newArea = "<div id='vScrollerMover' style='width:" +newWidth+ "px;'>";
			
			for (var n=0; n<this.vScrollerObjects.length; n++) {
				this.vScrollerObjects[n].style.cssText = this.vScrollerObjects[n].style.cssText + "; float:left;"
				
				if (n==0) {
					newArea += "<div id='vScrollerFirstObject' class='" +this.vScrollerObjects[n].className+ "' style='" +this.vScrollerObjects[n].style.cssText+ "'>" +this.vScrollerObjects[n].innerHTML+ "</div>";
				} else if (n==1) {
					newArea += "<div id='vScrollerSecondObject' class='" +this.vScrollerObjects[n].className+ "' style='" +this.vScrollerObjects[n].style.cssText+ "'>" +this.vScrollerObjects[n].innerHTML+ "</div>";
				} else {
					newArea += "<div class='" +this.vScrollerObjects[n].className+ "' style='" +this.vScrollerObjects[n].style.cssText+ "'>" +this.vScrollerObjects[n].innerHTML+ "</div>";
				}
			}
			
			for (n=0; n<this.vScrollerVisible; n++) {
				newArea += "<div class='" +this.vScrollerObjects[n].className+ "' style='" +this.vScrollerObjects[n].style.cssText+ "'>" +this.vScrollerObjects[n].innerHTML+ "</div>";
			}
			
			newArea += "</div>";
			this.vScrollerArea.innerHTML = newArea;
			this.vScrollerMover = getChilds(this.vScrollerArea, "vScrollerMover")[0];
			
			this.vScrollerStepSize = getChilds(this.vScrollerMover, "vScrollerSecondObject")[0].offsetLeft - getChilds(this.vScrollerMover, "vScrollerFirstObject")[0].offsetLeft;
			this.vScrollerStepCount = getChilds(this.vScrollerMover, "*").length;
			
			addTimeout (this, "object.move();", this.vScrollerPauseTime);
		} else {
			//Create Fake Mover
			var newWidth = (this.vScrollerObjects.length+this.vScrollerVisible)*this.vScrollerObjects[0].offsetWidth*2;
			var newArea = "<div id='vScrollerMover' style='width:" +newWidth+ "px;'>";
			
			for (var n=0; n<this.vScrollerObjects.length; n++) {
				this.vScrollerObjects[n].style.cssText = this.vScrollerObjects[n].style.cssText + "; float:left;"
				newArea += "<div class='" +this.vScrollerObjects[n].className+ "' style='" +this.vScrollerObjects[n].style.cssText+ "'>" +this.vScrollerObjects[n].innerHTML+ "</div>";
			}
			
			newArea += "</div>";
			this.vScrollerArea.innerHTML = newArea;
		}
	}
}

vScroller.prototype.move = function () {
	if (this.vScrollerPause == 0) {
		var vScrollerSpeedCurrent = this.vScrollerSpeed/(this.vScrollerStepSize/2)*(this.vScrollerStepSize-this.vScrollerStepCurrent);
		if (vScrollerSpeedCurrent < 1) {
			vScrollerSpeedCurrent = 1;
		}
		
		this.vScrollerPosition -= vScrollerSpeedCurrent;
		this.vScrollerStepCurrent += vScrollerSpeedCurrent;
		this.vScrollerMover.style.marginLeft = this.vScrollerPosition + "px";
		
		if (this.vScrollerStepCurrent >= this.vScrollerStepSize) {
			this.vScrollerPosition += (this.vScrollerStepCurrent-this.vScrollerStepSize);
			this.vScrollerMover.style.marginLeft = this.vScrollerPosition + "px";
			this.vScrollerStepCurrent = 0;
			this.vScrollerStep += 1;
			this.vScrollerPause=1;
			
			if (this.vScrollerStep == this.vScrollerStepCount-this.vScrollerVisible) {
				this.vScrollerPosition =0;
				this.vScrollerStepCurrent =0;
				this.vScrollerStep = 0;
			}
			
			addTimeout (this, "if (object.vScrollerPause == 1) { object.vScrollerPause=0; }", this.vScrollerPauseTime);
		}
	}
	
	addTimeout (this, "object.move();", 20);
}

function getChilds (root, id) {
	var tmp = root.getElementsByTagName("div");
	var ret = new Array();
	
	for (var n=0; n<tmp.length; n++) {
		if (tmp[n].parentNode == root) {
			if (id=="*" || tmp[n].id==id) {
				ret.push(tmp[n]);
			}
		}
	}
	
	return ret;
}

var timeoutStack = new Array();
function addTimeout (object, code, time) {
	var id = Math.floor(Math.random()*999999);
	timeoutStack[id] = object;
	setTimeout( 'doTimeout("' + id + '","' + code + '")', time);
}

function doTimeout (id, code) {
	var object = timeoutStack[id];
	eval(code);
}
