/*!
 * Info Bar
 * Version 1.0
 * Author MFE
 * Copyright (c) 2008, Denbel - www.denbel.nl
 */

// load namespace
Denbel.load( 'ui.InfoBar' );

/**
 * InfoBar constructor
 * @param container HTMLElement or string ID
 * @return Denbel.ui.InfoBar
 */
Denbel.ui.InfoBar = function( container )
{
  if( YAHOO.lang.isString( container ) )
  {
    this.container = container;
  }
  else if( YAHOO.lang.isObject( container ) )
  {
    this.containerEl = container;
  }
  
  this.init();
};

Denbel.ui.InfoBar.ICON_NONE = 0;
Denbel.ui.InfoBar.ICON_INFO = 1;
Denbel.ui.InfoBar.ICON_INFORMATION = 1;
Denbel.ui.InfoBar.ICON_WARN = 2;
Denbel.ui.InfoBar.ICON_WARNING = 2;
Denbel.ui.InfoBar.ICON_ERR = 3;
Denbel.ui.InfoBar.ICON_ERROR = 3;

// prototype
Denbel.ui.InfoBar.prototype =
{
  /**
   * container element
   * @var HTMLElement
   */
  containerEl: null,
  
  /**
   * container element ID
   * @var string
   */
  container: null,
  
  /**
   * The inner overlay
   * @var YAHOO.widget.Overlay
   */
  innerOverlay: null,
  
  /**
   * Initializes the bar
   * @return boolean
   */
  init: function()
  {
    if( this.container )
    {
      this.containerEl = YAHOO.util.Dom.get( this.container );
    }
    else if( this.containerEl && YAHOO.util.Dom.inDocument( this.containerEl ) )
    {
      this.container = this.containerEl.getAttribute( 'id' );
    }
    
    if( !YAHOO.util.Dom.inDocument( this.container ) || !this.containerEl )
    {
      throw Error( 'Container is invalid' );
      return false;
    }
    
    this.innerOverlay = new YAHOO.widget.Overlay( this.container,
    {
      visible: false,
      effect:
      {
        effect: YAHOO.widget.ContainerEffect.FADE,
        duration: 0.5
      }
    } );
    
    this.innerOverlay.hideEvent.subscribe( function( e, i, o )
    {
      //var start = YAHOO.util.Dom.getStyle( o.container, 'height' );
      
      this.setBody( '' );
      this.render();
      
      YAHOO.util.Dom.setStyle( o.container, 'display', 'none' );
      
      //var anim = new YAHOO.util.Anim( o.container,
      //{
      //  height: {
      //    from: start,
      //    to: 0
      //  }
      //}, Denbel.Website._fadeSpeed, YAHOO.util.Easing.easeOut );
      
      //anim.animate();
    }, this );
    
    this.innerOverlay.render();
    this.innerOverlay.hide();
    
    YAHOO.util.Dom.addClass( this.container, 'info-bar' );
    
    return true;
  },
  
  /**
   * Returns a value indicating the bar was shown
   * @return Boolean
   */
  isShown: function()
  {
    return ( YAHOO.util.Dom.hasClass( this.container, 'shown' ) );
  },
  
  /**
   * Shows the bar
   * @param icon the icon to show
   * @param message the message to show
   * @param time the number of milliseconds the bar is shown
   * @param Boolean close
   * @return void
   */
  show: function( icon, message, time, close )
  {
    if( !icon )
    {
      icon = Denbel.ui.InfoBar.ICON_NONE;
    }
    
    if( !message )
    {
      message = '';
    }
    
    if( close )
    {
      close = '<div class="close"><a rel="internal" id="lCloseInfoBar" href="#">x</a></div>';
    }
    else
    {
      close = '';
    }
    
    message = '<div class="msg">' + message + '</div>';
    
    var img = '';
    
    switch( icon )
    {
      case Denbel.ui.InfoBar.ICON_INFO:
      case Denbel.ui.InfoBar.ICON_INFORMATION:
        img = '<div class="icon icon-info"></div>';
        break;
      
      case Denbel.ui.InfoBar.ICON_WARN:
      case Denbel.ui.InfoBar.ICON_WARNING:
        img = '<div class="icon icon-warning"></div>';
        break;
      
      case Denbel.ui.InfoBar.ICON_ERR:
      case Denbel.ui.InfoBar.ICON_ERROR:
        img = '<div class="icon icon-error"></div>';
        break;
        
      default:
        img = '';
        break;
    }
    
    YAHOO.util.Dom.setStyle( this.container, 'display', '' );
    
    this.innerOverlay.render();
    
    this.innerOverlay.setBody( close + img + message + '<div class="clear"></div>' );
    this.innerOverlay.render();
    
    this.innerOverlay.show();
    
    YAHOO.util.Dom.addClass( this.container, 'shown' );
    
    if( close )
    {
      YAHOO.util.Event.addListener( 'lCloseInfoBar', 'click', function( e, o )
      {
        YAHOO.util.Event.stopEvent( e );
        o.innerOverlay.hide();
      }, this );
    }
    
    if( time )
    {
      YAHOO.lang.later( time, this, 'hide', null, false );
    }
  },
  
  /**
   * Hides the bar
   * @return void
   */
  hide: function()
  {
    this.innerOverlay.hide();
    YAHOO.util.Dom.removeClass( this.container, 'shown' );
  }
};
