// This file deals with all the functions relating to the homepage image slider gallery

$(document).ready(function() {		
	//Execute the slideShow
	if ($('#gallery')) {
		slideShow();		
	}
});

function slideShow() {
	var ie = false;
	if ($.browser.msie) {
		ie = true;
	}

	//Set the opacity of all images to 0
	if (ie) {
		$('#gallery a img').css({opacity: 0.0});
	}
	$('#gallery a').css({opacity: 0.0});
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
	$('#gallery_link').attr('href', current.attr('href'));
				
	if (current.attr('target')) {
		$('#gallery_link').attr('target', current.attr('target'));
	} else {
		$('#gallery_link').removeAttr('target');
	}
				
	if (current.hasClass('popup')) {
		$('#gallery_link').addClass('popup');
	} else {
		$('#gallery_link').removeClass('popup');
	}
	
	//Get the first image and display it (set it to full opacity)
	if (ie) {
		current.find('img').css({opacity: 1.0});
	}
	current.css({opacity: 1.0});
				
	//Get the caption of the first image from REL attribute and display it
	$('#gallery .cap_content').html(current.find('img').attr('rel')).animate({opacity: 1.0}, 400);
	
	if ($('#gallery a').length > 1) {
		//Call the gallery function to run the slideshow, 5000 = change to next image after 5 seconds
		setInterval('gallery()',5000);
	}
}

function gallery() {
	var ie = false;
	if ($.browser.msie) {
		ie = true;
	}

	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show') ?  $('#gallery a.show') : $('#gallery 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'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	$('#gallery_link').attr('href', next.attr('href'));
	
	if (next.attr('target')) {
		$('#gallery_link').attr('target', next.attr('target'));
	} else {
		$('#gallery_link').removeAttr('target');
	}
				
	if (next.hasClass('popup')) {
		$('#gallery_link').addClass('popup');
	} else {
		$('#gallery_link').removeClass('popup');
	}
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	/*var href = next.attr('href');
	alert(href);*/
	
	//Set the fade in effect for the next image, show class has higher z-index
	if (ie) {
		next.find('img').css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	}
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
				
	//Hide the current image
	if (ie) {
		current.find('img').animate({opacity: 0.0}, 1000).removeClass('show');
	}
	current.animate({opacity: 0.0}, 1000).removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .cap_bg').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	$('#gallery .cap_content').animate({opacity: 0.0},{ queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });
	
	// Animate the caption, opacity to 1.0 and height to 140px, a slide up effect
	$('#gallery .cap_bg').animate({opacity: 0.7},100 ).animate({height: '75px'},500 );
	
	// Caption content must have height of 140px
	$('#gallery .cap_content').animate({opacity: 1.0},100 ).animate({height: '85px'},500 );
	
	//Display the content
	$('#gallery .cap_content').html(caption);
}


