//console.log('Starting ops.js');

// Utility function
//$.fn.alert = function(text) {
  //this.each( function() { alert_text += $(this).html(); } )
//}


/* Etages */
$(function() {
  // Disable scrolling
  //$('body').css('overflow', 'hidden');
  
  /* BEGIN: Setting up navigation */
  //console.log($('#navigation a[href^=#]'));
  var active_section = ($(window.location.hash).size() == 1) ? $(window.location.hash) : $("#realisations");
  
  var sections = [];
  $('nav a[href^=#]').each(function() {
    ////console.log(this);
    sections.push($($(this).attr('href')));
    
    $(this).bind('click', function(event) {
      //console.log('Clicked: ' + event.target);
      //console.log('scrollTo to ');
      //console.log($($(this).attr("href")));
      $('nav a[href^=#]').removeClass('active');
      $(this).addClass('active');
      
      openCloseDoors();
      
      active_section = $($(this).attr("href"));
      $.scrollTo(
        active_section
        , { 
          duration: 1000,
          easing: "easeInOutBack"/* "easeOutBounce" */,
          onAfter: (function(ele) {
            return function() {window.location.hash = $(ele).attr("href");};
            })(this)
          }
      );
      return false;
    });
  }).filter('a[href^=#' + active_section.attr('id') + ']').addClass('active'); // first anchor active by default
  
  //console.log("Sections:");
  //console.log(sections);
  
  // Scroll to bottom by default
  $.scrollTo(active_section, {duration: 0});
  /* END: Setting up navigation */
  var i = 0;
  var previousScrollTop = $(window).scrollTop()
  $(window).bind('scroll', function(event) {
    // on laisse tomber cette idee
    if($(window)._scrollable().is(':animated')) {
      //console.log('Cant scroll, animation going on!!!');
      previousScrollTop = $(window).scrollTop();
      return;
    }
    
    var treshold = 100;
    var max = 0;
    var most_visible
    $(sections).each(function() {
      if (max < $.heightVisibleInViewport(this)) {
        max = $.heightVisibleInViewport(this);
        most_visible = this;
      }
    });
    
    //console.log(most_visible);
    //console.log(active_section);

    if ($(most_visible).attr('id') !== active_section.attr('id')) {
        //console.log('new active section');
        active_section = $(most_visible);
        var active_link = $('nav a[href^=#' + active_section.attr("id") + ']');
        $('nav a[href^=#]').removeClass('active');
        active_link.addClass('active');
        
        var real_id = $(most_visible).attr('id');
        $(most_visible).attr('id', real_id + '-tmp'); // prevent from scrolling when changing hash
        window.location.hash = active_link.attr("href");  // Set new url
        $(most_visible).attr('id', real_id); // remove tmp id, put original id
    }    
    //$('nav a[href^=#]' + $(most_visible).attr("id")).removeClass('active');
    //$(most_visible).addClass('active');

    return;
    
    // Get scrolling direction
    //console.log('scrolling ' + i++);
    var currentScrollTop = $(window).scrollTop();    
    var scrollDirection = (currentScrollTop > previousScrollTop) ? "down" : "up";
    previousScrollTop = currentScrollTop;
    
    //console.log("Scrolling " + scrollDirection);
   
    var sections_in_viewport = $(sections).filter(function() {
        return $.inviewport(this, {threshold: 150});
    });
    
    //console.log("Sections in viewport: ");
    //console.log(sections_in_viewport);
    //console.log(sections_in_viewport.last());
    
    if (sections_in_viewport.size() >= 2) {
      //console.log('Need to scroll!'); 
      if (scrollDirection == "up") {
        var section_to_scroll_to_id = sections_in_viewport.last()[0].attr('id');
      }
      else {
        var section_to_scroll_to_id = sections_in_viewport.first()[0].attr("id");
      }
      
      $('nav a[href^=#' + section_to_scroll_to_id + ']').click();
    }
    
    return;    
  });
});

