/* $Id: jlightbox.uncompressed.js,v 1.1.2.2 2007/09/19 00:34:00 sun Exp $ */
/**
 * jQuery Lightbox
 * @author
 *   Daniel F. Kudwien (sun), <http://www.unleashedmind.com>
 *   Warren Krewenki, <http://warren.mesozen.com>
 *
 * Based on Lightbox v2.03.3 by Lokesh Dhakar
 * <http://www.huddletogether.com/projects/lightbox2>
 *
 * Originally written using the Prototype framework and Script.aculo.us, now
 * re-written using jQuery. This version tries to stay as much comparable to the
 * original script as possible. There will be another, experimental edition of
 * jQuery Lightbox leveraging the complete jQuery framework.
 */
var SelectedPhotos = {
	fileLoadingImage: '/homes/sites/all/themes/custom/localleaders/images/ajax-loading-offwhite.gif',
	imageArray: new Array,
	activeImage: 0,
	fadeOutSpeed: 'fast',
	fadeInSpeed: 'normal',
	
	initialize: function() {	
		SelectedPhotos.updateImageList();
	},
	
	cancelNavOnClick: function() { return false; },
	
	//
	// updateImageList()
	// Loops through anchor tags looking for 'selected-photos' references and applies onclick
	// events to appropriate links. Can be rerun after dynamically adding images w/ajax.
	//
	updateImageList: function() {	
		// attach onclick function to any links with rel 'selected-photos'
		var anchors = $('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'selected-photos' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('selected-photos'))){
				anchor.onclick = function() { SelectedPhotos.start(this); return false; };
			}
		}
	},
	
	//
	//	start()
	//
	start: function(imageLink) {	

		SelectedPhotos.imageArray = new Array;
		imageNum = 0;		

		imageLink = $(imageLink);
	    largeImg = imageLink.next('a');  // class=photoLgLink
		// add single image to SelectedPhotos.imageArray
		SelectedPhotos.imageArray.push(new Array(imageLink.attr('href'), imageLink.attr('title'), largeImg));			

		SelectedPhotos.changeImage(imageNum);
	},

	//
	//	changeImage()
	//	Hide most elements and preload image in preparation for resizing image container.
	//
	changeImage: function(imageNum) {	
		// update global var
		SelectedPhotos.activeImage = imageNum;

		// hide current image
		$('#photoarea-caption, #clickMessage').fadeOut(SelectedPhotos.fadeOutSpeed);
		$('#photo').fadeOut(SelectedPhotos.fadeOutSpeed, SelectedPhotos.showNextImage );
		
	},
		
		
	showNextImage: function() {
		$('#loadIndicator').show();
		imgPreloader = new Image();
		
		// once image is preloaded, show it with a fadeIn effect
		imgPreloader.onload=function(){
			if(SelectedPhotos.imageArray[SelectedPhotos.activeImage][1]){
				$('#photoarea-caption').html(SelectedPhotos.imageArray[SelectedPhotos.activeImage][1]);
			} else {
				$('#photoarea-caption').html('').hide();
			}
			if(SelectedPhotos.imageArray[SelectedPhotos.activeImage][2].length ){ /* if there's a large image */
			    linkLg = SelectedPhotos.imageArray[SelectedPhotos.activeImage][2][0];
			    $('.link-largeimage').attr('href', $(linkLg).attr('href')).attr('title', 'Click to view larger image');
				Lightbox.updateImageList(); //reattach appropriate onclick functions
				
			    $('#photo').attr('title', 'Click to view larger image');
			    $('#clickMessage').fadeTo( SelectedPhotos.fadeInSpeed, 0.5 );
			    
			} else {
			    $('#photo').attr('title', '');
			    var anchors = $('.link-largeimage');
			    anchors.attr('href', '#');
			    anchors.attr('title', '');
				// loop through the anchor tags
				for (var i=0; i<anchors.length; i++){
					var anchor = anchors[i];
					anchor.onclick = SelectedPhotos.cancelNavOnClick;
				}
			}
			$('#photo').attr('src', SelectedPhotos.imageArray[SelectedPhotos.activeImage][0]);
			$('#photo, #photoarea-caption').fadeIn( SelectedPhotos.fadeInSpeed );
			$('#loadIndicator').hide();
			// clear onLoad, IE behaves irratically with animated gifs otherwise 
			if($.browser.msie) imgPreloader.onload=function(){};
		};
		imgPreloader.src = SelectedPhotos.imageArray[SelectedPhotos.activeImage][0];
	}


};

$(document).ready(function(){
	SelectedPhotos.initialize();
	SelectedPhotos.start( $('.photo-thumb a')[0] );
});
