// -----------------------------------------------------------------------------------
// 
// 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 = "gallery/photos/horses/";

// Define each photo's name, height, width, and caption
var photoArray = new Array(
	// Source, Width, Height, Caption
		//Paddy
		//new Array("paddy/paddy1.jpg", "371", "445", "Paddy - Sold"),
		//Unnamed Filly
		new Array("love2010filly/100401a.jpg", "428", "458", "Bandana"),
		new Array("love2010filly/100401b.jpg", "483", "442", "Bandana"),
		new Array("love2010filly/100401c.jpg", "500", "521", "Bandana"),
		new Array("love2010filly/100401d.jpg", "500", "487", "Bandana"),
		new Array("love2010filly/100401e.jpg", "500", "487", "Bandana"),
		//Checkers
		new Array("previous/090222danc9.jpg", "500", "435", "Checkers"),
		new Array("checkers/090222danc3.jpg", "500", "377", "Checkers"),
		new Array("checkers/090222danc4.jpg", "500", "497", "Checkers"),
		new Array("checkers/090222danc5.jpg", "500", "399", "Checkers"),
		new Array("checkers/090222danc6.jpg", "500", "682", "Checkers"),
		new Array("checkers/090222danc7.jpg", "500", "456", "Checkers"),
		new Array("checkers/090222danc8.jpg", "500", "546", "Checkers"),
		new Array("checkers/090222danc10.jpg", "500", "398", "Checkers"),
		//Lucky
		//new Array("lucky/090907luck2.jpg", "500", "470", "Zorro's Lucky Strait - price drop, now $2499"),
		//new Array("lucky/090907luck.jpg", "500", "506", "Zorro's Lucky Strait - price drop, now $2499"),
		//new Array("lucky/090621luck.jpg", "500", "482", "Zorro's Lucky Strait - price drop, now $2499"),
		//new Array("lucky/090621luc4.jpg", "500", "336", "Zorro's Lucky Strait - price drop, now $2499"),
		//new Array("lucky/090621luc3.jpg", "500", "318", "Zorro's Lucky Strait - price drop, now $2499"),
		//new Array("lucky/090621luc2.jpg", "500", "407", "Zorro's Lucky Strait - price drop, now $2499"),
		//new Array("lucky/081015luc.jpg", "500", "570", "Zorro's Lucky Strait - price drop, now $2499"),
		//new Array("lucky/081015luc2.jpg", "500", "395", "Zorro's Lucky Strait - price drop, now $2499"),
		//Black Ace
		//new Array("snickers/090907sni4.jpg", "500", "300", "Reiners Black Ace - price drop, now $750"),
		//new Array("snickers/090907sni3.jpg", "500", "313", "Reiners Black Ace - price drop, now $750"),
		//new Array("snickers/090907sni2.jpg", "500", "340", "Reiners Black Ace - price drop, now $750"),
		//new Array("snickers/090907sni1.jpg", "500", "379", "Reiners Black Ace - price drop, now $750"),
		//Zorro
		new Array("zorro/100520zor6.jpg", "500", "358", "Shots Flying Zorro"),
		new Array("zorro/100520zor15.jpg", "500", "464", "Shots Flying Zorro"),
		new Array("zorro/100520zor12.jpg", "500", "420", "Shots Flying Zorro"),
		new Array("zorro/100520zor1.jpg", "500", "323", "Shots Flying Zorro"),
		new Array("zorro/100520zor2.jpg", "500", "318", "Shots Flying Zorro"),
		new Array("zorro/100520zor10.jpg", "500", "407", "Shots Flying Zorro"),
		new Array("zorro/100520zor13.jpg", "500", "497", "Shots Flying Zorro"),
		new Array("zorro/100520zor14.jpg", "500", "472", "Shots Flying Zorro"),
		new Array("zorro/100520zor89.jpg", "500", "395", "Shots Flying Zorro"),
		new Array("zorro/sfzaug4.jpg", "500", "387", "Shots Flying Zorro"),
		new Array("zorro/mar7zor4.jpg", "300", "546", "Shots Flying Zorro"),
		//Strait Tuff Lady
		new Array("phantom/phantom1.jpg", "500", "440", "Strait Tuff Lady"),
		//Showcase Dancer
		new Array("dancer/danfil12.jpg", "500", "473", "Showcase Dancer"),
		new Array("dancer/danfil14.jpg", "500", "403", "Showcase Dancer"),
		new Array("dancer/dancer.jpg", "500", "378", "Showcase Dancer"),
		new Array("dancer/dancer3.jpg", "500", "513", "Showcase Dancer"),
		//Love Dat Money
		new Array("love/lcolt1.jpg", "500", "417", "Love Dat Money"),
		new Array("love2010filly/100401d.jpg", "500", "487", "Love Dat Money"),
		//Rockabye Heart
		//new Array("heart/hcolt52.jpg", "500", "435", "Domino Rockabye Heart"),
		//Previous Foals
		new Array("previous/marque4.jpg", "500", "403", "Zorro's Queen of Hearts, 4 weeks old"),
		new Array("previous/dfilly06.jpg", "500", "515", "Zorro's Lightning")
	
	/*new Array("090302bre.jpg", "500", "494", "Zorro's Heartbreaker"),
	new Array("090907sto1.jpg", "500", "469", "Zorro's Stormy Night"),
	new Array("090907sto2.jpg", "500", "401", "Zorro's Stormy Night"),
	new Array("090907sto4.jpg", "500", "445", "Zorro's Stormy Night"),
	new Array("090907sto5.jpg", "500", "478", "Zorro's Stormy Night"),
	new Array("090222danc10.jpg", "500", "408", "Checkers"),
	new Array("090222danc9.jpg", "500", "435", "Checkers"),
	new Array("090907che1.jpg", "500", "430", "Checkers"),
	new Array("090907che6.jpg", "500", "415", "Checkers"),
	new Array("090302che.jpg", "500", "560", "Checkers"),
	new Array("090322colt1.jpg", "500", "474", "Shots Flying Strait"),
	new Array("090322colt2.jpg", "500", "417", "Shots Flying Strait"),
	new Array("090322colt3.jpg", "500", "586", "Shots Flying Strait"),
	new Array("090322colt4.jpg", "500", "518", "Shots Flying Strait"),
	new Array("090322colt5.jpg", "500", "453", "Shots Flying Strait"),
	new Array("090322colt6.jpg", "500", "464", "Shots Flying Strait"),
	new Array("P1160718.jpg", "500", "429", "Shot of San Siemon"),
	new Array("090621que2.jpg", "500", "335", "Zorro's Queen of Hearts"),
	new Array("090621que3.jpg", "500", "359", "Zorro's Queen of Hearts"),
	new Array("090621que4.jpg", "500", "408", "Zorro's Queen of Hearts"),
	new Array("hfilly14.jpg", "500", "564", "Zorro's Queen of Hearts, 2 days old"),
	new Array("hfilly09.jpg", "500", "440", "Zorro's Queen of Hearts, 2 days old"),
	new Array("hfilly07.jpg", "500", "349", "Zorro's Queen of Hearts, 2 days old"),
	new Array("marque6.jpg", "500", "612", "Zorro's Queen of Hearts, 4 weeks old"),
	new Array("marque7.jpg", "500", "438", "Zorro's Queen of Hearts, 4 weeks old"),
	new Array("marque4.jpg", "500", "403", "Zorro's Queen of Hearts, 4 weeks old"),
	new Array("090621lit.jpg", "500", "430", "Zorro's Lightning"),
	new Array("081015lit2.jpg", "500", "310", "Lightning"),
	new Array("081015lit3.jpg", "500", "435", "Lightning"),
	new Array("081015lit4.jpg", "500", "421", "Lightning"),
	new Array("081015lit.jpg", "500", "394", "Lightning"),
	new Array("081015lq.jpg", "500", "421", "Lightning and Queenie"),
	new Array("081015ql2.jpg", "500", "282", "Lightning and Queenie"),
	new Array("081015ql3.jpg", "500", "361", "Lightning and Queenie"),
	new Array("dfilly04.jpg", "500", "338", "Showcase Dancer's filly, Lightning"),
	new Array("dfilmar4.jpg", "500", "421", "Lightning, 4 weeks old"),
	new Array("dfilmar5.jpg", "500", "447", "Lightning, 4 weeks old"),
	new Array("dfilmar8.jpg", "500", "628", "Lightning, 4 weeks old"),
	new Array("80415fil3.jpg", "500", "375", "Lightning"),
	new Array("090907luck2.jpg", "500", "470", "Zorro's Lucky Strait"),
	new Array("090907luck.jpg", "500", "506", "Zorro's Lucky Strait"),
	new Array("090621luck.jpg", "500", "482", "Zorro's Lucky Strait"),
	new Array("090621luc4.jpg", "500", "336", "Zorro's Lucky Strait"),
	new Array("090621luc3.jpg", "500", "318", "Zorro's Lucky Strait"),
	new Array("090621luc2.jpg", "500", "407", "Zorro's Lucky Strait"),
	new Array("081015luc.jpg", "500", "570", "Zorro's Lucky Strait"),
	new Array("081015luc2.jpg", "500", "395", "Zorro's Lucky Strait"),
	new Array("8302pf1.jpg", "500", "535", "Zorro's Lucky Strait"),
	new Array("8302pf3.jpg", "500", "308", "Zorro's Lucky Strait"),
	new Array("marluck1.jpg", "500", "480", "Zorro's Lucky Strait, 3 weeks old"),
	new Array("marluck5.jpg", "500", "593", "Zorro's Lucky Strait, 3 weeks old"),
	new Array("marluck6.jpg", "500", "447", "Zorro's Lucky Strait, 3 weeks old"),
	new Array("marluck7.jpg", "500", "353", "Zorro's Lucky Strait, 3 weeks old"),
	new Array("marluck9.jpg", "500", "769", "Zorro's Lucky Strait, 3 weeks old"),
	new Array("80410luc3.jpg", "500", "649", "Zorro's Lucky Strait"),
	new Array("090907sni4.jpg", "500", "300", "Reiners Black Ace"),
	new Array("090907sni3.jpg", "500", "313", "Reiners Black Ace"),
	new Array("090907sni2.jpg", "500", "340", "Reiners Black Ace"),
	new Array("090907sni1.jpg", "500", "379", "Reiners Black Ace"),
	new Array("8327snc.jpg", "500", "464", "Snickers' colt"),
	new Array("8327snc3.jpg", "500", "408", "Snickers' colt"),
	new Array("8327snc4.jpg", "500", "633", "Snickers' colt"),
	new Array("8415sni.jpg", "500", "529", "Snickers' colt"),
	new Array("dancer.jpg", "500", "378", "Showcase Dancer"),
	new Array("dancer3.jpg", "500", "513", "Showcase Dancer"),
	new Array("lcolt1.jpg", "500", "417", "Love Dat Money"),	
	new Array("hcolt52.jpg", "500", "435", "Domino Rockabye Heart"),
	new Array("phantom1.jpg", "500", "440", "Strait Tuff Lady"),
	new Array("090302ZOR.jpg", "500", "315", "Shots Flying Zorro"),
	new Array("sfzorro1.jpg", "500", "447", "Shots Flying Zorro"),
	new Array("sfzorro4.jpg", "500", "386", "Shots Flying Zorro"),
	new Array("sfzaug4.jpg", "500", "387", "Shots Flying Zorro"),
	new Array("sfzaug9.jpg", "500", "475", "Shots Flying Zorro"),
	new Array("mar7zor1.jpg", "500", "373", "Shots Flying Zorro"),
	new Array("mar7zor2.jpg", "500", "546", "Shots Flying Zorro"),
	new Array("mar7zor3.jpg", "500", "460", "Shots Flying Zorro"),
	new Array("mar7zor4.jpg", "300", "546", "Shots Flying Zorro"),
	new Array("mar7zor5.jpg", "500", "357", "Shots Flying Zorro"),
	new Array("ta3.jpg", "500", "273", "Three black babies"),
	new Array("777horses.jpg", "500", "229", "The Hidden Creek Estate family"),
	new Array("80615que1.jpg", "500", "409", "Zorro's Queen of Hearts"),
	new Array("80615que2.jpg", "500", "369", "Zorro's Queen of Hearts"),
	new Array("081015ql.jpg", "500", "359", "Zorro's Queen of Hearts"),
	new Array("081015que.jpg", "500", "460", "Zorro's Queen of Hearts"),	
	new Array("081015boys.jpg", "500", "292", "The Hidden Creek Estate boys")	*/
	);

// 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();
}
