BF_Request = function() {
	
	this._redirect = function(url) {
		document.location.href = url;
	}
	
	this.request = function(url, args) {
		
		// define response call
		var success = args.onSuccess;
		var resp = function(resp) {			
			var json = resp.getResponseHeader('X-json');
			var obj = null;
			if(json) {
				obj = eval('(' + resp.responseText + ')');			
			}
			var ajax_redir = resp.getHeader('X-ajaxlocation');
			if(ajax_redir) {
				if(! ajax_redir.match(/https?:\/\//)) {
					ajax_redir = BF_STATIC.web_root + ajax_redir;
				}
				//console.log("Ajax Redirect set to: %s", ajax_redir);
				this._redirect(ajax_redir);
			} else {
				success(resp, obj);
			}
		}.bind(this)
		args.onSuccess = resp;
		// bad auth cookie, delete cookies, and take them to login page
		if (!args.on401) args.on401 = function() {
			// be sure to delete user cookies
			var user = new BF_User();
			user.logout({noreload: true});
			// send them to login page
			document.location.href = BF_STATIC.web_root + '/signin?badauth=true'
		}
		
		// no failure define, used default
		if(! args.onFailure) {
			args.onFailure = function(resp) {
				alert('Sorry, but we are unable to process your request at this time.');
			}
		}

		// set session key if auth
		if(args.bf_auth && ! ( args.parameters && args.parameters.session_key ) ) {
			args.parameters.session_key = BFW_Util.getCookie(BFW_COOKIE);
		}
		if ( args.while_processing) {
			if ( args.while_processing.show ) args.while_processing.show = $(args.while_processing.show);
			if ( args.while_processing.hide ) args.while_processing.hide = $(args.while_processing.hide);
			if (args.while_processing.show) {args.while_processing.show.show();args.while_processing.show.removeClassName('hidden')}
			if (args.while_processing.hide) {args.while_processing.hide.hide();args.while_processing.hide.addClassName('hidden')}
			var _onSuccess = args.onSuccess;
			args.onSuccess = function(a,b){
				if (args.while_processing.show) {
					args.while_processing.show.hide();
					args.while_processing.show.addClassName('hidden');
				}
				if (args.while_processing.hide) {
					args.while_processing.hide.show();
					args.while_processing.hide.removeClassName('hidden');
				}
				_onSuccess(a,b);
			}
			var _onFailure = args.onFailure;
			args.onFailure = function(a,b) {
				if (args.while_processing.show) {
					args.while_processing.show.hide();
					args.while_processing.show.addClassName('hidden');
				}
				if (args.while_processing.hide) {
					args.while_processing.hide.show();
					args.while_processing.hide.removeClassName('hidden');
				}
				_onFailure(a,b);
			}
			var _on401 = args.on401;
			args.on401 = function(a,b) {
				if (args.while_processing.show) {
					args.while_processing.show.hide();
					args.while_processing.show.addClassName('hidden');
				}
				if (args.while_processing.hide) {
					args.while_processing.hide.show();
					args.while_processing.hide.removeClassName('hidden');
				}
				_on401(a,b);
			}
		}
		
		// make ajax call
		new Ajax.Request(url, args);
	}
}
