	var GCount = 0, GPos = 0, GDir, TO;
	var WaitTime = 3000, WaitPos;
	var loading1 = false, loading2 = false;
	var img;
	var PosText;
	var TextSrc;
	var paused = true;
	var Fullscreen = false;
	
	function ShowDiv (caption,id,close){
		var element;
		var i = 0;
		while (element = document.getElementById(caption+i)){
			if ((i == id)&&!(close&&(element.style.display == ''))) {element.style.display = '';}
			else {element.style.display = 'none';}
			i++;
		}
		if ((caption=='Content')&&(id != 2)) 
			Pause();
	}

	function Wait()
	{
		if (!loading2)
		{
			if (WaitPos < WaitTime)
			{
				WaitPos += 25;
				document.getElementById('TimeOut').style.width = 100*WaitPos/WaitTime+'px';
			}
				
			if ((WaitPos >= WaitTime)&&(!loading1))
			{
				ChangeImg();
				WaitPos = 0;
			}
		}
		TO = setTimeout("Wait()",25);
	}
	
	function Loaded()
	{
		loading1 = false;
	}
	
	function GalerieClick(dir,count)
	{
		clearTimeout(TO);
		GDir = 'img/Galerie/'+dir;
		GCount = count;
		document.getElementById('ImgPos').innerHTML = '1 / '+GCount;

		if (GCount == 2) GPos = 1;
		else GPos = 3;
			
		if (GCount >= 2)
		{
			loading2 = true;
			document.getElementById('Dia').src = GDir+'/1.jpg';
			
			loading1 = true;
			img = new Image();
			img.onLoad = Loaded();
			img.src = GDir+'/2.jpg';
			PosText = '2 / '+GCount;
			
			WaitPos = 0;
			TO = setTimeout("Wait()",25);

			paused = false;
			document.getElementById('PlayPauseImg').src = './img/Layout/pause.gif';
		}
		else
		{
			document.getElementById('Dia').src = GDir+'/1.jpg';
		}
		document.getElementById('TimeOut').style.width = '0px';
	}
	
	function ChangeImg()
	{
		loading2 = true;
		document.getElementById('Dia').src = img.src;
		document.getElementById('ImgPos').innerHTML = PosText;
		
		loading1 = true;
		img = new Image();
		img.onLoad = Loaded();
		img.src = GDir+'/'+GPos+'.jpg';
		TextSrc = 'load.php?c='+GDir+'/'+GPos+'.txt';
		PosText = GPos + ' / ' + GCount;
		
		GPos++;
		if (GPos > GCount) GPos = 1;
	}

	function MenuSelect()
	{
		document.getElementById('GMenu').style.display = 'none';
		document.getElementById('DiaControl').style.display = 'none';
	}
	
	function ShowControls()
	{
		if (GCount>1) document.getElementById('DiaControl').style.display = 'inline-block';
	}

	function HideControls()
	{
		document.getElementById('DiaControl').style.display = 'none';
	}
	
	function Minus()
	{
		if (WaitTime>1000) WaitTime -= 1000;
		document.getElementById('WaitTime').innerHTML = ''+(WaitTime/1000)+'s';
	}
	
	function Plus()
	{
		if (WaitTime<9000) WaitTime+=1000;
		document.getElementById('WaitTime').innerHTML = (WaitTime/1000)+'s';
	}
	
	function PlayPause()
	{
		if ((paused)&&(GCount > 1))
		{
			// Play
			paused = false;
			document.getElementById('PlayPauseImg').src = './img/Layout/pause.gif';
			Wait();
		}
		else Pause();
	}

	function Pause(){
		paused = true;
		document.getElementById('PlayPauseImg').src = './img/Layout/play.gif';
		clearTimeout(TO);
	}

	function Next()
	{
		if (GCount > 1)
		{
			ChangeImg();

			if (!((paused)&&(GCount > 1)))
				Pause();
		}
	}

	function Previous()
	{
		if (GCount > 1)
		{
			GPos -= 3;
			if (GPos < 1) GPos += GCount;
			
			loading1 = true;
			img = new Image();
			img.onLoad = Loaded();
			img.src = GDir+'/'+GPos+'.jpg';
			PosText = GPos + ' / ' + GCount;
			
			GPos++;
			if (GPos > GCount) GPos = 1;
			
			ChangeImg();

			if (!((paused)&&(GCount > 1)))
				Pause();
		}
	}
	
	function toggleFullscreen()
	{
		var DiaDiv = document.getElementById('DiaDiv').style;
		var Dia = document.getElementById('Dia');
		var DiaControl = document.getElementById('DiaControl').style;
		var Parent = document.getElementById('DiaParent').style;
		var Folders = document.getElementById('Folders').style;
		var DiaTable = document.getElementById('DiaTable').style;
		
		Fullscreen = !Fullscreen;
		
		if (Fullscreen){
			Folders.display = 'none';
			Parent.width = '800px';
			Parent.height = '600px';
			Dia.style.maxWidth = '800px';
			Dia.style.maxHeight = '600px';
			DiaTable.width = '800px';
			DiaTable.height = '600px';
			DiaDiv.width = '800px';
			Dia.className = 'Dia2';
		}
		else{
			Folders.display = '';
			Parent.width = '480px';
			Parent.height = '360px';
			Dia.style.maxWidth = '480px';
			Dia.style.maxHeight = '360px';
			DiaTable.width = '480px';
			DiaTable.height = '360px';
			Dia.className = 'Dia1';
			DiaDiv.width = '480px';
		}
	}
	
	function editCaption(id,caption){
		var newCaption = prompt("Beschriftung des Eintrags ändern:", caption);
		if ((newCaption != caption)&&(newCaption.length > 0)) window.location.href = 'edit.php?caption='+newCaption+'&id='+id;
		else alert('Keine Änderung erfolgt.');
		
	}
	
	function editPos(id){
		var newPos = prompt("Neue Position eingeben:", "");
		newPos;
		if ((newPos-1 != id)&&(newPos.length > 0)) window.location.href = 'edit.php?pos='+(newPos-1)+'&id='+id;
		else alert('Keine Änderung erfolgt.');
		
	}	
	
	function addLabel(){
		var newLabel = prompt("Beschriftung der Kategorie:", "");
		if ((newLabel.length > 0)) window.location.href = 'edit.php?newLabel='+newLabel;
		else alert('Keine Änderung erfolgt.');
		
	}

