
/*
<a href="#" onclick="Effect.BlindUp('home_splash_bg', {duration:0.5}); return false;">BlindUp()</a> | 
<a href="#" onclick="Effect.BlindDown('home_splash_bg', {transition: Effect.Transitions.spring}); return false;">BlindDown()</a>

Effect.DropOut('fade2');
Effect.Appear('fade2');

Effect.BlindUp('home_splash_bg', {duration:0.5});
Effect.BlindDown('home_splash_bg', {transition:Effect.Transitions.spring});

Effect.Move('name', {x:200, transition:Effect.Transitions.spring});

new Effect.BlindDown('vid_bg', {transition:Effect.Transitions.sinoidal, delay:1, duration:0.5});

new Effect.Opacity('header', {from:0, to:1, duration:0.5});

transition:Effect.Transitions.
	linear
	sinoidal
	reverse
	flicker
	wobble
	pulse
	spring
	full
*/

var xml;
var lastPage;
var lastThumb;
var pageIndex = 	0;				//What page of thumbnails to create.
var thumbsPerPage = 3;				//Max number of thumbnails to display on each page.
var pageCount = 	0;				//Used for div id. Incremented by 1 each time a new page is created.
var pagesArr = 		new Array();	//Holds list of page id's.
var transitioning = true;			//Prevents the scroll_l/scroll_r buttons being used when the gallery is animating.
var vidDownload;

var imageNum = 		0;
var lastImage;


/* LOAD XML */

function galleryLoaded(url){
	// notice the use of a proxy to circumvent the Same Origin Policy.
	//var url = '/proxy?url=' + encodeURIComponent('http://www.google.com/search?q=Prototype');
	//var url = '_lib/inc/gallery.php';
  	
	new Ajax.Request(url, {
	method: 'get',
	//parameters: {id:galleryID},
	onSuccess: successFunc,
	onFailure: failureFunc
	});
	
	function successFunc(response){
		xml = 			response.responseXML;
		lastThumb = 	xml.getElementsByTagName("section").length - 1;
		lastPage = 		Math.floor((lastThumb + 0.99) / thumbsPerPage);
		lastImage = 	xml.getElementsByTagName("image").length - 1;	
		
		document.getElementById("gallery_thumbs").style.display = 		"block";
		var pageObj = 	createPage();
		addPage(pageObj, "opacity");
		
		changeContent(imageNum);
	}
	
	function failureFunc(response){
		var str = "";
		for(var objName in response){
			str += 	objName + " -> " + response[objName];
		}
		alert("AJAX Request failure: " + url + "?gID=" + galleryID + "\nResponse:\n" + str);
	}
}




/* THUMBNAIL GALLERY */

function scrollDiv(dir){
	var newPage = 	false;
	//don't create a new page if animations are running
	if(transitioning){
		return;
	}
	//check that the new page won't be out of range.
	if((dir == "up") && (pageIndex - 1 >= 0)){
		pageIndex--;
		newPage = 	true;
	}
	if((dir == "down") && (pageIndex + 1 <= lastPage)){
		pageIndex++;
		newPage = 	true;
	}
	//make a new page if it's not out of range.
	if(newPage){
		var pageObj = 	createPage();
		addPage(pageObj, dir);
	}
}


function removePage(effect){
	//remove page div
	var divID = 		effect.element.getAttribute("id");
	var pageDiv = 		document.getElementById(divID);
	pageDiv.parentNode.removeChild(pageDiv);
	//remove divID from pagesArr
	for(var i = 0; i < pagesArr.length; i++){
		if(pagesArr[i] == divID){
			pagesArr.splice(i, 1);	
		}
	}
}


