/*********************************************************
 *	File:		  gallery.js
 *	Author:		MetaDesign
 *	Created:	July 24, 2007
 *
 *	Description:
 *	Javascript classes for photo gallery
 ********************************************************/

/*--------------------------------------------------------------
 * Class: Gallery
 *
 * Javascript class for photo gallery module.  Provides
 * client-side photo gallery image switching.
 *------------------------------------------------------------*/
var Gallery = Class.create();
Gallery.prototype = {

  /*--------------------------------------------------------------
   * Method: initialize
   *
   * Gets a handle to the form controls and display controls.
   * Saves the base url and gallery images.
   *
   * Parameters:
   * pImage         object    The image display
   * pCaption       object    The caption display
   * pBreadcrumb    object    The breadcrumb display
   * pPrevBtn       object    The previous button link
   * pNextBtn       object    The next button link
   * pBaseUrl       string    The base url for the gallery
   * pGalleryImages array     Array of objects with properties
   *                          "alt" and "imageUrl"
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  initialize: function(pImage, pCaption, pBreadcrumb, pPrevBtn, pNextBtn, pBaseUrl, pGalleryImages) {
    // Save the controls
    this.image = $(pImage);
    this.caption = $(pCaption);
    this.breadcrumb = $(pBreadcrumb);
    this.prevBtn = $(pPrevBtn);
    this.nextBtn = $(pNextBtn);

    // See if there's an index in our base url
    var urlIndex = pBaseUrl.match(/[0-9]+$/);
    // If no index in the url, start at 0
    if (!urlIndex) {
      this.index = 0;
    } else {
      this.index = parseInt(urlIndex);
    }
    
    // Strip out trailing index and/or slash and save base url
    this.baseUrl = pBaseUrl.replace(/\/$|\/[0-9]+$/, "");
    
    this.galleryImages = pGalleryImages;

    // Display the proper image.
    this.displayImage(this.index);
  },
  
  /*--------------------------------------------------------------
   * Method: displayImage
   *
   * Displays a specific image specified by index number.
   *
   * Parameters:
   * pIndex   int   The index of the image to display
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  displayImage: function(pIndex) {
    // Validate the index.
    if (pIndex >= 0 && pIndex < this.galleryImages.length) {
      // Find the correct image object.
      var newImage = this.galleryImages[pIndex];
      
      // Set the src, alt, and title.
      this.image.src = newImage.imageUrl;
      this.image.alt = newImage.alt;
      this.image.title = newImage.alt;
      
      // Set the caption and breadcrumb.
      this.caption.innerHTML = newImage.alt;
      // Set the image index.
      this.breadcrumb.innerHTML = this.breadcrumb.innerHTML.replace(/[0-9]+/, pIndex + 1);
      // Set the total number of images.
      this.breadcrumb.innerHTML = this.breadcrumb.innerHTML.replace(/[0-9]+([^0-9]*)$/, this.galleryImages.length + "$1");
      
      // Set the previous and next buttons.
      this.setPrevBtn(pIndex);
      this.setNextBtn(pIndex);
      
      // Update the current index.
      this.index = pIndex;
    }
  },
  
  /*--------------------------------------------------------------
   * Method: displayPrev
   *
   * Event handler for previous button.
   * Returns false to avoid default link behavior.
   *
   * Parameters:
   * None
   *
   * Return:
   * false
   *------------------------------------------------------------*/
  displayPrev: function() {
    this.displayImage(this.index - 1);
    return false;
  },
  
  /*--------------------------------------------------------------
   * Method: displayNext
   *
   * Event handler for next button.
   * Returns false to avoid default link behavior.
   *
   * Parameters:
   * None
   *
   * Return:
   * false
   *------------------------------------------------------------*/
  displayNext: function() {
    this.displayImage(this.index + 1);
    return false;
  },
  
  /*--------------------------------------------------------------
   * Method: doNothing
   *
   * Empty event handler for disabled buttons.
   * Returns false to avoid default link behavior.
   *
   * Parameters:
   * None
   *
   * Return:
   * false
   *------------------------------------------------------------*/
  doNothing: function() {
    return false;
  },
  
  /*--------------------------------------------------------------
   * Method: setPrevBtn
   *
   * Sets the display class, href, and onclick behavior for the
   * previous button.
   *
   * Parameters:
   * pIndex   int   The index with which to set the button.
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  setPrevBtn: function(pIndex) {
    if (pIndex > 0) {
      this.prevBtn.className = "galleryPrev";
      this.prevBtn.href = this.baseUrl + "/" + (pIndex - 1);
      this.prevBtn.onclick = this.displayPrev.bind(this);
    } else {
      // Disable the button
      this.prevBtn.className = "galleryPrevDisabled";
      this.prevBtn.href = "";
      this.prevBtn.onclick = this.doNothing;
    }
  },
  
  /*--------------------------------------------------------------
   * Method: setNextBtn
   *
   * Sets the display class, href, and onclick behavior for the
   * next button.
   *
   * Parameters:
   * pIndex   int   The index with which to set the button.
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  setNextBtn: function(pIndex) {
    if (pIndex < this.galleryImages.length - 1) {
      this.nextBtn.className = "galleryNext";
      this.nextBtn.href = this.baseUrl + "/" + (pIndex + 1);
      this.nextBtn.onclick = this.displayNext.bind(this);
    } else {
      // Disable the button
      this.nextBtn.className = "galleryNextDisabled";
      this.nextBtn.href = "";
      this.nextBtn.onclick = this.doNothing;
    }
  }
}

/*--------------------------------------------------------------
 * Class: GallerySwitcher
 *
 * Javascript class for gallery switching.  Provides
 * client-side gallery switching by pulldown.
 *------------------------------------------------------------*/
var GallerySwitcher = Class.create();
GallerySwitcher.prototype = {

  /*--------------------------------------------------------------
   * Method: initialize
   *
   * Gets a handle to the form controls and display controls.
   * Saves the curent gallery, a hash of available galleries
   * and the hotel name.
   *
   * Parameters:
   * pSwitcher    object    The pulldown to switch galleries
   * pDisplay     object    The gallery name display
   * pHotelName   string    The name of the hotel
   * pGallery     Gallery   The current gallery
   * pGalleries   hash      Hash of galleries by unique id
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  initialize: function(pSwitcher, pDisplay, pHotelName, pGallery, pGalleries) {
    // Save the form controls
    this.switcher = $(pSwitcher);
    this.display = $(pDisplay);
    
    // Attach gallery switching behavior to pulldown
    this.switcher.onchange = this.switchGallery.bind(this);
    
    // Save hotel name, the current gallery,
    // and the hash of available galleries
    this.hotelName = pHotelName;
    
    this.currGallery = pGallery;

    this.galleries = pGalleries;
  },

  /*--------------------------------------------------------------
   * Method: switchGallery
   *
   * Event handler for gallery switch pulldown.
   * Changes the images in the gallery and updates the gallery
   * display name.
   *
   * Parameters:
   * None
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  switchGallery: function() {
    this.currGallery.galleryImages = this.galleries[this.getSelectedGallery()];
    // The value of the selected item in the switcher is the base url.
    this.currGallery.baseUrl = this.switcher.options[this.switcher.selectedIndex].value;
    this.currGallery.displayImage(0);
    this.display.innerHTML = this.hotelName + " : " + this.getGalleryTitle();
  },

  /*--------------------------------------------------------------
   * Method: getSelectedGallery
   *
   * Retrieves the unique id of the selected gallery.
   * Determines the id from the value of the gallery switch
   * pulldown.
   *
   * Parameters:
   * None
   *
   * Return:
   * string     The unique id of the gallery.
   *------------------------------------------------------------*/
  getSelectedGallery: function() {
    var switcherValue = this.switcher.options[this.switcher.selectedIndex].value;
    return switcherValue.match(/[^/]+$/);
  },

  /*--------------------------------------------------------------
   * Method: getGalleryTitle
   *
   * Retrieves the title of the gallery from the gallery switch
   * pulldown.
   *
   * Parameters:
   * None
   *
   * Return:
   * string     The title of the gallery.
   *------------------------------------------------------------*/
  getGalleryTitle: function() {
    return this.switcher.options[this.switcher.selectedIndex].text;
  }
}
