var paintings;

var thumbnailmode = true;
var thumbnailcount = 15;
var thumbnailindex = 0;
var random = true;

function gpopup() {
	return document.getElementById("gpopup");
}

function loadgalleries () {
	var p = gpopup();
	for (var i=0; i < galleries.length; i++) {
		var galleryname = galleries[i][0];
		p.options[i] = new Option(galleryname,i.toString(),false, false);
	}
}

function loadgallery(i,loadp, loadt) {
	var gindex = i;
	var gallery = galleries[gindex];
	var garray = gallery[1];
	paintings = new Array();
	for (var i=0; i < garray.length; i++) {
		paintings[i] = apaintings[garray[i] - 1];
	}
	var p = popup();
	/* fix me - we are determining the page from the presense or absense of popup */
	if (p != null) {
		loadpopup(p);
		p.selectedIndex = 0;
/*
		if (loadp)
			loadpainting(p);
*/
	} else {
		thumbnailindex = 0;
		loadthumbnails();
	}
	if (loadt)
		gotothumbs();
}

function loadpopup(p) {
	p.options.length = 0; /* why doesn't p.options = new Array() work? */
	for (var i=0; i < paintings.length; i++) {
		var painting = paintings[i];
		p.options[i] = new Option(painting[1],i.toString(),false, false);
	}
}

function gotothumbs() {
			stopshow();
var img = document.getElementById("paintingimg");
	img.src = "blank.gif";
			thumbnailmode = true;
			var div = document.getElementById("thumbdiv");
			div.style.display = "block";
			div = document.getElementById("detaildiv");
			div.style.display = "none";
	var p = popup();
	var index = p.selectedIndex;
	if (index == -1)
		index = 0;
	thumbnailindex = Math.floor(index / thumbnailcount) * thumbnailcount;
initthumbnails();
return;
// old

	p = gpopup();
	var gindex = p.selectedIndex;
	var query = "?" + "gallery=" + gindex + "&" + "index=" + thumbnailindex;
	var page = "thumbnails.html"
	window.location=page + query;
// old
}

function thumbzoom(i) {
	thumbnailmode = false;
	var img = document.getElementById("paintingimg");
	img.src = "blank.gif";
	img.style.visibility = "hidden";

var div = document.getElementById("thumbdiv");
			div.style.display = "none";
			div = document.getElementById("detaildiv");
			div.style.display = "block";
			p = popup();
			p.selectedIndex = thumbnailindex + i;
			loadpainting(p);
	img.style.visibility = "visible";

return;

// old
	var query = "?" + "gallery=" + gpopup().selectedIndex + "&" + "index=" + (thumbnailindex + i);
	var page = "index.html"
	window.location=page + query;
// old
}

function thumbleft() {
	thumbnailindex = thumbnailindex - thumbnailcount;
	if (thumbnailindex < 0) {
		var remainder = paintings.length % thumbnailcount;
		if (remainder == 0)
			remaindex = thumbnailcount;
		thumbnailindex = paintings.length - remainder;
	}
	loadthumbnails();
}

function thumbright() {
	thumbnailindex = thumbnailindex + thumbnailcount;
	if (thumbnailindex >= paintings.length)
		thumbnailindex = 0;
	loadthumbnails();
}

function loadthumbnails() {
	for (var i=0; i < thumbnailcount && (i + thumbnailindex) < paintings.length; i++) {
		var painting = paintings[thumbnailindex + i];
		var file = "thumbs/" + painting[0] + ".jpg";
		timg = document.getElementById("thumb" + i.toString());
		timg.src = file;
		timg.parentNode.parentNode.style.visibility = "visible";
	}
	while (i < thumbnailcount) {
		timg = document.getElementById("thumb" + i.toString());
		timg.parentNode.parentNode.style.visibility = "hidden";
		i++;
	}
	span = document.getElementById("counter");
	var pagecount = Math.ceil(paintings.length / thumbnailcount);
	span.innerHTML = (Math.floor(thumbnailindex / thumbnailcount) + 1) + " of " + pagecount;
}

function initthumbnails() {
/* old
	loadgalleries();
	var index = gindexFromUrl();
	if (index == -1)
		index = 0;

	gpopup().selectedIndex = index;
old */
	loadgallery(gpopup().selectedIndex, true);
	/* have to figure out how to clear the query string once it is used */
	if (thumbnailindex == -1) {
		var index = indexFromUrl();
		if (index != -1)
			thumbnailindex = index;
		else 
			thumbnailindex = 0;
	}
	loadthumbnails();
}