function createPage(){
	var str;
	var divID = 	"item" + pageCount;
	pageCount++;
	
	//Work out xml record range
	var startNum = 	pageIndex * thumbsPerPage;
	var endNum = 	startNum + thumbsPerPage - 1;
	if(endNum > lastThumb){
		endNum = 	lastThumb;	
	}
	
	//Create new gallery page html/css
	//IE opacity hack: width/height style may be required if the parent div (#content) is set to absolute. Google: hasLayout
	str = 	'\n<div id="' + divID + '" style="position:absolute; display:none;">';
	str += 	'\n<div class="page">';
	for(var i = startNum; i <= endNum; i++){
		var uri = 		xml.getElementsByTagName("section")[i].getAttribute('uri');
		var title = 	xml.getElementsByTagName("section")[i].getAttribute('title');
		//This block gets the index of the first image in the section
		var findElem = 	xml.getElementsByTagName("section")[i].childNodes[1]; //Not childNodes[0] -> that's the white space (TEST IN IE!!!)
		var thisElem;
		var findIndex = 0;
		for(var j = 0; j <= lastImage; j++){
			thisElem = 	xml.getElementsByTagName("image")[j];
			if(findElem == thisElem){
				findIndex = 	j;
			}
		}
		
		str += 	'\n	<a href=\'' + 'javascript:changeContent(' + findIndex + ')' + '\'>';
		str += 	'\n	<img src="' + uri + '" />';
		str += 	'\n	<span class="thumb_title">' + title + '</span>';
		str += 	'\n	</a>';
	}
	str += 	'\n</div>';
	str += 	'\n</div>';
	
	return({divID:divID, str:str});
}


function addPage(pageObj, dir){
	var divID = 	pageObj.divID;
	var str = 		pageObj.str;
	
	//Add this page to the gallery content div
	var contentDiv = 			document.getElementById("thumbs_content");
	contentDiv.innerHTML += 	str;
	
	//Add the ID of the new div
	pagesArr.push(divID);
	
	//Get width of #content div
	var contentDivHeight = 	450;
	
	//Transition new page
	transitioning = 	true;
	if(dir == "down"){
		document.getElementById(divID).style.top = 	contentDivHeight + "px";
		new Effect.Move(divID, {y:-contentDivHeight, duration:0.5, position:'relative'});
		new Effect.Appear(divID, {from:0, to:1, duration:0.8, afterFinish:transEnd});
	}
	if(dir == "up"){
		document.getElementById(divID).style.top = 	-contentDivHeight + "px";
		new Effect.Move(divID, {y:contentDivHeight, duration:0.5, position:'absolute'});
		new Effect.Appear(divID, {from:0, to:1, duration:0.8, afterFinish:transEnd});
	}
	if(dir == "opacity"){
		new Effect.Appear(divID, {from:0, to:1, duration:0.8, afterFinish:transEnd});
	}
	
	//Transition other pages
	for(var i = 0; i < pagesArr.length - 1; i++){
		var pageDiv = 	document.getElementById(pagesArr[i]);
		if(dir == "down"){
			new Effect.Move(pageDiv, {y:-contentDivHeight, duration:0.5, position:'absolute', afterFinish:removePage});
		}
		if(dir == "up"){
			new Effect.Move(pageDiv, {y:contentDivHeight, duration:0.5, position:'absolute', afterFinish:removePage});
		}
	}
}

function transEnd(effect){
	transitioning = 	false;
}



function nextImage(){
	imageNum++;
	if(imageNum > lastImage){
		imageNum = 	0;	
	}
	changeContent(imageNum);
}

function previousImage(){
	imageNum--;
	if(imageNum < 0){
		imageNum = 	lastImage;	
	}
	changeContent(imageNum);
}



/* SHOW POPUP FOR IMAGE OR VIDEO */

/*
function changeContent(newImageNum){
	hideVideoOverlay();
	hideImageOverlay();
	changeContent2(newImageNum)
}
*/

