
$(function () {

  $('#tabs').tabs();

  $('#tabs').bind('tabsselect', function(event, ui) {
    $('#tabs a').removeClass('active');
    $(ui.tab).addClass('active');
  });


  $('.question-mark').cluetip( { cluetipClass: 'jtip', arrows: false, local: true,
                                 cluezIndex: 10000, dropShadow: true, hoverIntent: false });

  // show shopping sub-category when hovered over
  $('#n-shop').mouseover( function() {
    $('#main-nav-sub').slideDown();
    ('#n-shop a').attr('class', 'active');
  });

  $('#bd').mouseover( function() {
   $('#n-shop a').attr('class', '');
  });

  $('#n-home').mouseover( function() {
    $('#n-shop a').attr('class', '');
  });

  $('#n-farmers').mouseover( function() {
    $('#n-shop a').attr('class', '');
  });


  SlideShow.setup();
  SlideShow.rotate();



  Async.populateFreshThisWeek();
  Async.populateBlogEntries();

  if(window.location.pathname == '/') {
    $.getScript('/home');
  }
});


var Async = function() {

  return {

    // Populate the fresh this week list from twitter list.
    populateFreshThisWeek: function() {
      $('#fresh-this-week').hide();
      $('#fresh-this-week').load('/home/fresh_this_week', null, function() {
        $('#spinner-fresh-this-week').hide();
        $('#fresh-this-week').slideDown();
      });
    },

    // Populate the blog entries from the greenling blog.
    populateBlogEntries: function() {
      $('#blog-tab').load('/home/blog_entries', null, function() {
        $('spinner-blog-tab').hide();
      });
    },

    // Preload the passed image, appends to a no-show div that should be hidden
    preloadImage: function(imageName) {
      $('<img />')
        .attr('src', imageName)
        .load(function(){
          $('#no-show').append($(this));
      });
    }
  };
}();


var SlideShow = function() {
    var slideInterval;
    var showSlide = function(element) {
        // slideshow rollover
        $('.pic-select a').attr('class', 'bunk');
        $(element).children('a').attr('class', 'active');   // change active highlight

        var id = $(element).attr('id').split('-')[1]; // grab the id name of the list
        $('.slide').hide();
        $('#' + id).show(); // list id corresponds to the slideshow to display
    }
    var preloadImages = function() {
        $('.splash-slideshow .pic-select').each( function() {
          var id = $(this).attr('id').split('-')[1]; // grab the id name of the list
          $('#' + id).show().hide();
        })
        $('#minimum').show();
      }
  return {

    setup: function() {
      preloadImages();
      $('.splash-slideshow .pic-select').mouseover(function() {
          clearInterval(slideInterval);
          showSlide(this);
      });

      $('.splash-slideshow .pic-select').mouseout(function() {
          SlideShow.rotate();
      });
    },

      rotate: function() {
          var index = 1;
          slideInterval = setInterval(function() {
              index %= 5;
              showSlide($('.splash-slideshow .pic-select')[index]);
              index++;
          }, 3000);
      }

 };
}();


