﻿(function ($) {

  ZerkDialog = new JS.Class({

    extend: {
      alert: function (settings) {
        var html = "<div title='" + settings.title + "'>"
								 + "<p>" + settings.prompt + "</p>"
								 + "<div class='dialog-buttons'>"
								 + "<button type='button' class='ok'>OK</button>"
								 + "</div>";

        var dialog = this.display(html, settings);

        $(".ok", dialog.element).click(function () {
          if (settings.callback instanceof Function)
            settings.callback();
          dialog.close();
        });
      },
      confirm: function (settings) {
        var html = "<div title='" + settings.title + "'>"
								 + "<p>" + settings.prompt + "</p>"
								 + "<div class='dialog-buttons'>"
								 + "<button type='button' class='ok'>OK</button>"
								 + "<button class='close'>Cancel</button>"
								 + "</div>";

        var dialog = this.display(html, settings);

        $(".ok", dialog.element).click(function () {
          settings.callback();
          dialog.close();
        });
      },
      display: function (html, settings) {
        var dialog = new this(null, settings);
        dialog.display(html);
        return dialog;
      },
      show: function (activator, settings) {
        var dialog = new this(activator, settings);
        dialog.show.apply(dialog);
        return dialog;
      }
    },

    DEFAULTS: {
      modal: true,
      resizable: false,
      width: 476,
      close: ".ui-dialog-titlebar-close",
      type: "get",
      clearWorkingAfterSubmit: false,
      closeAfterSubmit: true,
      ajaxForm: false
    },

    initialize: function (activator, settings) {
      this.activator = $(activator);
      this.settings = $.extend({}, this.DEFAULTS, settings);
      this.hasActivator = this.activator; //&& this.activator.setWorking instanceof Function;
    },

    show: function () {
      var self = this;
      
      if( self.hasActivator) {
        var loc = self.activator.attr('href');
        self.activator.removeAttr('href');
      }
      //if (self.hasActivator) self.activator.setWorking(true);
      $.ajax({
        url: self._getUrl(),
        data: self._getData(),
        type: self.settings.type,
        cache: false,
        success: function (html) {
          self.display.apply(self, [html]);
        },
        complete: function () {
          if (self.hasActivator) self.activator.attr('href', loc);
          //if (self.hasActivator) self.activator.setWorking(false);
        }
      });
    },

    close: function () {
      if (this.element) {
        this.element.dialog("close");
        this.element.remove();
      }

      if (this.settings.afterClose instanceof Function)
        this.settings.afterClose.apply(this, [this.activator]);
    },

    display: function (html) {
      var scripts = $(html).filter("script");
      this.element = $(html).not("script");

      $("#modalDiv").html(this.element);
      scripts.appendTo(this.element);

      this.form = $("form", this.element);
      this.validator = this.form.validate({ errorElement: "div" });
      this.element.dialog(this.settings);

      $(".ui-dialog-titlebar-close").click(function () { $('.popup').remove(); });

      if (this.settings.ajaxForm)
        this.form.bind("submit", $.proxy(this._submit, this));
    },

    _submit: function (evt) {
      evt.preventDefault();

      if (!this.form.valid())
        return false;

      if (this.settings.beforeSubmit instanceof Function) {
        if (!this.settings.beforeSubmit.apply(this))
          return false;
      }

      var config = $.extend({ success: $.proxy(this._afterSubmit, this), error: $.proxy(this._onError, this) }, this.settings.ajax || {});
      //$("button[type=submit]", this.form).setWorking(true);

      this.form.ajaxSubmit(config);
    },

    _onError: function (request, status, error) {
      if (this.settings.onError instanceof Function)
        this.settings.onError.apply(this, [request, status, error, this.activator]);
    },



    _afterSubmit: function (response, status, xhr) {
      
      
      
      if (typeof response == "string" && response.indexOf("AZERROR") != -1) {
        if (this.settings.onError instanceof Function) {
          this.settings.onError.apply(this, [null, status, response, this.activator]);
          return;
        }
      }


      if (this.settings.afterSubmit instanceof Function)
        this.settings.afterSubmit.apply(this, [response, status, xhr, this.activator]);
      if (this.settings.closeAfterSubmit)
        this.close();
      //if (this.settings.clearWorkingAfterSubmit)
      //$("button[type=submit]", this.form).setWorking(false);
    },

    _getUrl: function () {
      if (!this.settings.url && this.activator)
        return this.activator.attr("href");
      else if (this.settings.url instanceof Function)
        return this.settings.url.apply(this, [this.activator, this.settings]);
      else
        return this.settings.url;
    },

    _getData: function () {
      if (this.settings.data instanceof Function)
        return this.settings.data.apply(this, [this.activator, this.settings]);
      else
        return this.settings.data;
    }

  });

})(jQuery);
