/*
BEHAVIOURS.JS
All operational functions and UI events
*/
var isIE = /MSIE (\d+\.\d+);/.test(navigator.userAgent);

// UI elements
var viewer, about, contact, wrapper;
var control, title, counter, progress;

var baseTitle = "Justin Slee Photographer: ";

var fadeIn, fadeOut;
var preloader;


/* Functions */
function initialise() {
	viewer   = document.getElementById("viewer");
	about    = document.getElementById("about");
	contact  = document.getElementById("contact");
	wrapper  = document.getElementById("wrapper");
	control  = document.getElementById("control");
	title    = document.getElementById("title");
	counter  = document.getElementById("counter");
	progress = document.getElementById("progress");
	
	fadeIn   = new OpacityTween(viewer, Tween.regularEaseNone, 0, 100, 0.5);
	fadeOut  = new OpacityTween(viewer, Tween.regularEaseNone, 100, 0, 0.5);
	
	fadeOut.onMotionFinished = updateImage;
	
	// add image theft overlay
	laminate = document.createElement("div");
	laminate.className = "laminate";
	
	viewer.appendChild(laminate);
	
	
	// Now, jump to hash...
	jumpToHash();
}

function jumpToHash() {
	if (window.location.hash != "") {
		var hasharray = window.location.hash.split("/");
		
		// first element is our target
		switch (hasharray[0]) {
			case "#about":
			case "#contact":
				loadSection(hasharray[0]);
				break;
			
			default:
				loadGallery(hasharray[0]);
		}
	}
	else {
		intro    = document.createElement("div");
		intro.id = "intro";
		intro.innerHTML = "<p class=\"enter\"><a href=\"#people\" onclick=\"closeIntro();\">Enter</a></p>\n<div id=\"swf\"></div>";
		
		wrapper.appendChild(intro);
		
		var swf_name = "intro_" + Math.floor(Math.random() * 3 + 1) + ".swf";
		swfobject.embedSWF("/lib/swf/" + swf_name, "swf", "852", "540", "9", "#ffffff", false, {}, { menu: "false" });
	}
}

function loadGallery(a) {
	// initialise
	about.style.display          = "none";
	contact.style.display        = "none";
	viewer.style.display         = "block";
	viewer.style.backgroundImage = "";
	
	control.style.display  = "block";
	progress.style.display = "none";
	title.innerHTML        = "Loading images&#8230;"
	counter.innerHTML      = "";
	
	if (typeof(a) == "string") a = document.getElementById("nav_" + a.replace(/#/, ""));
	

	// load image data
	folder      = "/" + a.hash.replace(/#/, "") + "/";
	working_set = eval(a.hash.replace(/#/, ""));
	
	index       = 0;
	count       = working_set.length;
	
	var images = new Array();
	
	for (i = 0; i < working_set.length; i++) {
		images.push(getFileName(i));
	}
	
	preloader = new ImagePreloader(images, startGallery, loadProgress);
	
	// styles update
	parentList     = getParentObject(a, "ul");
	parentListItem = getParentObject(a);
	
	for (i = 0; i < parentList.childNodes.length; i++) {
		thisNode = parentList.childNodes.item(i);
		if (thisNode.nodeName == "LI") { removeClass(thisNode, "this") }
	}
	
	addClass(parentListItem, "this");
	
	
	// browser update
	window.location.hash  = a.hash.toLowerCase();
	window.document.title = baseTitle + a.innerText;
	
	return false;
}

function advanceGallery(a) {
	doChange = true;
	
	switch (a.hash.toLowerCase()) {
		case "#back":
			if (index == 0) {
				index = (count - 1)
			}
			else {
				index--;
			}
			break;
		
		case "#next":
			if (index == (count - 1)) {
				index = 0;
			}
			else {
				index++;
			}
			break;
	}
	
	fadeOut.start();
	updateText();
	
	return false;
}

function loadSection(a) {
	// initialise
	viewer.style.display  = "none";
	control.style.display = "none";
	
	if (typeof(a) == "string") a = document.getElementById("nav_" + a.replace(/#/, ""));
	
	(a.hash == "#about") ? about.style.display = "block" : contact.style.display = "block";
	(a.hash == "#about") ? contact.style.display = "none" : about.style.display = "none";
	
	// styles update
	parentList     = getParentObject(a, "ul");
	parentListItem = getParentObject(a);
	
	for (i = 0; i < parentList.childNodes.length; i++) {
		thisNode = parentList.childNodes.item(i);
		if (thisNode.nodeName == "LI") { removeClass(thisNode, "this") }
	}
	
	addClass(parentListItem, "this");
	
	
	// browser update
	window.location.hash  = a.hash.toLowerCase();
	window.document.title = baseTitle + a.innerText;
	
	return false;
}	

function externalLink(a) {
	window.open(a, "popup");
	
	return false;
}

function closeIntro() {
	wrapper.removeChild(document.getElementById("intro"));
	loadGallery(document.getElementById("nav_people"));
}


/* Operationals */
function getFileName(i) {
	pair = (i == undefined) ? working_set[index].split(":") : working_set[i].split(":");
	src  = "/images" + folder + pair[0];
	
	return src;
}

function getImageTitle() {
	return (working_set[index].split(":")[1] == "") ? "Untitled" : working_set[index].split(":")[1];
}


/* Events & Callbacks */
function startGallery() {
	updateImage();
	updateText();
	progress.style.display = "block";
}

function loadProgress(progress) {
	title.innerHTML = "Loading images&#8230;" + progress + "%";
}

function updateImage() {
	viewer.style.backgroundImage = "url(" + getFileName() + ")";
	
	// start fade up
	fadeIn.start();
}

function updateText() {
	title.innerHTML = getImageTitle();
	counter.innerHTML = (index + 1) + " / " + count;
}


/* DOM tools */
function getParentObject(obj, type) {
	if (type == undefined) {
		// return direct parent
		obj = obj.parentNode;
	}
	else {
		// traverse tree to get specified parent
		while (obj.nodeName != type.toUpperCase()) {
			obj = obj.parentNode;
		}
	}
	
	return obj;
}


/* CSS Class Management */
function addClass(obj, css) {
	if (!hasClass(obj, css)) { obj.className += (" " + css) }
}

function removeClass(obj, css) {
	if (hasClass(obj, css)) {
		var reg = new RegExp('(\\s|^)' + css + '(\\s|$)');
		obj.className = obj.className.replace(reg, ' ');
	}
}

function hasClass(obj, css) {
	return obj.className.match(new RegExp('(\\s|^)' + css + '(\\s|$)'));
}
