/**
 * home.js: Synapsesoft home
 */
(function($) {
  $(document).ready(function() {
    // Setup UI
    // ========
    $('#news_list').newsBoard(3000);
    setupCarousel();
  });

  function setupCarousel() {
    var $carousel = $('#carousel nav ul'),
        $prev = $('.prev'),
        $next = $('.next');

    $('#carousel nav img').click(function(ev) {
      var $this = $(this),
          $imgCopy = $this.clone(),
          $imgHolder = $('#holder').children('a'),
          postUrl = $this.attr('data-posturl'),
          frag = document.createDocumentFragment();

      if($this.hasClass('current')) {
        return false;
      }

      $('#carousel nav img').removeClass('current');
      $this.addClass('current');

      $imgHolder.children('img').animate({
        width: 0,
        height: 0,
        opacity: 0.3
      }, 300, function() {  // fadeout old image
        $(this).remove(); // old image
        $imgCopy.css({
          width: 640,
          height: 350,
          opacity: 0.3,
          'z-index': 5
        })
        .animate({
          opacity: 1.0
        }, 700, 'easeOutExpo', function() {
          $imgHolder.attr({ href: postUrl }).append($imgCopy);
        }).appendTo($('#holder'));

      });

      return false;
    });

    $next.click(function(ev) {
      $carousel.animate({
        left: -1035
      }, 1200, 'easeOutQuart');

      $prev.toggleClass('show');
      $next.toggleClass('show');

      return false;
    });

    $prev.click(function(ev) {
      $carousel.animate({
        left: 0
      }, 1200, 'easeOutQuart');

      $prev.toggleClass('show');
      $next.toggleClass('show');

      return false;
    });
  }

})(jQuery);

