var current_img_id = null;

function ShowDescription() {
	var box = document.getElementById("box");
	box.style.display = "block";
}

function HideDescription() {
	var box = document.getElementById("box");
	box.style.display = "none";
}

function Show(src, width, height, id) {
	if ((width == 0 || height == 0) && src != null) {
		var i = new Image();
		i.src = src;
		width = i.width;
		height = i.height;
	}
	width = width ? width : 500;
	height = height ? height : 375;
	var x, y;
	if (self.innerHeight) { // all except Explorer 	
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)	{ // Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) { // other Explorers	
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	var top = (y - height - 2)/2 - 25;
	var left = (x - width - 2)/2;
	var show = document.getElementById("show");
	var shadow = document.getElementById("shadow");
	var gallery = document.getElementById("gallery");
	var caption = document.getElementById("caption");
	var media = document.getElementById("media");
	var description = document.getElementById("description");		
		
	if (src) {	
		media.innerHTML = "<img src=\"" + src + "\" />";
	}	
	else {
		media.innerHTML = "";
	}
	
	current_img_id = id;
	var d = document.getElementById(id);
	if (d != null) {	
		var divs = d.getElementsByTagName("div");		
		caption.innerHTML = divs[0].innerHTML;		
		description.innerHTML = divs[1].innerHTML;
	}
	else {
		caption.innerHTML = "";
		description.innerHTML = "";
	}
	
	show.style.top = top + "px";
	show.style.left = left + "px";
	show.style.width = width + 2 +"px";
	
	shadow.style.width = width + "px";
	shadow.style.height = height + 2 + "px";
		
	gallery.style.filter = "alpha(opacity=10)";
	gallery.style["-moz-opacity"] = "0.1";
	gallery.style.opacity = "0.1"
	
	show.style.display = "block";
	if (src) {
		shadow.style.left = left + media.offsetLeft + 6 + "px";
		shadow.style.top = top + media.offsetTop + 5 + "px";
		shadow.style.width = width + 2 + "px";
		shadow.style.height = height + 2 + "px";
		shadow.style.display = "block";
	}
	else {
		shadow.style.display = "none";
	}
}

function Hide() {
	var show = document.getElementById("show");
	var shadow = document.getElementById("shadow");
	var gallery = document.getElementById("gallery");
	gallery.style.filter = "";
	gallery.style["-moz-opacity"] = "";
	gallery.style.opacity = ""
	show.style.display = "none";
	shadow.style.display = "none";	
	var caption = document.getElementById("caption");
	var media = document.getElementById("media");
	var description = document.getElementById("description");	
	caption.innerHTML = "";
	media.innerHTML = "";
	description.innerHTML = "";
	current_img_id = null;
}

var scroll_media = null;
var scroll_amount = 0;
var scroll_direction = 0;
var scroll_timer_id = null;

function ScrollMe(media, direction) {
	scroll_media = media;
	scroll_direction = direction;
	var p = scroll_media.style.backgroundPosition.split(" ");
	if (scroll_direction == 1) {
		scroll_amount = p.length > 0 ? parseInt(p[0]) : 0;		
	}
	else {
		scroll_amount = p.length > 1 ? parseInt(p[1]) : 0;	
	}
	Scroll();
}

function Scroll() {
	if (scroll_media != null) {
		var p = scroll_media.style.backgroundPosition.split(" ");
		if (scroll_direction == 1) {
			scroll_media.style.backgroundPosition = --scroll_amount + "px " + p[1];
		}
		else {
			scroll_media.style.backgroundPosition = p[0] + " " + --scroll_amount + "px";
		}
		scroll_timer_id = window.setTimeout("Scroll()", 20);
	}
	else {
		StopScrolling();
	}
}

function StopScrolling() {
	window.clearTimeout(scroll_timer_id);
	scroll_media = null;
	scroll_amount = 0;
	scroll_direction = 0;
	scroll_timer_id = null;
}

function Go(direction) {
	var gallery = document.getElementById("gallery");
	var divs = gallery.getElementsByTagName("div");
	var media = [];
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className == "media") {
			media[media.length] = divs[i];
		}
	}
	for (var i = 0; i < media.length; i++) {
		if ("_" + media[i].id == current_img_id || current_img_id == null) {
			var next = null;
			if (current_img_id == null || i + direction >= media.length) {
				next = media[0];
			}
			else if (i + direction < 0) {
				next = media[media.length - 1]
			}
			else {
				next = media[i + direction];
			}
			var src = next.style.backgroundImage;
			if (src != null && src != "") {
				src = src.substring(4, src.length - 1);
			}
			else {
				src = null;
			}
			Show(src, 0, 0, "_" + next.id);
			break;
		}
	}
}

document.onkeyup = function(e) {
	e = e ? e : window.event;
	switch (e.keyCode) {
		case 37 : Go(-1); break;
		case 39 : Go(1); break;
	}
}