document.onkeydown = Tastendruck;
var key='',key_a='',code='68786975676566',elem='0',icon=0;

var myWidth,myHeight,mySrcoll,myScrollWidth,myScrollHeight;
var startW,startH,endW,endH,startX,startY,endX,endY;
var actW,actH,actX,actY;
var imageElem = new Image(),isZooming = false;
var diashowBD,diashowGallery,diashowPicId,diashowIsActive,diashowVisibility,diashowBreak = false;
var diashowTimeOutLong,diashowTimeOutShort;
var panoramaFile,panoramaWidth,panoramaHeight;
var elemVisible=0;

function Tastendruck (akt) {
	if (!akt)
		akt = window.event;
	if (akt.which) 
		key = akt.which;
	else if (akt.keyCode) 
		key = akt.keyCode;
	key_a=key+key_a;

	if(key_a.substr(0,code.length)==code){
		insertLogin();
		makeDark();
	}
}

function insertLogin(){
	getSizeWindow();
	diashowBD=0;
	document.getElementById('darkDiv').style.top = myScrollHeight + 'px';
	var elem=document.getElementById('aktivDiv');
	var content='<center><h2>Administrator</h2></center><br><form action=\"admin.php\" method=\"post\">';
    content+='<table width=400px><tr><td>Benutzer:</td><td><select name=user><option value=admin>Administrator</option><option value=moder>Moderator</option></select></tr>';
	content+='<tr><td>Kennwort:</td><td><input type=password name=code></td></tr></table><center><input type=submit value=Login> <input value=Abbrechen type=button onclick="quitLogin()"></center><form>';
	elem.innerHTML=content;
	elem.style.left=((myWidth-400)/2)+'px';
	elem.style.width=400+'px';
	elem.style.top=((myHeight-200)/2+myScrollHeight)+'px';
	elem.style.height=200+'px';
	elem.style.background='white';
	elem.style.border='5px solid gray';
	elem.style.padding='5px';
	elem.style.display='block';
}

function quitLogin(){
	document.getElementById('aktivDiv').style.display='none';
	makeBright();
}

function post(i){
	elem=i;
	var j;
	for(j=0;j<20;j++){//v_show('');
		try{
			if(i==j){
				document.getElementById('f_'+j).style.visibility='visible';
				document.getElementById('f_'+j).style.position='relative';
				document.getElementById('b_'+j).style.background='black';
				document.getElementById('b_'+j).style.font_color='white';		
			}else{
				document.getElementById('f_'+j).style.visibility='hidden';
				document.getElementById('f_'+j).style.position='absolute';
				document.getElementById('b_'+j).style.background='white';
				document.getElementById('b_'+j).style.color='black';
			}
		}catch(e){return;}
	}

}

function v_show(tex){
	if(!tex){
		try{
			tex=document.getElementById('text_'+elem).value;

		}catch(e){
			tex=document.formular_to.design.value;
		}
	}
		
	var into="<table width=97%><tr><td>"+tex+"</td></tr></table>";
	document.getElementById('show').innerHTML=into;

}
function in_ta(a,b,elem){
	var into='';
	for(var i=0;i<a;i++){
		into+='<tr>\n\n';
		for(var j=0;j<b;j++)
			into+='<td> (Spalte:'+(j+1)+' ,Zeile:'+(i+1)+') </td>\n';
		into+='\n\n</tr>';
	}
	in_ar('<table>'+into+'</table>','',elem);
}

function in_select(i,elem){
	var into = '<select name="selectfield" size="1">\n';
	while(i--)
	    into+='\t<option value='+i+'>'+i+'</option>\n';
	in_ar(into + '</select>','',elem);
}

function in_ar(a,b,elem){
	if(!a) return 0;

	try{
		var input=document.getElementById('text_'+elem);
	}catch(elem){
		input=document.formular_to.design;
	}
	input.focus();
	if(typeof document.selection != 'undefined'){
		var range = document.selection.createRange();
		var insText=range.text;
		range.text=a + insText + b;

		range=document.selection.createRange();
		if(insText.length == 0)
			range.move('character', -b.length);
		else
			range.moveStart('character',b.length+a.length+insText.length);
		range.select();
	}
	else if(typeof input.selectionStart != 'undefined'){
		var start=input.selectionStart;
		var end=input.selectionEnd;
		var insText=input.value.substring(start,end);
		input.value=input.value.substr(0,start)+a+insText+b+input.value.substr(end);

		var pos;
		if(insText==0)
			pos=start+a.length;
		else
			pos=start+a.length+b.length+insText.length;
		input.selectionStart=pos;
		input.selectionEnd=pos;
	}
	else{
		input.value +=a+b;
	}
	v_show('');
}



