﻿var popup = null;
var underlay;
function ShowImage(obj)
{
	if(!popup)
	{
		var parent = document.getElementsByTagName("body")[0];

		popup = document.createElement("div");
		parent.appendChild(popup);
		var image = document.createElement("img");
		popup.appendChild(image);
		var input = document.createElement("input");
		input.setAttribute("type", "text");
		input.onblur = function() { popup.style.display = "none"; underlay.style.display = "none"; };
		popup.appendChild(input);

		if(!underlay)
		{
			underlay = document.createElement("div");
			underlay.id = "underlay";
			document.body.appendChild(underlay);
			var frame = document.createElement("iframe");
			frame.frameBorder = 0;
			underlay.appendChild(frame);
		}

		popup.className = "popup";
		image.onload = function()
		{
			underlay.style.display = "block";
			popup.style.display = "block";
			popup.style.marginLeft = -(this.clientWidth / 2) + "px";
			popup.style.top = ((window.screen.availHeight - this.clientHeight)/2 - 20) + "px";
			popup.getElementsByTagName("input")[0].focus();
		};
	}
	popup.getElementsByTagName("img")[0].src = (obj.src || obj.getElementsByTagName("img")[0].src).replace(".jpg", ".full.jpg").replace(".gif", ".full.gif");

	return false;
}
var slideshow = function(group)
{
	if(typeof group == "undefined")
	{
		group = "all"
	}
	slideshow.initGroup(group)
	slideshow.group = group
	slideshow.index = 0
	slideshow.show(true)
}
slideshow.groups = {}
slideshow.group = "all"
slideshow.initGroup = function(group)
{
	if(typeof slideshow.groups[group] == "undefined")
	{
		var ar = new Array()
		var images = document.getElementById("slideshow-image").getElementsByTagName("img")
		for(var i = 0; i < images.length; i++)
		{
			if(group == "all" || images[i].className == group)
			{
				ar.push(images[i])
			}
		}
		slideshow.groups[group] = ar
	}
}
slideshow.interval = 3000
slideshow.init = function()
{
	slideshow.initGroup(slideshow.group)
	slideshow.index = 0
	slideshow.last = slideshow.groups[slideshow.group][0]
	document.getElementById("slideshow-image").getElementsByTagName("span")[0].innerHTML = slideshow.last.getAttribute("alt")
	slideshow.elapsed = new Date().valueOf()
	setInterval(function(){ slideshow.tick() }, slideshow.interval)
}
slideshow.tick = function()
{
	if(new Date().valueOf() - slideshow.elapsed < slideshow.interval - 500) return

	var images = slideshow.groups[slideshow.group]
	index = slideshow.index + 1
	if(index >= images.length)
	{
		index = 0;
	}
	slideshow.index = index
	slideshow.show(false)
}
slideshow.show = function(instant)
{
	var images = slideshow.groups[slideshow.group]
	var index = slideshow.index

	if(index >= images.length) return

	var to = images[index]
	var from = slideshow.last

	document.getElementById("slideshow-image").getElementsByTagName("span")[0].innerHTML = to.getAttribute("alt")

	if(from == to) return

	slideshow.last = to
	slideshow.elapsed = new Date().valueOf()

	from.style.zIndex = 1
	to.style.zIndex = 0
	$(to).show()
	if(instant)
	{
		$(from).hide()
	}
	else
	{
		$(from).fadeOut()
	}
}
$(slideshow.init)

