// Gaia Ajax Copyright (C) 2008 - 2009 Gaiaware AS. details at http://gaiaware.net/

/* 
 * Gaia Ajax - Ajax Control Library for ASP.NET
 * Copyright (C) 2008 - 2009 Gaiaware AS
 * All rights reserved.
 * This program is distributed under either GPL version 3 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Gaiaware AS
 * read the details at http://gaiaware.net
 */

/* ---------------------------------------------------------------------------
   Class basically wrapping the ASP.Panel WebControl class
   --------------------------------------------------------------------------- */
Gaia.Panel = Class.create();

// Inheriting from WebControl
Object.extend(Gaia.Panel.prototype, Gaia.WebControl.prototype);

// Inheriting from Container
Object.extend(Gaia.Panel.prototype, Gaia.Container.prototype);

// Adding Custom parts
Object.extend(Gaia.Panel.prototype, {

  // "Constructor"
  initialize: function(element, options){
    this.initializePanel(element, options);
  },

  initializePanel: function(element, options){
    // Calling base class constructor
    this.initializeWebControl(element, options);
    
    if (options && options.db)
        this.setDefaultButton(options.db);
  },
  
  setDirection: function(value) {
    if (value == 'LeftToRight')
        this.element.setAttribute('dir', 'ltr');
    else if (value == 'RightToLeft')
        this.element.setAttribute('dir', 'rtl');
    else if (this.element.hasAttribute('dir'))
        this.element.removeAttribute('dir');
        
    return this;
  },
  
  setGroupingText: function(value) {
    // if new group should be set
    var fieldSet = $$('#' + this.element.id + ' > fieldset').first();
    if ( value && value.length > 0 ) {
        
        // if previous group exists, then just rename it
        if ( fieldSet ) {
            fieldSet.firstDescendant().update(value);
            return this;
        }
        
        // create fieldset
        var fieldSet = new Element('fieldset');
        fieldSet.appendChild(new Element('legend').update(value));
        
        while (this.element.childNodes.length > 0) {
            fieldSet.appendChild(this.element.childNodes[0]);
        }
        
        this.element.appendChild(fieldSet);
    } else if (fieldSet) {
        // remove fieldset and legend if no grouping text
        fieldSet.removeChild(fieldSet.firstDescendant());
        
        while (fieldSet.childNodes.length > 0) {
            this.element.appendChild(fieldSet.childNodes[0]);
        }
        
        this.element.removeChild(fieldSet);
    }
    
    return this;
  },
  
  setFocus: function(){
    try{
      this.element.focus();
    } 
    catch (err) { }
    return this;
  },

  setVisible: function(value){
    value ? Element.show(this.element) : Element.hide(this.element);
    if( value != true ){

      // Making sure control and child controls is destroyed...
      this.destroy();
    }
    return this;
  },
  
  setDefaultButton: function(value) {
    if (!this.options.defaultButton) {
        if (value) {
            this.onKeyPress = this._keyPress.bind(this);
            Element.observe(this.element, 'keypress', this.onKeyPress);
        } else if (this.onKeyPress)
            Element.stopObserving(this.element, 'keypress', this.onKeyPress);
    }
    
    this.options.defaultButton = value;
  },
  
  _keyPress: function(event) {
    var element = Event.element(event);
    var keyCode = event.which ? event.which : event.keyCode;
    
    if (keyCode == Event.KEY_RETURN && !(element && (element.tagName.toLowerCase() == "textarea"))) {
        // try as Gaia control with click support
        var defaultButton = $G(this.options.defaultButton);
        if (!defaultButton || (typeof defaultButton.click == "undefined")) {
            // fallback to DOM control with click support
            defaultButton = $(this.options.defaultButton);
            if (!defaultButton || (typeof defaultButton.click == "undefined")) return;
        }
    } else return;
        
    defaultButton.click();
    Event.stop(event);
  },

  // destructor. override this one. 
  destroy: function(){
    this._panelDestroy();
  },
  
  // private destructor of panel
  _panelDestroy: function() {
    if (this.onKeyPress)
        Element.stopObserving(this.element, 'keypress', this.onKeyPress);
  
    // Destroying all CHILDREN controls
    this.destroyContainer();

    // Calling Object.destroy implementation...
    this._destroyImpl();
  },

  _getElementPostValue: function(){
    return '';
  }
});

Gaia.Panel.browserFinishedLoading = true;