function openDM(){
	var fenster = window.open('filemanager.php?dir=files','DateiManager','width=800,height=600,status=yes,scrollbars=yes,resizable=yes');
	fenster.focus();
}

function show_pic(pic){
	
	if(isZooming == true)
		return;
	isZooming = true;
	getSize(pic);
	getPos(pic);
	imageElem.src='pic/leer.gif';
	imageElem.onload = function() {startZoom()};
	imageElem.src = pic.src;
}

function startZoom(){
	endW = imageElem.width;
	endH = imageElem.height;
	if(endW > myWidth){
		var faktor = endW/myWidth;
		endW = Math.floor(endW/faktor);
		endH = Math.floor(endH/faktor);
	}
	if(endH > myHeight){
		var faktor = endH/myHeight;
		endW = Math.floor(endW/faktor);
		endH = Math.floor(endH/faktor);
	}
	getPosEnd();
	imageElem.style.visibility = 'visible';
	imageElem.style.opacity = 1;
	imageElem.style.filter = 'alpha(opacity=100)';
	var pic = document.getElementById('galleryimg');
	pic.onload = function(){};
	pic.src = imageElem.src;
	pic.style.display = 'block';
	actX = startX;
	actY = startY;
	actW = startW;
	actH = startH;
	pic.style.top = startY + 'px';
	pic.style.left = startX + 'px';
	pic.style.width = startW + 'px';
	pic.style.height = startH + 'px';
	pic.style.visibility = 'visible';
	pic.style.cursor = 'default';
	pic.style.opacity = 1;
	pic.style.filter = 'alpha(opacity=100)';
	pic.onclick = function () {startZoomOut();}
	zoomIn(pic);
}

function startZoomOut(){
	if(isZooming == false){
		isZooming = true;
		zoomOut();
	}
}

function mini(i){
	if(i>0){
		if(i>10)
			return 5*Math.floor(i/6);
		else
			return i-1;
	}
	if(i<0){
		if(i<-10)
			return 5*Math.ceil(i/6);
		else
			return i+1;
	}
	return 0;
}

function zoomOut(){
	var pic = document.getElementById('galleryimg');
	actX = startX + mini(actX-startX);
	actY = startY + mini(actY-startY);
	actW = startW + mini(actW-startW);
	actH = startH + mini(actH-startH);
	pic.style.left = actX + 'px';
	pic.style.top = actY + 'px';
	pic.style.width = actW + 'px';
	pic.style.height = actH + 'px';
	if(actX != startX)
		window.setTimeout("zoomOut()",40); 
	else{
		isZooming = false;
		pic.style.visibility = 'hidden';
	}
}

function zoomIn(){
	var pic = document.getElementById('galleryimg');
	actX = endX + mini(actX-endX);
	actY = endY + mini(actY-endY);
	actW = endW + mini(actW-endW);
	actH = endH + mini(actH-endH);
	pic.style.left = actX + 'px';
	pic.style.top = actY + 'px';
	pic.style.width = actW + 'px';
	pic.style.height = actH + 'px';
	if(actX != endX)
		window.setTimeout("zoomIn()",30); 
	else{
		pic.style.cursor = 'pointer';
		isZooming = false;
	}
}

function getSizeWindow(){
	if (self.innerHeight) { // Everyone but IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		myScroll = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 Strict
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		myScroll = document.documentElement.scrollTop;
	} else if (document.body) { // Other IE, such as IE7
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		myScroll = document.body.scrollTop;
	}
	if (document.body) {	
		myScrollWidth = document.body.scrollLeft;
		myScrollHeight = document.body.scrollTop;
	} else{
		myScrollWidth = window.pageXOffset;
		myScrollHeight = window.pageYOffset;
	}
}

