var nInterval;
$(document).ready(function() { 
	//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(){
	    window.clearTimeout(nInterval);
	    for (i=0; i<vaStack.length; i++)
	    {
	        if (vaStack[i].id == $(this).attr("id"))
	        {
	            current = i;
	            ShowItem(current);
	            break;
	        }
	    }
		return false;
		
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
	$(".image_thumb ul li").hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
			
	//Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
		return false;
	});
	
	$(".image_thumb ul li").each(function() {
      fillStack(this);
    });
    nInterval = setTimeout(shiftItems,5000)
});

var vaStack = new Array();
var current = 0;
function fillStack(obj)
{
    obj.id = "RotatingItem_"+vaStack.length;
    vaStack[vaStack.length++] = obj;
}

function shiftItems()
{
    if (current >= vaStack.length-1)
        current = -1;
    ++current;
    ShowItem(current);
}

function ShowItem(index)
{
    var imgAlt = $(vaStack[index]).find('img').attr("alt"); //Get Alt Tag of Image
	var imgTitle = $(vaStack[index]).find('a').attr("href"); //Get Main Image URL
	var imgDesc = $(vaStack[index]).find('.block').html(); 	//Get HTML of block
	var imgDescHeight = $(".main_image").find('.block').height();	//Calculate height of block	
	if ($(vaStack[index]).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 }, 350 , function() {
			$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 500 );
		});
		$(".main_image img").animate({ opacity: 0}, 350 , function() {
			$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
		});
		$(".main_image img").animate({ opacity: 1}, 350);
	}
	
	$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
	$(vaStack[index]).addClass('active');  //add class of 'active' on this list only
	
    nInterval = setTimeout(shiftItems,7000);
}
