
$(document).ready(function() {	 
    
     
    var featuredCarousel = new cInfiniteCarousel(".image_thumb ul li", 6000); 
    featuredCarousel.start();
    
	//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(){ 
		$(this).animatefeatured();
        featuredCarousel.sleep(6000);
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
        featuredCarousel.sleep(3000);
	});
			
	//Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	}); 
     
});//Close Function 
	
$.fn.animatefeatured = function () { 
    //Set Variables
	var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
	var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
	var imgDesc = $(this).find('.block_preview').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 }, 800 , function() {
			$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 800 );
			$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
		});
	}
	
	$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
	$(this).addClass('active');  //add class of 'active' on this list only
	//return false;
};


function cInfiniteCarousel(selector, duration) {
    this.curchild = null;
    this.selector = selector;
    this.duration = duration;
    this.DEBUG = false;
    this.timeout = null;
     
    this.init       = function() { this.curchild = $(this.selector + ":first"); }; 
    this.start      = function() { this.init(); this.run(); };
    this.stop      = function() { clearTimeout(this.timeout); this.curchild = null; };
    this.sleep      = function(duration) { clearTimeout(this.timeout); this.curchild = null; var self = this; this.timeout = setTimeout(function() { self.start(); }, duration); }
    this.run        = function() { if (this.curchild != null) { var self = this; this.timeout = setTimeout(function() { self.animate(); }, this.duration); } };
    
    this.next       = function() { this.curchild = this.curchild.next(); }; 
    
    this.animate    = function() {   
                        if (this.curchild != null) {
                            this.curchild.animatefeatured();
                            
                            if (this.curchild.is(":last-child")) {  
                                this.init();
                                if (this.DEBUG) console.log("RESTART");
                            } else {
                                this.next(); 
                                if (this.DEBUG) console.log("NEXT: " + this.curchild.find('img').attr("alt"));
                            }
                            this.run();
                        }
                    };
}

