(function(jQuery) {

   /**
    *
    * @param message
    * @param [title]
    */
   function Dialog(message, title, redirect) {
      this.init();
      this.show(message, title);
      this.redirect = redirect;
   }

   /**
    * Url for where the user should be redirected after clicking OK
    */
   Dialog.prototype.redirect = null;

   Dialog.prototype.init = function() {
      jQuery(document).bind('keyup.dialog', jQuery.proxy(this._handleKeyPress, this));
   };

   Dialog.prototype.destroy = function() {
      jQuery(document).unbind('keyup.dialog');
      jQuery(".imageContainer").remove();
   };

   Dialog.prototype.show = function(message, title) {

      jQuery(".imageContainer").remove();

      var
         container = jQuery("<div class='dialogContainer'></div>"),
         imageContainer = jQuery(
            "<div class='imageContainer'>" +
               "<div class='imageContainerInner'>" +
               "<h2 class='title'>" +
               "</h2>" +
               "<div class='content'>" +
               "</div>" +
               "<button class='okButton'>" +
               "OK" +
               "</button>" +
               "</div>" +
               "</div>")
            .appendTo(container);


      if (typeof message == "string") {
         imageContainer.find(".content").text(message);
      } else {
         imageContainer.find(".content").append(message);
      }
      
      imageContainer.find(".title").text(title || "Message");
      imageContainer.find(".okButton").click(jQuery.proxy(this._handleOKPressed, this));

      jQuery(document.body).append(container);

      return false;

   };

   Dialog.prototype._handleOKPressed = function() {
      this.destroy();
      if (this.redirect) {
         self.location.href = this.redirect;
      }
   };

   Dialog.prototype._handleKeyPress = function(event) {
      switch (event.keyCode) {
         case 27:
            this.destroy();
            break;
      }
   };

   window.Dialog = Dialog;

})(jQuery);
