var mode = "txt";

function flipTo(newMode) {
	if (newMode == 'flip') {
		if (mode == 'img') {
			flipTo('txt');
		} else {
			flipTo('img');
		}
		return false;
	}
	if ((document.getElementById("txt") != null) && (document.getElementById("img") != null)) {
		if (mode != newMode) {
			mode = newMode;
			if (mode == 'img') { // SWITCH TO IMAGE //////////////
				$("#txt").css({	zIndex: 20 });
				$("#img").css({	zIndex: 30 });
				
				$("#txtFrame").css({ borderLeftWidth: "0", 	borderTopWidth: "0" });
				$("#imgFrame").css({ borderRightWidth: "1px", 		borderTopWidth: "1px"});
				
				if (mono_pix.length > 1) {
					$("#imgNav").show();
					showSlide();
				}
				
				$("#txtNav").hide();
				
				startPong();
			} else { // SWITCH TO TEXT ///////////////////////////
				$("#txtFrame").css({	borderLeftWidth: "1px", 	borderTopWidth: "1px" });
				$("#imgFrame").css({ borderRightWidth: "0", 		borderTopWidth: "0"});
				
				if (mono_pix && (mono_pix.length > 1)) {
					$("#imgCaption").html("");
					$("#imgCopyright").html("");
					//alert("hiding imgCaption");
					$("#imgCaptionBox").hide();
					//alert("hiding imgNav");
					$("#imgNav").hide();
				}
				
				//alert("showing txtnav");
				$("#txtNav").show();
				
				stopPong();
				
				//alert("changing zIndex");
				$("#txt").css({	zIndex: 30 });
				$("#img").css({	zIndex: 20 });
			}
		}
	}
	return false;
}

function startPong() {
	if (document.getElementById("pingpong") != null) {
		$("#pingpong").show();
		movePong();
	}
}

function stopPong() {
	if (document.getElementById("pingpong") != null) {
		$("#pingpong").hide();
	}
}

function movePong(direct) {
	var wide = 359;
	var halfWide = Math.floor(wide/2);
	
	var high = 480; 
	var halfHigh = Math.floor(high/2);
	
	var paddleWidth = 30;
	var paddleHeight = 150;
	
	var gameOffset = Math.floor(Math.random() * (1 * paddleHeight));
	var activeHeight = (high - paddleHeight - (2 * gameOffset));
	var gameCenter = Math.floor((Math.random() * activeHeight) + gameOffset);
	if ((Math.random() * 1) < 0.5) gameOffset *= -1;
	
	var ballX = Math.floor( (Math.random() * halfWide) + (halfWide/2) - (paddleWidth / 2));
	var ballY = Math.floor( gameCenter + (Math.random() * (paddleHeight - paddleWidth)) );
	
	var ping = $("#ping");
	var pong = $("#pong");
	var ball = $("#ball");
	
	ping.css("top", ((gameCenter - gameOffset)+"px"));
	pong.css("top", ((gameCenter + gameOffset)+"px"));
	ball.css("left", (ballX + "px"));
	ball.css("top", (ballY + "px"));
	
	if (direct > 0) {
		return false;
	}
}

var starter = 1;
function frontPage() {
	if ((document.getElementById("txt") != null) && (document.getElementById("img") != null)) {
		if (starter == 0) {
			//$("#img").css("display", "none");
			$("#pingpong").hide();
			$("#txt").show();
			starter++;
			return false;
		} else {
			return true;
		}
	}
}



// IMAGES

var mono_pix_root = /mono_images/ 
var mono_pix_current = 0;
var mono_pix_next = mono_pix_current;

var mono_pix = new Array();
var mono_pix_loaded = new Array();
var mono_pix_loader = null;

var thisCaption, thisCopyright, thisAlt, thisURL;
thisCaption = thisCopyright = thisAlt = thisURL = "";

var loadingDelay = null;

