/*
ROBERT BLENDERMAN PHOTO GALLERY
Author: Shawn Raymond ( http://www.switchclick.com/ )
Requires:
	jQuery ( http://jquery.com/ )
	fancybox ( http://fancybox.net/ )
	waitforimages ( https://github.com/alexanderdickson/waitForImages )
*/

// collection
var rb = {}

// track state
rb.intState=0; // 0=initialized, 1=photos loaded, 2=intro complete and ready to respond

// photo class
rb.Photo = function( strF, strT, strC, strP, strB ){ this.filename=strF; this.title=strT; this.caption=strC; this.price=strP; this.banner=strB; this.width=0; }

// photo array
rb.rayPhotos=[];
rb.intCurrent=-1;

// photo preload
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_brock-street-backyard.jpg', 'Brock Street Backyard', '16 x 20 Oil', 1050, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_clarence-street-view.jpg', 'Clarence Street View', '16 x 20 Oil', 1050, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_clergy-alleyway.jpg', 'Clergy Alleyway', '16 x 20 Oil', 1050, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_do-not-block.jpg', 'Do Not Block Driveway', '16 x 20 Oil', 1050, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_hidden-view.jpg', 'Hidden View', '16 x 20 Oil', 1050, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_ly-s-place.jpg', 'Ly\'s Place', '16 x 20 Oil', 1050, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_shortcut.jpg', 'Shortcut', '16 x 20 Oil', 1050, '' );

rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_abstract-iii.jpg', 'Abstract III', '36 x 40 Oil', 2500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_arabesque.jpg', 'Arabesque', '30 x 36 Oil', 2000, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_blenderman4.jpg', 'Blenderman4', 'Abstract II 40 x 30 Oil', 2500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_chaos.jpg', 'Chaos', '36 x 36 Oil', 2100, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_complicated-thoughts.jpg', 'Complicated Thoughts', '30 x 36 Oil', 2000, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_down-under.jpg', 'Down Under', '34 x 48 Oil', 2500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_granite.jpg', 'Granite', '32 x 40 Oil', 2200, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_helter-skelter.jpg', 'Helter-Skelter', '36 x 30 Oil', 2000, 'new' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_kicking-horse-pass.jpg', 'Kicking Horse Pass', '36 x 36 Oil', 0, 'sold' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_river-bank.jpg', 'River Bank', '24 x 36 Oil', 1600, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_tango.jpg', 'Tango', '40 x 36 Oil', 2700, '' );

rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_gold-and-red-sail.jpg', 'Gold & Red Sail', '24 x 30 Oil', 0, 'sold' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_running-home.jpg', 'Running Home', '24 x 30 Oil', 1500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_sailing-colours-ii.jpg', 'Sailing Colours II', '24 x 30 Oil', 0, 'sold' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_sailin-past-the-mark.jpg', 'Sailing Past the Mark', '24 x 30 Oil', 1500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_sail_reflections.jpg', 'Sail Reflections', '24 x 30 Oil', 0, 'sold' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_summer-sale.jpg', 'Summer Sale', '30 x 40 Oil', 2500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_the-challenge-is-on.jpg', 'The Challenge Is On', '30 x 40 Oil', 2500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_waves-and-sails.jpg', 'Waves & Sails', '24 x 30 Oil', 1500, '' );
rb.intCurrent++; rb.rayPhotos[rb.intCurrent]=new rb.Photo( 'blenderman_young-sailors.jpg', 'Young Sailors', '30 x 24 Oil', 1500, '' );

// reset currently selected photo index
rb.intCurrent=parseInt( rb.rayPhotos.length/2 );
rb.intCurrentLast=rb.intCurrent-1;

// calculate slide.left to ensure centered
rb.rest=function(){
	// determine compounded widths for current photo
	var intDelta=0;
	for( var i=0; i<rb.intCurrent; i++ ) intDelta+=(rb.rayPhotos[i].width);
	intDelta+=(rb.rayPhotos[i].width/2);
	// window half-width subtract compounded width up to current index
	return ( $(document).width()/2 ) - ( intDelta );
}

// calculate direction
rb.isNext=function(){ if( rb.intCurrent>rb.intCurrentLast ) return true; return false; }

