﻿var current_feature = 0;
var total_features = 0;
var timeout_id;
var can_click = true;
function initialize_home(){
      $(".sliderNode").each(function(index,value){
        if(index == 0){
          $(this).show();
        }

        if(index == 0){
            //this is the first one, set the background color
            $('.body_color').animate({ backgroundColor: $(this).attr("data-background-color") }, 1000);
        }

        var the_id = "rm_slidernode_" + index;
        
        $(this).attr("id", the_id);
        
        createNavItem(the_id, index);
        total_features += 1;
      });

      $("#homePageSliderNav").css("width", slider_width);

      //stops and starts the timer based on user interaction with hero
      $("#homePageSlider").hover(
          function () {
                stop_the_timer();
          }, 
          function () {
                start_the_timer();
          }
        );

      start_the_timer();
}

function start_the_timer(){
    timeout_id = window.setTimeout(change_features, 7000);
}

function stop_the_timer(){
    window.clearTimeout(timeout_id);
}

function change_features(){

    current_feature += 1;
    if(current_feature >= total_features){
        current_feature = 0;
    }

    var new_feature = $("#homePageSliderNav a[data-index='" +current_feature + "']");
    
    current_feature = -99;
    //reset the current feature, will be set appropriately in function
    move_slider(new_feature);
}
    
    var slider_width = 0;
    function createNavItem(the_id, index){
      slider_width += 20;
        var className="";
        if(index == 0) { className="current"; }
        var the_link = $('<a href="javascript:void(0)" class="' + className + '" data-associated-slide="' + the_id + '" data-index="' + index + '">' + index + '</a>')
        $("#homePageSliderNav").append(the_link);
      $(the_link).click(function() { move_slider($(this));} );
    }
    
    function move_slider(selector){
        //prevent someone from going click crazy
        if(!can_click || current_feature == parseInt($(selector).attr("data-index"))) {return;}
        can_click = false;

        stop_the_timer();
        start_the_timer();
        current_feature = parseInt($(selector).attr("data-index"));

        var current_node = $("#homePageSliderNav .current");
        if($(current_node ).attr("data-associated-slide") == $(selector).attr("data-associated-slide")) {  return; }
      
        var current_image = $("#" + $(current_node).attr("data-associated-slide") + " .featured_image");
        var current_callout = $("#" + $(current_node).attr("data-associated-slide") + " .cta");
        var current_panel = $("#" + $(current_node).attr("data-associated-slide"));
      
        var new_node = $("#" + $(selector).attr("data-associated-slide"));

        $("#homePageSliderNav a").removeClass("current");
        $(selector).addClass("current");


        if($(new_node).attr("data-background-color") != '' && $(new_node).attr("data-background-color") != 'undefined')
        {
            $('.body_color').animate({ backgroundColor: $(new_node).attr("data-background-color") }, 1000);
        }


          $(current_image).animate(
                    { marginLeft: "-=1400", opacity: 0 }, {
                        duration: 1000,
                        easing: 'easeOutQuad',
                        complete: function () {
                            $(current_image).hide();
                            $(current_image).animate({
                                opacity: 1.0,
                                marginLeft: '+=1400'
                                }, 1, function() {
                                // Animation complete.
                            });
                        }
                    });

            $(current_callout).animate(
                    { opacity: 0 }, {
                        duration: 500,
                        easing: 'easeOutQuad',
                        complete: function () {
                          $(current_callout).hide();
                          $(current_panel).hide();
          
                          var new_image = $("#" + $(new_node).attr("id") + " .featured_image");
                          var new_cta = $("#" + $(new_node).attr("id") + " .cta");
                          $(new_image).show();
                          $(new_cta).show();
         
                          $(new_node).show('easeIn', function() {
                          // Animation complete.
                              $(current_callout ).animate({
                                opacity: 1.0
                                }, 1, function() {
                                    can_click = true;
                                    //turn interaction back on
                                });
         
                            });
                        }
                    });

    }
