/* 
	A simple plugin to extend the Impromptu interface 
	
	Originally this just included a basic confirm function but then other stuff was added like promptWithList which
	formats lists of info into a message from an array (for situations where you need to communicate several messages
	at once to the user in prompt format) 
	  
	TODO: Perhaps I just rename this plugin file to impromptu.util or impromptu.extensions? ebw 8/22
	
*/

(function($) {					
	$.extend({	
	  /*
	  Function to facilitate confirm operations
  	We can't truly emulate a JavaScript confirm because of the asynchronous nature of the fade 
  	animations and the fact that JavaScript doesn't support a thread sleep
  	so we can't pause the interpreter without burning CPU cycles

  	@param msg - The confirm message
  	@param yes_cb - The callback to perform on positive acknowledgement
  	@param no_cb - The callback to perform on negative acknowledgement
  	@param opt - Options for this Impromptu dialog
  	@return void
	  */
		confirm: function(msg, yes_cb, no_cb, opt){
			//extend options obj for confirm 
			opt = $.extend(
				{ 
					buttons: { No: false, Yes: true }, 
					//prefix: 'jqiconfirm', //handle for formatting the confirmation look and feel via css
					focus: 1,
					callback : function(v, m) {
						if (v && yes_cb) 
							yes_cb(v, m);
						else if (!v && no_cb)
						  no_cb(v, m);
					}
				},
				opt				
			);
			
			$.prompt(msg, opt); 
		},
		
		/* 
		@param msg - A message to serve as a prompt/preamble to the list of messages (just make an empty string if you don't want it)
		@param list - Array of messages to be translated into a bulleted list (which can be styled via CSS as needed)
		@param opt - Options for this Impromptu dialog
		@param hipri - (optional) If defined will display in high priority mode
		*/		
		promptWithList: function(msg, list, opt, hipri) {
		  var html = msg + '<ul class="impromptu_message_list">';
		  $.each(list, function(i, item) {
		    html += '<li>' + item + '</li>';
		  });
		  html += '</ul>';
		  (hipri) ? $.highPriorityPrompt(html, opt) : $.prompt(html, opt);
		},
		/* Rather than hack Impromptu directly this function serves to allow high priority prompts that will 
       overlay on top of any other thing going on on the page - e.g. BlockUI activity. 
       
       This could also be used to give prompt additional styling (error red, etc)
    */
    highPriorityPrompt: function(msg, o) {
      o = $.extend({ zIndex : 9999 }, o); //If not high enough on the z-axis just increment the number but this should be good enough
      $.prompt(msg, o); 
    }
	});
	
	
	//add a confirmation to a link click - NOTE for some reason confirmButton logic doesn't work on links
  $.fn.confirmLink = function(o) 
  {		
    o = $.extend({}, o);
  	return this.each(function() {
      var $l = $(this);
      var title = $l.attr('title');
      $l.attr('title', ''); //hide title 
    	$l.click(function(e) {
    	  $.confirm(o.message ? o.message : (title ? (o.encode_title ? unescape(title) : title) : 'Are you sure?'),
    	    function() { location.href = $l.attr('href'); } 
    	  );    	  
    	  e.preventDefault(); //prevent default action for this link but allow event bubbling
    	});
	  });
	};
	
	//add confirmation to a button
	$.fn.confirmButton = function(msg) 
  {		
  	return this.each(function() {
  	  var _submitted = false;
      var $b = $(this);
    	$b.click(function(e) {
    	  if (_submitted) {
    	    return true;
    	  }
    	  $.confirm(msg ? msg : 'Are you sure?',
    	    function() { _submitted = true; $b.click(); } 
    	  );    	  
    	  e.preventDefault(); //no click just yet
    	});
	  });
	};
	  
})(jQuery);


	
	

