$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}
function Timer(callback, delay) {
    var timerId, start, remaining = delay;

    this.pause = function() {
        window.clearTimeout(timerId);
        remaining -= new Date() - start;
    };

    this.resume = function() {
        start = new Date();
        timerId = window.setTimeout(callback, remaining);
    };
	this.restart = function() {
		window.clearTimeout(timerId);
        start = new Date();
        timerId = window.setTimeout(callback, delay);
    };

    this.resume();
}

var numTopFeatures = 0;
var currentTopFeature = 0;
var ssTimer;
$(document).ready(function(e) {
	
	$("#mapFeature a").each(function(index, element) {
		$(this).click(function(e) {
			e.preventDefault();
			$('html, body').animate({ scrollTop: 0 }, {duration:500, complete:function(){ openDirectionsSet(index); }});
		});
	});
    
	numTopFeatures = $("#featureContent").children(".featureThumb").length;
	$("#featureContent").children(".featureThumb").each(function(index, element) {
        $(this).click(function(e) {
            setTopFeature(index);
        });
		$($("#banner .topFeatureImage").get(index)).removeClass("active");
		$($("#banner .topFeatureImage").get(index)).css("display", "block");
		$($("#banner .topFeatureImage").get(index)).fadeOut(0);
		
		$($("#featureContent .featureText").get(index)).removeClass("active");
		$($("#featureContent .featureText").get(index)).css("display", "block");
		$($("#featureContent .featureText").get(index)).fadeOut(0);
    });
	

	var bottomHeight = 0;
	$("#bottomFeatureContainer .element .textElement").each(function(index, element) {
        /*$($(this).children("h2")[0]).click(function(e) {
			
            setBottomFeature(index);
        });*/
        if($($(this).children(".featureText")[0]).height() > bottomHeight){
        	bottomHeight = $($(this).children(".featureText")[0]).height();
        }
    });
    $("#leftNav").height(bottomHeight+360);

	
	setTopFeature(0);
	setBottomFeature(0);
	
	ssTimer = new Timer(function() {nextFeature();}, 4000);

	ssTimer.pause();
	ssTimer.resume();
	
	$("#featureContentWrapInner").mouseenter(function(e) {
        ssTimer.pause();
    });
	
	$("#featureContentWrapInner").mouseleave(function(e) {
        ssTimer.restart();
    });
	
	
});

function setTopFeature(index){
	currentTopFeature = index;
	
	$("#featureContent").children(".featureThumb").each(function(i, element) {
		if(i == index){
			$(this).addClass("active");
			//$($("#banner .topFeatureImage").get(i)).fadeIn(1000);
			//$($("#featureContent .featureText").get(i)).fadeIn(500);
			
			$($("#banner .topFeatureImage").get(i)).stop(true).show().animate({opacity:1}, {duration:1000});
			$($("#featureContent .featureText").get(i)).stop(true).show().animate({opacity:1}, {duration:500});
			
		}else{
			$(this).removeClass("active");
			//$($("#banner .topFeatureImage").get(i)).fadeOut(1000);
			//$($("#featureContent .featureText").get(i)).fadeOut(500);
			
			$($("#banner .topFeatureImage").get(i)).stop(true).animate({opacity:0}, {duration:1000, complete:autoHide});
			$($("#featureContent .featureText").get(i)).stop(true).animate({opacity:0}, {duration:500, complete:autoHide});
			
		}
    });
	
	
}
function autoHide(){
	$(this).hide();	
}
function nextFeature(){
	
	currentTopFeature += 1;
	
	if(currentTopFeature >= numTopFeatures){
		currentTopFeature = 0;
	}
	
	
	setTopFeature(currentTopFeature);
	
	ssTimer.restart();
	
}

function setBottomFeature(index){
	
	$("#bottomFeatureContainer .element .textElement").each(function(i, element) {
        
		if(i == index){
			
			$($(this).children("h2")[0]).addClass("active");
			$($(this).children(".featureText")[0]).addClass("active");
			
		}else{
			
			$($(this).children("h2")[0]).removeClass("active");
			$($(this).children(".featureText")[0]).removeClass("active");
			
		}
		
    });
	
}
