var pageWidth;
var pageHeight;
var overlay;
var curphoto;

function changeClass(cn) {
	var text = document.getElementById("cmod").lastChild;
	text.className = cn;
}

function getEventTarget(e) {
	var obj;

	if(e.target)
		obj = e.target;
	else if(e.srcElement)
		obj = e.srcElement;
	if(obj.nodeType == 3)
		obj = obj.parentNode;
	return obj;
}

function showOverlay(e) {
	if(!e) var e = window.event;

	var targetObj = getEventTarget(e);
	var cacheIndex = targetObj.id.replace(/t/gi, "");

	if(curphoto.firstChild.id != ("p" + cacheIndex)) {
		var img = document.createElement('IMG');

		img.setAttribute("id", "p" + cacheIndex);
		img.setAttribute("src", cache[cacheIndex].src);
		img.setAttribute("alt", targetObj.alt);
		img.style.width = cache[cacheIndex].width + "px";	// "640px";
		img.style.height = cache[cacheIndex].height + "px";	// 640 * (img.height / img.width) + "px";

		if(curphoto.firstChild.nodeName == "IMG")
			curphoto.replaceChild(img, curphoto.firstChild);
		else
			curphoto.insertBefore(img, curphoto.firstChild);

		curphoto.style.width = cache[cacheIndex].width + 2 + "px";
		curphoto.style.height = cache[cacheIndex].height + 42 + "px";

		positionOverlay();
	}

	setOpacity(curphoto, 0);
	fade(overlay, 0, 75, 500);
	setTimeout("fade(curphoto, 0, 100, 500)", 500);

	e.cancelBubble = true;
	if(e.stopPropagation) e.stopPropagation();
	return false;
}

function changeImage(i) {
	fade(curphoto, 100, 0, 500);
	setTimeout(
		function () {
			curphoto.firstChild.id = "p" + i;
			curphoto.firstChild.src = cache[i].src;
			curphoto.firstChild.alt = document.getElementById("t" + i).alt;
			curphoto.firstChild.style.width = cache[i].width + "px";
			curphoto.firstChild.style.height = cache[i].height + "px";
			curphoto.style.width = cache[i].width + 2 + "px";
			curphoto.style.height = cache[i].height + 42 + "px";
			positionOverlay();
			fade(curphoto, 0, 100, 500);
		}, 500
	);
}

function prevImage() {
	var newIndex = (curphoto.firstChild.id.replace(/p/gi, "") - 1) % cache.length;
	(newIndex < 0) ? changeImage(newIndex + cache.length) : changeImage(newIndex);
}

function nextImage() {
	changeImage(((curphoto.firstChild.id.replace(/p/gi, "") >> 0) + 1) % cache.length);
}

function hideOverlay() {
	fade(curphoto, 100, 0, 500);
	setTimeout("fade(overlay, 75, 0, 500)", 500);
}

function fade(obj, start, stop, time) {
	var timer = 0;

	if(start > stop) {	// Fade out
		speed = Math.round(time / (start - stop));
		for(i = start; i >= stop; i--) {
			setTimeout("setOpacity(" + obj.id + ", " + i +")", (timer * speed));
			timer++;
		}
	}
	else if(start < stop) {	// Fade in
		speed = Math.round(time / (stop - start));
		for(i = start; i <= stop; i++) {
			setTimeout("setOpacity(" + obj.id + ", " + i +")", (timer * speed));
			timer++;
		}
	}
}

function setOpacity(obj, opacity) {
	var styleObj = obj.style;

	styleObj.opacity = (opacity / 100);
	styleObj.KhtmlOpacity = (opacity / 100);
	styleObj.MozOpacity = (opacity / 100);
	styleObj.filter = "alpha(opacity=" + opacity + ")";

	(opacity == 0) ? styleObj.display = "none" : styleObj.display = "block";
}

function positionOverlay() {
	// Reset position and display offscreen so clientWidth and clientHeight have the correct values.
	curphoto.style.left = "-1000px";
	curphoto.style.top = "-1000px";
	curphoto.style.display = "block";

	if(curphoto.clientWidth > pageWidth - 16)
		curphoto.style.width = pageWidth - 32 + "px";
	else
		curphoto.style.width = curphoto.clientWidth - 16 + "px";

	if(curphoto.clientHeight > pageHeight - 16)
		curphoto.style.height = pageHeight - 32 + "px";
	else
		curphoto.style.height = curphoto.clientHeight - 16 + "px";

	curphoto.style.left = Math.max(0, (pageWidth - curphoto.clientWidth) / 2) + "px";
	curphoto.style.top = Math.max(0, (pageHeight - curphoto.clientHeight) / 2) + "px";
}

function setURLTarget() {
	var links = document.getElementsByTagName("a");
	for(i = 0; i < links.length; i++) {
		if(links[i].protocol != "javascript:" && links[i].parentNode.id == "copyright")
			links[i].onclick = function () {window.open(this.href); return false;};
		if(links[i].parentNode.className == "photo") {
			links[i].onclick = showOverlay;
			if(links[i].captureEvents)
				links[i].captureEvents(Event.CLICK);
		}
	}
}

function init() {
	pageWidth = document.documentElement.clientWidth;
	pageHeight = document.documentElement.clientHeight;
	overlay = document.getElementById("overlay");
	curphoto = document.getElementById("curphoto");
	setURLTarget();
	cachePhotos();
	//alert(getScrollerWidth());
}

function secureString(tld, dom, uname) {
	var at = "@";
	var dot = ".";
	var str = uname + at + dom + dot + tld;

	return "<a href=\"mailto:" + str + "\">" + str + "</a>";
}

function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';

    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(
        document.body.lastChild);

    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}