OnlineReception.Letter = Class.create({
  initialize: function( receptionID ) {
    this.receptionID        = receptionID;
    this.hiddenReplyButtons = [];
    this.contentBlock       = $( '_receptionLetterMsgs' );
  }, // initialize
  
  setMarkupTable: function( table ) {
    this.markupTable = table;
  }, // setMarkupTable
  
  getLastMsgID: function() {
    var rows  = this.markupTable.rows;
    var msgID = 0;
    
    for ( var i=rows.length-1; i >= 0; i-- ) {
      msgID = parseInt( rows[i].getAttribute( 'msgID' ) );
      if ( msgID > 0 ) break;
    } // for
    
    return msgID;
  }, // getLastMsgID
  
  addMessage: function( element ) {
    this.hideAddMsgForm();
    this.addInModalWin = element == undefined;
    
    var dst;
    var self = this;
    var url  = '/online_reception/msgForm/' + this.receptionID + '/';
    
    if ( this.addInModalWin ) {
      $modalWin.markup();
      dst = $modalWin.content;
    } else {
      var hiddenDiv = cr( 'DIV' );
      hiddenDiv.className = 'hidden';
      Body.append( hiddenDiv );
      dst = hiddenDiv;
    } // if
    
    var requestOptions = {
      method : 'GET',
      evalScripts : true,
      onComplete: function() {
        try {
          if ( self.addInModalWin ) {
            $modalWin.show();
          } else {
            self.insertAddMsgForm( element, hiddenDiv );
          } // if
        } catch( error ) {
          jsLog( error );
        } // try
      } // onComplete
    }
    
    new Ajax.Updater( dst, url, requestOptions );
  }, // addMessage
  
  hideReplyButton: function( btn ) {
    hideElement( btn );
    this.hiddenReplyButtons.push( btn );
  }, // hideReplyButton
  
  insertAddMsgForm: function( element, block ) {
    var row     = getParentByTagName( element, 'TR' );
    var newRow  = this.markupTable.insertRow( row.rowIndex+1 );
    var newCell = newRow.insertCell( 0 );
    
    newRow.className = 'addMsgForm';
    newCell.setAttribute( 'colspan', '2' );
    newCell.appendChild( block );
    block.className = '';
    
    this.hideReplyButton( element );
  }, // insertAddMsgForm
  
  hideAddMsgForm: function() {
    $modalWin.close();
    var formRows = this.markupTable.getElementsByClassName( 'addMsgForm' );
    
    for ( var i=0; i < formRows.length; i++ ) {
      rmObj( formRows[i] );
    } // for
    
    var btns = this.hiddenReplyButtons;
    for ( var i=0; i < btns.length; i++ ) {
      displayElement( btns[i] );
    } // for
  }, // hideAddMsgForm
  
  onMsgAdded: function() {
    var lastMsgID = this.getLastMsgID();
    var hiddenDiv = cr( 'DIV' );
    var self      = this;
    
    hiddenDiv.className = 'hidden';
    
    var url = '/online_reception/msgRows/' + this.receptionID + '/' ;
    var requestOptions = {
      method     : 'GET',
      parameters : { lastMsgID : lastMsgID },
      onComplete : function() {
        try {
          var tBody   = self.markupTable.getElementsByTagName( 'TBODY' )[0];
          var newRows = hiddenDiv.getElementsByTagName( 'TR' );
          
          if ( newRows.length > 0 ) {
            while ( newRows.length > 0 ) tBody.appendChild( newRows[0] );
          } // if
          
          rmObj( hiddenDiv );
          self.hideAddMsgForm();
        } catch( error ) {
          jsLog( error );
        } // try
      } // onComplete
    }
    
    Body.append( hiddenDiv );
    new Ajax.Updater( hiddenDiv, url, requestOptions );
  }, // onMsgAdded
  
  close: function() {
    var url = '/online_reception/letterClose/' + this.receptionID + '/';
    var requestOptions = {
      method: 'GET'
    }
    
    new Ajax.Updater( this.contentBlock, url, requestOptions );
  }, // close
  
  remove: function() {
    var url = '/online_reception/letterDelete/' + this.receptionID + '/';
    var requestOptions = {
      method: 'GET'
    }
    
    new Ajax.Updater( this.contentBlock, url, requestOptions );
  }, // remove
  
  removeMessage: function( msgID ) {
  } // removeMessage
}); // OnlineReception.Letter