function loadpainting(select) {
	var img = document.getElementById("paintingimg");
	var i = select.selectedIndex;
	var painting = paintings[i];
	var file = "images/" + painting[0] + ".jpg";
	var title = painting[1];
	var date = painting[2];
	var dim = painting[3];
	var med = painting[4];
	var span = document.getElementById("paintingtitle");
	/* bug with Safari: it does not reset width and height properly on the old img*/
	/* it will get bigger when necessary, but never smaller */
	/* begin Safari workaround */
	if (is_safari) {
		var parent = img.parentNode;
		parent.removeChild(img);
		img = document.createElement("img");
		parent.appendChild(img);
		img.id = "paintingimg";
		img.className = "painting";
	}
	/* end Safari workaround */
	img.src = file;
	span.innerHTML= '"' + title + '"' + "&nbsp;&mdash;&nbsp;" +  date + "&nbsp;&mdash;&nbsp;" + dim + "&nbsp;&mdash;&nbsp;" + med;
	span = document.getElementById("counter");
	span.innerHTML = (i+1) + " of " + select.options.length;
	
	/*
	window.location.hash = i.toString();
	*/

}

function popup() {
	return document.getElementById("popup");
}

function indexFromUrl () {
	/* check for query string index */
	var index = -1;
	var query=location.search.substr(1);
	var values = query.split("&");
	for (i=0; i < values.length; i++) {
		var str = values[i];
		var split = str.split("=");
		var key = split[0];
		var value = split[1];
		if (key == "index") {
			var j = parseInt(value);
			if (j != Number.NaN && j >= 0 && j < paintings.length) {
				index = j;
				break;
			}
		}
	}
	return index;
}

function gindexFromUrl () {
	/* check for query string index */
	var index = -1;
	var query=location.search.substr(1);
	var values = query.split("&");
	for (i=0; i < values.length; i++) {
		var str = values[i];
		var split = str.split("=");
		var key = split[0];
		var value = split[1];
		if (key == "gallery") {
			var j = parseInt(value);
			if (j != Number.NaN && j >= 0 && j < galleries.length) {
				index = j;
				break;
			}
		}
	}
	return index;
}

function init() {
	loadgalleries();
	var index = gindexFromUrl();
	if (index == -1)
		index = 0;
	gpopup().selectedIndex = index;
	loadgallery(index, false);
	p = popup();
	loadpopup(p);
	index = -1;
	var max = p.options.length;
	/*
	var hash = window.location.hash;

	if (hash != null && hash != undefined && hash != "") {
		hash = hash.substr(1);
		var newindex = parseInt(hash);
		if (newindex != Number.NaN)
			index = newindex;
	}
	*/
	/* check for query string index */
	index = indexFromUrl();
	if(index == -1) {
		if (random)
			index = randomindex();
		else
			index = 0;
	} 
	else {
		thumbnailmode = false;
	}
	p.selectedIndex = index; 
	if (thumbnailmode)
		gotothumbs();
	else
		loadpainting(p);
}

function randomindex() {
	return Math.floor(Math.random() * (paintings.length - 1));
}

function randomnext() {
	var p = popup();
	p.selectedIndex = randomindex();
	loadpainting(p);
}

function left() {
	if (thumbnailmode)
		return thumbleft();
	var p = popup();
	var max = p.options.length - 1;
	var i = p.selectedIndex - 1;
	if (i < 0)
		i = max;
	p.selectedIndex = i;
	loadpainting(p);
}

function right() {
	if (thumbnailmode)
		return thumbright();
	var p = popup();
	var max = p.options.length - 1;
	var i = p.selectedIndex + 1;
	if (i > max)
		i = 0;
	p.selectedIndex = i;
	loadpainting(p);
}

/* slide show stuff */

var showrunning = false;
var showintervaltimer;
var showseconds = 4 * 1000;
var showrandom = 0;
var showheight = 300;
var showwidth = 300;

function settiming(p) {
	showseconds = p.value * 1000;
	if (showrunning) {
		stopshow();
		startshow();
	}
}

function show() {
	var options="width=" + showwidth + ",height=" + showheight;
	var x = screen.availWidth;
	x = Math.floor(x / 2 - showwidth / 2);
	var y = screen.availHeight;
	y = Math.floor(y / 2 - showheight / 2);
	options += ",left=" + x + ",top=" + y;
	// options += ",screenX=" + x + ",screenY=" + y;
	options += ",scrollbars=no,resizable=no,status=no";
	var panel = window.open("show.html","show",options);
	panel.focus();
}

