AddMyHomeImageHandler.ADD_HOME_PATH = "/addhome.php";
AddMyHomeImageHandler.SUCCESS_MESSAGE = "The home was added successfully.";
AddMyHomeImageHandler.ERROR_MESSAGE = "There was an unspecified error";
AddMyHomeImageHandler.EXCEEDS_CAP_MESSAGE = "You have more than 100 homes.";
function AddMyHomeImageHandler(listingNum, source, mls, propertyType, imageId, notAddedImageURL, addedImageURL) {
  this.added = false;
  this.listingNum = listingNum;
  this.source = source;
  this.mls = mls;
  this.propertyType = propertyType;
  this.imageId = imageId;
  this.notAddedImageURL = notAddedImageURL;
  this.addedImageURL = addedImageURL;

  document.getElementById(this.imageId).style.cursor = "pointer";
  YAHOO.util.Event.addListener(document.getElementById(this.imageId), "click", this.add, this, true);

  this.success = function(o) {
    var code = parseInt(o.responseText);
    var message = "";
    var icon;
    var redirect = false;
    if(code >= 0) {
      message = AddMyHomeImageHandler.SUCCESS_MESSAGE;
      icon = YAHOO.widget.SimpleDialog.ICON_INFO;
    } else {
      if(code == -2) {
        message =  AddMyHomeImageHandler.EXCEEDS_CAP_MESSAGE;
        icon = YAHOO.widget.SimpleDialog.ICON_BLOCK;

      } else {
        message = AddMyHomeImageHandler.ERROR_MESSAGE;
        icon = YAHOO.widget.SimpleDialog.ICON_WARN;

      }
    }

    if(code < 0) {
      this.redirect(message);
    }
  }

  this.failure = function(o) {
    this.redirect(YAHOO.widget.SimpleDialog.ICON_WARN);
  }

  this.redirect = function(message) {
    window.location.href = '/searchresults2.php?err=' + escape(message);
   }

  this.scope = this;

}

AddMyHomeImageHandler.prototype.showDialog = function(message, icon) {
  var dialog =
    new YAHOO.widget.SimpleDialog("simpledialog1",
                                 { width: "300px",
                                   fixedcenter: true,
                                   visible: true,
                                   draggable: false,
                                   close: true,
                                   text: message,
                                   icon: icon,
                                   constraintoviewport: true,
                                   effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}  
                                 } );

  dialog.setHeader("My Homes");
  dialog.render(document.body);


}

AddMyHomeImageHandler.prototype.add = function() {
  if(!this.added) {
    this.added = true;
    var url = AddMyHomeImageHandler.ADD_HOME_PATH + '?cxp=1&listing_num=' + this.listingNum + '&mls=' + this.mls + '&source=' + this.source + '&property_type=' + this.propertyType;
    YAHOO.util.Connect.asyncRequest('GET', url, this);
    document.getElementById(this.imageId).src = this.addedImageURL;
  } else {
    window.location = '/myaccount/myhomes.php';    
  }
}