function changeContent(newImageNum){
	imageNum = 			newImageNum;
	var galleryTitle = 	xml.getElementsByTagName("image")[imageNum].parentNode.getAttribute('title');
	var uri = 			xml.getElementsByTagName("image")[imageNum].getAttribute('uri');
	var path_arr = 		uri.split(".");
	var ext = 			path_arr[path_arr.length - 1];
	switch (ext){
		case "gif":
		case "jpg":
		case "png":	
			imageOverlay(imageNum);
			break;
		case "flv":
			videoOverlay(imageNum);
			break;
		default:
			alert("File type ." + ext + " is not supported by this gallery.");
			break;
	}
	
	document.getElementById("gallery_title").innerHTML = 	galleryTitle;
}



/* IMAGE POPUP */

function imageOverlay(thumbNum){
	var title = 	xml.getElementsByTagName("image")[thumbNum].getAttribute('title');
	var uri = 		xml.getElementsByTagName("image")[thumbNum].getAttribute('uri');
	/*
	Note: not implemented
	document.getElementById("img_title").innerHTML = 	title;
	
	//Update gallery_info div
	var galleryInfoDiv = 		document.getElementById("gallery_info");
	galleryInfoDiv.innerHTML = 	'Displaying image ' + (imageNum + 1) + ' of ' + (lastImage + 1);
	*/
	
	document.getElementById("img_content").style.display = 	"none";
	document.getElementById("img_holder").style.display = 	"none";
	
	//need to load image as an object in order to get width/height in IE.
	//This won't work. IE won't return width/height values for the image:
	//document.getElementById("img_holder").innerHTML = 	'<img src="/dynamic/' + uri + '" id="img_ref" onload="positionImage()" />';
	var imgObj = new Image();
	imgObj.onload = function setImage(){
		document.getElementById("img_holder").innerHTML = 	'<img src="' + imgObj.src + '"/>';
		positionImage(imgObj);
	}
	imgObj.src = 	uri;
	
	
	//Hide flash content
	hideVideoOverlay();
	
	//show loading dial
	document.getElementById("img_loading").style.display = 	"block";
}

function positionImage(imgObj){
	var imgWidth = 		imgObj.width;
	var imgHeight = 	imgObj.height;
	
	//Can't get style.width / height
	var imgOverlayWidth = 	529;
	var imgOverlayHeight = 	525;
	
	//Set image position
	document.getElementById("img_content").style.left = 	Math.round((imgOverlayWidth - imgWidth - 20) / 2) + "px";
	document.getElementById("img_content").style.top = 		Math.round((imgOverlayHeight - imgHeight) / 2) + "px";
	document.getElementById("img_content").style.width = 	imgWidth + "px";
	document.getElementById("img_content").style.height = 	imgHeight + "px";
	document.getElementById("img_holder").style.width = 	imgWidth + "px";
	document.getElementById("img_holder").style.height = 	imgHeight + "px";
	//Set title position
	//document.getElementById("img_title").style.left = 	Math.round((imgOverlayWidth - imgWidth) / 2) + "px";
	
	//For IE need to fade in container *and* children.
	new Effect.Appear("img_content", {from:0, to:1, duration:0});
	new Effect.Appear("img_holder", {from:0, to:1, duration:0});
	//Frame
	new Effect.Appear("img_border_tl", {from:0, to:1, duration:0});
	new Effect.Appear("img_border_t", {from:0, to:1, duration:0});
	new Effect.Appear("img_border_tr", {from:0, to:1, duration:0});
	new Effect.Appear("img_border_l", {from:0, to:1, duration:0});
	new Effect.Appear("img_border_r", {from:0, to:1, duration:0});
	new Effect.Appear("img_border_bl", {from:0, to:1, duration:0});
	new Effect.Appear("img_border_b", {from:0, to:1, duration:0});
	new Effect.Appear("img_border_br", {from:0, to:1, duration:0});
	
	//Fade in objects except the image/frame
	new Effect.Appear("img_overlay", {from:0, to:1, duration:0.7});
	//new Effect.Appear("img_title", {from:0, to:1, duration:0.7});
	
	//hide loading dial
	document.getElementById("img_loading").style.display = 	"none";
}

