﻿$(document).ready(function() {
    var $headline_count;
    var $headline_interval;
    var $old_headline = 0;
    var $current_headline = 0;
    
    $headline_count = $("div.image_thumb ul li").size();
    $(".main_image .desc").show(); //Show Banner
    $(".main_image .block").animate({ opacity: 0.85 }, 1); //Set Opacity
    $(".image_thumb ul li:first").addClass('active');
    $(".image_thumb ul li").click(function(event) {
        var $tgt = $(event.target);
        if ($tgt.is('a')) {
            window.location = $(this).find(".desc a").attr("href"); return false;
        }
        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('h2').text(); 	//Get text of h2
        var imgDescHeight = $(".main_image").find('.block').height(); //Calculate height of block	

        if ($(this).is(".active")) {  //already active, then...
            return false; 
        } else {
            $old_headline = $('li', $(this).parent()).index(this);
            clearInterval($headline_interval);
            $(".main_image img").animate({ opacity: 0.1 }, 450);
            $(".main_image .block").animate({ opacity: 0.1, marginBottom: -imgDescHeight }, 350, function() {
                $(".main_image .block").html(imgDesc).animate({ opacity: 0.80, marginBottom: "0" }, 350);
                $(".main_image img").attr({ src: imgTitle, alt: imgAlt }).animate({ opacity: 1 }, 650);
            });
        }

        $(".image_thumb ul li").removeClass('active');
        $(this).addClass('active');  
        $headline_interval = setInterval(doRotate, 4500);
        return false

    }).hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    $headline_interval = setInterval(doRotate, 4500);
    function doRotate() {
        $current_headline = ($old_headline + 1) % $headline_count;
        $(".image_thumb ul li:eq(" + $current_headline + ")").click();
        $old_headline = $current_headline;
    }
});      
