/* ----------------------------------
 *
 * 2011-12-05 12:16 PM
 *
 *
 *
---------------------------------- */

$.fn.timeline = function(options) {
  settings = $.extend({
    sectionHeaders: '.timeline-left header'
  }, options);

  var $window = $(window);
  var $timeSectionHeaders = $(settings.sectionHeaders);
  var triggers = [];
  
  
  $timeSectionHeaders.each(function() {
    triggers.push($(this).offset().top);
    var minHeight = $(this).outerHeight()-40;
    var $thisRight = $(this).parent().siblings('ul');
    if($thisRight.length)
      $thisRight.height(Math.max(minHeight, $thisRight.height()));
    screenFit();
  });
  
  var userAgent = navigator.userAgent;
  if(Modernizr.positionfixed && !userAgent.match(/(iPhone|iPod|iPad|BlackBerry|Android)/)){
    $window.bind('scroll resize' , scrollHandler);
    //kick off
    scrollHandler();
  }
  
  function scrollHandler (){
    var scrollH = $window.scrollTop();
    if (scrollH > triggers[0] && scrollH < triggers[1])
      fixSectionHeader(0);
    else if (scrollH > triggers[1] && scrollH < triggers[2])
      fixSectionHeader(1);
    else if (scrollH > triggers[2] && scrollH < triggers[3])
      fixSectionHeader(2);
    else if (scrollH > triggers[3] && scrollH < triggers[4])
      fixSectionHeader(3);
    else if (scrollH > triggers[4] && scrollH < triggers[5])
      fixSectionHeader(4);
    else if (scrollH > triggers[5] && scrollH < triggers[6])
      fixSectionHeader(5);
    else if (scrollH > triggers[6] && scrollH < triggers[7])
      fixSectionHeader(6);
    else if (scrollH > triggers[7])
      fixSectionHeader(7);
    else
      fixSectionHeader(-1);
  }
  
  function fixSectionHeader(index){
    $timeSectionHeaders.removeClass('fixed');
    
    if (index != -1) {
      $timeSectionHeaders.eq(index).addClass('fixed');
      //set hash
      /*var newHash = $timeSectionHeaders.eq(index).closest('section').attr('id');
      if (typeof newHash != 'undefined'){
        window.location.hash = newHash;
        return false;
      }*/
    }
  }
   
};
