/*
* @name: 	functions.gallery.js
* @desc:	JQuery Gallery Interaction
* @author: 	Matt Kircher
* @modified: 	7.27.10
*/

function initHomeGallery(){
	if($('.home #masthead-gallery')){
		$gallery = $('#masthead-gallery');
		$gallery.find('img').animate({ opacity:1 }, 700);
		
		/* NOTES:
		* speed: how fast to transition
		* delay: how to fast start the first transistion, (timeout - delay)
		* timeout: how long between transitions
		*/
		$gallery.find('ul:eq(0)').cycle({ speed:850, delay:-1750, timeout:6500 });
		$gallery.find('ul:eq(1)').cycle({ speed:850, delay:-5250, timeout:6500 });
		$gallery.find('ul:eq(2)').cycle({ speed:850, delay:-3500, timeout:6500 });
	}
}

function initGallery(){
	
	if($('#gallery')){
		$gallery = $('#gallery');
		
		//add components
		$gallery.find('ul').addClass('thumbnails clearfix');		
		$gallery.find('img').each(function(){
			//add dropshadows
			$(this).wrap('<a href="#" title="Click to view this image..."></a>');
			$(this).parent().parent().prepend('<img class="dropshadow" src="/gsuniverse/sitetemplates/midatlantic/images/dropshadow.png" alt="" height="65" width="85" align="right" />');
		});		
		$gallery.find('.dropshadow').each(function(){
			$(this)
			.css("marginBottom", "-"+$(this).height()+"px")
			.css("marginRight", "-"+$(this).width()+"px")
		});
		
		//append control and display areas
		$gallery.prepend('<div id="gallery-controls"><a class="previous" href="#">Previous</a><a class="next" href="#">Next</a></div>');
		$gallery.prepend('<div id="gallery-image"></div>');
		
		//gallery functions
		$gallery.find('.thumbnails a').each(function(){
			
			$(this).click(function(){
				
				//toggle selected
				$(this)
				.parent().addClass('selected')
				.siblings().removeClass('selected');
				
				$link = $(this);
				$img = $(this).find('img');
				
				$('#gallery-image').stop().animate({ opacity:0 }, 700, function(){
					
					$(this).empty().append($img.clone());
					
					/* NOTES: if titles are desired, 
					*  append to image display area
					*
					* $(this).empty()
					* .append('<p class="title">'+$img.attr('alt')+'</p>')
					* .append($img.clone());
					*/
					
					$(this).animate({ opacity:1 }, 700, function(){
						
						/* NOTES: if titles are desired, show them with a nice font:
						*  Cufon.replace("#gallery-image .title", { fontWeight:400, fontFamily:"Helvetica Neue", hover:true, forceHitArea:true });
						*/
						$(this).find('.title').animate({ opacity:0.6 }, 700);
					});
				});
				
				return false;
			});
		});
		
		//gallery controls
		$('#gallery-controls .next').click(function(){
			
			if($gallery.find('.thumbnails .selected').next().index() == -1){
				$gallery.find('.thumbnails li a:first').click();	
			} else {
				$gallery.find('.thumbnails .selected').next().find('a').click();
			}
			return false;						  
		});
		$('#gallery-controls .previous').click(function(){
			if($gallery.find('.thumbnails .selected').prev().index() == -1){
				$gallery.find('.thumbnails li a:last').click();	
			} else {
				$gallery.find('.thumbnails .selected').prev().find('a').click();
			}
			return false;						  
		});
		
		//show gallery
		$gallery
		.find('.thumbnails a:first').click().end()
		.animate({ opacity:1 }, 700);
	}
}
	
/* INITIALIZATION */
$(document).ready(function(){
			      
	//wait until fully loaded, so as to 
	//support the image object completely
	$(window).load(function(){
		initHomeGallery();	//if masthead-gallery exists...
		initGallery();	//if normal gallery exists...
	});
});

