
AjaxFramework = Class.create({

	initialize: function() {
	},

	/*
	loadContent - loads an ajax request according to the specified options
	DomIdToLoadIn - the DOM id of the element to load the content into
	UrlToLoad - the url of the ajax request to use
	LoadingMessage - loading message to display
	*/
	loadContent: function(options) {
		// Add a random string to try to ensure against browser caching responses for a given request
		var hideSideBar = options.HideSideBar || false;
		if (options.LoadingMessage)
			$(options.DomIdToLoadIn).update(options.LoadingMessage);

		var loadContentResponseHandler = function(transport) {
			if (options.onSuccess && (options.onSuccess != null)) {
				options.onSuccess(transport);
			}
			var res = transport.responseJSON;
			var replaceContent = true;
			if (res != null && res.Status != null) {
				switch (res.Status) {
					case "PermissionDenied":
						replaceContent = false;
						if (res.Messsage != null)
							message = res.Message;
						else
							message = "Permission Denied";
						break;
				}
			}
			if (replaceContent)
				$(options.DomIdToLoadIn).update(transport.responseText);
		};

		new Ajax.Request(options.UrlToLoad, { method: 'get', evalScripts: true, onSuccess: function(transport) { loadContentResponseHandler(transport); } });
	},



	processRequest: function(options) {
		// Add a random string to try to ensure against browser caching responses for a given request
		var cacheKiller = "cachekiller=" + (new Date()).getTime();
		if (options.UrlToLoad.indexOf('?') > 0)
			options.UrlToLoad += "&" + cacheKiller;
		else
			options.UrlToLoad += "?" + cacheKiller;

		new Ajax.Request(options.UrlToLoad, { evalScripts: true, onComplete: options.onComplete, parameters: options.parameters });
	},

	/*
	loadLeftNavMenu - Loads the left nav menu
	*/
	loadLeftNavMenu: function(options) {
		options = options || {};
		var url = "/includes/leftNavMenu.php";
		var loadingMessage = "Loading menu..";
		if (options.LoadingMessage) {
			loadingMessage = options.LoadingMessage;
		}
		AF.loadContent({ DomIdToLoadIn: AjaxFramework.DomIDs.LeftNavMenu , UrlToLoad: url, LoadingMessage: loadingMessage, onSuccess: options.onSuccess });
	},

	/*
	loadTopBlogPosts - Loads the top two blog posts and puts them in the left nav
	*/
	loadTopBlogPosts: function(options) {
		options = options || {};
		var url = "/blog/topPosts.php";
		var loadingMessage = "Loading posts..";
		if (options.LoadingMessage) {
			loadingMessage = options.LoadingMessage;
		}
		AF.loadContent({ DomIdToLoadIn: AjaxFramework.DomIDs.TopBlogPostsList , UrlToLoad: url, LoadingMessage: loadingMessage, onSuccess: options.onSuccess });
	},


	// Gets the key code for the event, browser-specifically 
	getKeyCode: function(event) {
		event = event || {};

		// SEE http://unixpapa.com/js/key.html for information on browser handling of keystrokes!! 
		//  THE ORDER OF THE IF-BRANCHES BELOW MAKES A DIFFERENCE!! 
		if ((event.keyCode != null) && (event.keyCode > 0))
			return event.keyCode;
		// We must look in .charCode before .which so that in 'keypress' events we get the ASCII code in 
		//  Mozilla browsers instead of the Mozilla keycode that's in .which 
		else if ((event.charCode != null) && (event.charCode > 0))
			return event.charCode;
		else if ((event.which != null) && (event.which > 0))
			return event.which;
		else
			return 0;
	},


	bogus: function() { }


});


AjaxFramework.DomIDs = {};
AjaxFramework.DomIDs.TopBlogPostsList = "TopBlogPostsList";
AjaxFramework.DomIDs.LeftNavMenu = "LeftNavMenu";

AF = new AjaxFramework();