function hideImageOverlay(){
	if(lastThumb < 1){
		return;	
	}
	
	//For IE need to fade out container *and* children.
	new Effect.Fade("img_overlay", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_title", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_content", {from:1, to:0, duration:0.5});
	//Frame
	new Effect.Fade("img_border_tl", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_border_t", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_border_tr", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_border_l", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_border_r", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_border_bl", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_border_b", {from:1, to:0, duration:0.5});
	new Effect.Fade("img_border_br", {from:1, to:0, duration:0.5});
}



/* VIDEO POPUP */

function videoOverlay(thumbNum){
	var update = 	true;
	var title = 	xml.getElementsByTagName("image")[thumbNum].getAttribute('title');
	var uri = 		xml.getElementsByTagName("image")[thumbNum].getAttribute('uri');
	
	vidDownload = 	xml.getElementsByTagName("image")[thumbNum].getAttribute('download');
	document.getElementById("vid_download").style.visibility = 	"visible";
	if(vidDownload == null){
		document.getElementById("vid_download").style.visibility = 	"hidden";
	}
	
	//For IE need to fade in container *and* children.
	new Effect.Appear("vid_overlay", {from:0, to:1, duration:0.7});
	new Effect.Appear("vid_download", {from:0, to:1, duration:0.7});
	new Effect.Appear("vid_content", {from:0, to:1, duration:0.7});
	
	embedVideoFlash(uri, title);
	
	//Hide image content
	hideImageOverlay();
}

function hideVideoOverlay(){
	if(lastThumb < 1){
		return;	
	}
	//For IE need to fade out container *and* children.
	new Effect.Fade("vid_overlay", {from:1, to:0, duration:0.5});
	new Effect.Fade("vid_download", {from:1, to:0, duration:0.5});
	new Effect.Fade("vid_content", {from:1, to:0, duration:0.5});
	document.getElementById("vid_loading").style.display = 	"none";
}

function download(){
	window.location = vidDownload;
}

function flashMovie(movieName) {
    if(navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }else{
        return document[movieName];
    }
}

function embedVideoFlash(uri, title){
	var requiredMajorVersion = 	9;
	var requiredMinorVersion = 	0;
	var requiredRevision = 		115;
	
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(hasRightVersion){
		AC_FL_RunContent(
			'codebase',	'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'movie',	'gallery/video',
			'src',		'gallery/video',
			'name',		'myFlash',
			'id',		'myFlash',
			'flashVars','uri=' + uri + '&title=' + title,
			'width',	'100%',
			'height',	'100%',
			'align',	'middle',
			'salign',	'lt',
			'bgcolor',	'#ffffff',
			'wmode',	'transparent',
			'scale',	'showall',
			'quality',	'high',
			'play',		'true',
			'loop',		'true',
			'menu',		'false',
			'devicefont',			'false',
			'allowScriptAccess',	'sameDomain',
			'allowFullScreen',		'true',
			'pluginspage', 			'http://www.macromedia.com/go/getflashplayer'
		);
		document.getElementById("img_loading").style.display = 	"block";
	}else{
		var alternateContent =  '<div id="vid_no_flash">'
		+ '<p>Because of your browser setup, you can\'t view this content online. You do have options though:</p>'
		+ '<ul>'
		+ '<li><a href=' + vidDownload + '>Click here</a> to download a copy of this video. You can then watch it locally using a media player on your computer.</li>'
		+ '<li>Install the free Flash Player to fix this problem. '
		+ '<a href=http://www.macromedia.com/go/getflash/>Download it Here.</a></ul>'
		+ '</ul></div>';
		document.getElementById("vid_content").innerHTML = 		alternateContent;
		document.getElementById("vid_loading").style.display = 	"none";
	}
}
