﻿$.extend({
  /**
  * Create DialogBox by ID

  * @param { String } elementID
  */
  getOrCreateDialog: function (id) {

    var $box = $('#' + id);
    if (!$box.length) {
      $box = $('<div id="' + id + '"></div>').hide().appendTo('body');
    }
    return $box;
  }
});

/**
* Override javascript confirm() and wrap it into a jQuery-UI Dialog box
*
* @depends $.getOrCreateDialog
*
* @param { String } the alert message
* @param { String/Object } the confirm callback
* @param { Object } jQuery Dialog box options 
*/
function confirm(message, callback, options) {

  var defaults = {
    modal: true,
    resizable: false,
    buttons: {
      "Да": function () {
        $(this).dialog('close');
        return (typeof callback == 'string') ? window.location.href = callback : callback();
      },
      "Нет": function () {
        $(this).dialog('close');
        return false;
      }
    },
    show: 'fade',
    hide: 'fade',
    minHeight: 50,
    dialogClass: 'modal-shadow'
  };
  var $confirm = $.getOrCreateDialog('confirm');
  // set message
  $($confirm).html(message);
  // init dialog
  $confirm.dialog($.extend({}, defaults, options));
}

/**
* Override javascript alert() and wrap it into a jQuery-UI Dialog box
*
* @depends $.getOrCreateDialog
*
* @param { String } the alert message
* @param { Object } jQuery Dialog box options 
*/
function alert(message, options) {

  var defaults = {
    modal: true,
    resizable: false,
    buttons: {
      Ok: function () {
        $(this).dialog('close');
      }
    },
    show: 'fade',
    hide: 'fade',
    minHeight: 50,
    dialogClass: 'modal-shadow'
  };
  var $alert = $.getOrCreateDialog('alert');
  // set message
  $($alert).html(message);
  // init dialog
  $alert.dialog($.extend({}, defaults, options));
}

function info(message, options) {
  var defaults = {
    modal: true,
    resizable: false,
    show: 'fade',
    hide: 'fade',
    minHeight: 50,
    dialogClass: 'modal-shadow'
  };
  var $info = $.getOrCreateDialog('info');
  // set message
  $($info).html(message);
  // init dialog
  $info.dialog($.extend({}, defaults, options));  
}