function readSlide(which) {
	if (mono_pix.length > 1) {
		if (which < 0) {
			which = mono_pix.length - 1;
		} else if (which >= mono_pix.length) {
			which = 0;
		}
		
		thisURL = ("http://mono-kultur.com" + (mono_pix[which][0]) );
		thisAlt = (mono_pix[which][1]);
		thisCaption = (mono_pix[which][2]);
		thisCopyright = (mono_pix[which][3]);

		if (thisCaption == "") {
			var prevSlide = which - 1;
			for (prevSlide; prevSlide>=0; prevSlide--) {
				if (mono_pix[prevSlide][2] != "") {
					thisCaption = mono_pix[prevSlide][2];
					break;
				}
			}
		}
		
		if (thisCopyright == "") {
			var prevSlide = which - 1;
			for (prevSlide; prevSlide>=0; prevSlide--) {
				if (mono_pix[prevSlide][3] != "") {
					thisCopyright = mono_pix[prevSlide][3];
					break;
				}
			}
		}
		
		if (thisCaption != "") thisCaption = (decoder(thisCaption));
		if (thisCopyright != "") thisCopyright = (decoder(thisCopyright));
		if ((thisCaption != "") && (thisCopyright != "")) thisCaption = (thisCaption + "<br />");
		mono_pix_next = which;
	}
}

function loadSlide(which) {
	if (mono_pix_loader != null) mono_pix_loader = null;
	
	flipTo('img');
	readSlide(which);

	$("#imgCaption").html("Loading...");
	$("#imgCopyright").html("");
	$("#imgCaptionBox").show();
	// $("#imgLoading").show();
	
	mono_pix_loader = new Preloader(thisURL, true);
}

function loadNeighbours() {
	if (mono_pix.length > 1) {
		previousPic = mono_pix_current - 1;
		if (previousPic < 0) previousPic = mono_pix.length - 1;
		nextPic = mono_pix_current + 1;
		if (nextPic >= mono_pix.length) nextPic = 0;
		
		previousURL = ("http://mono-kultur.com" + (mono_pix[previousPic][0]) );
		nextURL = ("http://mono-kultur.com" + (mono_pix[nextPic][0]) );
		
		new Preloader(nextURL, false);
		if (previousURL != nextURL) new Preloader(previousURL, false);
	}
}

function showSlide() {	
	mono_pix_current = mono_pix_next;
	
	//$("#imgLoading").hide();
	var indexText = ((mono_pix_current+1) + " / " + mono_pix.length);
	$("#imgIndex").html(indexText);
	
	if ($("#mono_pix").get("src") != thisURL) {
		$("#mono_pic").set("src", thisURL);
		$("#mono_pic").set("alt", thisAlt);
	}
	
	$("#imgCaption").html(thisCaption);
	$("#imgCopyright").html(thisCopyright);
	
	if (((thisCaption == "") && (thisCopyright == "")) || (mode == "txt")) {
		$("#imgCaptionBox").hide();
	} else {
		$("#imgCaptionBox").show();
	}
	
	loadNeighbours();
	mono_pix_loader = null;
}

function previousSlide() { loadSlide(mono_pix_current - 1); }
function nextSlide() { loadSlide(mono_pix_current + 1); }

function decoder(encodedText) {
	var HEXCHARS = "0123456789ABCDEFabcdef"; 
	var encoded = "";
	if (encodedText != "") encoded = encodedText;
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				plaintext = unescape(urlText) = "error";
				break;
			}
		} else {
			plaintext += ch;
			i++;
		}
	}
	return plaintext;
}

function Preloader(url, callback) {
	this.newImage = new Image();
	if (callback) this.newImage.onload = function() { showSlide(); this.onload=null; }
	this.newImage.src = url;
}

$(document).ready(function(){
	if ((mono_pix_current != null) && (mono_pix.length > 1)) {
		readSlide(mono_pix_current);
		showSlide();
	}
});

function debug(msg) {
	msg = ('<span class="dbg">' + msg + '</span><br />');
	$("#debug").append(msg);
}
function cleardebug() {
	$("#debug").html("");
}