
var dialogCollection = { 
   collection: new Array(),
   add: function( bDialog ){ 
      this.collection.push( bDialog ); 
   },
   get: function(){ 
      return this.collection; 
   }
} 

var basicdialog = function( name, elem ){ 
   this.hidden = true;
   this.name = name; 
   var bdialog = this;
   if( typeof elem == 'undefined' )
   { 
      this.element = $("<div class='basic-dialog' id='" + name + "'></div>"); 
      this._content = $("<div class='content'></div>").appendTo(this.element); 
      this.close = $('<div></div>').click( function(e){ bdialog.hide() } ).addClass('close').appendTo( this.element ).text('X');
      $('body').append( this.element );
   } else { 
      elem = $(elem).addClass('basic-dialog').addClass('active');
      this.element = elem; 
   }
   
   dialogCollection.add( this ); 


   this.element.draggable();
   this.element.mousedown( function(e){ bdialog.activate(e); } ); 
}

basicdialog.prototype.show = function() { 
   this.hidden = false; 
   this.element.show();
   this.activate();   
} 

basicdialog.prototype.hide = function() { 
   this.hidden = true; 
   this.element.hide();
}

basicdialog.prototype.content = function( newContent ) { 
   this._content.html(newContent);
}

basicdialog.prototype.activate = function( ) { 
      var dialogs = dialogCollection.get(); 
      for( var i in dialogs ) 
      {
         if( this != dialogs[i]  )
         {
            dialogs[i].element.removeClass('active');
         } else { 
            dialogs[i].element.addClass('active');
         } 
      }
   }

