/**
 *
 * SMBUProductGalleryLightbox
 *
 *   Initializes a SMBU specific product gallery lightbox which
 *   may be opened using any element managed by SMBUProductGallery.Manager
 *
 */

var SMBUProductGallery = {};

SMBUProductGallery.Lightbox = Class.create( Control.Modal, {
  url:      '',
  aElement: '',
  options:  {},
  baseURL:  '/gallery_content.php?product=',
  initialize: function( $super, product, options ) {
    this.url = this.baseURL + product;
    this.aElement = new Element( 'a', {
      "href": this.url,
      "className": 'hidden_link'
    });
    this.aElement.writeAttribute( 'href', this.baseURL + product );
    Element.insert( document.body, this.aElement );
    var options = {
      "overlayOpacity": 0.75,
      "className": 'modal',
      "fade": true,
      "defaultTab": 'first'
    };
    $super( this.aElement, options );
  }
});

/**
 *
 * SMBUProductGallery.Manager
 *
 *   Handle clicks on registered form elements and launch lightbox
 *
 **/
SMBUProductGallery.Manager = Class.create({
  lightbox: {},
  initialize: function( lightbox ) {
    this.lightbox = lightbox;
  },
  addElement: function( element ) {
    Element.observe( element, 'click', this.launchOnElementClick.bindAsEventListener( this ) );
  },
  launchOnElementClick: function( event ) {
    this.lightbox.open();
  }
});

/**
 *
 * Legacy gallery functions
 *
 *   These are provided for backwards compatability.
 *   to make a lightbox link: 
 * 
 *   <a href="javascript:open[productname]Gallery()">link text</a>
 *  
 *   example, to create a Boom image gallery link:
 *   
 *   <a href="javascript:openBoomGallery()">photo gallery</a>
 *
 */
function openGallery( aElement ) {

  //new SMBUProductGalleryLightbox( aElement, 'squeezebox_classic' );
}
function openTransporterGallery() {
  //new SMBUProductGalleryLightbox( aElement, 'squeezebox_transporter' );
}
function openDuetGallery() {
  //new SMBUProductGalleryLightbox( aElement, 'squeezebox_duet' );
}
function openReceiverGallery() {
  //new SMBUProductGalleryLightbox( aElement, 'squeezebox_receiver' );
}
function openControllerGallery() {
  //new SMBUProductGalleryLightbox( aElement, 'squeezebox_controller' );
}
function openBoomGallery() {


}

/**
 *
 * Initializes necessary SMBUProductGallery.Manager and SMBUProductGallery.Lightbox instances
 * based on links in the form
 *
 */
document.observe('dom:loaded', function(){
  var ph = new Hash();
  ph.set( "transporter", "Transporter" );
  ph.set( "squeezebox_boom", "Boom" );
  ph.set( "squeezebox_duet", "Duet" );
  ph.set( "squeezebox_controller", "Controller" );
  ph.set( "squeezebox_receiver", "Receiver" );
  ph.set( "squeezebox_classic", "" );
  // create a lightbox for every product needed
  var lbh = new Hash();
  // create a manager for every product needed
  var lbmh = new Hash();
  $$( 'a' ).each( function( aElement ) {
    var onclick = aElement.readAttribute( 'href' );
    if( typeof( onclick ) != 'string' )
      return;
    ph.each( function( pair ) {
      // match correct product with legacy onClick text
      var s = "open" + pair.value + "Gallery";
      var r = new RegExp( s );
      if( !onclick.match( r ) )
        return;
      // create a lightbox for this product, if it does not exist
      if( typeof( lbh.get( pair.key ) ) == 'undefined' )
        lbh.set( pair.key, new SMBUProductGallery.Lightbox( pair.key ) );
      // create a manager for this product, if it does not exist
      if( typeof( lbmh.get( pair.key ) ) == 'undefined' )
        lbmh.set( pair.key, new SMBUProductGallery.Manager( lbh.get( pair.key ) ) );
      // register this aElement to launch lightbox
      lbmh.get( pair.key ).addElement( aElement );
    });
  });
});


