// $Id$

Drupal.behaviors.views_rotator = function(context) {
  $.each(Drupal.settings.views_rotator, function(id) {
    $('#' + id).cycle(this);
    if (this.next != undefined || this.prev != undefined) {
      var settings = this;
      $(settings.prev).addClass('views-rotator-prev');
      $(settings.next).addClass('views-rotator-next');
      $('.' + id).hover(function() {
        $(settings.prev).addClass('views-rotator-prev-hover');
        $(settings.next).addClass('views-rotator-next-hover');
      }, function() {
        $(settings.prev).removeClass('views-rotator-prev-hover');
        $(settings.next).removeClass('views-rotator-next-hover');
      });
    }
    // Add jQuery Cycle Pager Functionality
    if (this.pager != 'none') {
      switch(this.pager) {
        // Use pager, place it above 
        case 'top':
          $('#' + id)
          .before('<div id="nav-'+id+'" class="views-rotator-nav views-rotator-nav-top"></div>')
          .cycle({ pager: '#nav-'+id });
        break;
        // Use pager, place it below 
        case 'bottom':
          $('#' + id)
          .after('<div id="nav-'+id+'" class="views-rotator-nav views-rotator-nav-bottom"></div>')
          .cycle({ pager: '#nav-'+id });
        break;
      }
    };
    if (this.auto_height == 1) {
      var height = null;
      $('#' + id + ' .views-rotator-item').each(function() {
        var item_height = $(this).height();
        if (item_height > height) height = item_height;
      });
      $('#' + id).height(height);
    }
  });
};