/*!
 * Stage UI component
 * Version 1.0
 * Author M.F.Endenburg
 * Copyright (c) 2008 Denbel
 */
 
// load/create namespace
Denbel.load( 'ui.Stage' );

/**
 * constructor
 * @param Variant container
 * @param object configuration
 * @return Denbel.ui.Stage
 */
Denbel.ui.Stage = function( container, config )
{
  if( !container )
  {
    throw Error( 'Container is needed for Stage' );
    return;
  }
  
  if( YAHOO.lang.isString( container ) )
  {
    this.container = YAHOO.util.Dom.get( container );
    this.containerId = container;
  }
  else
  {
    this.containerId = container.getAttribute( 'id' );
    this.container = container;
  }

  if( !config )
  {
    config = {};
  }
      
  this.extra = config;
  
  this.init();
};

// prototype
Denbel.ui.Stage.prototype =
{
  /**
   * container object
   * @var HTMLElement
   */
  container: null,
  containerId: null,
  id: null,
  positions: [],
  contents: [],
  extra: {},
    
  /**
   * initializer
   * @return bool
   */
  init: function()
  {
    this.id = this.containerId;
    this.positions = [];
    this.contents = [];
  
    var children = this.container.childNodes;
    var i = 0;
    
    for( i = 0; i < children.length; i++ )
    {
      if( !children[i].tagName || children[i].tagName != 'DIV' )
      {
        continue;
      }
      
      this.positions.push( children[i] );
      this.contents.push( children[i].innerHTML );
  
      if( this.positions.length > 1 )
      {
        YAHOO.util.Dom.setStyle( children[i], 'margin-top', '-8px' );
      }
      
      YAHOO.util.Event.addListener( children[i], 'click', function( e, o )
      {
        YAHOO.util.Event.stopEvent( e );
        o.openPosition( this );
      }, this );
    }
    
    this.openPosition.call( this, 1 );
    
    return true;
  },
  
  /**
   * Opens a position by number
   * @param Variant pos index or element
   * @return void
   */
  openPosition: function( pos )
  {
    if( !pos )
    {
      throw Error( 'Invalid position given' );
      return;
    }
    
    if( !YAHOO.lang.isNumber( pos ) )
    {
      pos = pos.getAttribute( 'id' ).split( '_' )[1];
    }
    
    pos--;
    var left = null;
    
    for( var i = 0; i < this.positions.length; i++ )
    {
      if( pos == i )
      {
        if( YAHOO.env.ua.ie > 0 )
        {
          if( this.extra.sequence == 1 )
          {
            left = '0';
          }
          else
          {
            left = ( 142 * ( this.extra.sequence - 1 ) );
          }
        }
        
        this.positions[i].innerHTML = '<img style="position:relative;top:0px;left:0px;width:142px;height:207px;" alt="" src="/media/img/board_' + ( i + 1 ) + '.png" /><div style="position:absolute;top:' + ( 15 + ( 30 * ( i + 1 ) ) ) + 'px;' + ( ( left ) ? 'left:' + left + 'px;' : '' ) + 'width:136px;padding:3px;z-index:100;">' + this.contents[i] + '</div>';
        YAHOO.util.Dom.setStyle( this.positions[i], 'cursor', 'default' );
      }
      else
      {
        this.closePosition( ( i + 1 ) );
      }
    }
    
    if( YAHOO.env.ua.ie > 0 && YAHOO.env.ua.ie < 7 )
    {
      correctPNG();
    }
  },
  
  /**
   * Closes a position by number
   * @param Variant pos index or element
   * @return void
   */
  closePosition: function( pos )
  {
    if( !pos )
    {
      return;
    }
    
    if( !YAHOO.lang.isNumber( pos ) )
    {
      pos = pos.getAttribute( 'id' ).split( '_' )[1];
    }
    
    pos--;
    var box = this.positions[pos];
    
    if( !box )
    {
      return;
    }
  
    this.positions[pos].innerHTML = '<img style="width:142px;height:34px;" alt="" src="/media/img/board_close_' + ( pos + 1 ) + '.png" />';
    YAHOO.util.Dom.setStyle( this.positions[pos], 'cursor', 'pointer' );
  },
  
  /**
   * Converts this object to its string representation
   * @return string
   */
  toString: function()
  {
    return 'Denbel.ui.Stage ' + this.containerId;
  }
};
