/**
 * Product Info. / Utilities for Hardwoodbrokers.com
 */

if( !Element.show )
{
Element.extend({
  show: function() {
    this.style.display = "";
  }
});
}

if( !Element.hide )
{
Element.extend({
  hide: function() {
    this.style.display = "none";
  }
});
}

if( !Element.fadeIn )
{
Element.extend({
  fadeIn: function( options ) {
    this.show();
    this.effects(options).start({ opacity: [0,1] });
  }
});
}

if( !Element.fadeOut )
{
Element.extend({
  fadeOut: function( options ) {
    this.effects(options).start({ opacity: [1,0] });
    this.hide();
  }
});
}

var PIU = new Class({
  options: {
    uri: "/applications/piu/",
    content: "[CONTENT]"
  },

  initialize: function( elHandle, elPiuContent, options ) {
    this.setOptions(options);
    this.handle = $(elHandle);
    this.piu_content = $(elPiuContent);
    //this.handle.onclick = function(e) { this.show(); return false; }.bind(this);
    this.handle.addEvent( "click", function(e) { new Event(e).stop(); this.show(); }.bind(this) );
  },

  show: function() {
    this.handle.blur();
    if( this.piu_content.style.display != "none" ) return;

    this.handle.fireEvent("activatePiu", this.handle);

    this.piu_content.fadeIn();
  },

  hide: function() {
    this.handle.blur();

    this.handle.fireEvent("deactivatePiu", this.handle);

    this.piu_content.fadeOut();
  }
  
});

PIU.implement( new Options, new Events );


var PIUController = new Class({
  initialize: function() {
    this.pius = [];
    this.activePiu = null;
  },

  addPiu: function( elHandle, elContent ) {
    this.pius[this.pius.length] = new PIU(elHandle, elContent, { duration: 200, transition: Fx.Transitions.linear } );
    index = this.pius.length-1;
    
    $(elHandle).index = index;
    $(elHandle).addEvent( "activatePiu", function() {
      this.setActivePiu($(elHandle).index);
    }.bind(this) );
    
    return index;
  },

  setActivePiuClassname: function( strClassName ) {
    this.activePiuClassname = strClassName;
  },

  setActivePiu: function( index ) {
    //if( this.activePiu == this.pius[index] ) return;

    if( this.activePiu != null )
    {
      this.activePiu.handle.removeClass(this.activePiuClassname);
      this.activePiu.hide();
    }
    this.activePiu = this.pius[index];
    this.activePiu.handle.addClass(this.activePiuClassname);
  }

});

var PIU_Application = {
  initialize: function() {
    var piu_controller = new PIUController();
    piu_controller.setActivePiuClassname('piu_option_active');
    initiallyActiveIndex = piu_controller.addPiu( 'piu_product_specs_hdl', 'piu_product_specs' );
    piu_controller.addPiu( 'piu_bid_history_hdl', 'piu_bid_history' );
    piu_controller.setActivePiu( initiallyActiveIndex );
  }
}

