// -----------------------------------------------------------------------------------
// 
// 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;

// BN code to store whether to randomize first photo
var doRandomize = (!photoId)? true : false;
// BN code ends

// 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 = "images/slideshow/";

// Define each photo's name, height, width, and caption
var photoArray = new Array(
	// Source, Width, Height, Caption
	new Array("mixed20.jpg","250","184","mixed media"),
	new Array("fabric06.jpg","250","388","fabric"),
	new Array("jewelry11.jpg","250","386","jewelry"),
	new Array("fabric09.jpg","250","348","fabric"),
	new Array("jewelry14.jpg","250","364","jewelry"),
	new Array("mixed25.jpg","250","321","mixed media"),
	new Array("fabric01.jpg","250","179","fabric"),
	new Array("mixed01.jpg","250","234","mixed media"),
	new Array("glass01.jpg","250","326","stained glass"),
	new Array("mixed02.jpg","250","188","mixed media"),
	new Array("jewelry01.jpg","250","356","jewelry"),
	new Array("pottery01.jpg","250","207","pottery"),
	new Array("healthhealing01.jpg","250","250","health and healing"),
	new Array("metal01.jpg","250","358","metalwork"),
	new Array("mixed03.jpg","250","191","mixed media"),
	new Array("photo01.jpg","250","333","photography"),
	new Array("mixed21.jpg","250","238","mixed media"),
	new Array("mixed04.jpg","250","198","mixed media"),
	new Array("pottery02.jpg","250","205","pottery"),
	new Array("jewelry02.jpg","250","279","jewelry"),
	new Array("mixed05.jpg","250","415","mixed media"),
	new Array("fabric02.jpg","250","191","fabric"),
	new Array("glass02.jpg","250","351","stained glass"),
	new Array("photo02.jpg","250","190","photography"), 
	new Array("fabric07.jpg","250","213","fabric"),
	new Array("jewelry03.jpg","250","201","jewelry"),
	new Array("pottery03.jpg","250","190","pottery"), 
	new Array("fabric08.jpg","250","233","fabric"),    
	new Array("jewelry12.jpg","250","154","jewelry"),
	new Array("mixed06.jpg","250","150","mixed media"),
	new Array("soup01.jpg","250","186","soup"),
	new Array("healthhealing02.jpg","250","188","health and healing"),
	new Array("candles01.jpg","250","262","candles"),
	new Array("mixed07.jpg","250","323","mixed media"),
	new Array("pottery04.jpg","250","376","pottery"),
	new Array("baked01.jpg","250","198","baked goods"),
	new Array("mixed22.jpg","250","226","mixed media"),
	new Array("jewelry04.jpg","250","255","jewelry"),
	new Array("mixed08.jpg","250","346","mixed media"),
	new Array("photo03.jpg","250","188","photography"),   
	new Array("mixed26.jpg","250","371","fabric"),
	new Array("mixed09.jpg","250","270","mixed media"),
	new Array("pottery05.jpg","250","254","pottery"),
	new Array("glass03.jpg","250","339","stained glass"), 
	new Array("mixed24.jpg","250","286","mixed media"),
	new Array("fabric03.jpg","250","250","fabric"),
	new Array("jewelry05.jpg","250","364","jewelry"),
	new Array("healthhealing03.jpg","250","197","health and healing"),
	new Array("pottery06.jpg","250","173","pottery"),
	new Array("wood01.jpg","250","216","wood"),
	new Array("mixed10.jpg","250","332","mixed media"),
	new Array("photo04.jpg","250","376","photography"),
	new Array("mixed23.jpg","250","171","mixed media"),
	new Array("mixed11.jpg","250","258","mixed media"),
	new Array("jewelry13.jpg","250","373","jewelry"),
	new Array("pottery07.jpg","250","304","pottery"),
	new Array("jewelry06.jpg","250","252","jewelry"),
	new Array("salsa01.jpg","250","269","salsa"),
	new Array("mixed12.jpg","250","222","mixed media"),
	new Array("pottery08.jpg","250","226","pottery"),
	new Array("glass04.jpg","250","348","stained glass"),
	new Array("mixed13.jpg","250","322","mixed media"),
	new Array("metal02.jpg","250","308","metalwork"),
	new Array("healthhealing04.jpg","250","292","health and healing"),
	new Array("pottery09.jpg","250","183","pottery"),
	new Array("jewelry07.jpg","250","251","jewelry"),
	new Array("photo05.jpg","250","333","photography"),
	new Array("fabric04.jpg","250","188","fabric"),
	new Array("mixed14.jpg","250","285","mixed media"),
	new Array("jewelry08.jpg","250","188","jewelry"),
	new Array("pottery10.jpg","250","188","pottery"),
	new Array("mixed15.jpg","250","214","mixed media"),
	new Array("glass05.jpg","250","294","stained glass"),
	new Array("wood02.jpg","250","190","wood"),
	new Array("mixed16.jpg","250","230","mixed media"),
	new Array("photo06.jpg","250","307","photography"),
	new Array("jewelry09.jpg","250","311","jewelry"),
	new Array("fabric05.jpg","250","390","fabric"),
	new Array("pottery11.jpg","250","187","pottery"),
	new Array("mixed17.jpg","250","170","mixed media"),
	new Array("salsa02.jpg","250","351","salsa"),
	new Array("mixed18.jpg","250","167","mixed media"),
	new Array("baked02.jpg","250","300","baked goods"),
	new Array("jewelry10.jpg","250","214","jewelry"),
	new Array("mixed19.jpg","250","167","mixed media"),
	new Array("photo07.jpg","250","188","photography")
	);

// Number of photos in this gallery
var photoNum = photoArray.length;

// BN code to perform randomization for first slide
if (doRandomize) {
  photoId = Math.floor(Math.random()*photoNum);
}
// BN code ends

/*--------------------------------------------------------------------------*/

// 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();
}