
(function($) {

  $.fn.ping = function(func1, func2, duration)
  {

    this.each(func1);
    $.fn.ping.collection = this;
    setTimeout(function() { $.fn.ping.collection.each(func2);  }, duration);

    return this;

  };

  $.wait = function(message)
  {

    message = message || "Please Wait...";
    $("body").append($('<div id="wait_box"><p style="text-align: center; margin: auto auto;">' + message + '</p></div>'));
    $.wait_box = $("#wait_box").dialog({
      draggable: false,
      modal: true,
      resizable: false,
      closeOnEscape: false,
      dialogClass: 'wait_box',
      show: 'Fade',
      hide: 'Fade',
      open: function(event, ui) { 
        $(".wait_box .ui-dialog-titlebar-close").hide(); 
      },
      close: function(e, ui) {
        $(this).remove();
      }
    });

  };

  $.stopWaiting = function(message)
  {

    if(message) {
      $.wait_box.bind('dialogclose', function(e, ui) {
        $(this).remove();
        $.alert(message);
      });
    }

    $.wait_box.dialog('close');

  };

  $.alert = function(message, title)
  {
    title = title || 'Alert';
    $('body').append($('<div id="alert_box"><p><strong>' + message + '</strong></p></div>'));
    $('#alert_box').dialog({
      closeText: 'Ok',
      draggable: false,
      modal: true,
      resizable: false,
      close: function(e, ui) { $(this).remove(); },
      title: title,
      buttons: {
        "Ok": function() {
          $(this).dialog('close');
        }
      }
    });
    $('.ui-dialog').css('font-size', '68.5%');
  };

  $.confirm = function(message, action, title)
  {
    title = title || 'Confirm';
    $('body').append($('<div id="confirm_box"><p>' + message + '</p></div>'));
    $('#confirm_box').dialog({
      draggable: false,
      modal: true,
      resizable: false,
      title: title,
      close: function(e, ui) { $(this).remove(); },
      buttons: {
        "Yes": function()
        {
          $(this).dialog('close');
          action();
        },
        "No": function()
        {
          $(this).dialog('close');
        }
      }
    });
    $('.ui-dialog').css('font-size', '68.5%');
  };

  $.progress = function()
  {
    $('body').append($('<div id="progress_dialog"><div id="progress_text">0%</div><div id="upload_progress_bar" style="width: 100%; height: 20px;"></div></div>'));
    $('#progress_dialog').dialog({
      closeText: 'Cancel',
      draggable: false,
      modal: true,
      resizable: false,
      close: function(e, ui)
      {
        $(this).remove();
      },
      title: "Upload Progress",
      buttons: {
        "Cancel": function() {
          $(this).data('closed_manually', true);
          $(this).dialog('close');
        }
      }
    });
    $('.ui-dialog').css('font-size', '68.5%');
    $("#upload_progress_bar").progressbar();
  };

  $.fn.getCaretPosition = function() {

     field = this.get(0);
     var pos = 0;

     if(document.selection) {
       field.focus ();
       var sel = document.selection.createRange ();
       sel.moveStart('character', -field.value.length);
       pos = sel.text.length;
     } else if(field.selectionStart || field.selectionStart == '0') {
       pos = field.selectionStart;
     }

     return(pos);

   };

   $.fn.setCaretPosition = function(pos) {

     field = this.get(0);

     if(document.selection) {
       field.focus();
       var sel = document.selection.createRange ();
       sel.moveStart('character', -field.value.length);
       sel.moveStart('character', pos);
       sel.moveEnd('character', 0);
       sel.select();
     } else if(field.selectionStart || field.selectionStart == '0') {
       field.selectionStart = pos;
       field.selectionEnd = pos;
       field.focus();
     }

   };

})(jQuery);

