Dialog = function(options){
    var that = this;
  this.diag = {};
  this.defaults = {
      id: "alertDialog",
      width: 300,
      height: 120,
      title: "Information",
      message: "&nbsp;",
      autoOpen: false,
      autoClose: false,
      modal: true,
      adjustBottom: false,
      opacity: '0.85',
      backgroundColor: '#F2F2F2',
      cssClass: "myDialog",
      buttons: {'OK': function(){ jQuery(this).dialog('close'); }}
  };

  callback = function(){ alert(''); };

  // "Private" Methods
    var _create = function(){
        jQuery('body').append('<div id="'+that.id+'"></div>');
        jQuery("#"+that.id).dialog({
                                    autoOpen: that.autoOpen,
                                    bgiframe: true,
                                    width: that.width,
                                    height: that.height,
                                    modal: that.modal,
                                    closeOnEscape: false,
                                    draggable: false,
                                    overlay: {opacity: that.opacity, backgroundColor: that.backgroundColor},
                                    resizable: false,
                                    title: that.title
                                });
        that.update();
  };

  // Public Methods
  this.show = function(){
      jQuery('#'+this.id).dialog('open');
        if(this.autoClose)
            jQuery('#'+this.id).animate({left:"+=0"},2000,function(){ jQuery('#'+this.id).dialog('close'); });
    if (this.adjustBottom)
    {
        var newHeight = parseInt(jQuery("#"+this.id).height()) - 40;
        jQuery("#"+this.id).css('height',newHeight);
    }
  };

  this.hide = function() {
      jQuery('#'+this.id).dialog('close');
  };

  this.reDraw = function(){
      if(jQuery('#'+this.id).dialog('isOpen'))
      {
          this.hide();
          this.show();
      }
  };

  this.update = function(updatedParams){
      jQuery.extend(this, updatedParams);

      jQuery('#'+this.id).addClass(this.cssClass);
        jQuery("#"+this.id).data("modal.dialog",this.modal).data("overlay.dialog",{opacity: this.opacity, backgroundColor: this.backgroundColor});
        jQuery("#"+this.id).html('<div class="dialogMessage" id="'+this.id+'_dialogMessage">'+this.message+'</div>');
        jQuery('#ui-dialog-title-'+this.id).html('<span>'+this.title+'</span>');
        jQuery("#"+this.id).data("width.dialog", this.width).data("height.dialog", this.height);
        jQuery("#"+this.id).data("buttons.dialog", this.buttons );
        this.reDraw();
  };

  // Constructor
    for(var property in this.defaults) {
        if (this.defaults.hasOwnProperty(property)){
            this[property] = this.defaults[property];
        }
    }
    
    jQuery.extend(this, options);
    _create();
};

DeleteFavSimDialog = function(options){
    var _overrideAttrs = {};
    var _idToRemove;
    this.defaults = {
        click: function(linkObj, message){
            if(linkObj){
                _idToRemove = linkObj.attr('value');
            }

            if(message) {
                this.message = message;
                this.update();
            }

            this.show();
        },
        buttons: {
            'Yes, remove this favorite': function(){
                userControls.removeSimFromFavorites(_idToRemove);
                jQuery(this).dialog('close');
            },
            'No': function(){ jQuery(this).dialog('close'); }
        }
    };

  // Constructor
    for(var property in this.defaults) _overrideAttrs[property] = this.defaults[property];
    jQuery.extend(_overrideAttrs, options);

    return new Dialog(_overrideAttrs);
};

DeleteUserSimTagDialog = function(options){
    var _overrideAttrs = {};

    this.defaults = {
        click: function(linkObj, message){
            if(linkObj) jQuery("#"+this.id).data('target',linkObj.attr('value'));
            if(message) {
                this.message = message;
                this.update();
            }

            this.show();
        },
        buttons: {
            'Yes, delete this tag': function(){
                TagController.deleteUserSimulationTag(jQuery("#"+this.id).data('target'), {
                    callback: function(data) {
                    window.location.reload(true);
                    }
                });
                jQuery(this).dialog('close');
            },
            'No': function(){ jQuery(this).dialog('close'); }
        }
    };

  // Constructor
    for(var property in this.defaults) _overrideAttrs[property] = this.defaults[property];
    jQuery.extend(_overrideAttrs, options);

    return new Dialog(_overrideAttrs);
};

DeleteSimTagDialog = function(options){
    var _overrideAttrs = {};

    this.defaults = {
        click: function(linkObj, message){
            if(linkObj) {
                jQuery("#"+this.id).data('target',linkObj.attr('value'));
                jQuery("#"+this.id).data('simid',linkObj.attr('rel'));
            }
            if(message) {
                this.message = message;
                this.update();
            }
            this.show();
        },
        buttons: {
            'Yes, delete this tag': function(){
                TagController.deleteSimulationTag(jQuery("#"+this.id).data('target'), jQuery("#"+this.id).data('simid'), {
                    callback: function(data) {
                        window.location.reload(true);
                    }
                });
                jQuery(this).dialog('close');
            },
            'No': function(){ jQuery(this).dialog('close'); }
        }
    };

  // Constructor
    for(var property in this.defaults) _overrideAttrs[property] = this.defaults[property];
    jQuery.extend(_overrideAttrs, options);

    return new Dialog(_overrideAttrs);
};

deleteSimDialog = function(options){
    var _overrideAttrs = {};

    var _simPath;
    var _destination;

    this.defaults = {
        click: function(linkObj){
            if(linkObj) jQuery("#"+this.id).data('target',linkObj.attr('value'));
            this.show();
        },
        setSimPath : function(p) {
            _simPath = p;
        },
        setDestination : function(p) {
            _destination = p;
        },
        buttons: {
            'Yes, delete this simulation': function(){
                jQuery(this).dialog().parents('.ui-dialog').find('.ui-dialog-buttonpane').remove();
                jQuery("#"+this.id).html('<div style="text-align: center;"><br/>Deleting this simulation...<br/>Please wait, this operation may take a little while.<br/><br/><img src="'+Globals.imageBasePath+'/ajax-loader.gif"></div>');

                if (_simPath)
                {
                    simControls.deleteSim(_simPath, _destination);
                }
                else
                {
                    simControls.deleteSim(jQuery("#"+this.id).data('target'));
                }
            },
            'No': function(){ jQuery(this).dialog('close'); }
        }
    };

  // Constructor
    for(var property in this.defaults) _overrideAttrs[property] = this.defaults[property];
    jQuery.extend(_overrideAttrs, options);

    return new Dialog(_overrideAttrs);
};

