var expecting_callback = null;
(function($) {
	var self = this;
	self._is_authorized = false;
	$.fn.authBind = function(type, handler) { return $._auth_bind.init(this, type, handler); }
	
	$._auth_bind = {
		init: function(obj, type, handler) {
			obj = obj instanceof jQuery ? obj : $(obj);
			obj.bind(type, function(event) {
				var _el = $(this);
				if (self._is_authorized) {
					handler.call(_el, event);
				} else {
					event.stopImmediatePropagation();
					event.preventDefault();
					var callback = (function(object) {
						return function() {
							self._is_authorized = true;
							var e = new jQuery.Event(type);
							object.trigger(e);
						}
					})(_el);
					$.getJSON(
						config_wwwdomain + "/ajax/userservice/?cmd=islogged",
						function(data) {
							if (data.status == 1) {
								callback();
							}
							else {
								expecting_callback = callback;
								$(document).modalbox();
								$(document).modalbox('open',{ajax_url:'/user/login/?silent=1',popup_class: 'popup enter_site'});
							}
						}
					);
				}
			});
			return obj;
		}
	}		
})(jQuery);
