// JavaScript Document



var slideshow_interval;

$(document).ready(function(){
	
	   $("#divHomeTabTweetContent").tweet({
            username: "eogpod",
            join_text: "auto",
            avatar_size: 32,
            count: 2,
            auto_join_text_default: ", ",
            auto_join_text_ed: "we",
            auto_join_text_ing: "we were",
            auto_join_text_reply: "we replied to",
            auto_join_text_url: "we were checking out",
            loading_text: "loading tweets...",
            loading_error: "<br/>Cannot load <a href='http://twitter.com/#!/eogpod/' target='_blank'>G-POD Twitter</a>: too many consecutive requests from your IP address.<br/><br/> You can access latest G-POD Tweets from <a href='http://twitter.com/#!/eogpod/' target='_blank'>http://twitter.com/eogpod/</a>"
        });
		
	run_slideshow();
	$('#next').click(gallery_next);
	$('#prev').click(gallery_prev);
});

function run_slideshow(){
	//Set the opacity of all images to 0
	$('#divHomeTabImageRollingSlideshow a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#divHomeTabImageRollingSlideshow a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#divHomeTabImageRollingSlideshow .caption').css({opacity: 0.6});

	//Resize the width of the caption according to the image width
	$('#divHomeTabImageRollingSlideshow .caption').css({width: $('#divHomeTabImageRollingSlideshow a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#divHomeTabImageRollingSlideshow .content').html($('#divHomeTabImageRollingSlideshow a:first').find('img').attr('alt'))
	.animate({opacity: 0.6}, 400);
	
	//Call the gallery function to run the slideshow, 5000 = change to next image after 6 seconds
	slideshow_interval=setTimeout('gallery_next()',5000);

}

function gallery_next() {
	
	clearTimeout(slideshow_interval);
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#divHomeTabImageRollingSlideshow a.show')?  $('#divHomeTabImageRollingSlideshow a.show') : $('#divHomeTabImageRollingSlideshow a: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('caption'))? $('#divHomeTabImageRollingSlideshow a:first') :current.next()) : $('#divHomeTabImageRollingSlideshow a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('alt');	
	
	//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');
	
	//Set the opacity to 0 and height to 1px
	$('#divHomeTabImageRollingSlideshow .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.6 and heigth to 100px, a slide up effect
	$('#divHomeTabImageRollingSlideshow .caption').animate({opacity: 0.6},100 ).animate({height: '50px'},500 );
	
	//Display the content
	$('#divHomeTabImageRollingSlideshow .content').html(caption);
	
	slideshow_interval=setTimeout('gallery_next()',5000);
		
}

function gallery_prev() {
	
	clearTimeout(slideshow_interval);
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#divHomeTabImageRollingSlideshow a.show')?  $('#divHomeTabImageRollingSlideshow a.show') : $('#divHomeTabImageRollingSlideshow a:first'));

	//Get prev image, if it reached the beginning of the slideshow, rotate it back to the last image
	var prev = ((current.prev().length) ? ((current.prev().hasClass('caption'))? $('#divHomeTabImageRollingSlideshow a:first') :current.prev()) : $('#divHomeTabImageRollingSlideshow a:last'));	
	
	//Get next image caption
	var caption = prev.find('img').attr('alt');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	prev.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#divHomeTabImageRollingSlideshow .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.6 and heigth to 100px, a slide up effect
	$('#divHomeTabImageRollingSlideshow .caption').animate({opacity: 0.6},100 ).animate({height: '50px'},500 );
	
	//Display the content
	$('#divHomeTabImageRollingSlideshow .content').html(caption);
	
	slideshow_interval=setTimeout('gallery_next()',5000);
		
}