// image responder
rb.show=function(){
	// calculate direction
	var intAnimateDirection=( rb.intCurrentLast>rb.intCurrent )?-1:1;
	// clear currently selected indicator
	$('#trayPhoto'+rb.intCurrentLast).removeClass('current');
	// hide display object
	$('#show').stop().animate({ opacity:0, top:'100px' }, 100, function(){
		// add currently selected indicator
		$('#trayPhoto'+rb.intCurrent).addClass( 'current' );
		// set path for display link for fancybox
		$('#show a').attr('href', 'images/gallery800/' + rb.rayPhotos[rb.intCurrent].filename );
		// add details
		var strPrice=(rb.rayPhotos[rb.intCurrent].price)?' - $'+rb.rayPhotos[rb.intCurrent].price : '';
		$('#details').html('<div><b>'+ rb.rayPhotos[rb.intCurrent].title +'</b><i>'+ rb.rayPhotos[rb.intCurrent].caption + strPrice +'</i><span>Click to enlarge</span></div>');
		// toggle details
		$('#details').css('display', 'block');
		// update displayed image and offset position for followup animation
		$('#show a').html('<img src="images/gallery500/' + rb.rayPhotos[rb.intCurrent].filename + '" />');
		if( rb.rayPhotos[rb.intCurrent].banner!='sold' ) $('#sold').hide();
		if( rb.rayPhotos[rb.intCurrent].banner!='new' ) $('#new').hide();
		// wait for resolve
		$('#show').waitForImages(function() {
			$('#show')
				// fix total width
				.css({ 'width': $('#show a img').width()+'px' })
				// move up for vertical down fade in
				.css({ 'top':'-50px' })
				// fade in
				.animate({ opacity:1, top:'0px' }, 300, function(){
					if( rb.rayPhotos[rb.intCurrent].banner=='sold' ) $('#sold').fadeIn();
					if( rb.rayPhotos[rb.intCurrent].banner=='new' ) $('#new').fadeIn();
				 });
		});
	});
	// update slide.left if intro is complete
	if( rb.intState >= 2 ) rb.slideReset();
}

// slide centering
rb.slideReset=function(){
	// animate
	$('#slide').stop().animate({ left:rb.rest()+'px' }, 300 );
}

/*
INTRO ========================================
*/
rb.intro=function(){
	// slide in
	$('#slide').animate({ left:rb.rest() }, 500, function() {
		// trigger current image
		rb.show();
		// show buttons
		$('#next').fadeIn('fast');
		$('#prev').fadeIn('fast', function(){
			// update system state
			rb.intState++;
		});
	});
}

// ================================================================================
// jQuery attachments
$(document).ready(function() {

	/*
	MAKE TRAY ========================================
	*/
	// create tray
	$('body').append( '<div id="tray"><div id="slide"></div></div>' );
	// hide slide offscreen
	$('#slide').css( 'left', $(window).width()+'px' );
	// generate nodes
	var strN='';
	for( var i=0; i < rb.rayPhotos.length; i++ ) strN+='<a href="#" identity="'+i+'" id="trayPhoto' + i + '"><img src="images/gallery50/' + rb.rayPhotos[i].filename + '" /></a>';
	// attach nodes
	$('#slide').html( strN );

	/*
	WHEN THUMBS ADDED TO DOM ARE FULLY LOADED ========================================
	*/
	$('#slide').waitForImages(function() {
		var objPhoto;
		// iterate and assign widths
		for( var i=0; i<rb.rayPhotos.length; i++ ) {
			objPhoto=$('#trayPhoto'+i);
			rb.rayPhotos[i].width=( objPhoto.width() + 8 ); // parseInt(objPhoto.css('margin-right'))
		}
		// update system state
		rb.intState++;
		// invoke intro
		rb.intro();
	});

	/*
	BUTTONS ========================================
	*/
	$('#next a').click(function(){
		rb.intCurrentLast=rb.intCurrent++;
		if( rb.intCurrent == rb.rayPhotos.length ) rb.intCurrent=0;
		rb.show();
		return false;
	});
	$('#prev a').click(function(){
		rb.intCurrentLast=rb.intCurrent--;
		if( rb.intCurrent == -1 ) rb.intCurrent=rb.rayPhotos.length-1;
		rb.show();
		return false;
	});
	$('#slide a').click(function(){
		rb.intCurrentLast=rb.intCurrent;
		rb.intCurrent=$(this).attr('identity');
		rb.show();
		return false;
	});
	$('#details, #sold, #new').click(function(){
		$('#show a').click();
	});

	/*
	FANCYBOX ========================================
	*/
	$.fn.fancybox.defaults.transitionIn='elastic';
	$.fn.fancybox.defaults.transitionOut='elastic';
	$('#show a').fancybox();
	$('#portrait').fancybox();
	$('#book').fancybox();

	/*
	COPYRIGHT ========================================
	*/
	$('.copyright span').html(new Date().getFullYear());

	/*
	EMAIL ========================================
	*/
	var strEmail='infoqrobertblendermanscom'.replace('q','@').replace('s','.');
	document.getElementById('email').href='mailto:'+strEmail;
	document.getElementById('email').innerHTML=strEmail;

});




