$(function(){
/*
     *   Project SLIDE
     */
    $('ul.project-list').each(function(){
      var $links = $(this).find('li');
      var $contents = $(this).parents('.new_projects:first').find('.new_project-container .new_project');
      var interval = null;
      var goInterval = 5000;
      if ( $links.length != $contents.length )
        return ;

      var goToNext = function() {
        var $next = $links.filter('.selected').next('li');
        if ( $next.length == 0 )
          $next = $links.eq(0);
        selectItem.call($next.get(0));
      }

      var selectItem = function(){
        if ( !$(this).hasClass('selected') ) {
          $links.filter('.selected').removeClass('selected');
          $(this).addClass('selected');
          $contents.filter('.selected').removeClass('selected');
          $contents.eq($(this).attr('_num')).addClass('selected');
        }
      }

      var run = function() {
        interval = setInterval(goToNext, goInterval);
      }

      var stop = function() {
        clearInterval(interval);
      }

      $('.new_project-container').add(this).hover(function(){
        stop();
      }, function(){
        run();
      });

      $links.each(function(num){
        $(this).attr('_num', num);
      });

      $links.click(selectItem);

      run();
    });
});
