
var ki = 0;
window.addEventListener('load', function(){ki = new KIG();ki.initialize();}, false);

function KIG(){

//------------------------------------ Globals ----------------------------------------------
var body;
var animation = new Array();
var intanimation = 0;

var galleries = new Array();
var cur_gallery = -1;
var cur_file = 1;
var cur_files = 0;

var preloading = new Array(-1, null);
var cur_load;
var loaded = 0;
var int_preloading;

var windowX;
var windowY;
var modus = 0;
var mousestatus = 0;
var oldx;
var oldy;
var pre_add = 0;
var pre_gre = 0;
var to_slideshow;
var oversizex;
var oversizey;
var posx;
var posy;
var firstrun = 1;
//------------------------------------ End Globals/Start main Animation functions ------------------------------------------

function ANIOBJ(type_, aniarray_, original_, after_){
	this.type = type_;
	this.aniarray = aniarray_;
	this.original = original_;
	this.after = after_;
	this.step = 0;
	if(type_ == 0 || type_ == 1 || type_ == 2)this.aniarraylength = aniarray_.length/2;
	if(type_ == 3)this.aniarraylength = aniarray_.length;
}

function doanimation(){
	if(animation.length == 0){
		clearInterval(intanimation);
		intanimation = 0;
		return;
	}
	if(intanimation == 0){
		intanimation = setInterval( doanimation, 30 );
	} else {
		var i;
		for(i = 0; i < animation.length; i++){
			var actobj = animation[i];
			if( actobj.step >= actobj.aniarraylength ){
				animation.splice(i, 1);
				if(i != animation.length-1)i--;
				if(actobj.type == 3){
					if(actobj.aniarray[actobj.step-1] == 0)
						actobj.original.style.display = "none";
									}
				if(actobj.after != null){
					actobj.after[0](actobj.after[1], actobj.after[2], actobj.after[3], actobj.after[4], actobj.after[5], actobj.after[6]);
				}
				continue;
			}
			if(actobj.type == 0){
				actobj.original.style.left = actobj.aniarray[2*actobj.step] + "px";
				actobj.original.style.top = actobj.aniarray[2*actobj.step+1] + "px";
				actobj.step++;
				continue;
			}
			if(actobj.type == 1){
				actobj.original.style.width = actobj.aniarray[2*actobj.step] + "px";
				actobj.original.style.height = actobj.aniarray[2*actobj.step+1] + "px";
				actobj.step++;
				continue;
			}
			if(actobj.type == 2){
				actobj.original.style.width = actobj.aniarray[2*actobj.step] + "px";
				actobj.original.style.height = actobj.aniarray[2*actobj.step+1] + "px";
				actobj.original.style.marginLeft = -parseInt(0.5*actobj.aniarray[2*actobj.step]) + "px";
				actobj.original.style.marginTop = -parseInt(0.5*actobj.aniarray[2*actobj.step+1]) + "px";
				actobj.step++;
				continue;
			}
			if(actobj.type == 3){
								actobj.original.style.opacity = actobj.aniarray[actobj.step]*0.01;
								actobj.step++;
				continue;
			}
		}
	}
}

//----------------------------------- Helper functions --------------------------------------------

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" || getStyle(obj, "position") == "absolute")
		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];
}

//----------------------------------- End Helper functions / Start object animation functions --------------------------------------------

function shadebody(show, continueobj) {
	if(show == 1){
				body.style.overflow = "hidden";
				$("blende").style.display = "block";
	} else {
				body.style.overflow = "auto";
				$("blende").style.display = "none";
	}
	if(continueobj != null)continueobj[0](continueobj[1], continueobj[2], continueobj[3], continueobj[4], continueobj[5], continueobj[6]);
}


