// -----------------------------------------------------------------------------------
// 
// This page coded by Scott Upton
// http://www.uptonic.com | http://www.couloir.org
//
// Modified and hacked up by Bernard Zajac
// http://www.zhytek.com.au
//
// 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
//
// -----------------------------------------------------------------------------------
// --- version date: 11/20/05 --------------------------------------------------------


// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1];

// if no photoId supplied then set default
var photoId = (!photoId)? 0 : photoId;

// CSS border size x 2
var borderSize = 0;

// Number of photos in this gallery
var photoNum = photoArray.length;


/*--------------------------------------------------------------------------*/

// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

/*--------------------------------------------------------------------------*/

var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(photoId) {
		this.photoId = photoId;
		this.photo = 'photo';
		this.photoBox = 'photo_container';
		this.captionBox = 'caption_container';
		this.caption = 'caption';
		this.loader = 'photo_loading';
	},
	getCurrentSize: function() {
		var photoBoxElement = $(this.photoBox);
		var captionElement = $(this.captionBox);
		Element.cleanWhitespace(photoBoxElement);
		Element.makePositioned(photoBoxElement);
		var photoBoxDims = Element.getDimensions(photoBoxElement);
		var captionDims = Element.getDimensions(captionElement);
		this.wCur = photoBoxDims.width - borderSize;
		this.hCur = photoBoxDims.height - borderSize;
		this.captionCur = captionDims.width;
	},
	
	getNewSize: function() {
		this.wNew = parseInt(photoArray[photoId][1]);
		this.hNew = parseInt(photoArray[photoId][2]);
		this.captionNew = (this.wNew+borderSize)-10;
	},
	
	getScaleFactor: function() {
		this.getNewSize();
		this.getCurrentSize();
		this.captionScale = (this.captionNew / this.captionCur) * 100;
		this.xScale = (this.wNew / this.wCur) * 100;
		this.yScale = (this.hNew / this.hCur) * 100;
	},
	setNewPhotoParams: function() {
		Element.setSrc(this.photo,photoArray[photoId][0]);
	},
	setPhotoCaption: function() {
		Element.setInnerHTML(this.caption,photoArray[photoId][3]);
	},
	resizePhotoBox: function() {
		this.getScaleFactor();
		
		new Effect.Scale(
			this.photoBox, 
			this.yScale, 
			{
				scaleX: false, 
				scaleY: true, 
				scaleContent: false, 
				duration: 0.3, 
				queue: 'front'
			}
		);
		
		new Effect.Scale(
			this.photoBox, 
			this.xScale, 
			{
				scaleY: false, 
				scaleX: true, 
				scaleContent: false, 
				delay: 0.4, 
				duration: 0.3 
				
			}
		)
		
		new Effect.Scale(this.captionBox, this.captionScale, {scaleY: false, scaleX: true, scaleContent: false, delay: 0.7, duration: 0.2});
		
		
	},
	showPhoto: function(){
		new Effect.Fade(this.loader, {delay: 0.5, duration: 0.3});
		new Effect.Appear(this.photo, {duration: 0.5, queue: 'end'});
	},

	// added by Bernie Zajac for thumbnail listing.
	specificPhoto: function(pID) {
		photoId = pID;
		this.initSwap();
	},

	initSwap: function() {
		// Begin by hiding main elements
		Element.show(this.loader);
		Element.hide(this.photo);

		// Set new dimensions and source, then resize
		this.setNewPhotoParams();
		this.resizePhotoBox();
		if (showDescriptions == true)	this.setPhotoCaption();
		
	}
}

/*--------------------------------------------------------------------------*/

// Establish CSS-driven events via Behaviour script
var myrules = {
	'#photo' : function(element){
		element.onload = function(){
			var myPhoto = new Slideshow(photoId);
			prevPhotoId = photoId
			myPhoto.showPhoto();
		}
	},
	// added by bernie zajac for thumbnail listing.
	'.thumbnail' : function(element) {
		element.onclick = function() {
			var elementId = element.id;
			var newPhotoId = elementId.split("thmb");
			newPhotoId = newPhotoId[1];
			var myPhoto = new Slideshow(newPhotoId);
			myPhoto.specificPhoto(newPhotoId);
		}		
	},
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}

};



// Add window.onload event to initialize
Behaviour.addLoadEvent(init);
Behaviour.apply();
function init() {
	var captureSlideShow = new Slideshow(photoId);
	captureSlideShow.initSwap();
}