/*
BEHAVIOURS.JS
All operational functions and UI events
*/
var isIE = /MSIE (\d+\.\d+);/.test(navigator.userAgent);

// UI elements
var gallery, viewer, about, title, caption;

var baseTitle = "Jack Tew Productions: ";

var fadeIn, fadeOut;
var preloader;


/* Functions */
function initialise() {
	gallery  = document.getElementById("gallery");
	viewer   = document.getElementById("viewer");
	about    = document.getElementById("about");
	title    = document.getElementById("title");
	caption  = document.getElementById("caption");
	
	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);
	
	loadGallery("#film");
}

function loadGallery(a) {
	// initialise
	about.style.display          = "none";
	gallery.style.display        = "block";
	viewer.style.display         = "block";
	viewer.style.backgroundImage = "";
	
	hashValue = (typeof(a) == "string") ? a : a.hash;
	
	// load image data
	folder      = "/gallery/" + hashValue.replace(/#/, "") + "/";
	working_set = eval(hashValue.replace(/#/, ""));
	
	index       = 0;
	count       = working_set.length;
	
	var images = new Array();
	
	for (i = 0; i < count; i++) {
		images.push(getFileName(i));
	}
	
	preloader = new ImagePreloader(images, startGallery, loadProgress);
	
	// browser update
	window.location.hash  = hashValue.toLowerCase();
	window.document.title = baseTitle + ((a.innerText == undefined) ? "Film" : a.innerText);
	
	
	return false;
}

function advanceGallery(a) {
	doChange = true;
	
	switch (a.hash.toLowerCase()) {
		case "#previous":
			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
	gallery.style.display  = "none";
	
	(a.hash == "#about") ? about.style.display = "block" : contact.style.display = "block";
	
	
	// browser update
	window.location.hash  = a.hash.toLowerCase();
	window.document.title = baseTitle + a.innerText;
	
	return false;
}


/* Operationals */
function getFileName(i) {
	pair = (i == undefined) ? working_set[index].split("|") : working_set[i].split("|");
	src  = folder + pair[0];
	
	return src;
}

function getImageTitle() {
	return (working_set[index].split("|")[1] == "") ? "Untitled" : working_set[index].split("|")[1];
}

function getImageCaption() {
	return (working_set[index].split("|")[2] == "") ? "<p>No description provided.</p>" : working_set[index].split("|")[2];
}

function externalLink(a) {
	window.open(a, "popup");
	
	return false;
}


/* Events & Callbacks */
function startGallery() {
	updateImage();
	updateText();
}

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();
	caption.innerHTML = getImageCaption();
}


/* 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|$)'));
}

