// -----------------------------------------------------------------------------------
// 
// This page coded by Ian Foulds, derived from ShowPix 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 frameworks copyright their respective owners
//
// -----------------------------------------------------------------------------------
// --- version date: 05/09/2008 ------------------------------------------------------


/* Basic settings to allow URL-based bookmarking
------------------------------------------------------------*/

// Specify your design's photo CSS border size
var borderSize = 0;

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

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

// Multiply user-supplied CSS border size from layout
var borderSize = borderSize*2;

// Thumbnails shown already?
var hasThumbs = false;


/* ShowPix Class - show image results from  a 'flickr.photos.search' call
------------------------------------------------------------*/
var ShowSinglePix = new Class({
	initialize: function(){
		this.registerEvents();
		this.tweenTimer	= 500;
		this.playTimer	= 5000;
		this.tweenType	= Fx.Transitions.Quint;
		this.loadDelay	= 0;
		this.loadTimer	= this.tweenTimer + this.loadDelay;
		this.photoInfo	= {};
	},
	setNewPhotoParams: function(){
		// Set source of new image
		$('Photo').setProperty('src', this.photoInfo.src);
	},
	setPhotoCaption: function(){
		// Add caption from gallery array
		$('Caption').set('html', '<a href="http://www.flickr.com/photos/'+ this.photoInfo.owner + '/' + this.photoInfo.id + '/" title="View at Flickr...">' + this.photoInfo.title + '</a>');
		
		// Pop caption link in new window
		$$('#Caption a').addEvent("click", function(e) {
			window.open(this.href);
			new Event(e).stop();
		});
	},
	setPhotoDescription: function(){
		// Set the description
		$('Description').set('text', this.photoInfo.description);
	},
	startResize: function(pid){
		// Get current photo dimensions
		this.wCur = $('Container').getStyle('width').toInt();
		this.hCur = $('Container').getStyle('height').toInt();
		
		// Get new photo dimensions from Flickr
		new MooPix().callFlickrUrl({method: 'flickr.photos.getSizes', photo_id: pid});
	},
	endResize: function(rsp){
		// Set new photo dimensions from response
		this.wNew = rsp.sizes.size[3].width;
		this.hNew = rsp.sizes.size[3].height;
		
		if(this.hCur == this.hNew && this.wCur == this.wNew){
			// Set photo source and links on delay (Firefox bug)
			this.setNewPhotoParams.delay(10,this);
		} else {
			// Set photo source and links
			this.setNewPhotoParams();
			// Only resize if needed
			var myPhoto = new Fx.Morph($('Container'), { duration: this.tweenTimer, transition: this.tweenType});
			var myCaption = new Fx.Morph($('CaptionContainer'), { duration: this.tweenTimer, transition: this.tweenType });
			myPhoto.start({ 'height': this.hNew, 'width': this.wNew });
			myCaption.start({'width': this.wNew});
		}
		this.setPhotoCaption();
		this.setPhotoDescription();
	},
	showPhoto: function(){		
		// Show photo block, but hide loading graphic
		$('Photo').setStyle('display','block');
		$('Loading').setStyle('display','none');
		// Fade photo in
		$('Photo').fade(1);
		// Fade in caption
		$('CaptionContainer').setStyle('display', 'block');
		$('CaptionContainer').fade(1);
		// Fade in description
		$('DescriptionContainer').setStyle('display', 'block');
		$('DescriptionContainer').fade(1);
	},
	initSwap: function(){
		// Resize container and set caption
		this.startResize(this.photoInfo.id);
	},
	loadPhoto: function(pid){
		// Show loading graphic
		$('Loading').setStyle('display','block');
		// Hide the photo
		$('Photo').setStyles({opacity: 0, display: 'none'});
		// Hide the caption
		$('CaptionContainer').setStyles({opacity: 0, display: 'none'});
		// Hide the description
		$('DescriptionContainer').setStyles({opacity: 0, display: 'none'});
		// Start by fetching the photo info
		new MooPix().callFlickrUrl({method: 'flickr.photos.getInfo', photo_id: pid});
	},
	endLoadPhoto: function(rsp){		
		// Set new photo info
		this.photoInfo = {
			'id': rsp.photo.id,
			'owner': rsp.photo.owner.nsid,
			'src': 'http://static.flickr.com/'+rsp.photo.server+'/'+rsp.photo.id+'_'+rsp.photo.secret+'.jpg',
			'title': rsp.photo.title._content,
			'description' : rsp.photo.description._content
		};
		this.initSwap();
	},
	registerEvents: function(){
		// Add events for slideshow
		$('Photo').addEvent( 'load', function(){ this.showPhoto.delay(this.loadTimer); }.bind(this));
	}
});