function getStyleProperty(element, key) {
	if (document.defaultView) {
		return document.defaultView.getComputedStyle(element,"").getPropertyValue(key);
	}
	else {
		return element.currentStyle[key];
	}
}

function showreveal1(div,direction) {
	div.clipRect[2] += direction ? 10 : -10;
	if (direction && div.clipRect[2] >= div.fullHeight) {
		div.style.clip = 'rect(auto)';
		clearInterval(div.intervalID);
		div.intervalID = null;
	}
	else if (!direction && div.clipRect[2] <= 0) {
		div.style.visibility = "hidden";
		clearInterval(div.intervalID);
		div.intervalID = null;
	}
	div.style.clip = 'rect(' + div.clipRect.join('px ') + 'px)';
}

function show2() {
		var spanel = document.getElementById("spanel");
		if (spanel.intervalID != undefined && spanel.intervalID != null)
			return;
		var visibility = getStyleProperty(spanel,"visibility");
		if (visibility == "visible") {
			spanel.clipRect = [0, spanel.offsetWidth + 2, spanel.offsetHeight, 0];
			spanel.intervalID = setInterval(function() {showreveal1(spanel,0);}, 10);
			// spanel.style.visibility = "hidden";
		}
		else {
		var el = document.getElementById("target");	
		var container = document.getElementById("container");
		var offsetLeft = container.offsetLeft;
		var width1 = parseInt(getStyleProperty(container,"width"));
		var width2 = parseInt(getStyleProperty(spanel,"width"));
		offsetLeft = offsetLeft + (width1 / 2) - (width2 / 2);
		/* spanel.style.top = el.offsetTop - 5 + "px"; */
		spanel.style.top = el.offsetTop - ((is_fx || is_moz) ? 5 : 3) + "px";
		spanel.style.left = offsetLeft + "px";
		setbuttonstate();
		spanel.fullHeight = spanel.offsetHeight;
		spanel.clipRect = [0, width2 + 2, 0, 0]; // have to manually add padding to width?!
		spanel.style.clip = 'rect(' + spanel.clipRect.join('px ') + 'px)';
		spanel.intervalID = setInterval(function() {showreveal1(spanel,1);}, 10);
		spanel.style.visibility = "visible";
		}
}

function hide2() {
		var spanel = document.getElementById("spanel");
		spanel.style.visibility = "hidden";
}

function showcallback() {
	if (showrandom)
		randomnext();
	else
		right();
}
	
function startshow() {
	if (thumbnailmode)
		thumbzoom(0);
	showintervaltimer = setInterval("showcallback()", showseconds);
	showrunning = true;
}

function stopshow() {
	clearInterval(showintervaltimer);
	showrunning = false;
}

		function setbuttonstate() {
			var img = document.getElementById("playbutton");
			img.src = "controls/" + ((showrunning) ? "pause.gif" : "play.gif");
			var button = document.getElementById("randomtoggle");
			button.checked = showrandom;
			var label = document.getElementById("buttonlabel")
			label.innerHTML = (showrunning) ? "Pause" : "Play";
			var timing = document.getElementById("timing");
			var value = showseconds / 1000;
			timing.value = value;
		}
		
		function togglestate () {
			if (showrunning)
				stopshow();
			else
				startshow();
			setbuttonstate();			
		}
		
		function togglerandom() {
			showrandom = !showrandom;
			setbuttonstate();	
		}		

function homeinit() {
			var list = document.getElementById("gallerylist");
			for (var i = 0; i < galleries.length; i++) {
				var title = galleries[i][0];
				var li = document.createElement("li");
				list.appendChild(li);
				var link = document.createElement("a");
				link.href="paintings.html?gallery=" + i;
				li.appendChild(link);
				link.innerHTML = title;
			}
			var featured = galleries[0][1];
			paintings = new Array();
			for (var i=0; i < featured.length; i++) {
				paintings[i] = apaintings[featured[i] - 1];
			}
			var img = document.getElementById("homeimage")
			var index = Math.floor(Math.random() * (paintings.length - 1));
			var file = "images/home/" + paintings[index][0] + ".jpg";
			img.src = file;
			var link = document.getElementById("homeimagelink")
			link.href = "paintings.html?gallery=0&index=" + index;
		}

function clickIE4(){
	if (event.button==2){
		return false;
	}
}

function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			return false;
		}
	}
}

if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
	document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("return false;")
		
