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

// Define each photo's name, height, width, and caption
var photoArray = new Array(
	// Source, Width, Height, Caption
new Array('_Purple_Jojo.jpg', '750', '555', ''),
new Array('20081205221140_jojo_1.jpg', '740', '555', ''),
new Array('20081026203602_blue_lazer_color_1.jpg', '740', '555', ''),
new Array('20081026202942_blue_lazer2.jpg', '740', '555', ''),
new Array('20081026202359_blue_lazer.jpg', '740', '555', ''),
new Array('20081026202204_cheshma.jpg', '740', '555', ''),
new Array('20081003115532_chimney.jpg', '740', '555', ''),
new Array('20081003115346_hilter_s_bunker.jpg', '740', '555', ''),
new Array('20081003114836_2lea.jpg', '740', '555', ''),
new Array('20081003114055_the_autumn.jpg', '740', '555', ''),
new Array('20080801164421__upload_roofs.jpg', '740', '555', ''),
new Array('20080801164210__upload_clock_2.jpg', '740', '555', ''),
new Array('20080801162401__upload_bricks.jpg', '740', '555', ''),
new Array('20080801160326__upload_view_1.jpg', '740', '555', ''),
new Array('20080801155413__upload_zombie_tools.jpg', '740', '555', ''),
new Array('20080801155037__upload_clock_1.jpg', '416', '555', ''),
new Array('20080717192852_spinka.jpg', '739', '555', ''),
new Array('20080717091755_amstel.jpg', '586', '555', ''),
new Array('20080711220621_trees.jpg', '740', '555', ''),
new Array('20080711195408_zeleno.jpg', '740', '555', ''),
new Array('20080623180734__6223577.jpg', '740', '555', ''),
new Array('20080623180712__6223573.jpg', '740', '555', ''),
new Array('20080619141336_summer_sky.jpg', '740', '555', ''),
new Array('20080619141121_lilavoto_2.jpg', '740', '555', ''),
new Array('20080619140958_lilavoto.jpg', '740', '555', ''),
new Array('20080619140751_yellow.jpg', '740', '555', ''),
new Array('20080619140544_a_tree_lightgreen.jpg', '740', '555', ''),
new Array('20080619140355_kloni_1.jpg', '740', '555', ''),
new Array('20080619140112_lake.jpg', '740', '555', ''),
new Array('20080619135949_the_church_1.jpg', '740', '555', ''),
new Array('20080619135829_pleveli.jpg', '740', '555', ''),
new Array('20080619135715_light-me-up.jpg', '740', '555', ''),
new Array('20080618212111_summer_time.jpg', '740', '555', ''),
new Array('20080618211704_the_spider.jpg', '740', '555', ''),
new Array('20080618211559_no_water.jpg', '740', '555', ''),
new Array('20080618211457_the_moon.jpg', '740', '555', ''),
new Array('20080618211354_my_fikus.jpg', '740', '555', ''),
new Array('20080617180038_june.jpg', '739', '555', ''),
new Array('20080617180000_father_daughter.jpg', '739', '555', ''),
new Array('20080506174935_sushina_small.jpg', '740', '555', ''),
new Array('20080506174819_4_dead_cat_.jpg', '740', '555', ''),
new Array('20080506174636_3_dead_cat_.jpg', '740', '555', ''),
new Array('20080506174433_2_dead_cat_.jpg', '740', '555', ''),
new Array('20080506173826_1_dead_cat_.jpg', '740', '555', ''),
new Array('20080504214630_baba.jpg', '354', '555', ''),
new Array('20080504212913_nedri.jpg', '370', '555', ''),
new Array('20080504203543_laleta_amsterdam_international_airport.jpg', '740', '555', ''),
new Array('20080504203209_flight_8.jpg', '740', '555', ''),
new Array('20080504202927_flight_7.jpg', '740', '555', ''),
new Array('20080504202739_flight_6.jpg', '740', '555', ''),
new Array('20080504202609_flight_5.jpg', '740', '555', ''),
new Array('20080504202402_flight_3.jpg', '740', '555', ''),
new Array('20080504202255_flight_2.jpg', '740', '555', ''),
new Array('20080504202112_flight_11.jpg', '740', '555', ''),
new Array('20080504201928_flight_10.jpg', '740', '555', ''),
new Array('20080504201713_flight_1.jpg', '740', '555', ''),
new Array('20080424200857_helicopercheto.jpg', '740', '555', ''),
new Array('20080422194319_window.jpg', '740', '555', ''),
new Array('20080422192659_yellow_blossom.jpg', '740', '555', ''),
new Array('20080422192628_leaf.jpg', '740', '555', ''),
new Array('20080422192546_lamp.jpg', '740', '555', ''),
new Array('20080422192443_green.jpg', '740', '555', ''),
new Array('20080422192333_rest.jpg', '740', '555', ''),
new Array('20080422192231_green_and_sharp.jpg', '740', '555', ''),
new Array('20080422192153_flower-house.jpg', '740', '555', ''),
new Array('20080422192112_drops.jpg', '740', '555', ''),
new Array('20080422192027_blossom.jpg', '740', '555', ''),
new Array('20080421192937_yellows_small.jpg', '740', '555', ''),
new Array('20080420182701_plakat.jpg', '740', '555', ''),
new Array('20080420181727_lights.jpg', '740', '555', ''),
new Array('20080420181414_narcis.jpg', '740', '555', ''),
new Array('20080420164957_purple.jpg', '740', '555', ''),
new Array('20080420164601_lamp_small.jpg', '456', '555', ''),
new Array('20080420163131_narcisi.jpg', '740', '555', ''),
new Array('20080420162357_cat.jpg', '740', '555', ''),
new Array('20080420162144_yellow.jpg', '740', '555', ''),
new Array('20080420162038_yellow_purple.jpg', '740', '555', ''),
new Array('20080420132257__fas.small.jpg', '740', '555', ''),
new Array('20080420124347_lice_small.jpg', '740', '555', ''),
new Array('20080419201822_helicoptercheto.jpg', '740', '555', ''),
new Array('20080419172133_lista_esen_small.jpg', '741', '555', ''),
new Array('20080419171645_tree_small.jpg', '740', '555', ''),
new Array('20080419171338_happyness_small.jpg', '740', '555', ''),
new Array('20080419171014_hrast_small.jpg', '740', '555', ''),
new Array('20080419170605_me.jpg', '673', '555', ''),
new Array('20080418071105_narcis_too_small.jpg', '740', '555', ''),
new Array('20080418064653_ever_green_small.jpg', '740', '555', ''),
new Array('20080418064353_narcis_orange_small.jpg', '740', '555', ''),
new Array('20080418064136_small_orange_small.jpg', '740', '555', ''),
new Array('20080418062620_small_blue_small.jpg', '740', '555', ''),
new Array('20080418062348_aliens_small.jpg', '740', '555', ''),
new Array('20080418062110_spring_leaf_small.jpg', '740', '555', ''),
new Array('20080417202835__small_razklon.jpg', '740', '555', ''),
new Array('20080417202711__small_sleeping-tulip.jpg', '740', '555', ''),
new Array('20080412225143_shushu_kirizi.jpg', '740', '555', ''),
new Array('20080412223804_moon.jpg', '740', '555', ''),
new Array('20080412222733_spring.jpg', '740', '555', ''),
new Array('20080412222525_winered_flowers.jpg', '740', '555', ''),
new Array('20080412222254_spring.jpg', '740', '555', ''),
new Array('20080412221038_sorrow.jpg', '740', '555', ''),
new Array('20080412220857_red_and_green-yellow.jpg', '740', '555', ''),
new Array('20080412220437_fireworks.jpg', '740', '555', ''),
new Array('20080412220305_purple_and_small.jpg', '740', '555', ''),
new Array('20080412220112_wind_fellow2.jpg', '740', '555', ''),
new Array('20080412215934_the_sunny_clock.jpg', '740', '555', ''),
new Array('20080412215658_wind_fellow1.jpg', '740', '555', ''),
new Array('20080412215245_groznite.jpg', '740', '555', ''),
new Array('20080412214926_dam_square.jpg', '740', '555', ''),
new Array('20080412214704_dam_square_people.jpg', '740', '555', ''),
new Array('20080412214543_mdcc.jpg', '740', '555', ''),
new Array('20080412214400_sun_clock_2.jpg', '740', '555', ''),
new Array('20080412214112_dam_statuq_3.jpg', '740', '555', ''),
new Array('20080412213820_dam_statuq_2.jpg', '740', '555', ''),
new Array('20080412213518_dam_statuq_1.jpg', '740', '555', ''),
new Array('20080412213342_dam_statuq.jpg', '740', '555', ''),
new Array('20080412212546_the_golden_clock.jpg', '740', '555', ''),
new Array('20080412211446_clock_on_the_bricks.jpg', '740', '555', ''),
new Array('20080412211030_sun_clock_1.jpg', '740', '555', ''),
new Array('20080412210534_ornament_1.jpg', '740', '555', ''),
new Array('20080412205956_hm_10.jpg', '740', '555', ''),
new Array('20080412205757_hm_9.jpg', '740', '555', ''),
new Array('20080412205537_hm_8.jpg', '740', '555', ''),
new Array('20080412205202_hm_7.jpg', '740', '555', ''),
new Array('20080412204926_hm_6.jpg', '740', '555', ''),
new Array('20080412204507_hm_5.jpg', '740', '555', ''),
new Array('20080412204116_hm_4.jpg', '740', '555', ''),
new Array('20080412203636_hm_3.jpg', '740', '555', ''),
new Array('20080412202653_hm_2.jpg', '740', '555', ''),
new Array('20080412195639_hm_1.jpg', '740', '555', ''),
new Array('20080412195213_fire_master.jpg', '740', '555', ''),
new Array('20080412194918_lion_king.jpg', '740', '555', ''),
new Array('20080412194346_a_cat_in_a_bar_.jpg', '740', '555', ''),
new Array('20080412194215_horse_police.jpg', '740', '555', ''),
new Array('20080412193558_lonly_cloud.jpg', '740', '555', ''),
new Array('20080412191952_ams_spring.jpg', '740', '555', ''),
new Array('20080412191454_beer_in_the_grass.jpg', '740', '555', ''),
new Array('20080406222440___roofs.jpg', '740', '555', ''),
new Array('20080406222328___lebed_1.jpg', '740', '555', ''),
new Array('20080406195957___theatre_face_beauty.jpg', '740', '555', ''),
new Array('20080406195608___ivy.jpg', '740', '555', ''),
new Array('20080406194901___hooks_1.jpg', '740', '555', ''),
new Array('20080406194737___purple_small_flowers.jpg', '740', '555', ''),
new Array('20080406194634___sad_flower.jpg', '740', '555', ''),
new Array('20080406194543___freska_1.jpg', '740', '555', ''),
new Array('20080406194018___flower_yellow_white.jpg', '740', '555', ''),
new Array('20080406193912___home_in_the_river_1.jpg', '740', '555', ''),
new Array('20080406193732___flag_1.jpg', '740', '555', ''),
new Array('20080406193218___face_on_a_terrace_1.jpg', '740', '555', ''),
new Array('20080406192948___dove_1.jpg', '740', '555', ''),
new Array('20080406192626___crown_amsterl_bridge.jpg', '740', '555', ''),
new Array('20080406192447___chaika_fly_1.jpg', '740', '555', ''),
new Array('20080406192302___chaika_1.jpg', '740', '555', ''),
new Array('20080406191606___boat_1.jpg', '740', '555', ''),
new Array('20080406191513___architecture_rasteniq_museum.jpg', '740', '555', ''),
new Array('20080405225135____4051983.jpg', '740', '555', ''),
new Array('20080405215934____4051954.jpg', '740', '555', ''),
new Array('20080405214727____4051977.jpg', '740', '555', ''),
new Array('20080405213739____4051975.jpg', '740', '555', ''),
new Array('20080405213219____4051971.jpg', '740', '555', ''),
new Array('20080405211830____4051952.jpg', '740', '555', ''),
new Array('20080405211448____4051947.jpg', '740', '555', ''),
new Array('20080405211120____4051940.jpg', '740', '555', ''),
new Array('20080405211020____4051932.jpg', '740', '555', ''),
new Array('20080405204117_football.jpg', '740', '555', ''),
new Array('20080405203117__new_cheshma.jpg', '740', '555', ''),
new Array('20080405202934__new_voda_1.jpg', '740', '555', '')
);

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