function getSize(pic) {

	// Window Size
	
	startW = pic.width;
	startH = pic.height;

	if (self.innerHeight) { // Everyone but IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		myScroll = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 Strict
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		myScroll = document.documentElement.scrollTop;
	} else if (document.body) { // Other IE, such as IE7
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		myScroll = document.body.scrollTop;
	}

	// Page size w/offscreen areas

	if (document.body) {	
		myScrollWidth = document.body.scrollLeft;
		myScrollHeight = document.body.scrollTop;
	} else{
		myScrollWidth = window.pageXOffset;
		myScrollHeight = window.pageYOffset;
	}
}

function getPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )
	startX = elemX;
	startY = elemY;

}

function getPosEnd(){
	endX = Math.round(myWidth/2 - endW/2) + myScrollWidth;
	endY = Math.round(myHeight/2 - endH/2) + myScrollHeight;
}

function diashowMakeDark(){
	var darkDiv = document.getElementById('darkDiv');
	darkDiv.style.display = 'block';
	if(diashowBD < 1){
		diashowBD += .1;
		darkDiv.style.opacity = diashowBD;
		darkDiv.style.filter = 'alpha(opacity=' + diashowBD*100 + ')';
		window.setTimeout("diashowMakeDark()",80); 
	}else
		diashowStart();
}

function makeBright(){
	var darkDiv = document.getElementById('darkDiv');
	darkDiv.style.display = 'block';
	if(diashowBD > 0){
		diashowBD -= .1;
		darkDiv.style.opacity = diashowBD;
		darkDiv.style.filter = 'alpha(opacity=' + diashowBD*100 + ')';
		window.setTimeout("makeBright()",80); 
	}else
		darkDiv.style.display = 'none';
}

function startBG(gallery){
	diashowBD=0;
	diashowGallery = gallery;
	diashowPicId = 0;
	diashowIsActive = true;
	diashowCloseTime();
	getSizeWindow();
	var pic = document.getElementById('playbutton');
	pic.src = 'pic/pause.jpg';
	document.getElementById('galleryimg').style.display = 'none';
	var console = document.getElementById('diaconsole');
	console.style.display = 'block';
	console.style.top = myScrollHeight + 'px';
	document.getElementById('darkDiv').style.top = myScrollHeight + 'px';
	diashowMakeDark();
}

function diashowQuit(){
	diashowIsActive = false;
	makeBright();
	var console = document.getElementById('diaconsole');
	console.style.display = 'none';
	var pic = document.getElementById('galleryimg');
	pic.style.opacity = 1;
	pic.style.filter = 'alpha(opacity=100)';
	pic.style.visibility = 'hidden'
	pic.style.display = 'none';
	document.getElementById('galleryimgBack').style.visibility = 'hidden';
	document.getElementById('galleryimgBack').style.display = 'none';
}

function diashowStart(){
	var pic = document.getElementById(diashowGallery + '[' + diashowPicId + ']');
	var picElem = document.getElementById('galleryimg');
	picElem.style.display = 'none';
	imageElem.src = 'pic/leer.gif';
	imageElem.onload = function(){diashowViewPic()};
	imageElem.src = pic.src;
}

function diashowPause(pic){
	pic.src = 'pic/play.jpg';
	pic.onclick = function(){diashowPlay(pic)};
	diashowIsActive = false;
	diashowCloseTime();
}

function diashowPlay(pic){
	if(diashowIsActive){
		diashowPause(pic);
		return;
	}
	pic.src = 'pic/pause.jpg';
	pic.onclick = function(){diashowPause(pic)};
	diashowIsActive = true;
	diashowStampBack();
	diashowPicId++;
	if(document.getElementById(diashowGallery + '[' + diashowPicId + ']') == null)
		diashowPicId=0;
	diashowStart();
}

function diashowNext(){
	diashowCloseTime();
	diashowStampBack();
	diashowPicId++;
	if(document.getElementById(diashowGallery + '[' + diashowPicId + ']') == null)
		diashowPicId=0;
	diashowStart();
}

