$(document).ready(function() {		
	
	//Execute the slideShow
	//slideShow();

});

var interval = '';

function slideShow() {

	//Set the opacity of all images to 0
	$('#home-gallery div').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#home-gallery div:first').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	$('#featured-non-profit-container .caption-text').html($('#home-gallery div:first').find('img').attr('rel'))
	.animate({opacity: 1}, 400);
	
	var title = $('#home-gallery div:first').find('img').attr('title');
	
	var splitTitle = title.split('|',2);
	
	$('#learn-more a').attr('href','/participating-non-profits?t='+splitTitle[0]+'&np='+splitTitle[1]);
	
	//Get next image caption
	var urlLink = $('#home-gallery div:first').find('img').attr('link');
	
	if (urlLink == '')
	{
	
		$('#featured-non-profit-border').live('click',function() {
															   
			window.location.href = $('#learn-more a').attr('href');
								  
		});
	
	}

	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	var interval = window.setInterval('gallery()',10000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#home-gallery div.show')?  $('#home-gallery div.show') : $('#home-gallery div:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('featured-non-profit-caption'))? $('#home-gallery div:first') :current.next()) : $('#home-gallery div:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	var title = next.find('img').attr('title');
	
	var splitTitle = title.split('|',2);
	
	$('#learn-more a').attr('href','/participating-non-profits?t='+splitTitle[0]+'&np='+splitTitle[1]);
	
	//Get next image caption
	var urlLink = next.find('img').attr('link');
		
	var target = next.find('img').attr('target');
	
	if (urlLink != '')
	{
		
		if (target != '')
		{
			
			$('#learn-more a').attr('href','#');
			
			$('#featured-non-profit-border').live('click',function() {
						
				popUp(urlLink);
				
				return false;
									  
			});
			
		}
		else
		{
			
			$('#featured-non-profit-border').live('click',function() {
																   
				window.location.href = urlLink;
									  
			});
		
		}
		
		$('#featured-non-profit-border').live('mouseenter',function() {
															   
			$(this).css('cursor','pointer');
								  
		}).live('mouseleave',function() {
															   
			$(this).css('cursor','default');
			
		});
		
	}
	else
	{
		
		$('#featured-non-profit-border').live('click',function() {
															   
			window.location.href = $('#learn-more a').attr('href');
								  
		});
		
		
	}
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');
	
	if (caption != '')
	{
		
		if (target != '')
		{
	
			//Set the opacity to 0 and height to 1px
			$('#featured-non-profit-container .featured-non-profit-caption').hide();
				
		}
		else
		{
	
			//Set the opacity to 0 and height to 1px
			$('#featured-non-profit-container .featured-non-profit-caption').animate({opacity: 0.0}, { queue:false, duration:50 }).animate({height: '1px'}, { queue:true, duration:300 });	
			
			//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
			$('#featured-non-profit-container .featured-non-profit-caption').animate({opacity: 1},100 ).animate({height: '51px'},500 );
			
			//Display the content
			$('#featured-non-profit-container .caption-text').html(caption);
		
		}
	
	}
	else
	{
	
		//Set the opacity to 0 and height to 1px
		$('#featured-non-profit-container .featured-non-profit-caption').hide();
	
	}
	
	
}
