(function($){
    $.prompts = {}
    $.prompts.prompt = function(options) {
        if (!typeof options.allowZeroLen === 'undefined') options.required = !options.allowZeroLen;
        var o = $.extend({},$.prompts.dlg_defaults,$.prompts.prompt.defaults,options);
        o.body= $('<div>');
        var r = function(){
                  if (o.required && b.val() == '') {
                      $.prompts.alert('You must enter a value before continuing','Error');
                  } else {
                    if (o.onValidate(b.val())){
                      o.onComplete(b.val());
                      dlg.dialog('close');
                    } else
                      b.focus();
                  }
                };
        var a = $('<p>').html(o.message).appendTo(o.body),
            b = $('<input>')
                  .addClass('ui-widget-content ui-corner-all')
                  .attr('type','text')
                  .val(o.value)
                  .bind('keypress',function(e){if (e.charCode==13 || e.keyCode==13) r();})
                  .css({
                      margin          : '8px 0 5px',
                      padding         : '.4em',
                      width           : '95%',
                      display         : 'block'   
                  })
                  .appendTo(o.body);

        o.buttons = {
            Cancel      : function() {
                $(this).dialog('close');
            },
            Ok          : function() {
                r();
            }
        };
        var dlg=_createdialog(o);
    }
    $.prompts.prompt_multi = function(options) {
        if (typeof options.fields === 'undefined') return;
        var input,o = $.extend(true,{},$.prompts.dlg_defaults,$.prompts.prompt.defaults,options);
        o.body = $('<div>');
        var r = function(){
          var val,ret={},invalid = false;
          for (var key in o.fields){
            val = $.trim($('#input-item-' + key).val());
            ret[key] = val; 
            if (o.fields[key].required && val == '') {
              invalid = true;
            }
          }
          if (invalid)
            $.prompts.alert('You must fill in all required fields before continuing','Error');
          else {
            if (o.onValidate(ret)){
              o.onComplete(ret);
              dlg.dialog('close');
            }
          }
        }
        for (var key in o.fields){
          obj = $.extend(true,{},$.prompts.prompt_field_defaults,o.fields[key]);
          $('<label>').attr('for','input-item-' + key).html(obj.label + (obj.required ? '' : ' (optional)')).appendTo(o.body);
          switch (obj.type){
            case "textarea":
              input = $('<textarea>').attr('rows',obj.lines).css('resize','none');
              break;            
            case "select":
              input = $("<select>");
              for (var sel in obj.select){
                $("<option>").val(sel).html(obj.select[sel]).appendTo(input);
              }
              break;
            default:
              input = $('<input>')
                .bind('keypress',function(e){if (e.charCode==13 || e.keyCode==13) r();})
                .attr('type','text');
          }
/*
          if (obj.lines > 1) {
            input = $('<textarea>').attr('rows',obj.lines).css('resize','none');            
          } else {
            input = $('<input>');
            input.bind('keypress',function(e){if (e.charCode==13 || e.keyCode==13) r();});
          }
*/
          input
            .addClass('ui-widget-content ui-corner-all')
            .attr({
              name          : 'input-item-' + key,
              id            : 'input-item-' + key
            })
            .val(obj.value)
            .css({
                margin              : '4px 0 8px',
                padding             : '.4em',
                width               : '100%',
                'box-sizing'        : 'border-box',
                '-moz-box-sizing'   : 'border-box',
                display             : 'block'
            })
            .appendTo(o.body);
          //o.body.append()
        }
        o.buttons = {
            Cancel      : function() {
                $(this).dialog('close');
            },
            Ok          : function() {
                r();
            }
        };
        var dlg=_createdialog(o);
    }
    $.prompts.alert = function(options,title){
        
        if (typeof options === 'object') {
          var o = $.extend({},$.prompts.dlg_defaults,$.prompts.alert.defaults,options);
        } else {
          var o = $.extend({},$.prompts.dlg_defaults,$.prompts.alert.defaults);
          o.message = options;
          if (arguments.length = 2)
            o.title = title;
        }
        o.body = $('<div></div>').html(o.message);
        o.buttons = {
            Ok          : function() {
                $(this).dialog('close');
            }
        };
        _createdialog(o);
    }
    $.prompts.confirm = function(options){
        var o = $.extend({},$.prompts.dlg_defaults,$.prompts.confirm.defaults,options);
        o.superConfirm = !(typeof o.challenge === 'undefined' || o.challenge == '');
        o.body = $('<div></div>');
        if (o.superConfirm){
            var a = $('<p></p>')
                      .html(o.message)
                      .css("margin-bottom","20px")
                      .appendTo(o.body),
                b = $('<p></p>')
                      .html("Enter '" + o.challenge + "' to continue:")
                      .appendTo(o.body),
                d = $('<input></input>')
                      .addClass('ui-widget-content ui-corner-all')
                      .attr('type','text')
                      .css({
                          margin          : '2px 0 5px',
                          padding         : '.4em',
                          width           : '60%',
                          display         : 'block'   
                      })
                      .appendTo(o.body);
                 d.keyup(function(){
                    if (d.val().toLowerCase()==o.challenge.toLowerCase())
                      dlg.parent().find(".ui-button:lt(1)").attr("disabled",false).removeClass("ui-state-disabled");
                    else                    
                      dlg.parent().find(".ui-button:lt(1)").attr("disabled",true).addClass("ui-state-disabled");
                 })
                 d.bind('keypress',function(e){
                    if ((e.charCode==13 || e.keyCode==13) && d.val().toLowerCase()==o.challenge.toLowerCase()) {
                      o.onConfirm();
                      dlg.dialog('close');
                    }
                 });            
        } else {
            o.body.html(o.message);
        }
        o.hideClose = true;
        o.buttons = {
            Yes         : function() {
                o.onConfirm();
                $(this).dialog('close');
            },
            No          : function() {
                o.onNo();
                $(this).dialog('close');
            }
        };
        if (o.cancel) {
          o.closeOnEscape=false;
          o.buttons['Cancel'] = function() {
            o.onCancel();
            $(this).dialog('close');
          }
        }
        var dlg = _createdialog(o);
    }
    $.prompts.block = function(options) {
        if (typeof options === 'object')
          var o = $.extend({},$.prompts.block.defaults,options);
        else {
          var o = $.prompts.block.defaults;
          o.message = options;
        }
        if (o.id == undefined || o.id == '')  
          return;
        if ($('#'+o.id).length > 0)
          return;        
        o.closeOnEscape = false;
        o.draggable = false;
        o.resizable = false;
        o.hideClose = true;
        o.body = $('<div>').html(o.message); 
          _createdialog(o);
    }
    $.prompts.unblock = function(){
        var a = $('#' + $.prompts.block.defaults.id);
        if (a.length != 0) {
          a.dialog('close');
        } 
      
    }
    $.prompts.dlg_defaults = {
        closeOnEscape : true,
        draggable     : true,
        resizable     : false,
        hideClose     : false,
        //width         : 'auto',
        //height        : 'auto',
        minWidth      : 300,
        //maxWidth      : 600,
        minHeight     : 150
        
    }
    $.prompts.alert.defaults = {
        message       : 'Enter you message here',
        title         : ''
    }
    $.prompts.prompt.defaults = {
        message       : '',
        title         : 'Enter your title Here',
        allowZeroLen  : false,
        required      : true,
        onValidate    : function(t){return true},
        onComplete    : function(t){}
    }
    $.prompts.confirm.defaults = {
        message       : 'Are you sure you would like to continue?',
        title         : 'Confirm?',
        cancel        : false,
        challenge     : '',  
        onConfirm     : function(){},
        onCancel      : function(){},
        onNo          : function(){}
    }
    $.prompts.prompt_field_defaults = {
        label         : 'Label',
        value         : '',
        required      : false,
        lines         : 1,
        type          : "text",          // options: select,text,textarea
        select        : {}
    }
    $.prompts.block.defaults = {
        message       : 'Loading... ',
        title         : 'Please Wait',
        id            : 'sk_block_dlg_20090923'
    }
    function _debug(d){
      if (window.console && window.console.log)
        window.console.log(d);
    }
    function _createdialog(o){
        var a = $('<div></div>').addClass('cbs-prompt').css('display','none').prependTo('body');
        if (o.id != undefined && o.id != '')
          a.attr('id',o.id);
        //$('<div></div>').html(o.message).appendTo(a);
        o.body.appendTo(a);
        a.dialog({
            modal         : true,
            draggable     : o.draggable,
            title         : o.title,
            resizable     : o.resizable,
            buttons       : o.buttons,
            position      : 'center',
            width         : o.width,
            height        : o.height,
            minWidth      : o.minWidth,
            minHeight     : o.minHeight,
            maxWidth      : o.maxWidth,
            maxHeight     : o.maxHeight,
            stack         : true,
            closeOnEscape : o.closeOnEscape,
            close         : function(){
                $(this).dialog("destroy");
                a.remove();
            },
            open          : function(){
                if (o.hideClose)
                  $(this).parent().find(".ui-dialog-titlebar-close").hide();
                if (o.superConfirm)
                  $(this).parent().find(".ui-button:lt(1)").attr("disabled",true).addClass("ui-state-disabled");
                
            }
        });
        return a;
    }
})(jQuery);                                                                                                  
