var newSlideShow;



function tryOpenSlimBox(src, text)
{
	if ((src) && (src != ''))
		{
		Slimbox.open(src, text);
		}
}

function tryAltOpenSlimBox(src, alternativeSrc, text)
{
	if ((src) && (src != ''))
		{
		Slimbox.open(src, text);
		}
	else if ((alternativeSrc) && (alternativeSrc != ''))
			{
		Slimbox.open(alternativeSrc, text);
		}
}

function setUpHtmlSlideShow(array, delayInSeconds, targetElement)
{

}

function createNewHtmlSlideShow(array, delayInSeconds, slimboxonly, start, targetId, controlBarId, startButtonId, stopButtonId, loadCallback, pIndicatorId, pItemIndicatorImg, pItemIndicatorHL)
{
	if(delayInSeconds > 0)
	{
		newSlideShow = new HtmlSlideShow("newSlideShow", array, start, delayInSeconds, targetId, controlBarId, startButtonId, stopButtonId, loadCallback, pIndicatorId, pItemIndicatorImg, pItemIndicatorHL);
		Slimbox.closeCallBack = function(){newSlideShow.switchInternal(false)};
		registerNHMouseOver();
		registerNHMouseOut();
	}
	else
	{
		var req = new Request.HTML({ 
		onSuccess: function(html)
		{
			$(targetId).empty(); //.set('text', ''); //clear
			$(targetId).adopt(html); //inject
		},
		onFailure: function(xhr) 
		{
			$(targetId).set('text', 'The request failed.');
		}
		});
		req.send({url: array[0], method: 'get'});
	}
}

function registerNHMouseOver()
{
	if((newSlideShow.urlArray.length > 1) && (document.getElementById(newSlideShow.ctrlId)))
	{
		document.getElementById(newSlideShow.targetId).onmouseover = newSlideShowShowControlBar;
		document.getElementById(newSlideShow.ctrlId).onmouseover = newSlideShowShowControlBar;
	}
}

function registerNHMouseOut()
{
	if((newSlideShow.urlArray.length > 1) && (document.getElementById(newSlideShow.ctrlId)))
	{
		document.getElementById(newSlideShow.targetId).onmouseout = newSlideShowHideControlBar;
		document.getElementById(newSlideShow.ctrlId).onmouseout = newSlideShowHideControlBar;
	}
}

function newSlideShowShowControlBar()
{
	newSlideShow.showControlBar();
}

function newSlideShowHideControlBar()
{
	//newSlideShow.hideControlBar();
}

