// on doc ready, attach functionality for the news fader
$(function() {

  var curr = 1;
  var viewing = 0;
  var items = 0;
     
  // init the selected css for the first(default) trigger
  $("#trigger-1").css({'background-color': '#dedd3b', 'border': '1px solid #282e46'});
  $(".fade1").fadeIn("slow");

  // set up triggers for news fader
  // for each link in the menu, increment item count and
  // set up click and hover functions
  $("#horiz-menu ul li a").each(function(index) {
    items++;

    // set the onclicks
    $(this).click(function() {
      fader(index);
    });

    // if the user hovers over the link, dont rotate
    $(this).hover(
      function() {
        $(this).addClass("hovering");
        viewing = 1;
      },
      function() {
        $(this).removeClass("hovering");
        viewing = 0;
      });
  });

  $(".nav-sub-menu").hover(
    function() {
      $(this).addClass("hovering");
      viewing = 1;
    },
    function() {
      $(this).removeClass("hovering");
      viewing = 0;
    }
  );


  // function to perform the fading
  function fader(index) {
    // update the curr value and form the correct id/class
    // indexing starts at 0, my form starts at 1, so correct it
    curr = (index+1);
    var currid = "#trigger-" + (index+1);
    var currclass = ".fade" + (index+1);

    // fade out any .fadeout items and 
    $(".fadeout").fadeOut("slow", function() { 
      $(".fadeout").hide(); 
      $(".horiz-menu-trigger").css({'background-color': '#b5cfe3', 'border': '1px solid #3773a1'});
      $(currid).css({'background-color': '#dedd3b', 'border': '1px solid #282e46'});
    });

    $(currclass).fadeIn("slow", function() { 
      $(currclass).show(); 
    });
  }


  // this gets called by the timer, and will 
  // rotate the item if the user isnt viewing it
  // also checks to make sure we dont fade an out of bounds item
  function rotate_highlights() {
    if(viewing==0) {
      if(curr >= items) {
        curr = 0;
      }
      fader(curr);
    }
  }
      
  setInterval(rotate_highlights, 5000);
      
});


