(function($) {
 	$.fn.postAjax = function(options){

		this.each(function(){
			new postAjaxP(options);
		});
		return this;
	}
	var postAjaxP = function(options){this.init(options)}

	postAjaxP.prototype = {
		options : null,
		init : function(options){
			this.options = jQuery.extend({
				disapearTimerId	: 0
				,spinner_id		: 'loader'
				,type			: "POST"
				,callbackFunc	: null
				,data			: {}
				,showLoading	: true
				,showGrayOut	: false
				,button			: null
				,loadElem		: null
			},options);

			postAjaxP.prototype.options = this.options;
			if (this.options.url) {
				this.send();
			}
		},
		send : function(){
			if (this.options.showLoading) {
				this.hideButton();
				this.showSpinner();
				if (this.options.showGrayOut) {
					this.grayOut(true);
				}
			}
			this.hideMessage('error');
			if (this.options.loadElem) {
				$(this.options.loadElem).load(this.options.url,this.options.data,function(responseText) {postAjaxP.prototype.onLoadSuccess(responseText);});
			} else {
				$.ajax({
	    			url: this.options.url,
	    			type: this.options.type,
	    			data: this.options.data,
					dataType: 'json',
					success: function (data, textStatus) {
							postAjaxP.prototype.onSuccess(data);
					}

				});
			}

		},
		onLoadSuccess : function (data,textStatus,XMLHttpRequest) {
			try {
				if (this.options.showLoading) {
					if (this.options.showGrayOut) {
						this.grayOut(false);
					}
					if (this.options.callbackFunc && typeof(this.options.callbackFunc) == 'function') {
						try {
							this.options.callbackFunc.call(null, data);
						} catch(e) {
							this.showException(response, e);
						}
					}
					this.hideSpinner();
					this.showButton();
				}
			} catch(e) {
				this.showException(data, e);
			}
		},
		onSuccess : function(response) {
			try {
				if (this.options.showLoading) {
					if (this.options.showGrayOut) {
						this.grayOut(false);
					}
				}
				if (response.invalid_fields) {
					this.showFieldsError(response.invalid_fields);
				} else {
					$('.form_error_message ').hide();
				}
				if (response.error) {
					this.showMessage('error', response.error);
				}

				if (response.redirect_url && response.redirect_url != '') {
					window.location.href = response.redirect_url;
				}
				if ((!response.error || response.error == '') && this.options.callbackFunc && typeof(this.options.callbackFunc) == 'function') {

					try {
						this.options.callbackFunc.call(null, response);
					} catch(e) {
						this.showException(response, e);
					}
				}
				if (this.options.showLoading && response.error) {
					this.hideSpinner();
					this.showButton();
				}

			} catch(e) {
				this.showException(response, e);
			}
		},
		showException: function(originalRequest, exception) {
			console.log("%s", exception);
		},
		hideMessage : function(message_type) {
			$('#'+message_type).hide();
			clearTimeout(this.options.disapearTimerId);
		},
		showFieldsError : function(errors) {
			$('.form_error_message ').hide();
			for (a in errors) {
				eval('str=errors.'+a);
				obj = document.getElementById(a+'_err');
				if (obj != undefined) {
					obj.innerHTML = str;
					obj.style.display = 'block';
				}
			}
		},
		showMessage : function(message_type, message) {
			if (message && message != '') {
				$('#'+message_type).html('<span>'+message+'</span>');
				$('#'+message_type).show();
				this.options.disapearTimerId = setTimeout(function() {postAjaxP.prototype.hideMessage(message_type);},4000);
			}
		},
		showSpinner : function() {
			if (this.options.spinner_id) {
				if (this.options.showGrayOut) {
				$('#'+this.options.spinner_id).css({
  						left:   ($(window).width() - $('#'+this.options.spinner_id).width()) / 2
					}).show();
				} else {
					$('#'+this.options.spinner_id).show();
				}
;
			}
		},
		hideSpinner : function() {
			if (this.options.spinner_id) {
				$('#'+this.options.spinner_id).hide();
			}
		},
		hideButton : function() {
			if (this.options.button) {
				$(this.options.button).hide();
			}
		},
		showButton : function() {
			if (this.options.button) {
				$(this.options.button).show();
			}
		},
		grayOut: function (vis, options) {
		     var options = options || {};
		     var zindex = options.zindex || 50;
		     var opacity = options.opacity || 5;
		     var opaque = (opacity / 100);
		     var bgcolor = options.bgcolor || '#000000';
		     var dark=document.getElementById('darkenScreenObject');
		    if (!dark)
		    {
		     // The dark layer doesn't exist, it's never been created.  So we'll
		     // create it here and apply some basic styles.
		     var tbody = document.getElementsByTagName("body")[0];
		     var tnode = document.createElement('div');
		     tnode.style.position='absolute';
		     tnode.style.top='0px';
		     tnode.style.left='0px';
		     tnode.style.overflow='hidden';
		     tnode.style.display='none';
		     tnode.id='darkenScreenObject';
		     tbody.appendChild(tnode);
		     dark=document.getElementById('darkenScreenObject');
		     }

		    if (vis)
		     {
		      var pageWidth='100%';
		      var pageHeight='100%';
		     dark.style.opacity=opaque;
		     dark.style.MozOpacity=opaque;
		     dark.style.filter='alpha(opacity='+opacity+')';
		     dark.style.zIndex=zindex;
		     dark.style.backgroundColor=bgcolor;
		     dark.style.width= pageWidth;
		     dark.style.height= pageHeight;
		     dark.style.display='block';
		       }
		     else
		     {
		      dark.style.display='none';
		     }
		 }
	}

})(jQuery);