function fade(id, endfade, continueobj){

	var count = 5;

	var obj = $(id);
	if(!obj){
		if(continueobj != null)continueobj[0](continueobj[1], continueobj[2], continueobj[3], continueobj[4], continueobj[5], continueobj[6]);
		return;
	}

	var actfade;
	if(obj.style.display == "none"){
		actfade = 0;
	} else {
				actfade = parseInt(getStyle(obj, "opacity")*100);
			}

		obj.style.opacity = actfade*0.01;
		obj.style.display = "block";

	var step;
	var ma = new Array();
	
	step = Math.round((endfade - actfade)/count);
	for(var i = 0; i < count; i++){
		ma.push(actfade+i*step);
	}
	ma.push(endfade);

	animation.push( new ANIOBJ(3, ma, obj, continueobj) );
	if(intanimation == 0)doanimation();
}


// type: 1 = normal, 2 = horizontal zentriert
function resize(id, endwidth, endheight, type, continueobj) {

	var count = 10;

	var obj = $(id);
	if(!obj){
		if(continueobj != null)continueobj[0](continueobj[1], continueobj[2], continueobj[3], continueobj[4], continueobj[5], continueobj[6]);
		return;
	}

	obj.style.display = "block";

	var temp = getDim(obj);
	var actw = temp[0];
	var acth = temp[1];

	var step;
	var mawidth = new Array();
	var maheight = new Array();
	var ma = new Array();
	
	step = Math.round((endwidth - actw)/count);
	while(step != 0){
		if( Math.abs(endwidth-actw) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		actw += step; 
		mawidth.push(actw);
	}
	mawidth.push(endwidth);
	step = Math.round((endheight - acth)/count);
	while(step != 0){
		if( Math.abs(endheight-acth) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		acth += step;
		maheight.push(acth);
	}
	maheight.push(endheight);
	if(mawidth.length > maheight.length){
		var diff = mawidth.length - maheight.length;
		for(var i = 0; i < diff; i++)maheight.push(endheight);
	} else {
		var diff = maheight.length - mawidth.length;
		for(var i = 0; i < diff; i++)mawidth.push(endwidth);
	}
	for(var i = 0; i < maheight.length; i++){
		ma.push(mawidth[i]);
		ma.push(maheight[i]);
	}	

	animation.push( new ANIOBJ(type, ma, obj, continueobj) );
	if(intanimation == 0)doanimation();
}


function move(id, endleft, endtop, continueobj) {

	var count = 10;

	var obj = $(id);
	if(!obj){
		if(continueobj != null)continueobj[0](continueobj[1], continueobj[2], continueobj[3], continueobj[4], continueobj[5], continueobj[6]);
		return;
	}

	obj.style.display = "block";

	var actx = acty = 0;
	if(obj.style.position == "absolute" || obj.style.position == "fixed"){
		actx = parseInt(obj.style.left);
		acty = parseInt(obj.style.top);
	} else {
		if (obj.offsetParent) {
			actx = obj.offsetLeft;
			acty = obj.offsetTop;
		}
	}

	if(obj.style.position != "fixed")obj.style.position = "absolute";
	obj.style.left = actx + "px";
	obj.style.top = acty + "px";
	obj.style.margin = "0px";

	var step;
	var maleft = new Array();
	var matop = new Array();
	var ma = new Array();
	
	step = Math.round((endleft - actx)/count);
	while(step != 0){
		if( Math.abs(endleft-actx) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		actx += step; 
		maleft.push(actx);
	}
	maleft.push(endleft);
	step = Math.round((endtop - acty)/count);
	while(step != 0){
		if( Math.abs(endtop-acty) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		acty += step; 
		matop.push(acty);
	}
	matop.push(endtop);
	if(maleft.length > matop.length){
		var diff = maleft.length - matop.length;
		for(var i = 0; i < diff; i++)matop.push(endtop);
	} else {
		var diff = matop.length - maleft.length;
		for(var i = 0; i < diff; i++)maleft.push(endleft);
	}
	for(var i = 0; i < matop.length; i++){
		ma.push(maleft[i]);
		ma.push(matop[i]);
	}	

	animation.push( new ANIOBJ(0, ma, obj, continueobj) );
	if(intanimation == 0)doanimation();			
}

//------------------------------------ Ajax functions -------------------------------------

// adr = site adress with post variables to send, result = function called when ready, params = additional params besides responseTest as first param
function getHTTP(adr, result, params) {
	var xmlhttp;
	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/");	}
		}
	}
	var temp = adr.indexOf("?");
	var sendstring = "";
	if(temp != -1){
		sendstring = adr.substr(temp+1);
		adr = adr.substr(0, temp);
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4){
			if(xmlhttp.status == 200){
				if(result)result(xmlhttp.responseText, params);
			} else {
				errormsg(adr);
			}
		}
	}
	xmlhttp.open("POST", adr, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	xmlhttp.send(sendstring);
}


function errormsg(page) {
	alert("Page '" + page + "' was not found.");
}

//------------------------------------------------- Main navigation functions ---------------------------------

//direction: 1 = right, -1 = left, 2 = top, -2 = bottom, 0 = no animation
//params: like "id=value&more=value"
this.inc = function(gallerynumber, direction, params) {
	var startfrom = 1;
	if(params){
		startfrom = Number(params.substr(10))+1;
		params = "?reldir=./fotos/&gallery=" + galleries[gallerynumber].title + "&gallerynumber=" + gallerynumber + "&" + params;
	} else
		params = "?reldir=./fotos/&gallery=" + galleries[gallerynumber].title + "&gallerynumber=" + gallerynumber;
	if(!direction)direction = 1;
	if(cur_gallery != gallerynumber){
		cur_gallery = gallerynumber;	
		cur_files = Number($(gallerynumber+"_count").innerHTML);
	}
	cur_file = startfrom;
	getHTTP("./fotos/foto_base/foto_functions.php" + params, gotinc, new Array(direction, gallerynumber));
}

function gotinc(responseText, params) {
	var direction = params[0];
	var gallery = galleries[params[1]];
	var galleryid = gallery.id;

	var mc = $(galleryid+"_content");
	var ms = $(galleryid+"_switcher");
	if(!mc || !ms)return;
	mc.id += "_";
	ms.id += "_";

	var temp = responseText.indexOf(":");
	var infos = responseText.substr(0, temp);
	responseText = responseText.substr(temp+1);
	temp = infos.indexOf("|");
	var breite = Number(infos.substr(0, temp));
	infos = infos.substr(temp+1);
	temp = infos.indexOf("|");
	var hoehe = Number(infos.substr(0, temp));
	infos = infos.substr(temp+1);
	var farbe = infos;
	
	gallery.style.background = farbe;
	gallery.style.width = breite + "px";
	gallery.style.height = hoehe + "px";

	var xpos = 0;
	var ypos = 0;
	var xend = 0;
	var yend = 0;
	if(direction == 1){
		xpos = breite + 50;
		ypos = 0;
		xend = -xpos;
		yend = ypos;		
	} else if(direction == -1){
		xpos = -breite - 50;
		ypos = 0;
		xend = -xpos;
		yend = ypos;
	} else if(direction == 2){
		xpos = 0;
		ypos = -hoehe - 50;
		xend = 0;
		yend = -ypos;
	} else if(direction == -2){
		xpos = 0;
		ypos = hoehe + 50;
		xend = 0;
		yend = -ypos;
	} else {
		xpos = 0;
		ypos = 0;
		xend = 0;
		yend = 0;
	}
	ms.style.left = xpos + "px";
	ms.style.top = ypos + "px";
	ms.innerHTML = responseText;
	ms.style.display = "block";
	mc.style.width = breite + "px";
	mc.style.height = hoehe + "px";
	move( galleryid+"_content_", xend, yend, new Array(function(){mc.id = galleryid+"_switcher";mc.innerHTML = "";mc.style.display = "none";}) );
	move( galleryid+"_switcher_", 0, 0, new Array(function(){ms.id = galleryid+"_content";ms.style.position = "relative";ms.style.top="0px";ms.style.left="0px";}) );
	}

this.getImage = function(picid, direction, params) {
	var obj = $(picid);
	if(!obj)return;

	clearInterval(int_preloading);
	
	var temp = picid.indexOf("_");
	var gallerynumber = Number(picid.substr(0, temp));
	var picdir = obj.alt;

	cur_gallery = gallerynumber;
	cur_file = Number(picid.substr(temp+1));
	cur_files = Number($(gallerynumber+"_count").innerHTML);

	temp = calcpicsize(picid);
	var picx = temp[0];
	var picy = temp[1];
	var winx = Math.round(picx + 10);
	var winy = Math.round(picy + 10);
	
		
	if(params)
		params = "?reldir=./fotos/&file=" + picdir + "&x=" + picx + "&y=" + picy + "&" + params;
	else
		params = "?reldir=./fotos/&file=" + picdir + "&x=" + picx + "&y=" + picy;
	if(!direction)direction = 1;

	if(direction == 1){
		$("foto").style.width = "0px";
		$("foto").style.height = winy + "px";
		$("foto").style.background = "#ffffff";
		shadebody( 1, new Array( fade, "thepicture", 0, new Array(resize, "foto", winx, winy, 2, null) ) );
	} else if(direction == 2){
		$("foto").style.background = "none";
		fade( "thepicture", 0, new Array(resize, "foto", winx, winy, 2, null) );
	} else if(direction == 3){
		removepreview();
		$("foto").style.background = "none";
		fade( "thepicture", 0, new Array(resize, "foto", winx, winy, 2, new Array(fade, "foto", 100, null)) );
	}

	$("loadme").style.visibility = "visible";
		getHTTP("./fotos/foto_base/foto_fullimg.php" + params, gotpic, gallerynumber);
	}

function gotpic(responseText, gallerynumber){
	if(intanimation != 0){
		setTimeout( function(){
			gotpic(responseText);
		}, 250 );
		return;
	}
	$("foto").innerHTML = responseText;
	var node = $("thepicture");
	var temp = 0;
	for(var j = 0; j < 8; j++){
		var obj = $("ppl"+j);
		obj.style.display = "block";
	}
	node.onload = function(){
		temp = 1;
		$("loadme").style.display = "none";
		if($("loadme").style.visibility == "hidden")return;
		preloadgallery();
		node.style.display = "none";
		node.style.visibility = "visible";
		fade( "thepicture", 100, new Array(function(){
			if($("thecomment")){
								var commtext = $("thecomment").innerHTML;
				commtext = commtext.replace("%x", cur_file);
				commtext = commtext.replace("%X", cur_files);
				$("thecomment").innerHTML = commtext;
								$("thecomment").style.visibility = "hidden";
				$("thecomment").style.display = "block";				
				var commentheight = getDim($("thecomment"))[1] + 5;
				if(getDim($("foto"))[1] + 2*commentheight > windowY){
						$("thecomment").style.paddingTop = "5px";				
						$("thecomment").style.top = "auto";
						$("thecomment").style.bottom = "0px";
				}
				$("thecomment").style.display = "none";	
				$("thecomment").style.visibility = "visible";
				ki.showcomment(1);
			}
									if(modus == 0)modus = 1;
			if(modus == 2){
				to_slideshow = setTimeout( function(){
					nextslide();
				}, 3000 );
			}
		}) );
	}
	setTimeout( function(){
		if(temp == 0)$("loadme").style.display = "block";
	}, 250);
}

this.showcomment = function(show){
	if($("thecomment")){
		if(show == 1)
			fade("thecomment", 100, null);
		else
			fade("thecomment", 0, null);
	}
}

this.closeImage = function(){
	modus = 0;
	clearInterval(int_preloading);
	clearTimeout(to_slideshow);
	$("loadme").style.visibility = "hidden";
	for(var j = 0; j < 8; j++){
		var obj = $("ppl"+j);
		obj.style.display = "none";
		obj.style.left = "-200px";
	}
	$("kiarrow1").style.display = "none";
	$("kiarrow2").style.display = "none";
	var sizes = getDim($("foto"));
	$("foto").style.background = "#ffffff";
	fade( "thepicture", 0, new Array( resize, "foto", sizes[0], 0, 2, new Array( shadebody, 0, new Array( function(){$("foto").innerHTML = "";$("foto").style.background="none";fade("foto", 100, null);} ) ) ) );
}

function calcpicsize(picid){
	var obj = $(picid);
	if(!obj)return;
	var picx = parseInt(obj.style.textIndent);
	var picy = parseInt(obj.style.lineHeight);
	var winx = picx + 10;
	var winy = picy + 10;
	var original = 1;
		var xlimiter = 900;
	var ylimiter = 900;
		if(xlimiter > windowX)xlimiter = windowX;
	if(ylimiter > windowY)ylimiter = windowY;	
		if(winy > ylimiter || winx > xlimiter){
		var k;
		if( (picx / picy) > 1){
			k = picy / picx;
			picx = xlimiter-10;
			picy = k*picx;
			if(picy > ylimiter-10){
				picy = ylimiter-10;
				picx = (1/k) * picy;
			}
		} else {
			k = picx / picy;
			picy = ylimiter-10;
			picx = k*picy;
			if(picx > xlimiter-10){
				picx = xlimiter-10;
				picy = (1/k) * picx;
			}
		}
		picx = Math.round(picx);
		picy = Math.round(picy);
		original = 0;
	}
		return new Array(picx, picy, original);
}

function preloadgallery(){

	if(preloading[0] != cur_gallery){
		preloading = new Array(cur_files+1);
		preloading[0] = cur_gallery;
		loaded = 0;
	}

	if(loaded >= cur_files)return;
	cur_load = cur_file;

	int_preloading = setInterval( function(){
		if(loaded >= cur_files)clearInterval(int_preloading);
		if(cur_load > cur_files)cur_load = 1; 
		if(preloading[cur_load] == null){
			var picid = preloading[0]+"_"+cur_load;
			var temp = calcpicsize(picid);
			preloading[cur_load] = new Image();
			loaded++;
			if(temp[2] == 1)
				preloading[cur_load].src = "./fotos/foto_galleries/"+$(picid).alt;
			else
				preloading[cur_load].src = "./fotos/foto_base/foto_makepic.php?fullimg=1&file="+$(picid).alt+"&width="+temp[0]+"&height="+temp[1];
			if(preloading[cur_load].complete){
				cur_load++;
			}
		} else {
			if(preloading[cur_load].complete){
				cur_load++;
			}
		}
	}, 250);
}



function viewdim(){
	if (self.innerHeight) // all except Explorer
	{
		windowX = self.innerWidth;
		windowY = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		windowX = document.documentElement.clientWidth;
		windowY = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		windowX = document.body.clientWidth;
		windowY = document.body.clientHeight;
	}
}

this.slideshow = function(gallerynumber){
	var temp = cur_file;
	if(cur_gallery != gallerynumber)temp = 1;
	modus = 2;
	ki.getImage(gallerynumber+"_"+temp);
}

function nextslide(){
	if(modus == 2){
		ki.showcomment(0);
		var temp = cur_file+1;
		if(temp > cur_files)temp = 1;
		ki.getImage(cur_gallery+"_"+temp, 2);
	}
}

this.mouseclicked = function(aEvent) {
	if(mousestatus == 0 && modus == 1){
		modus = 0;
		aEvent = aEvent ? aEvent : window.event;
		x = aEvent.clientX ? aEvent.clientX : aEvent.pageX;
		y = aEvent.clientY ? aEvent.clientY : aEvent.pageY;
		ki.showcomment(0);
		if(x > windowX*0.66 && cur_files != 1){
			if(cur_file+1 > cur_files)cur_file = 0;
			ki.getImage(cur_gallery+"_"+(cur_file+1), 2);
			$("kiarrow2").style.display = "none";
		}
		else if(x < windowX*0.33 && cur_files != 1){
			if(cur_file-1 == 0)cur_file = cur_files+1;
			ki.getImage(cur_gallery+"_"+(cur_file-1), 2);
			$("kiarrow1").style.display = "none";
		}
		else {
			ki.closeImage();
		}
	}
	if(modus == 2)ki.closeImage();
}

this.previewclicked = function(aEvent, gotos) {
		modus = 0;
		mousestatus = 0;
		aEvent = aEvent ? aEvent : window.event;
		x = aEvent.clientX ? aEvent.clientX : aEvent.pageX;
		ki.showcomment(0);
		if(x > windowX*0.5){
			if(cur_file+1 > cur_files)cur_file = 0;
			ki.getImage(cur_gallery+"_"+(cur_file+gotos), 3);
		} else if(x < windowX*0.5){
			if(cur_file-1 == 0)cur_file = cur_files+1;
			ki.getImage(cur_gallery+"_"+(cur_file-gotos), 3);
		}
}

function mousemoved(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){
		if(!x)return;
		mousestatus = 0;
		removepreview();
	}
	
	if(mousestatus == 1){
		if(y > pre_gre){
			pre_gre+=10000;
			if(pre_add > 8 -1)return;
			var temp = cur_file+1+pre_add;
			if(temp > cur_files){
				pre_gre-=10000;
				return;
			}
			temp = $(cur_gallery+"_"+temp).alt;
			$("ppl"+pre_add).style.left = windowX + 200 + "px";
			$("ppl"+pre_add).innerHTML = "<img src='./fotos/foto_base/foto_makepic.php?file="+temp+"' />";
			var tmp1 = pre_add;
			var tmp2 = mousestatus;
			var tmp3 = y;
			
			$("ppl"+tmp1).firstChild.onload = function(){
				if(mousestatus != tmp2){
					pre_gre-=10000;
					return;
				}
				if(tmp1 == 0)pre_gre = tmp3 + 10000 - 20;
				$("ppl"+tmp1).style.top = pre_gre-10000 + "px";
				move("ppl"+tmp1, windowX - 185 - 6, pre_gre-10000, null);
				pre_add++;
				pre_gre = pre_gre + 30 + $("ppl"+tmp1).firstChild.offsetHeight - 10000;
				triggerpreview(oldx, oldy);
			}
		}
	}
	
	if(mousestatus == -1){
		if(y > pre_gre){
			pre_gre+=10000;
			if(pre_add > 8 -1)return;
			var temp = cur_file-1-pre_add;
			if(temp < 1){
				pre_gre-=10000;
				return;
			}
			temp = $(cur_gallery+"_"+temp).alt;
			$("ppl"+pre_add).style.left = -200 + "px";
			$("ppl"+pre_add).innerHTML = "<img src='./fotos/foto_base/foto_makepic.php?file="+temp+"' />";
			var tmp1 = pre_add;
			var tmp2 = mousestatus;
			var tmp3 = y;
			
			$("ppl"+tmp1).firstChild.onload = function(){
				if(mousestatus != tmp2){
					pre_gre-=10000;
					return;
				}
				if(tmp1 == 0)pre_gre = tmp3 + 10000 - 20;
				$("ppl"+tmp1).style.top = pre_gre-10000 + "px";
				move("ppl"+tmp1, 35 + 6, pre_gre-10000, null);
				pre_add++;
				pre_gre = pre_gre + 30 + $("ppl"+tmp1).firstChild.offsetHeight - 10000;
				triggerpreview(oldx, oldy);
			}	
		}
	}
	
	if(mousestatus == 0){
		if(x > windowX*0.66 && cur_files != 1) {
			$("kiarrow2").style.marginLeft = getDim($("foto"))[0]*0.5 + "px";
			$("kiarrow2").style.display = "block";
		} else if(x < windowX*0.33 && cur_files != 1){
			$("kiarrow1").style.marginLeft = -getDim($("foto"))[0]*0.5 - 14 + "px";
			$("kiarrow1").style.display = "block";
		} else {
			$("kiarrow1").style.display = "none";
			$("kiarrow2").style.display = "none";
		}
		if(y > windowY - 70 && x < 410){
			if(firstrun == -1){
				$("kithelp").style.display = "block";	
				firstrun = -2;
				ki.showtooltip();
			}
		} else {
			if(firstrun == -2){
				$("kithelp").style.display = "none";
				firstrun = -1;
			}
		}
	}
}


function removepreview(){
		for(var j = 0; j < 8; j++){
			var obj = $("ppl"+j);
			var temp = findPos(obj);
			if(temp[0] < windowX/2)
				temp[0] = -200;
			else
				temp[0] = windowX + 200;
			move("ppl"+j, temp[0], temp[1], null);
		}
		pre_add = 0;
		pre_gre = 0;
}	

this.showtooltip = function(){
	var node = $("kithelp");
	var tooltip = Number(node.className.substr(2));
	getHTTP("./fotos/foto_base/foto_tooltip.php?reldir=./fotos/&tooltip="+tooltip, gottt, null);
}

function gottt(responseText){
	var node = $("kithelp");
	node.innerHTML = responseText;
	var tooltip = Number(node.className.substr(2));
	var temp = tooltip + 1;
	if(tooltip == 5)temp = 1;
	node.className = "tt"+temp;
}


//----------------------------- Keyboard functions -----------------------------------------

function preventDefaultAction(aEvent) {	
		aEvent.preventDefault();
		}

function taste(aEvent) {
	aEvent = aEvent ? aEvent : window.event;
	if( aEvent.keyCode == 27) {
		if(modus == 2){
			var node = $("loadme");
			node.style.marginLeft = "-8px";
			node.style.marginTop = "-8px";
			node.src = "./fotos/foto_base/foto_loading.gif";
		}
		ki.showcomment(0);
		$("kithelp").style.display = "none";
		ki.closeImage();
				preventDefaultAction(aEvent);
				return false;
	}
		if((aEvent.keyCode == 35 || aEvent.keyCode == 34 || aEvent.keyCode == 39) && modus == 1) {
		var temp = cur_file+1;
		if(temp > cur_files)temp = 1;
		ki.getImage(cur_gallery+"_"+temp, 2);
		$("kiarrow2").style.display = "none";
		ki.showcomment(0);
				preventDefaultAction(aEvent);
				return false;
	}
	if((aEvent.keyCode == 36 || aEvent.keyCode == 33 || aEvent.keyCode == 37) && modus == 1) {
		var temp = cur_file-1;
		if(temp == 0)temp = cur_files;
		ki.getImage(cur_gallery+"_"+temp, 2);
		$("kiarrow1").style.display = "none";
		ki.showcomment(0);
				preventDefaultAction(aEvent);
				return false;
	}
	if(aEvent.keyCode == 19 && modus == 2) {
		var node = $("loadme");
		if(to_slideshow != 0){
			clearTimeout(to_slideshow);
			to_slideshow = 0;
			node.src = "./fotos/foto_base/foto_break.png";
			node.style.marginTop = "-15px";
			node.style.marginLeft = "-15px";
			node.style.display = "block";
		} else {
			node.style.display = "none";
			node.style.marginLeft = "-8px";
			node.style.marginTop = "-8px";
			node.src = "./fotos/foto_base/foto_loading.gif";
			nextslide();
		}
				preventDefaultAction(aEvent);
				return false;
	}
}

//---------------------------------- Init functions -------------------------------------------

this.initialize = function(){
	body = document.body;
	var node;	
	if(!$("blende")){
				node = document.createElement("div");
		node.style.width = "100%";
		node.style.height = "100%";
		node.style.background = "#000000";
		node.style.opacity = 0.8;
				node.id = "blende";
		node.style.position = "fixed";
		node.style.left = "0px";
		node.style.top = "0px";
		node.style.zIndex = 100;
		node.style.cursor = "pointer";
		node.onclick = function(event){ ki.mouseclicked(event); };
		node.style.display = "none";
		body.appendChild(node);
	}
		if(!$("loadme")){
		node = document.createElement("img");
		node.id = "loadme";
		node.style.marginLeft = "-8px";
		node.style.marginTop = "-8px";
		node.style.position = "fixed";
		node.style.top = "50%";
		node.style.left = "50%";
		node.style.zIndex = 5000;
		node.src = "./fotos/foto_base/foto_loading.gif";
		node.alt = "";
		node.style.display = "none";
		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.zIndex = 1000;
		node.style.cursor = "pointer";
				node.src = "./fotos/foto_base/foto_back.png";		
				node.alt = "";
		node.onclick = function(event){ ki.mouseclicked(event); };
		node.style.display = "none";
		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.zIndex = 1000;
		node.style.cursor = "pointer";
				node.src = "./fotos/foto_base/foto_next.png";		
				node.alt = "";
		node.onclick = function(event){ ki.mouseclicked(event); };
		node.style.display = "none";
		body.appendChild(node);
	}
	if(!$("foto")){
		node = document.createElement("div");
		node.id = "foto";
		node.style.position = "fixed";
		node.style.top = "50%";
		node.style.left = "50%";
		node.style.width = "10px";
		node.style.height = "10px";
		node.style.display = "none";
		node.style.zIndex = 1000;
		node.style.cursor = "pointer";
		node.style.background = "#ffffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		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.style.display = "none";
		node.style.zIndex = 2000;
		node.style.cursor = "pointer";
		node.style.border = "3px solid #f0ffff";
		body.appendChild(node);
	}
			document.addEventListener('keypress', taste, false);
	window.addEventListener('resize', viewdim, false);
	document.addEventListener('mousemove', mousemoved, false);
	viewdim();
	initgallery();
}

function initgallery() {
	var temp = document.getElementsByTagName("div");
	for(var i = 0; i < temp.length; i++){
		if(temp[i].className.indexOf("fotogallery") >= 0){
			temp[i].id = temp[i].title + "_" + i;
			galleries.push(temp[i]);
		}
	}
	if(galleries.length == 0){
						return;
	} else {
		showgallery(0);
	}
}

function showgallery(gallerynumber){

	var gallery = galleries[gallerynumber];
	gallery.innerHTML = "";
	gallery.style.position = "relative";
	gallery.style.padding = "0px";
	gallery.style.overflow = "hidden";
	
	var node = document.createElement("div");
	node.id = gallery.id + "_content";
	node.style.position = "relative";
	node.style.height = "100%";
	node.style.width = "100%";
	node.style.overflow = "hidden";
	node.style.display = "none";
	gallery.appendChild(node);

	node = document.createElement("div");
	node.id = gallery.id + "_switcher";
	node.style.position = "absolute";
	node.style.height = "100%";
	node.style.width = "100%";
	node.style.overflow = "hidden";
	node.style.display = "none";
	gallery.appendChild(node);
	
	cur_gallery = gallerynumber;
	ki.inc(gallerynumber, -2);
	if(gallerynumber+1 < galleries.length)
		showgallery(gallerynumber+1);
	else {
		cur_gallery = -1;
					}
}
	

}