/*
BEHAVIOURS.JS
All operational functions and UI events
*/
var isIE = /MSIE (\d+\.\d+);/.test(navigator.userAgent);

// UI elements
var viewer;
var info, title, desc;

var vimeoIFrame, videoclose, hasvideo;

var fadeIn, fadeOut;
var preloader;


/* Functions */
function initialise() {
	viewer   = document.getElementById("viewer");
	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);
	
	// add description box
	info     = document.createElement("div");
	info.id  = "description";
	
	title    = document.createElement("h3");
	title.id = "title";
	
	desc     = document.createElement("div");
	desc.id  = "caption";
	
	info.appendChild(title);
	info.appendChild(desc);
	
	viewer.appendChild(info);
	
	// add hasvideo badge
	hasvideo    = document.createElement("div");
	hasvideo.id = "hasvideo";
	
	
	// add video close
	videoclose         = document.createElement("div");
	videoclose.id      = "videoclose";
	
	viewer.onmouseover = showInfoPanel;
	viewer.onmouseout  = showInfoPanel;
	viewer.onclick     = showVideo;
	
	info.style.display = "none";
	
	buildGalleryData();
	loadGallery();
	
	
	// vimeo linking
	vimeoIFrame             = document.createElement("iframe");
	vimeoIFrame.id          = "vimeo";
	vimeoIFrame.width       = vimeo_width;
	vimeoIFrame.height      = vimeo_height;
	vimeoIFrame.frameBorder = 0;
	
	vimeoIFrame.playing     = false;
}

function showInfoPanel(state) {
	var mode = (info.style.display == "block" || state == false) ? "none" : "block";

	info.style.display   = mode;
	info.style.marginTop = (-400 + (400 - info.offsetHeight)) + "px";
	
	videoclose.style.display = info.style.display;
	hasvideo.style.display   = info.style.display;
}

function loadGallery() {
	// initialise
	viewer.style.display         = "block";
	viewer.style.backgroundImage = "";
	
	
	// load image data
	folder      = "/gallery/";
	index       = 0;
	count       = gallery.length;
	
	var images = new Array();
	
	for (i = 0; i < count; i++) {
		images.push(getFileName(i));
	}
	
	preloader = new ImagePreloader(images, startGallery, loadProgress);

	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;
	}
	
	showInfoPanel(false);
	hideVideo();
	
	fadeOut.start();
	updateText();
	
	return false;
}

function showVideo() {
	if (!vimeoIFrame.playing) {
		if (viewer.vimeo != null) {
			vimeoIFrame.src = "http://player.vimeo.com/video/" + viewer.vimeo + "?color=" + vimeo_color + "&autoplay=1&portrait=0&title=0&byline=0";
			viewer.appendChild(vimeoIFrame);
			viewer.appendChild(videoclose);
			
			vimeoIFrame.playing = true;
		}
	}
	else {
		showInfoPanel(true);
		hideVideo();
	}
}

function hideVideo() {
	try {
		var obj = getParentObject(vimeoIFrame);
		
		obj.removeChild(vimeoIFrame);
		obj.removeChild(videoclose);
		
		vimeoIFrame.playing = false;
	}
	catch(e) {
	}
}


/* Operationals */
function getFileName(i) {
	pair = (i == undefined) ? gallery[index].split("|") : gallery[i].split("|");
	src  = folder + pair[0];
	
	return src;
}

function getImageTitle() {
	return (gallery[index].split("|")[1] == "") ? "Untitled" : gallery[index].split("|")[1];
}

function getImageCaption() {
	return (gallery[index].split("|")[2] == "") ? "<p>No description provided.</p>" : gallery[index].split("|")[2];
}

function getVideoData() {
	return (gallery[index].split("|")[3] == "") ? null : gallery[index].split("|")[3];
}

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() + ")";
	
	// check for video content
	var data = getVideoData();
	viewer.vimeo = data;
	
	if (data != null) {
		addCSSClass(viewer, "handcursor");
		viewer.appendChild(hasvideo);
	}
	else {
		removeCSSClass(viewer, "handcursor");
		
		try {
			getParentObject(hasvideo).removeChild(hasvideo);
		}
		catch(e) {
		}
	}
	
	// start fade up
	fadeIn.start();
}

function updateText() {	
	title.innerHTML = getImageTitle();
	desc.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 addCSSClass(obj, css) {
	if (!hasCSSClass(obj, css)) { obj.className += (" " + css) }
}

function removeCSSClass(obj, css) {
	if (hasCSSClass(obj, css)) {
		var reg = new RegExp('(\\s|^)' + css + '(\\s|$)');
		obj.className = obj.className.replace(reg, ' ');
	}
}

function hasCSSClass(obj, css) {
	return obj.className.match(new RegExp('(\\s|^)' + css + '(\\s|$)'));
}

