// -----------------------------------------------------------------------------------
// 
// 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
//
// -----------------------------------------------------------------------------------
// --- version date: 11/28/05 --------------------------------------------------------


// 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
var photoId = (!photoId)? 0 : photoId;

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

// Photo directory for this gallery
var photoDir = "portfolio/slides/";

/*
// Preload Slides
if (document.images)
	{
	pic1= new Image(600,385); pic1.src="portfolio/slides/slide-1.jpg";
	pic2= new Image(600,385); pic2.src="portfolio/slides/slide-2.jpg";
	pic3= new Image(600,385); pic3.src="portfolio/slides/slide-3.jpg";
	pic4= new Image(600,385); pic4.src="portfolio/slides/slide-4.jpg";
	pic5= new Image(600,385); pic5.src="portfolio/slides/slide-5.jpg";
	pic6= new Image(600,385); pic6.src="portfolio/slides/slide-6.jpg";
	pic7= new Image(600,385); pic7.src="portfolio/slides/slide-7.jpg";
	pic8= new Image(600,385); pic8.src="portfolio/slides/slide-8.jpg";
	pic9= new Image(600,385); pic9.src="portfolio/slides/slide-9.jpg";
	pic10= new Image(600,385); pic10.src="portfolio/slides/slide-10.jpg";
	pic11= new Image(600,385); pic11.src="portfolio/slides/slide-11.jpg";
	pic12= new Image(600,385); pic12.src="portfolio/slides/slide-12.jpg";
	pic13= new Image(600,385); pic13.src="portfolio/slides/slide-13.jpg";
	pic14= new Image(600,385); pic14.src="portfolio/slides/slide-14.jpg";
	pic15= new Image(600,385); pic15.src="portfolio/slides/slide-15.jpg";
	pic16= new Image(600,385); pic16.src="portfolio/slides/slide-16.jpg";
	pic17= new Image(600,385); pic17.src="portfolio/slides/slide-17.jpg";
	pic18= new Image(600,385); pic18.src="portfolio/slides/slide-18.jpg";
	pic19= new Image(600,385); pic19.src="portfolio/slides/slide-19.jpg";
	pic20= new Image(600,385); pic20.src="portfolio/slides/slide-20.jpg";
	pic21= new Image(600,385); pic21.src="portfolio/slides/slide-21.jpg";
	pic22= new Image(600,385); pic22.src="portfolio/slides/Gio.jpg";
	pic23= new Image(600,385); pic23.src="portfolio/slides/MC.jpg";
	pic24= new Image(600,385); pic24.src="portfolio/slides/Megan.jpg";
	pic25= new Image(600,385); pic25.src="portfolio/slides/Port.jpg";
	pic26= new Image(600,385); pic26.src="portfolio/slides/SMU.jpg";
	}
*/

// Define each photo's name, height, width, and caption
var photoArray = new Array(
	// Source, Width, Height, Caption
	new Array("slide-1.jpg", "600", "385", "The Boston Celtics"),
	new Array("slide-2.jpg", "600", "385", "Salty Bear Adventure Travel"),
	new Array("slide-3.jpg", "600", "385", "Emera Energy"),
	new Array("slide-4.jpg", "600", "385", "Emera Energy"),
	new Array("slide-5.jpg", "600", "385", "DaimlerChrysler"),
	new Array("slide-6.jpg", "600", "385", "Women's Health in Rural Communities"),
	new Array("slide-7.jpg", "600", "385", "Canadian Recording Industry Association"),
	new Array("slide-8.jpg", "600", "385", "Emera Energy"),
	new Array("slide-9.jpg", "600", "385", "Shakespeare By The Sea"),
	new Array("Gio.jpg", "600", "385", "Gio Restaurant"),
	new Array("slide-10.jpg", "600", "385", "Amadeus Guitar Duo"),
	new Array("slide-11.jpg", "600", "385", "SpellRead"),
	new Array("slide-12.jpg", "600", "385", "Atlantic Lottery Corportation"),
	new Array("slide-13.jpg", "600", "385", "Lord Nelson Hotel"),
	new Array("slide-14.jpg", "600", "385", "Nova Scotia Power"),
	//new Array("slide-15.jpg", "600", "385", "Henson College"),
	new Array("slide-16.jpg", "600", "385", "Gillian King Consulting"),
	new Array("slide-17.jpg", "600", "385", "Dale Kavanagh"),
	new Array("slide-18.jpg", "600", "385", "Creative Wellness Solutions"),
	new Array("slide-19.jpg", "600", "385", "Recovery Resource Board"),
	new Array("slide-20.jpg", "600", "385", "Cyberplex"),
	new Array("SMU.jpg", "600", "385", "Saint Mary's University"),
	new Array("MC.jpg", "600", "385", "McInnes Cooper"),
	new Array("Megan.jpg", "600", "385", "Megan Leslie"),
	new Array("Port.jpg", "600", "385", "Port of Halifax"),
	new Array("slide-21.jpg", "600", "385", "ANCHOR (Capital Health)")
	);

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

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

// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
	getWidth: function(element) {
   	element = $(element);
   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   	element = $(element);
    	element.style.height = h +"px";
	},
	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;
	}
});

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

var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(photoId) {
		this.photoId = photoId;
		this.photo = 'Photo';
		this.photoBox = 'Container';
		this.prevLink = 'PrevLink';
		this.nextLink = 'NextLink';
		this.captionBox = 'CaptionContainer';
		this.caption = 'Caption';
		this.counter = 'Counter';
		this.loader = 'Loading';
	},
	getCurrentSize: function() {
		// Get current height and width, subtracting CSS border size
		this.wCur = Element.getWidth(this.photoBox) - borderSize;
		this.hCur = Element.getHeight(this.photoBox) - borderSize;
	},
	getNewSize: function() {
		// Get current height and width
		this.wNew = photoArray[photoId][1];
		this.hNew = photoArray[photoId][2];
	},
	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;
	},
	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));
	},
	setPhotoCaption: function() {
		// Add caption from gallery array
		Element.setInnerHTML(this.caption,photoArray[photoId][3]);
		Element.setInnerHTML(this.counter,((photoId+1)+'/'+photoNum));
	},
	resizePhotoBox: function() {
		this.getScaleFactor();
		new Effect.Scale(this.photoBox, this.yScale, {scaleX: false, duration: 0.3, queue: 'front'});
		new Effect.Scale(this.photoBox, this.xScale, {scaleY: false, delay: 0.5, duration: 0.3});
		// Dynamically resize caption box as well
		Element.setWidth(this.captionBox,this.wNew-(-borderSize));
	},
	showPhoto: function(){
		new Effect.Fade(this.loader, {delay: 0.5, duration: 0.3});
		// Workaround for problems calling object method "afterFinish"
		new Effect.Appear(this.photo, {duration: 0.5, queue: 'end', afterFinish: function(){Element.show('CaptionContainer');Element.show('PrevLink');Element.show('NextLink');}});
	},
	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.show(this.loader);
		Element.hide(this.photo);
		Element.hide(this.captionBox);
		Element.hide(this.prevLink);
		Element.hide(this.nextLink);
		// Set new dimensions and source, then resize
		this.setNewPhotoParams();
		this.resizePhotoBox();
		this.setPhotoCaption();
	}
}

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

// Establish CSS-driven events via Behaviour script
var myrules = {
	'#Photo' : function(element){
		element.onload = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.showPhoto();
		}
	},
	'#PrevLink' : function(element){
		element.onmouseover = function(){
			soundManager.play('beep');
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.prevPhoto();
			soundManager.play('select');
		}
	},
	'#NextLink' : function(element){
		element.onmouseover = function(){
			soundManager.play('beep');
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.nextPhoto();
			soundManager.play('select');
		}
	},
	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();
	soundManagerInit();
}