// News Karussel Javscript

(function($) {
  // The DOM is ready
  jQuery(function($) {
    $('#karussel').newsKarussel();
  });
  
  $.fn.newsKarussel = function() {
    var // CONST
        ANIM_LENGTH = 43.75, // em, the same as '#karussel' and '#karussel li.item' width
        ANIM_DURATION = 600, // ms
        ANIM_EASING = 'expoEaseInOut'
    ;
    
    return this.each(function() {
      var $karussel = $(this).removeClass('no-js'),
          $list = $karussel.children('ul'),
          $items = $karussel.find('li.item'),
          $itemLinks = $items.find('a'),
          itemCount = $items.length,
          currentIdx = 0, newIdx = 0,
          $navigation = $.fn.newsKarussel.buildNavigation(itemCount).insertBefore($karussel)
      ;
      
      $navigation.find('a:first').addClass('active1');
      $items.filter(':not(:first)').find('a').attr('tabindex', '-1');
      
      $karussel.bind({
        gotoNextItem: function() {
          var idx = currentIdx == itemCount-1 ? 0 : currentIdx+1;
          $karussel.trigger('gotoItem', idx);
        },
        
        gotoPrevItem: function() {
          var idx = currentIdx == 0 ? itemCount-1 : currentIdx-1;
          $karussel.trigger('gotoItem', idx);
        },
        
        gotoItem: function(event, idx) {
          if ( currentIdx == idx ) return;
          $itemLinks.attr('tabindex', '-1');
          newIdx = idx;
          $items.eq(newIdx).find('a').removeAttr('tabindex');
          $karussel.trigger('animate');
          currentIdx = newIdx;
        },
        
        animate: function() {
          var animLength = newIdx == 0 ? '0' : '-' + (newIdx * ANIM_LENGTH) + 'em';
          $list.animate({ left: animLength }, ANIM_DURATION, ANIM_EASING, function() {} );
        }
      });
      
      // Handle clicks on Navigation
      $navigation.click(function(event) {
        var target = $(event.target);
        if ( target.is('a') ) {
          event.preventDefault();
          $(this).find('a').removeClass('active1 active2 active3 active4 active5 active6');
          var index = target.parent().index();
          target.addClass('active'+(index+1));
          $karussel.trigger('gotoItem', target.parent().index());
        }
      });
      
      // Enlarge Clickarea and show with hover effect
      $items.find('.itemContent')
      .hover(function() { $(this).toggleClass('itemContentHover'); })
      .click(function(event) {
        if ( $(event.target).is('a') ) return true;
        window.location = $(this).find('h2 a').attr('href');
      })
      ;
    });
  };
  
  $.fn.newsKarussel.buildNavigation = function(itemCount) {
    var navigation = $('<ul id="karusselNavigation"></ul>'),
        itemsString = '';
    for ( var i=0,j=itemCount; i<j; i++ ) { itemsString += '<li><a class="n'+(i+1)+'" href="#"><span class="hidden">News '+(i+1)+'</span></a></li>'; }
    $(itemsString).appendTo(navigation);
    return navigation
  }
})(jQuery);
