﻿function setActive(element) {
    //Set Variables
    var imgAlt = $(element).find('img').attr("alt"); //Get Alt Tag of Image
    var imgTitle = $(element).find('a').attr("href"); //Get Main Image URL
    var imgDesc = $(element).find('.block').html(); 	//Get HTML of block
    var imgDescHeight = $(".main_image").find('.block').height(); //Calculate height of block	
    var targetLink = $(element).find('.block h2 a').attr('href'); 	//Get HTML of block

    if ($(element).is(".active")) {  //If it's already active, then...
        window.open(targetLink, '_blank');
        return false; // Don't click through
    } 
	else 
	{
	    //Animate the Teaser
	    //$(".main_image .desc").hide();
	    $(".main_image .desc").show();
        $(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function() 
		{
            $(".main_image .block").html(imgDesc).animate({ opacity: 0.65, marginBottom: "0" }, 250);
        });

        $(".main_image .imgContainer").hide();
        $(".main_image .imgContainer img").attr({ src: imgTitle, alt: imgAlt });
        $(".main_image .imgContainer").fadeIn(1000);
	}

    $(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
    $(element).addClass('active');  //add class of 'active' on this list only

    $('.main_image .imgContainer a#mainLink').attr('href', targetLink);
    return false;
}

$(document).ready(function() {

    //preloadImages();
    //Show Banner
    //$(".main_image .desc").hide(); //Show Banner
    $(".main_image .desc").show(); //Show Banner
    $(".main_image .block").animate({ opacity: 0.85 }, 1); //Set Opacity

    var targetLink = $('.image_thumb ul li .block h2 a').attr('href');
    $('.main_image .imgContainer a#mainLink').attr('href', targetLink);
    //show desc on hover
    /*$(".main_image").hover(function() {
        $(".main_image .desc").show(); //Show Banner
        $(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function() {
            $(".main_image .block").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 250);
        });
    });*/

    //Click and Hover events for thumbnail list
    //$(".image_thumb ul li:first").addClass('active');
    $(".image_thumb ul li").click(function() {

		ImgTimerStop("imgTimer");
		BarTimerStop("barTimer");
		ImgTimerStart(10000, "imgTimer");
		BarTimerStart(10000, "barTimer");
		
        setActive($(this));
        return false;
    }).hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

	ImgTimerStart(10000, "imgTimer");
	BarTimerStart(10000, "barTimer");
	
    //timer
	function ImgTimerStart(delay, label)
	{
		$("div.image_thumb ul").everyTime(delay, label, function() {
			$("div#timeSlider").css("width", 0);
			var next = $(".image_thumb ul li.active").next("li");
			//$(".image_thumb ul li.active").removeClass("active");
			if (next.length == 0) {
				next = $(".image_thumb ul li:first");
			}
			setActive(next);
		});
	}
	function ImgTimerStop(label)
	{
	    $("div.image_thumb ul").stopTime(label);
	}
	function BarTimerStart(delay, label)
	{
		$("div#timeSlider").everyTime(delay / 467, "barTimer", function(i) {
			var w = parseInt($(this).css("width").replace(/px/i, ""));
			w += 2;
			//$(this).html(w + 1);

			$(this).css("width", w);
			if (w >= 934) {
				$(this).css("width", 0);
			}
		});
	}
	function BarTimerStop(label)
	{
		$("div#timeSlider").css("width", 0);
	    $("div#timeSlider").stopTime(label);
	}

});                   //Close Function