function HtmlSlideShow(pSelfName, pURLArray, pStart, pDelayInSeconds, pTargetElemId, pControlBarId, pStartButtonId, pStopButtonId, pLoadCallback, pIndicatorId, pItemIndicatorImg, pItemIndicatorHLImg) 
{

	this.urlArray = pURLArray;
	this.selfName = pSelfName;
	
	this.currentIndex = 0;
	this.targetId = pTargetElemId;
	this.delay = pDelayInSeconds * 1000;
	
	this.ctrlId = pControlBarId;
	this.startCtrlId = pStartButtonId;
	this.stopCtrlId = pStopButtonId;
	this.recentIndex = 0;
	this.ctrlFadeOutHandle = false;
	
	this.intervalHandle = false;
	this.stoppedInternal = false;
	
	this.showItemIndicator = pIndicatorId != null;
	this.indicatorId = pIndicatorId;
	this.itemIndicatorImg = pItemIndicatorImg;
	this.itemIndicatorHLImg = pItemIndicatorHLImg;

	var j=0;
	if(!(document.getElementById(this.targetId))) return;
	this.req = new Request.HTML({ 
		onSuccess: function(html)
		{
			$(pTargetElemId).empty(); //.set('text', ''); //clear
			$(pTargetElemId).adopt(html); //inject
			if(pLoadCallback)
			{
				pLoadCallback(html);
			}
		},
		onFailure: function(xhr) 
		{
			$(pTargetElemId).set('text', 'The request failed.');
		}
	});
	
	//Methods:
	this.getCurrentURL = function()
	{
		return this.urlArray[this.currentIndex];
	}
	
	this.loadCurrentFile = function()
	{
		var currURL = this.getCurrentURL();
		if(this.itemIndicators) 
		{
			this.itemIndicators[this.recentIndex].src = this.itemIndicatorImg;
			this.itemIndicators[this.currentIndex].src = this.itemIndicatorHLImg;
		}
		this.req.send({url: currURL, method: 'get'}); //BUG: Hier passiert beim ersten mal mist!!!
		this.recentIndex = this.currentIndex;
	}	
	
	this.nextSlide = function()
	{
		this.currentIndex++;
		this.currentIndex = this.currentIndex % this.urlArray.length;
		this.loadCurrentFile();
	}
	this.prevSlide = function()
	{
		this.currentIndex--;
		this.currentIndex = (this.currentIndex + this.urlArray.length) % this.urlArray.length;
		this.loadCurrentFile();
	}

	this.hideStartShowStopButton = function()
	{
		if(document.getElementById(this.startCtrlId) && document.getElementById(this.stopCtrlId))
		{
			document.getElementById(this.startCtrlId).style.display = "none";
			document.getElementById(this.stopCtrlId).style.display = "inline";
		}
	}
	
	this.showStartHideStopButton = function()
	{
		if(document.getElementById(this.startCtrlId) && document.getElementById(this.stopCtrlId))
		{
			document.getElementById(this.startCtrlId).style.display = "inline";
			document.getElementById(this.stopCtrlId).style.display = "none";
		}
	}	
	this.gotoItem = function(index)
	{
		if(index >= 0 && index < this.urlArray.length) this.currentIndex = index;
		this.loadCurrentFile();
	}
	this.stopSlideShow = function()
	{
		window.clearInterval(this.intervalHandle);
		this.intervalHandle = false;
		this.showStartHideStopButton();
		this.stoppedInternal = false; //Wird überschrieben falls nicht
	}

	this.startSlideShow = function()
	{
		//this.nextSlide();
		this.nextSlide();
		this.intervalHandle = window.setInterval(this.nextSlide.bind(this), this.delay);
		this.hideStartShowStopButton();
		this.stoppedInternal = false;
	}
	
	this.switchInternal = function(always)
	{
		if(always == true)
		{
			if(this.intervalHandle == false)
			{
				this.startSlideShow();
			}
			else
			{
				this.stopSlideShow();
			}
		}
		else
		{
			if((this.intervalHandle == false) && (this.stoppedInternal == true))
			{
				this.startSlideShow();
			}
			else if(this.intervalHandle != false)
			{
				this.stopSlideShow();
				this.stoppedInternal = true;
			}
		}
	}
	
	this.switchSlideShow = function()
	{
		this.switchInternal(true);
	}
	
	this.showControlBar = function()
	{
		var controlbar = document.getElementById(this.ctrlId);
		if(this.ctrlFadeOutHandle != false)
		{
			window.clearTimeout(this.ctrlFadeOutHandle);
			this.ctrlFadeOutHandle = false;
		}
		if(controlbar)
			controlbar.style.display = "block";
	}
	
	this.hideControlBar = function()
	{
		var controlbar = document.getElementById(this.ctrlId);
		if((controlbar) && (this.ctrlFadeOutHandle == false))
			this.ctrlFadeOutHandle = window.setTimeout(this.selfName + ".doHideControlBar()", 2000);
	}
	
	this.doHideControlBar = function()
	{
		document.getElementById(this.ctrlId).style.display = "none";
		this.ctrlFadeOutHandle = false;
	}
	
	this.showHighRes = function()
	{
		if(this.highResEnabled)
		{
			Slimbox.open(this.imageHighResArray[this.currentIndex].src, "");
			this.switchInternal(false);
		}
	}


// end of Methods
	
	
	if(this.urlArray.length < 2) {
		this.stopSlideShow();
		var controlbar = $(this.ctrlId);
		controlbar.toggleClass('hidden');
		controlbar.style.display = 'none';
		if(this.urlArray.length > 0)
		{
			this.loadCurrentFile();
		}
		else
		{
		}
	}
	else
	{
		if (this.showItemIndicator) {
			var iIndis = document.getElementById(this.indicatorId);
			this.itemIndicators = new Array(this.urlArray.length);
			for (var i = 0; i < this.urlArray.length; i++) {
				var img = document.createElement('img');
				img.src = this.itemIndicatorImg;
				img.onclick = this.gotoItem.bind(this, i);
				iIndis.appendChild(img);
				this.itemIndicators[i] = img;
			}
		}
		this.loadCurrentFile();
		if(pStart)
			this.startSlideShow();
		else
			this.stopSlideShow();
		this.showControlBar();
	}
	

}

function openslimbox(imgArray, imgElemId)
{
	var newArray = new Array();
	for(var i = 0; i < imgArray.length; i++)
	{
		if(imgArray[i] != '')
		{
			newArray.push(new Array(imgArray[i]));
		}
	}
	Slimbox.open(newArray, 0, {loop:true, counterText: "Bild {x} von {y}"});
}

