ImageLooper.instance = function(section, dataCollection) {
	if (!ImageLooper._instance) {
		ImageLooper._instance = new ImageLooper(section, dataCollection);
	}
	
	return ImageLooper._instance;
}
ImageLooper._instance = null;
ImageLooper.FIX_ID = "fix";
ImageLooper.IMAGE_LINK_ID = "imageLink";
ImageLooper.TEXT_ID = "text";
ImageLooper.IMAGE_ID = "image";
ImageLooper.ONE_SECOND = 1000;
ImageLooper.FADER_STEP = 4;
ImageLooper.FADER_INTERVAL = 40;
function ImageLooper(section, dataCollection) {
	this.section = section;
	this.dataCollection = dataCollection;
	this.waiter = null;
	this.currentIndex = 0;
	this.waiting = false;
	this.imageReplacer = null;
}
ImageLooper.prototype.init = function(skipFirstOne) {
	if (this.dataCollection.length > 0) {
		var tmpCounter = 0;
		var tmpCurrentIndex = -1;
		var obj = this;
		this.waiter = setInterval(function() {
			if (!obj.waiting) {
				if (obj.currentIndex != tmpCurrentIndex) {
					tmpCounter = 0;
					tmpCurrentIndex = obj.currentIndex;
					if (obj.currentIndex != 0 || !skipFirstOne) {
						obj._populateData(obj.currentIndex);
					}
				} else {
					tmpCounter++;
				}
				if (tmpCounter == obj.dataCollection[obj.currentIndex].showTime) {
					obj.currentIndex++;
					if (obj.currentIndex >= obj.dataCollection.length) {
						obj.currentIndex = 0;
						skipFirstOne = false;
					}
				}
			}
		}, ImageLooper.ONE_SECOND);
	}
}
ImageLooper.prototype._initImageReplacer = function() {
	if (!this.imageReplacer && document.getElementById(ImageLooper.IMAGE_ID)) {
		this.imageReplacer = document.createElement("img");
		this.imageReplacer.style.position = "absolute";
		this.imageReplacer.style.filter = "alpha(opacity: 0)";
		this.imageReplacer.style.opacity = "0";
		
		document.getElementById(ImageLooper.IMAGE_ID).parentNode.appendChild(this.imageReplacer);
	}
	this.imageReplacer.style.top = document.getElementById(ImageLooper.IMAGE_ID).offsetTop + "px";
	this.imageReplacer.style.left = document.getElementById(ImageLooper.IMAGE_ID).offsetLeft + "px";
}
ImageLooper.prototype._populateData = function(index) {
	this.waiting = true;
	this._initImageReplacer();
	
	var obj = this;
	
	this.imageReplacer.src = BASE_URL + "files/frontendimages/frontendimage_" + this.section + "/" + this.dataCollection[index].picPath + "?rand=" + Math.random();
	this.imageReplacer.onload = function() {
		var currentImage = document.getElementById(ImageLooper.IMAGE_ID);
		var stepIndex = 0;
		var fadeWaiter = setInterval(function() {
			if (stepIndex < 100) {
				currentImage.style.filter = "alpha(opacity: " + (100 - stepIndex) + ")";
				currentImage.style.opacity = (100 - stepIndex) / 100;
				
				obj.imageReplacer.style.filter = "alpha(opacity: " + stepIndex + ")";
				obj.imageReplacer.style.opacity = stepIndex / 100;
				
				stepIndex += ImageLooper.FADER_STEP;
			} else {
				clearInterval(fadeWaiter);
				currentImage.src = obj.imageReplacer.src;

				currentImage.style.filter = "alpha(opacity: 100)";
				currentImage.style.opacity = "1";
				obj.imageReplacer.style.filter = "alpha(opacity: 0)";
				obj.imageReplacer.style.opacity = "0";
				
				document.getElementById(ImageLooper.FIX_ID).onclick = function() {
					if (obj.dataCollection[index].url) {
						window.location = obj.dataCollection[index].url;
					}
				}
				document.getElementById(ImageLooper.IMAGE_LINK_ID).onclick = function() {
					if (obj.dataCollection[index].url) {
						window.location = obj.dataCollection[index].url;
					}
				}
				document.getElementById(ImageLooper.TEXT_ID).innerHTML = obj.dataCollection[index].text;
				document.getElementById(ImageLooper.TEXT_ID).style.color = "#" + obj.dataCollection[index].textColor;
				
				obj.waiting = false;
			}
			/*
			 -moz-opacity:0 ;
			filter:alpha(opacity: 0);
			opacity: 0;
			*/
		}, ImageLooper.FADER_INTERVAL);
	}
}
