// -----------------------------------------------------------------------------------
// 
// 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/
//
// Associated APIs copyright their respective owners
//
// Modified by Taras Mankovski from taras.mankovski.com
// demo of gallery at http://www.lidalicious.com/gallery/index.php
// -----------------------------------------------------------------------------------
// --- version date: 10/30/06 --------------------------------------------------------


// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1] - 1;

// if no photoId supplied then set default
photoId = (!photoId)? 0 : photoId;

// Number of photos in this gallery
var photoNum = photoArray.length;

// Effect queues
var boxq = Effect.Queues.get('boxq');
var photoq = Effect.Queues.get('photoq');

var timeout;

/*--------------------------------------------------------------------------*/

// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
	getWidth: function(element) {
   	element = $ID(element);
   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
   	element = $ID(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   	element = $ID(element);
    	element.style.height = h +"px";
	},
	setSrc: function(element,src) {
    	element = $ID(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $ID(element);
    	element.href = splitURL[0]+href;
	},
	setDirref: function(element,href) {
    	element = $ID(element);
    	element.href = href;
	},
	setInnerHTML: function(element,content) {
		element = $ID(element);
		element.innerHTML = content;
	}
});

/*--------------------------------------------------------------------------*/

var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(photoId) {
		this.photoId = photoId;
		this.photo = 'Photo';
		this.outBox = 'OuterContainer';
		this.photoBox = 'PhotoContainer';
		this.prevLink = 'PrevLink';
		this.viewPhoto = 'ViewPhoto';
		this.nextLink = 'NextLink';
		this.captionBox = 'CaptionContainer';
		this.caption = 'Caption';
		this.counter = 'Counter';
		//this.loader = 'Loading';
	},
	getCurrentSize: function() {
		// Get current height and width
		this.wCur = Element.getWidth(this.photoBox);
		this.hCur = Element.getHeight(this.photoBox);
		this.wOutCur = Element.getWidth(this.outBox);
		this.hOutCur = Element.getHeight(this.outBox);
		this.wCapCur = Element.getWidth(this.outBox);
	},
	getNewSize: function() {
		// Get current height and width
		this.wNew = photoArray[photoId][1];
		this.hNew = photoArray[photoId][2];
		this.wOutNew = (photoArray[photoId][1])-(-4);
		this.hOutNew = (photoArray[photoId][2])-(-4);
		this.wCapNew = (photoArray[photoId][1])-(-4);
	},
	getScaleFactor: function() {
		this.getCurrentSize();
		this.getNewSize();		
		// Scalars based on change from old to new
		this.xScale = (this.wNew / this.wCur) * 100;
		this.yScale = (this.hNew / this.hCur) * 100;
		this.xScale_out = (this.wOutNew / this.wOutCur) * 100;
		this.yScale_out = (this.hOutNew / this.hOutCur) * 100;
		this.xScale_cap = (this.wCapNew / this.wCapCur) * 100;
	},
	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.viewPhoto,"#" + (photoId+1));
		Element.setHref(this.nextLink, "#" + (photoId+1));
	},
	setPhotoCaption: function() {
		// Add caption from gallery array
		Element.setInnerHTML(this.caption,photoArray[photoId][3]);
		Element.setInnerHTML(this.counter,((photoId+1)+'/'+photoNum));
	},
	setLinkSize: function() {
		Element.setWidth(this.prevLink,Math.floor(((this.wNew) * 0.33)));
		Element.setWidth(this.viewPhoto,Math.floor(((this.wNew) * 0.33)));
		Element.setWidth(this.nextLink,Math.floor(((this.wNew) * 0.33)));
		Element.setHeight(this.prevLink, this.hNew);
		Element.setHeight(this.viewPhoto, this.hNew);
		Element.setHeight(this.nextLink, this.hNew);
	},
	resizePhotoBox: function() {
		Element.setWidth(this.photoBox,photoArray[photoId][1]);
		Element.setHeight(this.photoBox,photoArray[photoId][2]);
	},
	resizePhoto: function() {
		Element.setWidth(this.photo,photoArray[photoId][1]);
		Element.setHeight(this.photo,photoArray[photoId][2]);	
	},
	resizeOutBox: function() {
		this.getScaleFactor();
		new Effect.Parallel( 
		[
			new Effect.Scale(this.outBox, this.yScale_out, {scaleX: false, duration: 0.4, queue:{position: 'front', scope: 'boxq'}}),
			new Effect.Scale(this.outBox, this.xScale_out, {scaleY: false, duration: 0.4, queue:{position: 'front', scope: 'boxq'}}),
			new Effect.Scale(this.captionBox, this.xScale_cap, {scaleY: false, duration: 0.4,  queue:{position: 'front', scope: 'boxq'}}),
			new Effect.Scale(this.photoBox, this.yScale, {scaleX: false, duration: 0.4, queue:{position: 'front', scope: 'boxq'}}),
			new Effect.Scale(this.photoBox, this.xScale, {scaleY: false, duration: 0.4, queue:{position: 'front', scope: 'boxq'}})
		], {queue:{position: 'front', scope: 'boxq'}});
	},
	showPhoto: function(){
		// Workaround for problems calling object method "afterFinish"
		//new Effect.Fade(this.loader, {duration:0.2, queue:{position: 'end', scope: 'boxq'}});	
		new Effect.Appear(this.photo, {duration: 0.2, queue:{position:'end', scope:'boxq'}});
		new Effect.Appear(this.counter, {duration: 0.2, queue:{position:'end', scope:'boxq'}});
		new Effect.Appear(this.caption, {duration: 0.2, queue:{position:'end', scope:'boxq'}});
	},	
	nextPhoto: function(){
		// Figure out which photo is next
		(photoId == (photoArray.length - 1)) ? photoId = 0 : photoId++;
		this.initSwap();
	},
	prevPhoto: function(){
		// Figure out which photo is previous
		(photoId == 0) ? photoId = photoArray.length - 1 : photoId--;
		this.initSwap();
	},
	initSwap: function() {
		// Begin by hiding main elements
		Element.hide(this.photo);
		//Element.show(this.loader);
		// Set new dimensions and source, then resize
		this.setNewPhotoParams();	
		this.setPhotoCaption();
		this.resizeOutBox();
		this.resizePhoto();
		this.showLinks();
	},
	showLinks: function(){
		this.setLinkSize();
		Element.show('PrevLink');
		Element.show('ViewPhoto');
		Element.show('NextLink');
	},
	autoplay: function() {
		if (autoplay) {
			timeout = setTimeout("var myPhoto = new Slideshow(photoId);myPhoto.nextPhoto();", autoplay_dur);
		}
	}	
}

/*--------------------------------------------------------------------------*/

// Establish CSS-driven events via Behaviour script
var myrules = {
	'#Photo' : function(element){
		element.onload = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.showPhoto();
			myPhoto.autoplay();					
		}
	},
	'#PrevLink' : function(element){
		element.onmouseover = function(){
		}
		element.onclick = function(){
			clearTimeout(timeout);
			var myPhoto = new Slideshow(photoId);
			myPhoto.prevPhoto();
		}
	},
	'#ViewPhoto' : function(element){
		element.onclick = function(){
			clearTimeout(timeout);
			var Lightbox = Class.create();
			myLightbox.start();
			var myPhoto = new Slideshow(photoId);
			myPhoto.getNewSize();
			myPhoto.showLinks(); 
		}
	},
	'#NextLink' : function(element){
		element.onmouseover = function(){
		}
		element.onclick = function(){
			clearTimeout(timeout);
			var myPhoto = new Slideshow(photoId);
			myPhoto.nextPhoto();
		}
	},
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}
};

// Add window.onload event to initialize
Behaviour.addLoadEvent(init);
Behaviour.apply();
function init() {
	var myPhoto = new Slideshow(photoId);
	myPhoto.initSwap();
}
