var CONTROLLER = 'markmoore';

/**
 * Page-specific JavaScript initialization:
 *   - event handlers for Promo units
 *   - Slideshow
 */
Controller.prototype.pageInit = function()
{
  this.promosInit();

  $.getJSON(SLIDE_DATA, function(data, status)
  {
    if (data.length > 1)
    {
      $('#home_slideshow').slideshow(
      {
        type: 'auto',
        slides: data
      });
    }
  });
}

/**
 * Properties for the Promo unit
 */
Controller.prototype.promos = {
  parentDivSelector:    '.block_03 > .units',
  childDivSelector:     '.unit',
  nextPreviousSelector: '.block_04 > .bg_img_link',
  previousSelector:     '.block_04 > .previous',
  nextSelector:         '.block_04 > .next',
  parentDiv:            null,
  width:                null,
  count:                null,
  currentPromo:         0
};

/**
 * Initialize Promo unit
 */
Controller.prototype.promosInit = function()
{
  // Set properties
  this.promos.parentDiv = $(this.promos.parentDivSelector);
  this.promos.count = this.promos.parentDiv.find(this.promos.childDivSelector).length;
  this.promos.width = this.promos.parentDiv.find(this.promos.childDivSelector).outerWidth();
  this.promos.parentDiv.width(this.promos.width * this.promos.count);

  // Set click event handlers
  if (this.promos.count > 2)
  {
    $(this.promos.nextPreviousSelector).show().click(function() {
      window[CONTROLLER].handlePromoClick(this);
      return false;
    });
  }
};

/**
 * Handle Promo unit click events
 */
Controller.prototype.handlePromoClick = function(anchor)
{
  var $anchor = $(anchor);
  var forward = $anchor.hasClass('next');
  if ((!forward && this.promos.currentPromo > 0) || (forward && this.promos.currentPromo < this.promos.count - 2))
  {
    if (forward)
    {
      this.promos.parentDiv.animate({
        'left': '-=' + this.promos.width + 'px'
      }, 'slow');
      this.promos.currentPromo++;
    }
    else
    {
      this.promos.parentDiv.animate({
        'left': '+=' + this.promos.width + 'px'
      }, 'slow');
      this.promos.currentPromo--;
    }
  }
}

/**
 * Instantiate and initialize a Controller object
 */
$(document).ready(function()
{
    window[CONTROLLER] = new Controller();
    window[CONTROLLER].init();
});

