
function showPic(whichpic) {
	if (!document.getElementById("placeholder")) return true; 
	var placeholder = document.getElementById("placeholder");
	var swf_name = whichpic.getAttribute("name");
	var swf_filename = swf_name + ".swf"
	var so = new SWFObject("./m/"+swf_filename, swf_name, "600", "325", "7", "#b20000");
	if (!so.write("placeholder")) {
		var source = whichpic.getAttribute("href");
		while (placeholder.childNodes.length > 0) {
			placeholder.removeChild(placeholder.firstChild);
		}
		var img = document.createElement("img");
		img.setAttribute("src", source);
		img.setAttribute("alt", "");
		placeholder.appendChild(img);
	}
	return false;
}

function preparePlaceholder() {
	if (!document.createElement || 
		!document.createTextNode || 
		!document.getElementById || 
		!document.getElementById("photogallery")) return false;
	var placeholder = document.createElement("div");
	placeholder.setAttribute("id", "placeholder");
	var img = document.createElement("img");
	img.setAttribute("src", "./i/suteishilogo-3D.gif");
	img.style.paddingTop = 60 + "px";
	var para1 = document.createElement("p")
	var txt1 = document.createTextNode("Photo Gallery");
	para1.appendChild(txt1);
	placeholder.appendChild(img);
	placeholder.appendChild(para1);
	var photogallery = document.getElementById("photogallery");
	insertAfter(placeholder, photogallery);
}

function preparePhotoGallery() {
	if (!document.getElementsByTagName ||
		!document.getElementById ||
		!document.getElementById("photogallery")) return false;
	var photogallery = document.getElementById("photogallery");
	// event handler for links
	var links = photogallery.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) {
		links[i].onclick = function() {
			return showPic(this);
		}
	}
}

function setDirection(e) {
	if (!document.getElementById ||
		!document.getElementById("photogallery")) return false;
	if (!e) e = window.event;
	var photogallery = document.getElementById("photogallery");
	if (!photogallery.direction) return false;
	if (!photogallery.prevX) {
		if (photogallery.direction == "left")
			photogallery.prevX = -99999;
		else if (photogallery.direction == "right")
			photogallery.prevX = 99999;
	}
	if (e) {
		var delta = 50;
		if (e.pageX) {
			if (e.pageX < photogallery.prevX - delta && photogallery.direction != "right") {
				photogallery.direction = "right";
			} else if (e.pageX > photogallery.prevX + delta && photogallery.direction != "left") {
				photogallery.direction = "left";
			}
			photogallery.prevX = e.pageX;
		} else if (e.clientX) {
			var posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			if (posX < photogallery.prevX - delta && photogallery.direction != "right") {
				photogallery.direction = "right";
			} else if (posX > photogallery.prevX + delta && photogallery.direction != "left") {
				photogallery.direction = "left";
			}
			photogallery.prevX = posX;
		}
	}
}

function movePictures(elementId, left_end, right_end, picspace_width, interval) {
	if (!document.getElementsByTagName ||
		!document.getElementById ||
		!document.getElementById("photogallery")) return false;
	var photogallery = document.getElementById(elementId);
	if (!photogallery.direction || 
		photogallery.head == null || 
		photogallery.tail == null) return false;
	var thumbnails = photogallery.getElementsByTagName("a");
	for (var i = 0; i < thumbnails.length; i++) {
		if (!thumbnails[i].style.left) return false;
	}
	if (photogallery.movement) clearTimeout(photogallery.movement);
	var direction = photogallery.direction; 
	if (direction == "right") {
		for (var i = thumbnails.length - 1; i >= 0; i--) {
			var xpos = parseInt(thumbnails[i].style.left);
			xpos++;
			if (xpos > right_end && i == photogallery.tail) {
				// send it to the head
				var headLeftPos = parseInt(thumbnails[photogallery.head].style.left);
				xpos = headLeftPos - picspace_width;
				// update head and tail values
				photogallery.head = i
				photogallery.tail = (i - 1 == -1) ? thumbnails.length-1 : (i - 1);
			}
			thumbnails[i].style.left = xpos + "px";
		}
	} else if (direction == "left") {
		for (var i = 0; i < thumbnails.length; i++) {
			var xpos = parseInt(thumbnails[i].style.left);
			xpos--;
			if (xpos < left_end && i == photogallery.head) {
				// send it to the tail
				var tailRightPos = parseInt(thumbnails[photogallery.tail].style.left) + picspace_width;
				xpos = tailRightPos;
				// update head and tail values
				photogallery.head = (i + 1 == thumbnails.length) ? 0 : (i + 1);
				photogallery.tail = i;
			}
			thumbnails[i].style.left = xpos + "px";
		}
	}
	var repeat = "movePictures('"+elementId+"', "+left_end+", "+right_end+", "+picspace_width+", "+interval+")";
	photogallery.movement = setTimeout(repeat, interval);
}

function prepareSlideshow() {
	if (!document.getElementsByTagName ||
		!document.getElementById ||
		!document.getElementById("photogallery")) return false;
	var photogallery = document.getElementById("photogallery");
	var img_space_width = 150;
	var interval = 15;
	// for scrolling effect
	photogallery.style.position = "relative";
	photogallery.style.width = "700px";
	photogallery.style.overflow = "hidden";
	photogallery.style.visibility = "visible";
	photogallery.direction = "left";
	photogallery.onmouseover = setDirection;
	// set the initial positions
	var thumbnails = photogallery.getElementsByTagName("a");
	var num_pics = thumbnails.length;
	var left_endpoint = -250; // gotta start somewhere
	var right_endpoint = img_space_width * (num_pics - 1) + left_endpoint;
	for (var i = 0; i < thumbnails.length; i++) {
		thumbnails[i].style.position = "absolute";
		thumbnails[i].style.top = 15 + "px"
		thumbnails[i].style.left = (i * img_space_width - 225) + "px";
	}
	photogallery.head = 0;
	photogallery.tail = thumbnails.length - 1;
	movePictures("photogallery", left_endpoint, right_endpoint, img_space_width, interval);
}

addLoadEvent(preparePlaceholder);
addLoadEvent(preparePhotoGallery);
addLoadEvent(prepareSlideshow);