/* Author: 

*/

$(document).ready(function() {

 /*
  * Splashscreen
  */

  // /*/*console.log("$(window).height() :", $(window).height())*/*/;

  $('#splashscreen').delay('2000').fadeOut('slow', function () {
    if ($(window).height() < 1100) {
      $('body,html').delay(200).animate({scrollTop: '121px'}, 500, 'easeOutQuad');
    }
  });


 /*
  * Hide the header
  */

  if ($(window).height() < 1100 && $('#splashscreen').length === 0) {
    $('body,html').delay(200).animate({scrollTop: '121px'}, 500, 'easeOutQuad');;
  }

  /*
   * Call jquery.innerfade.js
   */
  
  $('.slideshow').innerfade({
    animationtype: 'fade',
    speed: 'slow',
    timeout: 5000,
    type: 'sequence'
  });


  /*
   * add animated navigation to the products menu
   */

  var
    $projectsList = $(".references #projects-list"),
    $sections     = $projectsList.find("h2"),
    $nav          = $projectsList.find("nav")
  ;
  
  $nav.each(function(index) {
    var
      $list         = $(this).find("ul"),
      items         = $list.find("li").length, // total number of show
      show          = Math.floor(18 - $nav.length*2 + 2), // number of items displayed
      position      = 0, // initial position
      itemHeight    = $list.find("li").height(), // list item height
      listHeight    = show*itemHeight + 'px' // list container height
    ;
    
    // if there is more products to show...
    if (items > show) {
      $list.css('height', listHeight);
      productScroll($list, position); // reset the products list position

      // add navigation buttons and hide them by default
      $(this)
        .prepend('<div class="previous-projects"><a href="#">Projets précédents</a></div>')
        .append('<div class="next-projects"><a href="#">Projets suivants</a></div>')
      ;

      var
        $previous = $nav.find(".previous-projects a").hide(),
        $next = $nav.find(".next-projects a").hide()
      ;

      // add behavior to the next button
      $next.show().bind("click", function() {
        position = position + show;
        if (position > items - show) {
          position = items - show;
          $(this).hide("fast");
        };
        if (position > 0) {
          $previous.show("fast");
        };
        productScroll($list, position);
      });

      // add behavior to the previous button
      $previous.hide().bind("click", function() {
        position = position - show;
        if (position <= 0) {
          position = 0;
          $(this).hide("fast");
        };
        if (position < items) {
          $next.show("fast");
        };
        productScroll($list, position);
      });
    }

  });

  // Create sub-sections toggle
  if ($sections.length > 1) {
    $sections
      .addClass('active')
      .css('cursor', 'pointer')
      .wrapInner('<span></span>')
      .click(function() {
        $sections
          .removeClass('current')
          .next().slideUp()
        ;
        $(this)
          .addClass('current')
          .next().slideDown()
        ;
      })
      .filter(":not(:first)").next().hide()
    ;
  }


  // Animated list in the medias
  $('#medias').each(function(index) {
    var
      $nav = $(this),
      $list = $nav.find("ul"),
      items = $list.find("li").length, // total number of show
      show = 7, // number of items displayed
      position = 0 // initial position
    ;

    $nav
      .prepend('<div class="previous"></div>')
      .append('<div class="next"></div>')
    ;

    if (items > show) {
      // reset the products list position
      productScroll($list, position);

      // add navigation buttons and hide them by default
      $nav.find('.previous').append('<a href="#">Projets précédents</a>');
      $nav.find('.next').append('<a href="#">Projets suivants</a>');

      var
        $previous = $nav.find(".previous a").hide(),
        $next = $nav.find(".next a").hide()
      ;

      // add behavior to the next button
      $next.show().bind("click", function(event) {
        event.preventDefault();
        // /*/*console.log("current position :", position)*/*/;
        position = position + show;
        // /*/*console.log("new position :", position)*/*/;
        if (position > items - show) {
          position = items - show;
          $(this).fadeOut("fast");
        };
        if (position > 0) {
          $previous.fadeIn("fast");
        };
        productScroll($list, position);
      });

      // add behavior to the previous button
      $previous.hide().bind("click", function(event) {
        event.preventDefault();
        // /*/*console.log("current position :", position)*/*/;
        position = position - show;
        // /*/*console.log("new position :", position)*/*/;
        if (position <= 0) {
          position = 0;
          $(this).fadeOut("fast");
        };
        if (position < items) {
          $next.fadeIn("fast");
        };
        productScroll($list, position);
      });
    }
  });


  // Fancybox
  $(".newsletter #medias li a").fancybox({
    'padding'             : 0,
    'margin'              : '15px',
    'autoScale'           : false,
    'overlayShow'         : false,
    'hideOnContentClick'  : true
  });
  
  $(".medias #medias li a.popup").fancybox({
    'padding'             : 0,
    'margin'              : '15px',
    'autoScale'           : true,
    'overlayShow'         : false,
    'hideOnContentClick'  : false
  });
  
  $("footer a.popup").fancybox({
    'width'               : '300px',
    'overlayShow'         : false,
    'hideOnContentClick'  : true
  });

});

// scroll the products list to a specific position
function productScroll (list, position) {
  var
    divOffset = list.offset().top,
    pOffset = list.find("li:eq("+ position +")").offset().top,
    pScroll = pOffset - divOffset
  ;
  list.animate({scrollTop: '+=' + pScroll + 'px'}, 1000, 'easeOutQuad');
}

// scroll the page 121px down to hide the header
function hideHeader () {
  $('body,html').delay('500').animate({scrollTop: '121px'}, 500, 'easeOutQuad');
}
