// -----------------------------------------------------------------------------------
//
// This page coded by Scott Upton
// http://www.uptonic.com | http://www.couloir.org
//
// This work is licensed under a Creative Commons License
// Attribution-ShareAlike 2.0
// http://creativecommons.org/licenses/by-sa/2.0/
//
// moo.fx, behaviour, prototype, and soundmanager libraries
// copyright their respective owners
//
// -----------------------------------------------------------------------------------
// --- version date: 03/22/06 --------------------------------------------------------

// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1] - 1;
var menuViz = false;
var linkViz = false;
var loadingViz = false;

// if no photoId supplied then set default
var photoId = (!photoId)? 0 : photoId;

/*--------------------------------------------------------------------------*/
// Set ground rules for effects
function init() {
	// Establish menu and loading effects
	myMenu = new fx.Height('linkBox',{duration: 600});
	myLoading = new fx.Combo('loadBox',{duration: 400});
	// Start the show
	mySlideshow.initSwap();
}

// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src;
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href;
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},
	// Override hide, show functions in prototype
	hide: function(element) {
		element = $(element);
		element.style.display = 'none';
	},
	show: function(element) {
		element = $(element);
		element.style.display = '';
	}
});

/*--------------------------------------------------------------------------*/
// Slideshow class
var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(reqYear,curYear,curGal,photoId) {
		this.reqYear 		= reqYear;
		this.curYear 		= curYear;
		this.curGal 		= curGal;
		this.photoId 		= photoId;
		this.photo 			= 'photo';
		this.prevLink 		= 'prevLink';
		this.nextLink 		= 'nextLink';
		this.counter 		= 'counter';
		this.loadImg		= 'loadImg';
		this.loadImgSrc		= 'images/v6/ajax-loader.gif'
		this.loadImgTmpSrc	= 'images/v6/c.gif'
		this.menuToggle		= $('toggleMenu');
		this.controller 	= 'controller.php';
		this.yearList		= 'yearList';
		this.galleryList	= 'galleryList';
	},
	setNewPhotoParams: function() {
		// Set source of new image
		Element.setSrc(this.photo,photoDir + photoArray[photoId][0]);
		// Set anchor for bookmarking
		Element.setHref(this.prevLink, "#" + (photoId+1));
		Element.setHref(this.nextLink, "#" + (photoId+1));
	},
	setCounter: function() {
		// Add counter to indicate current photo of how many total
		Element.setInnerHTML(this.counter,((photoId+1)+' of '+photoNum));
	},
	nextPhoto: function(){
		// Figure out which photo is next
		(photoId == (photoNum - 1)) ? photoId = 0 : photoId++;
		this.toggleLoading();
		this.initSwap();
	},
	prevPhoto: function(){
		// Figure out which photo is previous
		(photoId == 0) ? photoId = photoNum - 1 : photoId--;
		this.toggleLoading();
		this.initSwap();
	},
	toggleMenu: function() {
		// Show appropriate menu state when invoked
		if(menuViz == false){
			this.ajaxYears();
			this.ajaxGalleries();
			myMenu.custom(433,0);
			menuViz = true;
			this.menuToggle.className = 'open';
		} else {
			myMenu.custom(0,433);
			menuViz = false;
			this.menuToggle.className = '';
		}
	},
	toggleLoading: function() {
		// Toggle loading layer on/off
		myLoading.toggle();
	},
	toggleNextPrev: function() {
		// Toggle the next/previous links on/off
		if(linkViz == false){
			Element.hide(this.nextLink);
			Element.hide(this.prevLink);
			linkViz = true;
		} else {
			Element.show(this.nextLink);
			Element.show(this.prevLink);
			linkViz = false;
		}
	},
	ajaxYears: function() {
		// Request years for which galleries exist
		var pars = 'action=years&reqYear=' +this.reqYear+ '&year=' +this.curYear+ '&gal=' +this.curGal;
		new ajax (this.controller, {postBody: pars, update: this.yearList});
	},
	ajaxGalleries: function() {
		// Request galleries in selected year
		var pars = 'action=galleries&reqYear=' +this.reqYear+ '&year=' +this.curYear+ '&gal=' +this.curGal;
		new ajax (this.controller, {postBody: pars, update: this.galleryList});
	},
	ajaxPhotoSwap: function() {
		// Request next photo in series
		var pars = 'action=photo&year=' +this.curYear+ '&gal=' +this.curGal+ '&id=' +photoId;
		new ajax (this.controller, {postBody: pars, onComplete: function(request){$('photo').src = request.responseText;}});
	},
	initSwap: function() {
		// Hide photo and next/previous links temporarily
		Element.setSrc(this.loadImg,this.loadImgSrc);
		Element.hide(this.photo);
		// Set values for bookmarkable links
		Element.setHref(this.prevLink, "#" + (photoId+1));
		Element.setHref(this.nextLink, "#" + (photoId+1));
		// Request src for new image
		this.ajaxPhotoSwap();
		this.setCounter();
		// Hide next/previous links immediately
		this.toggleNextPrev();
	},
	endSwap: function() {
		// Set events once swap has occurred
		Element.setSrc(this.loadImg,this.loadImgTmpSrc);
		Element.show(this.photo);
		this.toggleLoading();
		// Show next/previous links after delay
		setTimeout(this.toggleNextPrev.bind(this),600);
	}
}

/*--------------------------------------------------------------------------*/
// Establish CSS-driven events via Behaviour script
var myrules = {
	'#photo' : function(element){
		element.onload = function(){
			setTimeout("mySlideshow.endSwap();",1000);
		}
	},
	'#prevLink' : function(element){
		element.onclick = function(){
			mySlideshow.prevPhoto();
		}
	},
	'#nextLink' : function(element){
		element.onclick = function(){
			mySlideshow.nextPhoto();
		}
	},
	'#toggleMenu' : function(element){
		element.onclick = function(){
			mySlideshow.toggleMenu();
		}
	},
	'#recBox a' : function(element){
		element.onclick = function(){
			window.open(this.href);
			return false;
		}
	},
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}
};

// Initialize event handlers
Behaviour.register(myrules);
Behaviour.addLoadEvent(init);
Behaviour.apply();
