﻿function hero() {
    this.storyTotal = 0;
    this.storySum = 1;
    this.timeoutID = null;
}
var hero = new hero();

$(document).ready(function() {

    hero.storyTotal = $("#hero-container .hero").length;
    if (hero.storySum < hero.storyTotal) {
        hero.storySum += 1;
    }

    $("#hero-container").mouseenter(function() {
        clearTimeout(hero.timeoutID);
    }).mouseleave(function() {
        hero.timeoutID = setTimeout('move(' + hero.storySum + ')', 8000);
    });

    $("#hero-navigation li.nav-item").click(function() {
        var index = $(this).index() + 1;
        hero.storySum = index;
        move(hero.storySum);
    });
    $("#hero-navigation li#nav-prev").click(function() {
        var currStory = hero.storySum - 1;
        if (currStory <= 0) { currStory = hero.storyTotal; }
        hero.storySum = currStory - 1;
        move(currStory);
    });
    $("#hero-navigation li#nav-next").click(function() {
        var currStory = hero.storySum;
        if (currStory == hero.storyTotal + 1) { currStory = 1; }
        move(currStory);
    });

    setStoryMove();

});

function setStoryMove() {
    //alert("current: " + hero.storySum + "/ntotal: " + hero.storyTotal);
    hero.timeoutID = setTimeout('move(' + hero.storySum + ')', 8000);
}

function move(showThis) {
    var currNav = "#heroNav-" + showThis;
    var showThis = "#hero-" + showThis;
    for (i = 1; i <= hero.storyTotal; i++) {
        var hideThis = "#hero-" + i;
        $(hideThis).css("z-index", 0).fadeOut(500);
        $("#hero-navigation li.nav-item").each(function() {
            $(this).removeClass("active");
        });
    }
    $(showThis).css("z-index", 1).fadeIn(500);
    $(currNav).addClass("active");
    clearTimeout(hero.timeoutID);
    
    if (hero.storySum == hero.storyTotal) {
        hero.storySum = 1;
    }
    else {
        hero.storySum++;
    }
    
    setStoryMove();
}