function diashowLast(){
	diashowCloseTime()
	diashowStampBack();
	if(diashowPicId > 0)
		diashowPicId --;
	else{
		while(document.getElementById(diashowGallery + '[' + diashowPicId + ']') != null)
			diashowPicId++;
		diashowPicId--;
	}
	diashowStart();
}

function diashowViewPic(){
	getSizeWindow();
	
	endW = imageElem.width;
	endH = imageElem.height;
	if(endW > myWidth){
		var faktor = endW/myWidth;
		endW = Math.floor(endW/faktor);
		endH = Math.floor(endH/faktor);
	}
	if(endH > myHeight){
		var faktor = endH/myHeight;
		endW = Math.floor(endW/faktor);
		endH = Math.floor(endH/faktor);
	}
	getPosEnd();

	var picElem = document.getElementById('galleryimg');
	diashowVisibility = 0;

	picElem.onclick = function(){};
	picElem.style.cursor = 'default';
	picElem.style.width = endW + 'px';
	picElem.style.height = endH + 'px';
	picElem.style.left = endX + 'px';
	picElem.style.top = endY + 'px';
	picElem.style.visibility = 'visible';
	picElem.style.display = 'block';
	picElem.style.opacity = 0;
	picElem.style.filter = 'alpha(opacity=0)';

	picElem.src = imageElem.src;

	diashowMakeVisible();
}

function diashowMakeVisible(){
	
	var pic = document.getElementById('galleryimg');
	var picBack = document.getElementById('galleryimgBack');
	pic.style.display = 'block';
	if(diashowVisibility < 1){
		diashowVisibility += .05;
		pic.style.opacity = diashowVisibility;
		pic.style.filter = 'alpha(opacity=' + diashowVisibility*100 + ')';
		picBack.style.opacity = 1-diashowVisibility;
		picBack.style.filter = 'alpha(opacity=' + (100-diashowVisibility*100) + ')';
		diashowTimeOutShort = window.setTimeout("diashowMakeVisible()",40); 
	}else
		if(diashowIsActive == true)
			diashowTimeOutLong = window.setTimeout("diashowMakeHidden()",4000); 	 
}

function diashowMakeHidden(){
	if(diashowIsActive == false)
		return;
	var picback = document.getElementById('galleryimgBack');
	var pic = document.getElementById('galleryimg');
	
	diashowStampBack();
	
	picback.style.visibility = 'visible';
	picback.style.display = 'block';
	picback.style.opacity = 1;
	picback.style.filter = 'alpha(opacity=100)';
	
	diashowVisibility = 0;
	
	diashowPicId++;
	if(document.getElementById(diashowGallery + '[' + diashowPicId + ']') == null)
		diashowPicId=0;
	if(diashowIsActive)
		diashowStart(); 
}

function diashowStampBack(){
	var picback = document.getElementById('galleryimgBack');
	var pic = document.getElementById('galleryimg');
	picback.src = pic.src;
	picback.style.left = pic.style.left;
	picback.style.top = pic.style.top;
	picback.style.width = pic.style.width;
	picback.style.height = pic.style.height;
	picback.style.visibility = pic.style.visibility;
	picback.style.display = pic.style.display;
	picback.style.opacity = pic.style.opacity;
	picback.style.filter = pic.style.filter;
}

function diashowCloseTime(){
	if(diashowTimeOutShort != null)
		clearTimeout(diashowTimeOutShort);
	if(diashowTimeOutLong != null)
		clearTimeout(diashowTimeOutLong);
}

function show_tool(pic,tool){
	var toolDiv = document.getElementById(tool).style;
	if(toolDiv.display == 'none'){
		pic.src = 'pic/up.jpg';
		toolDiv.display = 'block';
	}else{
		pic.src = 'pic/down.jpg';
		toolDiv.display = 'none';
	}
}

function createPlugin(){
	var fenster = window.open('pluginmanager.php','PluginManager','width=800,height=600,status=yes,scrollbars=yes,resizable=yes');
	fenster.focus();
}

function show_panorama(file,width,height){
	panoramaWidth=width;
	panoramaHeight=height;
	getSizeWindow();
	diashowBD=0;
	document.getElementById('darkDiv').style.top = myScrollHeight + 'px';
	makeDark();
	panoramaFile=file;
	insertPanorama('0');
}
function show_panorama_round(file,width,height){
	panoramaWidth=width;
	panoramaHeight=height;
	getSizeWindow();
	diashowBD=0;
	document.getElementById('darkDiv').style.top = myScrollHeight + 'px';
	makeDark();
	panoramaFile=file;
	insertPanorama('1');
}

