// BUILT ON TOP OF JQUERY /////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
/*************************************************************************************************/

$(document).ready(function() {
						   
	$("ul li:first-child").addClass("first-child");
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// dynamically move some assets...
	
	$('.grabTheImage').each(function() {
		if ($(this).find('img')) {
			$(this).find('img').eq(0).appendTo($(this).parent().find('.grabbedImage a'));
		}
	});
	
	// same approach for the gallery, with the extra <li> wrap around each <img> too
	$(".pro-bono .galleryContent img").appendTo(".pro-bono ul.gallery");
	$(".pro-bono ul.gallery img").wrap("<li></li>");
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// simple galleria gallery
	//jQuery(function($) { $('.gallery .maincol ul').galleria(); }); 
	
	// more advanced galleria gallery
	jQuery(function($) {
		
		//$('.pro-bono .maincol ul').addClass('gallery'); // adds new class name to maintain degradability
		$(".pro-bono .maincol ul.gallery li:first-child").addClass("active") // makes first image active
		
		
		$('ul.gallery').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			//insert    : '#main_image', // the containing selector for our main image
			insert	  : 'undefined',
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				/***********************************************************************************************************/
				// put the gallery thumbnails on top of the main image
				$(".galleria_container").insertAfter("ul.galleria");
				
				// and then this little routine wraps a parent div round the galleria ul
				// and then works out the necessary margin-left in order to centre the ul within its parent
				/*$("ul.galleria").wrap("<div class='galleria_thumbs'></div>");
				$("ul.galleria").css('margin-left', ( (601-$("ul.galleria").width()) /2 ));*/
				/***********************************************************************************************************/
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.6);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.6';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.6); } // don't fade out if the parent is active
				)
			}
		});
	});

	
});
