
var ki = 0;

window.addEventListener('load', function(){ki = new KIG();}, false);

function KIG(){
	
//------------------------------------Animation functions ---------------------------------

	var body = document.body;
	var continuewith = new GOON(new Array(), new Array());
	var active = 0;
	
	function GOON(funcnames, funcparams){
		this.pointer = -1;
		this.functions = funcnames;
		this.funcparams = funcparams;
	}
	
	function continue_(){
		continuewith.pointer++;
		if(continuewith.pointer >= continuewith.functions.length){
			active = 0;
			return;
		} else
			continuewith.functions[continuewith.pointer](continuewith.funcparams[continuewith.pointer]);
	}
	
	function $(id) {
		return document.getElementById(id);
	}
	
	function getStyle(el,styleProp)	{
				return document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
			}
	
	function findPos(obj) {
		if(getStyle(obj, "position") == "fixed")
			return [parseInt(getStyle(obj, "left")), parseInt(getStyle(obj, "top"))];
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return [curleft,curtop];
	}
	
	
	function getDim(obj) {
		var y = obj.offsetHeight;
		var x = obj.offsetWidth;
				x = x - parseInt(getStyle(obj, "padding-left")) - parseInt(getStyle(obj, "padding-right")) - parseInt(getStyle(obj, "border-left-width")) - parseInt(getStyle(obj, "border-right-width"));
		y = y - parseInt(getStyle(obj, "padding-top")) - parseInt(getStyle(obj, "padding-bottom")) - parseInt(getStyle(obj, "border-top-width")) - parseInt(getStyle(obj, "border-bottom-width"));
			
		return [x, y];
	}
	
	
	// continue function with params: show
	function shadebody(params) {
		var show = parseInt(params[0]);	
		if(show == 0){
			$("blende").style.display = "block";
			continue_();
		} else {
			$("blende").style.display = "none";
			continue_();
		}
	}
	
	
	// continue function with params: show
	function scrollbars(params) {
		var show = parseInt(params[0]);
	
		if(show == 0){
						body.style.overflow = "hidden";
						continue_();
		} else {
						body.style.overflow = "auto";
						continue_();
		}
	}
	
	// continue function with params: id, alpha, withcontinue
	function fade(params){
		var intfading;
		var actfade;
		var endfade;
		var fadeadd;
		var divtofade;
		var withcontinue = parseInt(params[2]);
		
		if(withcontinue == 1)active = 1;
	
		divtofade = $(params[0]);
				actfade = parseInt(getStyle(divtofade, "opacity")*100);
	
				endfade = parseInt(params[1]);
		if(endfade != 0)divtofade.style.display = "block";
		if(actfade == endfade){
			continue_();
			return;
		}
		if(actfade < endfade)
			fadeadd=15;
		else
			fadeadd=-15;
		intfading = setInterval( function() {
			actfade+=fadeadd;
						if(actfade == 100)
				divtofade.style.opacity = 0.99;
			else if(actfade == 0)
				divtofade.style.opacity = 0.01;
			else
				divtofade.style.opacity = actfade*0.01;
						if((actfade>=endfade && fadeadd>0)||(actfade<=endfade && fadeadd<0))
			{
				if(endfade == 0)divtofade.style.display = "none";
				clearInterval(intfading);
				if(withcontinue == 1)continue_();
			}
		}, 25);
	}
	
	
	// continue function with params: type, width, height, id, withmargin
	function resize(params) {
		var intresizing;
		var actx;
		var acty;
		var endx;
		var endy;
		var step;
		var stepsx;
		var stepsy;
		var width = parseInt(params[1]);
		var height = parseInt(params[2]);		
		var withmargin = parseInt(params[4]);
		var count;
		var divtoresize;
		
		active = 1;
		
		divtoresize = $(params[3]);
		divtoresize.style.display = "block";
		
		switch(parseInt(params[0])){
			case 1:
				divtoresize.style.height = height+"px";
				if(withmargin == 1)divtoresize.style.marginTop = -0.5*height+"px";
			break;
			case 2:
				divtoresize.style.width = width+"px";
				if(withmargin == 1)divtoresize.style.marginLeft = -0.5*width+"px";
			break;
		}
		var temp = getDim(divtoresize);
		count = 7;
		actx = temp[0];
		stepsx = new Array();
		stepsx.push(0);
		step = (width-actx)/count;
		endx = actx;
		while(step != 0){
			if( Math.abs(width-endx) < Math.abs(2*step) ){
				step = Math.round(0.4*step);
			}
			endx += step; 
			stepsx.push(step);
		}
		endx = stepsx.length;
		acty = temp[1];
		stepsy = new Array();
		stepsy.push(0);
		step = (height-acty)/count;
		endy = acty;
		while(step != 0){
			if( Math.abs(height-endy) < Math.abs(2*step) ){
				step = Math.round(0.4*step);
			}
			endy += step;
			stepsy.push(step);
		}
		endy = stepsy.length;
		if(endx > endy){
			for(var i = 0; i < endx - endy; i++)stepsy.push(0);
		} else {
			for(var i = 0; i < endy - endx; i++)stepsx.push(0);
			endx = endy;
		}
		step = 0;
		intresizing = setInterval( function(){
			actx+=stepsx[step];
			acty+=stepsy[step];
			divtoresize.style.visibility = 'hidden';			divtoresize.style.width = actx + "px";
			divtoresize.style.height = acty + "px";
			if(withmargin == 1){
				divtoresize.style.marginLeft = actx*-0.5 + "px";
				divtoresize.style.marginTop = acty*-0.5 + "px";
			}
			divtoresize.style.visibility = 'visible';			step++;	
			if(step == endx) {
				divtoresize.style.width = width + "px";
				divtoresize.style.height = height + "px";
				if(withmargin == 1){
					divtoresize.style.marginLeft = width*-0.5 + "px";
					divtoresize.style.marginTop = height*-0.5 + "px";
				}
				clearInterval(intresizing);
				continue_();
			}		
		}, 25);
	}
	
	
	// continue function with params: type, left, top, id, withcontinue
	function move(params) {
		var intmoving;
		var actx;
		var acty;
		var endx;
		var endy;
		var step;
		var stepsx;
		var stepsy;
		var count;
		var leftie;
		var topie;
		var divtomove;
		var withcontinue = parseInt(params[4]);
		
		if(withcontinue == 1)active = 1;
		
		divtomove = $(params[3]);
		divtomove.style.display = "block";
		
		if(getStyle(divtomove, "position") == "static"){
			divtomove.style.position = "absolute";
		}
		
		leftie = parseInt(params[1]);
		topie = parseInt(params[2]);
		switch(parseInt(params[0])){
			case 1:
				divtomove.style.top = topie+"px";
			break;
			case 2:
				divtomove.style.left = leftie+"px";
			break;
		}
		var temp = findPos(divtomove);
		count = 10;
		actx = temp[0];
		stepsx = new Array();
		stepsx.push(0);
		step = (leftie-actx)/count;
		endx = actx;
		while(step != 0){
			if( Math.abs(leftie-endx) < Math.abs(2*step) ){
				step = Math.round(0.4*step);
			}
			endx += step; 
			stepsx.push(step);
		}
		endx = stepsx.length;
		acty = temp[1];
		stepsy = new Array();
		stepsy.push(0);
		step = (topie-acty)/count;
		endy = acty;
		while(step != 0){
			if( Math.abs(topie-endy) < Math.abs(2*step) ){
				step = Math.round(0.4*step);
			}
			endy += step;
			stepsy.push(step);
		}
		endy = stepsy.length;
		if(endx > endy){
			for(var i = 0; i < endx - endy; i++)stepsy.push(0);
		} else {
			for(var i = 0; i < endy - endx; i++)stepsx.push(0);
			endx = endy;
		}
		step = 0;
		intmoving = setInterval( function(){
			divtomove.style.visibility = 'hidden';			actx+=stepsx[step];
			acty+=stepsy[step];
			divtomove.style.left = actx + "px";
			divtomove.style.top = acty + "px";
			divtomove.style.visibility = 'visible';			step++;	
			if(step == endx) {
				divtomove.style.left = leftie + "px";
				divtomove.style.top = topie + "px";
				clearInterval(intmoving);
				if(withcontinue == 1)continue_();
			}
		}, 25);
	}

//------------------------------------ Ajax functions -------------------------------------

	function initAjax(){
		try { 
			xmlhttp = new XMLHttpRequest();
		} catch (e) { 
			try { 
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) { 
				try	{ 
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
				} catch (e) { alert("WARNING: Your browser doesn't support AJAX. Please get Firefox at http://www.mozilla-europe.org/");	}
			}
		}
	}

	
	function getHTTP(adr, result, params) {
		if(waitingResponse == 0) {
			waitingResponse = 1;
			initAjax();
			xmlhttp.onreadystatechange = function() {
				if(xmlhttp.readyState == 4){
					if(xmlhttp.status == 200){
						waitingResponse = 0;
						if(result)result(params);
					}
				}
			}
			xmlhttp.open("POST", adr, true);
			xmlhttp.send('');
		}
	}
	
//------------------------------- Main Image functions ------------------------------------

	/* kind = 0: Bild komplett neu öffnen
	 kind = 1: Nur Resize durchführen */
	
	this.getImage = function(id, kind) {
		if(waitingResponse == 1)return;
		if(int_preloadingimgs != 0){
			clearInterval(int_preloadingimgs);
			int_preloadingimgs = 0;
		}
		cur_id = id.substring(0,2);
		if($(id).style.color)
			addforcomm = 18;
		else
			addforcomm = 0;
		bildhoehe = parseInt($(id).style.lineHeight)+12 + addforcomm;
		bildbreite = parseInt($(id).style.textIndent)+12;
				var aspect;
				if(bildhoehe > window.innerHeight){
			aspect = bildbreite/(bildhoehe-addforcomm);
			bildhoehe = window.innerHeight;
					bildbreite = parseInt(aspect*(bildhoehe-addforcomm));
		}
				if(bildbreite > window.innerWidth){
			aspect = (bildhoehe-addforcomm)/bildbreite;
			bildbreite = window.innerWidth;
					bildhoehe = parseInt(aspect*bildbreite+addforcomm);
		}
						var temp1 = parseInt(id.substring(4));
		cur_file = temp1;
		pln = temp1;
		nextid = $(cur_id + "ld"+(temp1+1));
		previd = $(cur_id + "ld"+(temp1-1));
		if(kind == 0){
			picturediv.innerHTML = "";
			picturediv.style.width = "0px";
			continuewith = new GOON(new Array(scrollbars, shadebody ,resize), new Array(new Array("0"), new Array("0") ,new Array(1 ,bildbreite, bildhoehe, "foto", 1)));
			continue_();
		} else {
			$("comm").style.display = "none";
			continuewith = new GOON(new Array(fade, resize), new Array(new Array("thepicture", 0, 1), new Array(0 ,bildbreite, bildhoehe, "foto", 1)));
			continue_();
		}
		getHTTP("ki/ki_base/ki_fullimg.php?reldir=ki/&file="+$(id).alt+"&x="+(bildbreite-12)+"&y="+(bildhoehe-12-addforcomm), ki.imgload, null);
	}
	
	
	this.imgload = function(){
		if(active == 1){
			setTimeout(function(){
				ki.imgload();
			}, 250);
			return;
		}	
		picturediv.innerHTML = xmlhttp.responseText;
		if(int_loadingimg == 0){
			int_loadingimg = setInterval(function(){
				if(active == 0){
					if($("thepicture").complete){
						clearInterval(int_loadingimg);
						int_loadingimg = 0;
						$("loadme").style.display = "none";
						$("comm").style.display = "block";
												fade(new Array("thepicture", 100, 0));
						startpreloading(cur_id);
						if(modus == 2){
							to_dia = setTimeout(nextdia, 5000);
						}
						if(modus <= 1){
							modus = 1;
							mousestatus = 0;
													}
					} else {
						$("loadme").style.display = "block";
					}
				}
			}, 250);
		}
	}
	
	
	this.closeImage = function() {
				modus = 0;
		removepreview();
		$("loadme").style.display = "none";
		$("kiarrow1").style.display = "none";
		$("kiarrow2").style.display = "none";
		$("loadme").src = "ki/ki_base/ki_loading.gif";
		if(int_loadingimg != 0){
			clearInterval(int_loadingimg);
			int_loadingimg = 0;
			startpreloading(cur_id);
		}
		if(to_dia != 0){
			clearTimeout(to_dia);
			to_dia = 0;
		}
		if($("comm"))$("comm").style.display = "none";
		if(active == 0){
			continuewith = new GOON(new Array(resize, shadebody, scrollbars), new Array(new Array(2, bildbreite, 0, "foto", 1), new Array("1"), new Array("1")) );
			fade(new Array("thepicture", 0, 1));
		} else {
			$("foto").style.display = "none";
			$("loadme").style.display = "none";
			shadebody(new Array("1"));
			scrollbars(new Array("1"));
		}
	}

//------------------------------- Navigationsfunktionen -----------------------------------
	
	this.nextsite = function(startfrom, id, direction) {
		if(int_preloadingimgs != 0){
			clearInterval(int_preloadingimgs);
			int_preloadingimgs = 0;
		}
		cur_id = id;
		cur_file = startfrom + 1;
		pln = startfrom;
		var always = "";
		if(pwright == 1)always = "&always=1";
		getHTTP("ki/ki_base/ki_koschtit.php?reldir=ki/"+always+"&gallery="+galleries[id-10].id+"&number="+id+"&startfrom="+startfrom, thumbspageloaded, direction );
	}

	
	function thumbspageloaded(direction) {
		var id = galleries[cur_id-10].id;
		
		$("koschtit"+cur_id).id = "switchingid";
		continuewith = new GOON(new Array( function(){$(id).removeChild($("switchingid"));startpreloading(cur_id);active = 0;} ), new Array(null) );
		
		if(direction == 1){
						$("switchingid").style.cssFloat = "left";
						$(id).innerHTML += xmlhttp.responseText;
			$("koschtit"+cur_id).style.width = "0px";
			$("switchingid").style.marginLeft = "-5px";
			resize(new Array(1, 0, getDim($("switchingid"))[1], "switchingid", 0));
			resize(new Array(1, parseInt($("switchingid").style.width), getDim($("switchingid"))[1], "koschtit"+cur_id, 0));
		} else {
						$("switchingid").style.cssFloat = "right";
						$(id).innerHTML += xmlhttp.responseText;
			$("koschtit"+cur_id).style.width = "0px";
			$("switchingid").style.marginLeft = "-5px";
			resize(new Array(1, 0, getDim($("switchingid"))[1], "switchingid", 0));
			resize(new Array(1, parseInt($("switchingid").style.width), parseInt($("switchingid").style.height), "koschtit"+cur_id, 0));
		}
	}	
	
	
	function startpreloading(id){
		var temp1 = $("koschtit"+id).childNodes;
		var temp2 = temp1.length;
		var temp3 = 0;
		for(var i = 0; i < temp2; i++){
			if(!temp1[i].alt)continue;
			temp3++;
		}
		if(id != load_id){
			preload = new Array();
			load_id = id;
		}
		int_preloadingimgs = setInterval( function() {
			if(waitingResponse == 0){
				if(preload[pln]){
					if(!preload[pln].complete){
						return;
					} else {
						if(pln == temp3-1){
							clearInterval(int_preloadingimgs);
							int_preloadingimgs = 0;
							return;
						}
						pln++;
					}
				}
				if(!preload[pln]){
					if($(load_id + "ld" + (pln+1))){
						preload[pln] = new Image();
						preload[pln].src = "ki/ki_galleries/"+$(load_id + "ld" + (pln+1)).alt;
						return;
					}
				}
			}
		}, 300);
	}
	
	
	this.diashow = function(nummer) {
		modus = 2;
		$("blende").style.cursor = "pointer";
		$("foto").style.cursor = "pointer";
		if(cur_id != nummer)cur_file = 1;
		cur_id = nummer;
		this.getImage(cur_id + "ld"+cur_file, 0);
	}
	
	
	function nextdia(){
		clearTimeout(to_dia);
		to_dia = 0;
		if(nextid != null){
			ki.getImage(cur_id + "ld" + nextid.id.substring(4), 1);
		} else {
			ki.getImage(cur_id + "ld1", 1);
		}
		$("comm").style.display = "none";
	}
	
			
//---------------------- Previewfunktionen ------------------------------------------------
	
	function viewdim(){
		if (self.innerHeight) // all except Explorer
		{
			windowX = self.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			windowX = document.documentElement.clientWidth;
		}
		else if (document.body) // other Explorers
		{
			windowX = document.body.clientWidth;
		}
	}
	
	
	this.mousemoved = function(aEvent) {
		if(modus == 1){
			aEvent = aEvent ? aEvent : window.event;
			var x = aEvent.clientX ? aEvent.clientX : aEvent.pageX;
			var y = aEvent.clientY ? aEvent.clientY : aEvent.pageY;
			oldx = x;
			oldy = y;
			triggerpreview(x,y);	
		}
	}
	
		
	function triggerpreview(x, y) {
		var temp = 0;
		if(mousestatus != 0)temp = 200;
		
		if(x > windowX - 35 - temp){
			mousestatus = 1;
			$("kiarrow2").style.display = "none";
		} else if(x < 40 + temp){
			mousestatus = -1;
			$("kiarrow1").style.display = "none";
		} else if(mousestatus != 0){
			mousestatus = 0;
			removepreview();
		}
		
		if(mousestatus == 1){
			if(y > pre_gre){
				if(pre_add > 8 -1)return;
				var temp = $(cur_id + "ld"+(cur_file+pre_add+1));
				if(temp != null){		
					$("ppl"+pre_add).style.left = windowX + 200 + "px";
					$("ppl"+pre_add).style.display = "block";
					$("ppl"+pre_add).innerHTML = "<img src='ki/ki_base/ki_makepic.php?file="+temp.alt+"' />";
					pre_gre+=10000;
					var tmp1 = pre_add;
					var tmp2 = mousestatus;
					var tmp3 = y;
					var damn = setInterval( function(){
						if(mousestatus != tmp2){
							clearInterval(damn);
							removepreview();
						}
						if($("ppl"+tmp1).firstChild.complete){
							clearInterval(damn);
							if(tmp1 == 0)pre_gre = tmp3 + 10000 - 20;
							move(new Array(1, windowX - 185 - 2, pre_gre-10000, "ppl"+tmp1, 0));
							pre_add++;
							pre_gre = pre_gre + 50 + $("ppl"+tmp1).firstChild.offsetHeight - 10000;
							triggerpreview(oldx, oldy);
						}
					}, 100);
				}
			}
		}
		
		if(mousestatus == -1){
			if(y > pre_gre){
				if(pre_add > 8 -1)return;
				var temp = $(cur_id + "ld"+(cur_file-pre_add-1));
				if(temp != null){
					$("ppl"+pre_add).style.left = -200 + "px";
					$("ppl"+pre_add).style.display = "block";
					$("ppl"+pre_add).innerHTML = "<img src='ki/ki_base/ki_makepic.php?file="+temp.alt+"' />";
					pre_gre+=10000;
					var tmp1 = pre_add;
					var tmp2 = mousestatus;
					var tmp3 = y;
					var damn = setInterval( function(){
						if(mousestatus != tmp2){
							clearInterval(damn);
							removepreview();
						}
						if($("ppl"+tmp1).firstChild.complete){
							clearInterval(damn);
							if(tmp1 == 0)pre_gre = tmp3 + 10000 - 20;
							move(new Array(1, 35 + 2, pre_gre-10000, "ppl"+tmp1, 0));
							pre_add++;
							pre_gre = pre_gre + 50 + $("ppl"+tmp1).firstChild.offsetHeight - 10000;
							triggerpreview(oldx, oldy);
						}
					}, 100);
				}
			}
		}
		
		if(mousestatus == 0){
			if(x > windowX*0.66) {
				$("thepicture").style.cursor = "auto";
				if(nextid != null){
					$("kiarrow2").style.marginLeft = bildbreite*0.5 + "px";
					$("kiarrow2").style.display = "block";
				}
			} else if(x < windowX*0.33){
				$("thepicture").style.cursor = "auto";
				if(previd != null){
					$("kiarrow1").style.marginLeft = -bildbreite*0.5 - 14 + "px";
					$("kiarrow1").style.display = "block";
				}
			} else {
				$("kiarrow1").style.display = "none";
				$("kiarrow2").style.display = "none";
				$("thepicture").style.cursor = "pointer";
			}
		}
	}
	
	
	this.previewclicked = function(aEvent, gotos) {
		if(waitingResponse == 0 && active == 0){
			mousestatus = 0;
			removepreview();	
			aEvent = aEvent ? aEvent : window.event;
			x = aEvent.clientX ? aEvent.clientX : aEvent.pageX;
			y = aEvent.clientY ? aEvent.clientY : aEvent.pageY;
			
			if(x > windowX*0.5){
				this.getImage(cur_id + "ld" + (cur_file + gotos), 1);
			} else if(x < windowX*0.5){
				this.getImage(cur_id + "ld" + (cur_file - gotos), 1);
			}
		}
	}
	
	
	function removepreview(){
			for(var j = 0; j < 8; j++){
				$("ppl"+j).style.display = "none";
			}
			pre_add = 0;
			pre_gre = 0;
			$("blende").style.cursor = "default";
	}	
	
	
	this.mouseclicked = function(aEvent) {
		if(mousestatus == 0 && modus == 1){
			aEvent = aEvent ? aEvent : window.event;
			x = aEvent.clientX ? aEvent.clientX : aEvent.pageX;
			y = aEvent.clientY ? aEvent.clientY : aEvent.pageY;
			
			if(x > windowX*0.66 && nextid != null){
				this.getImage(cur_id + "ld" + (cur_file + 1), 1);
				$("kiarrow2").style.display = "none";
			}
			else if(x < windowX*0.33 && previd != null){
				this.getImage(cur_id + "ld" + (cur_file - 1), 1);
				$("kiarrow1").style.display = "none";
			}
			else if(x > windowX*0.33 && x < windowX*0.66){
				ki.closeImage();
			}
		}
		if(modus == 2)ki.closeImage();
	}
	
//----------------------- Tastaturfunktionen ----------------------------------------------
	
	function preventDefaultAction(aEvent) {	
				aEvent.preventDefault();
					}
	
	
	this.taste = function(aEvent) {
		aEvent = aEvent ? aEvent : window.event;
		if(aEvent.keyCode == 27) {
			ki.closeImage();
						preventDefaultAction(aEvent);
						return false;
		}
		if(waitingResponse == 0 && active == 0 && modus != 0){
			if(aEvent.keyCode == 19 && modus == 2)
			{
				if(to_dia != 0){
					$("loadme").src = "ki/ki_base/ki_break.png";
					fade(new Array("loadme", 100, 0));
					clearTimeout(to_dia);
					to_dia = 0;
				} else {
					fade(new Array("loadme", 0, 0));
					$("comm").style.display = "none";
					nextdia();
				}
								preventDefaultAction(aEvent);
								return false;
			}
			if(aEvent.keyCode == 35 || aEvent.keyCode == 34) {
				if(nextid != null){
					if(to_dia != 0){
						clearTimeout(to_dia);
						to_dia = 0;
					}
					removepreview();
					$("comm").style.display = "none";
					ki.getImage(cur_id + "ld" + nextid.id.substring(4), 1);
				}
								preventDefaultAction(aEvent);
								return false;
			}
			if(aEvent.keyCode == 36 || aEvent.keyCode == 33) {
				if(previd != null){
					if(to_dia != 0){
						clearTimeout(to_dia);
						to_dia = 0;
					}
					removepreview();
					$("comm").style.display = "none";
					ki.getImage(cur_id + "ld" + previd.id.substring(4), 1);
				}
								preventDefaultAction(aEvent);
								return false;
			}
						fensterhoehe = window.innerHeight - bildhoehe;
			fensterbreite = window.innerWidth - bildbreite;
						if(aEvent.keyCode == 40){
								preventDefaultAction(aEvent);
								if(oversizey == 1 && posy >= fensterhoehe){
					picturediv.style.top = parseInt(picturediv.style.top) - 20 + "px";
					posy-=20;
					return false;
				}
			}
			if(aEvent.keyCode == 38){
								preventDefaultAction(aEvent);
								if(oversizey == 1 && posy <= 0){
					picturediv.style.top = parseInt(picturediv.style.top) + 20 + "px";
					posy+=20;
					return false;
				}
			}
			if(aEvent.keyCode == 39){
								preventDefaultAction(aEvent);
								if(oversizex == 1 && posx >= fensterbreite){
					picturediv.style.left = parseInt(picturediv.style.left) - 20 + "px";
					posx-=20;
					return false;
				}
			}
			if(aEvent.keyCode == 37){
								preventDefaultAction(aEvent);
								if(oversizex == 1 && posx != 0){
					picturediv.style.left = parseInt(picturediv.style.left) + 20 + "px";
					posx+=20;
					return false;
				}
			}
		}	
	}
	
//------------------------------ Setting up input functions -------------------------------

	window.addEventListener('resize', viewdim, false);
	viewdim();
		document.addEventListener('keypress', this.taste, false);
		document.addEventListener('mousemove', this.mousemoved, false);

//----------------------------- Creating maintags -----------------------------------------

	var node;
	
		if(!$("blende")){
				var node = document.createElement("div");
		node.style.width = "100%";
		node.style.height = "100%";
		node.style.background = "#000000";
		node.style.opacity = "0.3";
				node.id = "blende";
		node.style.position = "fixed";
		node.style.left = "0px";
		node.style.top = "0px";
		node.style.display = "none";
		node.style.zIndex = 100;
		node.onclick = function(event) { ki.mouseclicked(event); };
		body.appendChild(node);
	}
	
	if(!$("loadme")){
		node = document.createElement("img");
		node.id = "loadme";
		node.style.marginTop = "-15.5px";
		node.style.marginLeft = "-15.5px";
		node.style.position = "fixed";
		node.style.top = "50%";
		node.style.left = "50%";
		node.style.display = "none";
		node.src = "ki/ki_base/ki_loading.gif";
		node.alt = "";
		body.appendChild(node);
	}
	
	if(!$("kiarrow1")){
		node = document.createElement("img");
		node.id = "kiarrow1";
		node.style.marginTop = "-12px";
		node.style.position = "fixed";
		node.style.top = "50%";
		node.style.left = "50%";
		node.style.display = "none";
		node.src = "ki/ki_base/ki_back.png";
		node.alt = "";
		node.onclick = function(event) { ki.mouseclicked(event); };
		body.appendChild(node);
	}
	
	if(!$("kiarrow2")){
		node = document.createElement("img");
		node.id = "kiarrow2";
		node.style.marginTop = "-12px";
		node.style.position = "fixed";
		node.style.top = "50%";
		node.style.left = "50%";
		node.style.display = "none";
		node.src = "ki/ki_base/ki_next.png";
		node.alt = "";
		node.onclick = function(event) { ki.mouseclicked(event); };
		body.appendChild(node);
	}
	
	if(!$("foto")){
		node = document.createElement("div");
		node.id = "foto";
		node.className = "koschtit";
		node.onclick = function(event) { ki.mouseclicked(event); };
		body.appendChild(node);
	}
	
			if(!$("ppl0")){
			node = document.createElement("div");
			node.id = "ppl0";
			node.onclick = function(event) { ki.previewclicked(event, 1); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
			if(!$("ppl1")){
			node = document.createElement("div");
			node.id = "ppl1";
			node.onclick = function(event) { ki.previewclicked(event, 2); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
			if(!$("ppl2")){
			node = document.createElement("div");
			node.id = "ppl2";
			node.onclick = function(event) { ki.previewclicked(event, 3); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
			if(!$("ppl3")){
			node = document.createElement("div");
			node.id = "ppl3";
			node.onclick = function(event) { ki.previewclicked(event, 4); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
			if(!$("ppl4")){
			node = document.createElement("div");
			node.id = "ppl4";
			node.onclick = function(event) { ki.previewclicked(event, 5); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
			if(!$("ppl5")){
			node = document.createElement("div");
			node.id = "ppl5";
			node.onclick = function(event) { ki.previewclicked(event, 6); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
			if(!$("ppl6")){
			node = document.createElement("div");
			node.id = "ppl6";
			node.onclick = function(event) { ki.previewclicked(event, 7); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
			if(!$("ppl7")){
			node = document.createElement("div");
			node.id = "ppl7";
			node.onclick = function(event) { ki.previewclicked(event, 8); };
			node.style.position = "fixed";
			node.style.left = "-200px";
			node.style.top = "0px";
			node.className = "previewpic";
			body.appendChild(node);
		}
	
	if(!$("kithelp")){
		node = document.createElement("div");
		node.id = "kithelp";
		node.onmouseover = function() { ki.hidetooltip(); }
		body.appendChild(node);
	}
	
//------------------------------ Gallery Loading ------------------------------------------

	this.initgallery = function() {
		temp = document.getElementsByTagName("div");
		galleries = new Array();
		for(var i = 0; i < temp.length; i++){
			if(temp[i].className.indexOf("koschtitgallery") >= 0){
				galleries.push(temp[i]);
			}
		}
		if(galleries.length == 0){
			$("blende").style.display = "none";
			$("loadme").style.display = "none";
						return;
		} else {
			cur_id = 0;
			var always = "";
			if(ki.pwright == 1)always = "&always=1";
			getHTTP("ki/ki_base/ki_koschtit.php?reldir=ki/"+always+"&gallery="+galleries[0].id+"&number=10", showgallery, galleries[0].id );
		}
	}
	
	function showgallery(id) {
		$(id).innerHTML = xmlhttp.responseText;
		$(id).style.background = $("koschtit"+(10+cur_id)).style.background;
		$(id).style.width = $("koschtit"+(10+cur_id)).style.width;
		$("koschtit"+(10+cur_id)).style.display = "block";
		if(id == galleries[galleries.length-1].id){
						setTimeout( function(){
								$("loadme").style.opacity = "1";
							}, 700);
			cur_id = 10;
			startpreloading(10);
		} else {
			cur_id++;
			var always = "";
			if(ki.pwright == 1)always = "&always=1";
			getHTTP("ki/ki_base/ki_koschtit.php?reldir=ki/"+always+"&gallery="+galleries[cur_id].id+"&number="+(cur_id+10), showgallery, galleries[cur_id].id );
		}
	}

//---------------------------- Global Variables -------------------------------------------

	var xmlhttp;
	var waitingResponse = 0;

	var oldx;
	var oldy;
	var cur_path = "";
	var pw = "";
	var pwright = 0;
	var mousestatus = 0;
	var cur_id;
	var cur_file = 1;
	var pre_add = 0;
	var pre_gre = 0;
	var load_id;
	var pln = 0;
	var preload;
	var int_preloadingimgs = 0;
	var int_loadingimg = 0;
	var int_previewloading = 0;
	var to_dia = 0;
	var nextid;
	var previd;
	var addforcomm = 0;
	var oversizex;
	var oversizey;
	var posx;
	var posy;
	var windowX;
	var bildbreite;
	var bildhoehe;
	var numpic = 1;
	var modus = 0;
	var galleries;
	var temp;
	
	var picturediv = $("foto");

//-------------------------------- Admin Login --------------------------------------------	
	
		this.initgallery();
		
}