/*
 * PostBackJSON (for jQuery)
 * version: 1.0 (12/03/2009)
 * @requires jQuery v1.2 or later
 *
  *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2009 Byss.pl
 *
 */
 
(function($) {
	
  $.postBackJSON = function() {
  }

  /*
   * Public, $.showme methods
   */

  $.extend($.postBackJSON, {
		
		showErrorMessage: function(text){

			_showme = getObject('showme');
			_showme.html(text);
			$.showme.showDialog(_showme, 400, 'Error');
			
		}
		
	});	
	
  $.fn.postBackJSON = function(options) {
		
		_self = $(this);
		_form = getObject(options.form);
		_update = getObject(options.update);
		_refresh = getObject(options.refresh);
		
		getObject('faction').val(options.action);
		
    options = $.extend({
			referer: options.referer || "dialog",
			dataType: "json",
			beforeSubmit:  function (){
				activateProgress(_self);
			},
			success: function (jsonData, responseText) {
				deactivateProgress();
				
				if (jsonData.success){
					
						if( options.callback )
							options.callback.apply();						
					
						if (options.referer == 'dialog'){
							_update.dialog({
								 close: function(event, ui) { alert('1'); }
							});
						}	
						
						if (_refresh && options.refreshAction){
							_refresh.refresh({url: options.refreshAction});
						}
						
				}else if (jsonData.error){
					
					if (options.referer == 'dialog')
							_update.dialog('close');
					
					jQuery.showDialogError(jsonData.content, jsonData.width);
					
				}else{
					
					if (options.debug == 1)
						alert(jsonData.content);
					
					if (_update)
						_update.html(jsonData.content);
				}		
				
				//unbind submit event
				if (options.unbind)
					_form.unbind('submit');
				
			}
    }, options || {});
		
		_form.submit(function(){
			
			$(this).ajaxSubmit(options);
			
			return false;
			
		});
					
		_form.submit();
		
	}
})(jQuery);

