/**
 * @file cirrus_site_feature.js
 */
(function($) {
  
  Drupal.behaviors.cirrus_site_feature = {
    attach: function() {
      $('a.popup').each(function () { 
        var j = $(this);
        
        if (!this.href.match('popup=1')) {
          this.href = this.href + '?popup=1';
        }
        if (!j.attr('rel')) {
          j.attr('rel', 'shadowbox;width=750');
        }
      });
    }
  };
  
})(jQuery);


;
/**
 * @file banner.js
 * Javascript to bring the banners to life.  Includes a fade in and a slide in.
 *
 * @author Matthew Oliveira
 */

/**
 * Encapsulates all the functionality of the Cirrus
 * banners in a JS object.  Follows the revealing module pattern.  Use 
 * BANNER.init() to activate the banner.
 */
var BANNER = (function ($) {
  var num_slides,
    curr_timeout,
    container_sel,
    bg_sel,
    caption_sel,
    banner_h,
    delay,
    auto_pos;

  /**
   * Initialize the banner, including computing parameters, initializing the 
   * navigation and setting the initial timeout.
   */
  function init(cs, bgs, caps, h, del, ap) {
    container_sel = cs || '';
    bg_sel = bgs || '';
    caption_sel = caps || '';
    delay = del || 8000;
    auto_pos = ap === undefined ? true : ap;
    banner_h = h || 280;
    num_slides = $(container_sel + ' .views-row').length;

    // Nothing to do if we only have one slide..
    if (num_slides > 1) {
      // Initialize the navigation
      nav_init();
      // Set the initial timeout to change the slide
      curr_timeout = setTimeout(function () {change_slide(1);}, delay);
    }
  }

  /**
   * Setup the banner navigation.
   */
  function nav_init() {
    var pager = $('<ul />').addClass('pager'),
      link,
      i;

    for (i = 0; i < num_slides; i += 1) {
      link = $('<a />').attr('href', 'javascript:void(0);');
      if (i === 0) {
        link.addClass('active');
      }
      pager.append($('<li />').append(link));
    }

    pager.find('a').click(function() {
      var index = $(this).index('.pager a');

      change_slide(null, index);
    });

    // Add in the navigation
    $(container_sel + ' .view-content').append(pager);
  }

  /**
   * Slide in the caption of the given slide.
   * 
   * @param index
   *  Zero based index of the slide to animate.
   */
  function slide_in_caption(index) {
    var caption = $(container_sel + ' .views-row:eq(' + index + ') ' + caption_sel),
      width = caption.width();

    // Show, position and slide in the specified caption
    if (auto_pos) {
      caption.css({
        top: (banner_h - caption.height()) / 2,
        left: -1 * width
      });
    }
    else {
      // This case is used for the homepage, where the padding on the caption
      // is incorporated into the caption image.
      caption.css({
        top: 0,
        left: -1 * (width / 2)
      });
    }
    caption
      .show()
      .animate({ left: auto_pos ? '30px' : '0px' }, {
        duration: 800,
        easing: 'easeOutQuad',
        complete: function () {
          if (curr_timeout) {
            clearTimeout(curr_timeout);
          }
          curr_timeout = setTimeout(function () {change_slide(1);}, delay);
        }
      });
  }

  /**
   * Change the slide based on direction, or jump to the next slide.
   * 
   * @param direction
   *  The direction to move the slides in, either -1 of 1
   * @param index
   *  Slide to jump to.  This parameter is optional.  When used, direction is 
   *  ignored.
   */
  function change_slide(direction, index) {
    var curr_index,
      slides = $(container_sel + ' .views-row'),
      curr_slide,
      next_index,
      trigger,
      move_on = 0.8;

    // Clear any pending timeout
    if (curr_timeout) {
      clearTimeout(curr_timeout);
    }
    // Stop any current animations
    $(bg_sel + ',' + caption_sel).stop(true, true);

    // Figure out current and next slide
    curr_slide = slides.filter('.active');
    curr_index = $(container_sel + ' .views-row').index(curr_slide);
    if (direction === null) {
      next_index = index;
    }
    else {
      next_index = curr_index + direction
    }
    if (next_index < 0) {
      next_index = num_slides;
    } 
    else if (next_index >= num_slides) {
      next_index = 0;
    }
    // Don't bother doing anything if staying on the same slide
    if (curr_index == next_index) {
      return;
    }

    // Prepare the next slide by positioning the caption and showing the
    // background
    slides.eq(next_index).find(bg_sel).show();
    curr_slide.find(caption_sel).hide().end().find(bg_sel).fadeOut({
      duration: 2000,
      easing: 'easeOutQuad',
      step: function(progress) {

        if (!trigger && progress > move_on) {
          trigger = true;
          slide_in_caption(next_index);
        }
      },
      complete: function() {
        slides.removeClass('active').eq(next_index).addClass('active');
        $(container_sel + ' .pager a').removeClass('active').eq(next_index).addClass('active');
      }
    })
  }
  
  /**
   * Stop the timer.  Not called in code, for development only, if I ever
   * need to stop the timer in the browser.
   */
  function stop() {
    clearInterval(curr_timeout);
  }
  
  /**
   * Resume the timeout animation.  Not called in code, for development only.
   */
  function resume() {
    curr_timeout = setTimeout(function () { change_slide(1); }, delay);
  }

  return {init: init, stop: stop, resume: resume};
})(jQuery);
;

