/**
 * MindCMS 3
 *
 * @class   msSelectButton
 * @extends msButton
 * JS msSelectButton object definition
 *
 * @package CMS3
 * @author Raluca NECHITA <raluca.nechita@mindbench.nl>
 * @copyright Mindbench Active Media B.V.
 * @version $Id: msDropdownButton.js 7188 2009-01-03 15:36:34Z root $
 */


var msDropdownButton = Class.create(msButton, {
  xtype: 'dropdown-button',
   
  //a reference to the custom content will be drawn
  viewer: false,

  // redefine the constructor
  initialize: function ($super,config) {
    var THIS = this;
    $super(config);
    
    if(!this.viewer) {
      Object.extend(this, { viewer: eval(this.config.viewer) });
      if(this.viewer) {
        this.viewer.getParent = function () {
          return THIS;
        }
      }
    }
  },
  
  // redefine the build method
  build: function ($super) {  
    var THIS = this;
   
    $super();
         
    if (
      this.config.verify_localized &&
      (window[this.config.verify_localized] == undefined || window[this.config.verify_localized].length == 0)) { 
      return;
    }      
          
    var id = this.config.key.replace(/\x20/g,'');
    var input_name = 'btn-' + id ;
    var container_name = id + 'container';    
    
    this.el.id = input_name;
    
    this.box = new msBoxContainer(id);

    //adds the dropdown icon
    var anchor_span = new Element('span');
    
//	  anchor_span.setStyle({ 'height':'20px', 'border': '1px red solid'});
	  
    var anchor = new Element('img', {'class': 'trigger' });
    anchor.src = '/shared/img/msr-ribbon/buttons/arrow-down.gif';
    anchor_span.appendChild(anchor); 
    $(this.el).select('p')[0].appendChild(anchor_span); 
    $(this.el).select('p')[0].addClassName('msr-arrow-down-newline');
    
    
    Event.observe(anchor_span, 'click', this.clickListner.bindAsEventListener(
      this,
      { 'container_name': container_name, 'input_name': input_name }
    ));
    
    this.trigger = anchor_span;
  },
  
  clickListner: function(event, ci) {
    Event.stop(event);
    if(this.viewer) {
      this.box.populateBox(this.viewer.getEl(ci.container_name, ci.input_name));    
    }
    else if (this.config.action.trigger_callback) {
      eval(this.config.action.trigger_callback.method)(event, this.trigger);
    }
  }
});
