/*
  QuickDialogs by PayteR v1.0.0
 */
(function($){
  $.q = function (options) {
    var returnArray = [];

    var defaults = {
      disabled: false,
      autoOpen: true,
      dialogClass: 'alert',
      draggable: true,
      height: 'auto',
      hide: null,
      maxHeight: 400,
      maxWidth: 1000,
      minHeight: 150,
      minWidth: 300,
      modal: true,
      overlayClose: false,
      position: 'center', 
      resizable: true,
      show: null,
      stack: true,
      value: '',
      width: 500,
      zIndex: 1000,
      
      buttonOk: 'Ok',
      buttonCancel: 'Cancel',
      content : "null",
      title:'Alert'
    };
    $.extend(defaults, options);

    arguments.callee.dialog = function (content, title, callback, args) {
      this._processArgs(content, title, args);  
      
      if(options.content){                  
        var id = 'dialog' + new Date().getTime();
        var opts = ' id="' + id + '"';
        opts += ' style="display:none"';
        opts += ' title="' + this.ucfirst(options.title) + '"';
      
        var div = '<div' + opts + '>' + options.content + '</div>';
      
        $('body').append(div);
           
        var buttonsOpts = {}
        if(options.buttonOk){
          buttonsOpts[options.buttonOk] = function() {
            $( this ).dialog( "close" );          
          };   
        }
           
        $('#' + id).dialog({
          disabled:options.disabled,
          autoOpen: options.autoOpen,
          dialogClass: options.dialogClass,
          draggable: options.draggable,
          height: options.height,
          hide: options.hide,
          maxHeight: options.maxHeight,
          maxWidth: options.maxWidth,
          minHeight: options.minHeight,
          minWidth: options.minWidth,
          modal:options.modal,
          position: options.position, 
          resizable: options.resizable,
          show: options.show,
          stack: options.stack,
          width: options.width,
          zIndex: options.zIndex,
          buttons: buttonsOpts,
          close: function() {
            if( callback ) callback(true);
          },
          open: function() {
            if(options.overlayClose){
              var thisdialog = $(this);
              $(".ui-widget-overlay").css('cursor', 'pointer').click(function(){
                thisdialog.dialog("close")
              })
            }
            $(".ui-dialog-buttonset button").removeClass('ui-state-focus');            
          }
        });      
      }
    };
    
    arguments.callee.confirm = function (content, title, callback, args) {
      this._processArgs(content, title, args);  
      
      if(options.content){                  
        var id = 'dialog' + new Date().getTime();
        var opts = ' id="' + id + '"';
        opts += ' style="display:none"';
        opts += ' title="' + this.ucfirst(options.title) + '"';
      
        var div = '<div' + opts + '>' + options.content + '</div>';
      
        $('body').append(div);

        var buttonsOpts = {}
        if(options.buttonOk){
          buttonsOpts[options.buttonOk] = function() {            
            if( callback ) returnArray[id] = true;
            $( this ).dialog( "close" );
          };
          buttonsOpts[options.buttonCancel] = function() {
            if( callback ) returnArray[id] = false;
            $( this ).dialog( "close" );
          };
        }
           
        $('#' + id).dialog({
          disabled:options.disabled,
          autoOpen: options.autoOpen,
          dialogClass: options.dialogClass,
          draggable: options.draggable,
          height: options.height,
          hide: options.hide,
          maxHeight: options.maxHeight,
          maxWidth: options.maxWidth,
          minHeight: options.minHeight,
          minWidth: options.minWidth,
          modal:options.modal,
          position: options.position, 
          resizable: options.resizable,
          show: options.show,
          stack: options.stack,
          width: options.width,
          zIndex: options.zIndex,
          buttons: buttonsOpts,
          beforeClose: function() {
            if( callback ) callback(returnArray[id]);
          },
          open: function() {
            if(options.overlayClose){
              var thisdialog = $(this);
              $(".ui-widget-overlay").css('cursor', 'pointer').click(function(){
                thisdialog.dialog("close")
              })
            }
            $(".ui-dialog-buttonset button").removeClass('ui-state-focus');            
          }
        });      
      }
    };
    
    arguments.callee.prompt = function (content, title, value ,callback, args) {
      this._processArgs(content, title, args, value);  
      
      if(options.content){                  
        var id = 'dialog' + new Date().getTime();
        var opts = ' id="' + id + '"';
        opts += ' style="display:none"';
        opts += ' title="' + this.ucfirst(options.title) + '"';
      
        var div = '<div' + opts + '>' + options.content;
        div += '<input type="text" style="width:97%" value="' + options.value + '"/></div>';
      
        $('body').append(div);

        var buttonsOpts = {}
        if(options.buttonOk){
          buttonsOpts[options.buttonOk] = function() {  
            if( callback ) returnArray[id] = true;
            $( this ).dialog( "close" );
          };
          buttonsOpts[options.buttonCancel] = function() {
            if( callback ) returnArray[id] = false;
            $( this ).dialog( "close" );
          };
        }
           
        $('#' + id).dialog({
          disabled:options.disabled,
          autoOpen: options.autoOpen,
          dialogClass: options.dialogClass,
          draggable: options.draggable,
          height: options.height,
          hide: options.hide,
          maxHeight: options.maxHeight,
          maxWidth: options.maxWidth,
          minHeight: options.minHeight,
          minWidth: options.minWidth,
          modal:options.modal,
          position: options.position, 
          resizable: options.resizable,
          show: options.show,
          stack: options.stack,
          width: options.width,
          zIndex: options.zIndex,
          buttons: buttonsOpts,
          beforeClose: function() {
            if( callback && returnArray[id] ) callback($('#' + id + ' input').val());
          },
          open: function() {
            if(options.overlayClose){
              var thisdialog = $(this);
              $(".ui-widget-overlay").css('cursor', 'pointer').click(function(){
                thisdialog.dialog("close")
              })
            }
            $(".ui-dialog-buttonset button").removeClass('ui-state-focus');            
          }
        });      
      }
    };        
        
    arguments.callee._processArgs = function(content, title, args, value){
      options = $.extend({},defaults);
      if(typeof(content)=='string'){        
        options = $.extend(options, args);
        options.content = content;
        options.title = typeof(title) == 'string' ? title : options.title;      
        options.value = value;  
      }else{
        console.log("LiveDialogs: Input values are not correct!")
      }
    }
    
    arguments.callee.ucfirst = function (s) {
      var fl = s.substr(0, 1);
      return fl.toUpperCase() + s.substr(1);
    }
    
  };
  
  //You can put in default arguments
  qInit = function(args) {
    $.q(args);
  }
  
  qAlert = function(content, title, callback, args) {       
    $.q.dialog(content, title, callback, args);
  }
	
  qConfirm = function(message, title, callback, args) {
    $.q.confirm(message, title, callback, args);
  };
		
  qPrompt = function(message, title, value, callback, args) {
    $.q.prompt(message, title, value, callback, args);
  };
  
  //Force qInit
  try {
    $.q.dialog();
  } catch (exception) {       
    qInit();
  } 
})(jQuery);
