	/*	linuxuser.at Media Blog 			
	¦		GPLv2	
	¦		author: chris @ linuxuser dot at
	*/
	

var pic_shown = 0;
var cur_fn;
var cur_preload = 0;
var intUpdate;
var intUpdate2;
var menuFX;
var infoFX;
var menuShown = 0;
var slideon = 0;
var slideShowInterval = 4000;
var preloaded = 0;
var safari_keycounter = 1;

document.onkeyup = showUp;

var max_preload = 10;
  document.preloads = new Array();

function showpic(fn) {
		fn = unescape(fn);
//		alert(fn);
		if ((fn.substr(0,1) == "<") || (fn.substr(0,1) == '&')) {
			/* Embedded Object */
			content_space.style.display="block";
			mainimg.style.display="none";
			content_space.innerHTML = fn;

			out_field = document.getElementById("mainimg");
			out_field.onclick = do_nothing;

			if (pic_shown == 0) { new Effect.BlindDown('content'); }
		} else {
			/* Picture */
			content_space.style.display="none";
			mainimg.style.display="block";

			out_field = document.getElementById("mainimg");
			out_field.src = "";
			out_field.src = fn;

			out_field.onclick = hidepic;
	
			if (pic_shown == 0) { new Effect.BlindDown('content'); }

			preload_onDemand(fn);
		}

		pic_shown = 1;
		cur_fn = escape(fn);
}

function do_nothing() {
	/* don't do anything :P */
}

function hidepic() {
	new Effect.BlindUp('content');
	clearTimeout(intUpdate2);
	hideInfoText();
	slideon = 0;	
	pic_shown = 0;
}

function showUp(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
 	if ((evt.keyCode == "68") || (evt.keyCode == "39")) {
 		/* 'd' or right arrow */
		if (is_safari) { 		
 			safari_keycounter++;
 			if (safari_keycounter % 2 == 1) showNext();
 		} else { showNext(); }
 			 	
 	} else if ((evt.keyCode == "65") || (evt.keyCode == "37")) {
 		/* 'a' or left arrow */
		if (is_safari) { 		
 			safari_keycounter++;
	 		if (safari_keycounter % 2 == 1) showLast();	 	
 		} else { showLast(); }
 			 	
 	} else if ((evt.keyCode == "81") || (evt.keyCode == "27")) {
 		/* 'q' or ESC */
 		hidepic();
 	} else if ((evt.keyCode == "83") || (evt.keyCode == "145") || (evt.keyCode == "44") || (evt.keyCode == "144")) {
 		/* 's' or roll, druck, num */
 		toggleSlideShow();
 	} else if ((evt.keyCode == "61") || (evt.keyCode == "35")) {
 		/* + and Pos1 */
 		slideShowInterval += 1000;
		infotext.innerHTML = (slideShowInterval / 1000).toString(10) + "s";
		clearTimeout(intUpdate2);
		intUpdate2 = setTimeout("slideShowNext()", slideShowInterval);
 		
 	} else if ((evt.keyCode == "109") || (evt.keyCode == "36")) {
 		/* - and End */
 		if (slideShowInterval > 0) slideShowInterval -= 1000;
		infotext.innerHTML = (slideShowInterval / 1000).toString(10) + "s";
		clearTimeout(intUpdate2);
		intUpdate2 = setTimeout("slideShowNext()", slideShowInterval);
 	}
}

function showLast() {
	build_imgarr();
	a = img_arr;

	while ((a[1] != "") && (a[1] != cur_fn)) { a.shift(); }

	if (a[1] != "") { 
		showpic(a[0]);
	}
}

function showNext() {
	build_imgarr();
	a = img_arr;

	while ((a[0] != "") && (a[0] != cur_fn)) { a.shift(); }

	if (a[1] != "") { 
		showpic(a[1]);
		preload_onDemand(cur_fn)
	}
}

function preload() {
	build_imgarr();
	a = img_arr;
	
	if (a.length > 1) {	
		i = 0;
		while ((a[cur_preload] != "") && (i < max_preload)) {
			if (a[cur_preload].substr(0,1) != "&") {
				document.preloads[cur_preload] = new Image();
				document.preloads[cur_preload].src = a[cur_preload];
//			infotext.innerHTML += cur_preload + ": " + a[cur_preload] + "<br>"; 
				cur_preload++;
				i++;
			}
		}

	}
}

intUpdate = setTimeout("preload()", 1000);

function preload_onDemand(picfn) {
	build_imgarr();
	a = img_arr;
	if (a.length > 1) {
	
	for (i=0; ((a[i] != "") && (a[i] != picfn)); i++) {
		/* Do Nothing, Just Count */
	}
	
	if (i >= (cur_preload - (max_preload/2))) {
		preload();
	}
	
	}
}

function slideShowNext() {
	build_imgarr();
	a = img_arr;

	while ((a[0] != "") && (a[0] != cur_fn)) { a.shift(); }

	fn = unescape(a[1]);

	if ((fn.substr(0,1) == "<") || (fn.substr(0,1) == '&')) {
		// Embedded Object
		cur_fn = a[1];
		slideShowNext();
	} else {
		showNext();
		clearTimeout(intUpdate2);
		intUpdate2 = setTimeout("slideShowNext()", slideShowInterval);
	}
}

function toggleSlideShow() {
	if (slideon == 0) {
		/* Start Slideshow */
		slideon = 1;
		
		if (!pic_shown) { 		
			build_imgarr();
			a = img_arr;

			/* Don't Show Video as First Slide */ 
			fn = unescape(a[0]);
			if ((fn.substr(0,1) == "<") || (fn.substr(0,1) == '&')) {
				cur_fn = a[0];
				slideShowNext();
			} else {
				showpic(a[0]);
				intUpdate2 = setTimeout("slideShowNext()", slideShowInterval);
			}
		}
		
		showInfoText();
	} else {
		/* Stop Slideshow */
		clearTimeout(intUpdate2);
		slideon = 0;
		hideInfoText();
	}	
}


function showInfoText() {
	infoFX = Effect.Appear('infotext');
}

function hideInfoText() {
	infoFX = Effect.Fade('infotext');
}
