var currSectionIndex = 1;
var maxSectionIndex = 6;
var currFadeToIndex = 0;
var maxFadeToIndex = [1,1,1,1,1,1];
var timing = false;

$(function(){

        $(".section-image").css({'display':'none'});
        $(".section-text").css({'display':'none'});
        $('.section-'+currSectionIndex+'-slide-'+currFadeToIndex).css({'display':'block'});
        $('.section-'+currSectionIndex+'-text').css({'display':'block'});
        $('.section-'+currSectionIndex+'-header').addClass('highlight');
        for(var i=1; i<=6; i++) {
            for(var j=1; j<=5; j++) {
                if($('.section-'+i+'-slide-'+j).length > 0) {
                    maxSectionIndex = i;
                    maxFadeToIndex[i] = j;
                }
            }
        }

        $('.heronav_rew').click(function(){
            fadeToPrev(true);
            $(this).blur();
            return false;
        });

        $('.heronav_ff').click(function(){
            fadeToNext(true);
            $(this).blur();
            return false;
        });

        $('.section-header').click(function(e){
            var t = $(this);
            goto_section(t.attr('id').split('-')[1]);
            setTimeout(function(){return function(){t.blur();};}(),10);
            e.stopPropagation();
            return false;
        });

        fadeToNext(false);
});

function fadeToNext(was_clicked) {

    $('.section-'+currSectionIndex+'-slide-'+currFadeToIndex).fadeOut(800);
    if(currFadeToIndex==maxFadeToIndex[currSectionIndex]) {
        currFadeToIndex=1;
        $('.section-'+currSectionIndex+'-text').hide(0);
        $('#section-'+currSectionIndex+'-header').removeClass('highlight');
        if(currSectionIndex==maxSectionIndex) {
            currSectionIndex = 1;
        }
        else {
            currSectionIndex++;
        }
        $('.section-'+currSectionIndex+'-text').show(0);
        $('#section-'+currSectionIndex+'-header').addClass('highlight');
    }
    else {
        currFadeToIndex++;
    }
    $('.section-'+currSectionIndex+'-slide-'+currFadeToIndex).removeClass('hidden').fadeIn(800);
    if(was_clicked) {
        clearTimeout(timing);
        timing = setTimeout("fadeToNext(false);",10000);
    }
    else {
        timing = setTimeout("fadeToNext(false);",4000);
    }
}

function fadeToPrev(was_clicked) {

    $('.section-'+currSectionIndex+'-slide-'+currFadeToIndex).fadeOut(800);
    if(currFadeToIndex==1) {
        $('.section-'+currSectionIndex+'-text').hide(0);
        $('#section-'+currSectionIndex+'-header').removeClass('highlight');
        if(currSectionIndex==1) {
            currSectionIndex = maxSectionIndex;
        }
        else {
            currSectionIndex--;
        }
        $('.section-'+currSectionIndex+'-text').show(0);
        $('#section-'+currSectionIndex+'-header').addClass('highlight');
        currFadeToIndex=maxFadeToIndex[currSectionIndex];
    }
    else {
        currFadeToIndex--;
    }
    $('.section-'+currSectionIndex+'-slide-'+currFadeToIndex).removeClass('hidden').fadeIn(800);
    if(was_clicked) {
        clearTimeout(timing);
        timing = setTimeout("fadeToNext(false);",10000);
    }
    else {
        timing = setTimeout("fadeToPrev(false);",4000);
    }
}

function goto_section(section) {
    $('.section-'+currSectionIndex+'-slide-'+currFadeToIndex).fadeOut(800);
    $('.section-'+currSectionIndex+'-text').hide(0);
    $('#section-'+currSectionIndex+'-header').removeClass('highlight');

    currSectionIndex = parseInt(section);
    currFadeToIndex = 1;

    $('#section-'+currSectionIndex+'-header').addClass('highlight');
    $('.section-'+currSectionIndex+'-text').show(0);
    $('.section-'+currSectionIndex+'-slide-'+currFadeToIndex).fadeIn(800);
    
    clearTimeout(timing);
    timing = setTimeout("fadeToNext(false);",4000);
}
