var total_events = $('div.hp_fevent').length;
var current_index = 0;
var next = $('div.hp_fevent').eq(1);
var next_index = 1;
var current = $('div.hp_fevent').eq(current_index);


function eventSwitch(){
	$(current).hide();
	$(current).fadeOut('fast',function(){
		next_index = (current_index + 1 == total_events) ? 0 : current_index + 1;
		next = $('div.hp_fevent').eq(next_index);
		$(next).fadeIn('fast');
		current = $(next);
		current_index = next_index;
	});
}

$(document).ready(function() {	

	$('div.image_thumb ul li a').each(function(){
		$.preloadImages($(this).attr('href'));
	});

	//Show Banner
	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity

	//Click and Hover events for thumbnail list
	$(".image_thumb ul li:first").addClass('active'); 
	$(".image_thumb ul li").click(function(){ 
		
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $(this).find('a').attr("rel"); //Get Main Image URL
		var imgLink = $(this).find('a').attr("href"); // Get URL (linkto)
		var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
		var imgDescHeight = $(".main_image").find('.block').height();	//Calculate height of block	
		
		if ($(this).is(".active")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			//Animate the Teaser				
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
				$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "10px" }, 250 );
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
				$(".main_image a").attr({href: imgLink});
			});
		}
		
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
		$(this).addClass('active');  //add class of 'active' on this list only
		
		// Timers
		clearInterval(timer);
		timer = setInterval( 'slideSwitch()', 10000 );
		return false;
		
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
			
	//Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	});
	
	$(function() {
		timer = setInterval( 'slideSwitch()', 10000 );
	});
	
	/* Featured Events Rotator */
	if($('div.hp_fevent').length>1){
		var autoPlay = setInterval(eventSwitch,5500);
	}
});

jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
}

function slideSwitch() {
	$next = $('.image_thumb ul li.active').next();
	if ( $next.length == 0 ) $next = $('.image_thumb ul li:first');	
	$next.click();
}