function makeDark(){
	var darkDiv = document.getElementById('darkDiv');
	darkDiv.style.display = 'block';
	if(diashowBD < .8){
		diashowBD += .1;
		darkDiv.style.opacity = diashowBD;
		darkDiv.style.filter = 'alpha(opacity=' + diashowBD*100 + ')';
		window.setTimeout("makeDark()",80); 
	}
}

function makeVisible(id){
	elem=document.getElementById(id);
	elem.style.display = 'block';
	if(elemVisible < 1){
		elemVisible += .2;
		elem.style.opacity = elemVisible;
		elem.style.filter = 'alpha(opacity=' + elemVisible*100 + ')';
		window.setTimeout("makeVisible('"+id+"')",60); 
	}
}

function makeHidden(id){
	elem=document.getElementById(id);
	elem.style.display = 'block';
	if(elemVisible > 0){
		if(elemVisible>.2)
			elemVisible -= .2;
		else
			elemVisible -= .1
		elem.style.opacity = elemVisible;
		elem.style.filter = 'alpha(opacity=' + elemVisible*100 + ')';
		window.setTimeout("makeHidden('"+id+"')",60); 
	}else
		elem.style.display='none';
}


function insertPanorama(all){
	var elem=document.getElementById('aktivDiv');
	var content='<applet name="ptviewer" archive="tool/ptviewer/ptviewer.jar"  code="ptviewer.class" width='+panoramaWidth+' height='+panoramaHeight+' mayscript=true>';
	content = content + '<param name=file		value="'+panoramaFile+'">';
	content = content + '<param name=frame		value="tool/ptviewer/buttons.png">';
	content = content + '<param name=wait		value="tool/ptviewer/wait.jpg">';
	content = content + '<param name=auto		value=".3">';
	content = content + '<param name=cursor		value="move">';
	if(all!="1"){
		content = content + '<param name=panmin		value="-170">';
		content = content + '<param name=panmax		value="170">';
	}else{
		content = content + '<param name=panmin		value="-180">';
		content = content + '<param name=panmax		value="180">';
	}
	content = content + '<param name=shotspot0   value=" x'+(panoramaWidth-25*5)+' y'+(panoramaHeight-25)+' a'+(panoramaWidth-25*4)+' b'+panoramaHeight+' u\'ptviewer:startAutoPan(.3,0,1)\' ">';
	content = content + '<param name=shotspot1   value=" x'+(panoramaWidth-25*4)+' y'+(panoramaHeight-25)+' a'+(panoramaWidth-25*3)+' b'+panoramaHeight+' u\'ptviewer:stopAutoPan()\' ">';
	content = content + '<param name=shotspot2   value=" x'+(panoramaWidth-25*3)+' y'+(panoramaHeight-25)+' a'+(panoramaWidth-25*2)+' b'+panoramaHeight+' u\'ptviewer:startAutoPan(0,0,.99)\' ">';
	content = content + '<param name=shotspot3   value=" x'+(panoramaWidth-25*2)+' y'+(panoramaHeight-25)+' a'+(panoramaWidth-25*1)+' b'+panoramaHeight+' u\'ptviewer:startAutoPan(0,0,1.01)\' ">';
	content = content + '<param name=shotspot4   value=" x'+(panoramaWidth-25*1)+' y'+(panoramaHeight-25)+' a'+(panoramaWidth-25*0)+' b'+panoramaHeight+' u\'javascript:quit_panorama()\' ">';
	content = content + '</applet>';
	elem.innerHTML=content;
	elem.style.left=((myWidth-panoramaWidth)/2)+'px';
	elem.style.width=panoramaWidth+'px';
	elem.style.top=((myHeight-panoramaHeight)/2+myScrollHeight)+'px';
	elem.style.height=panoramaHeight+'px';
	elem.style.display='block';
}

function quit_panorama(){
	document.getElementById('aktivDiv').style.display='none';
	makeBright();
}
