/**
 * @author russells
 */
	
	function toProperCase(s) {
		return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
	}	
	
		ApplicationState = function(stateData, bookmarkValue){
			this.stateData = stateData;
			this.changeUrl = stateData + ":" + bookmarkValue;
		}
	
		dojo.extend(ApplicationState, {
			back: function(){
				displayAction (this.changeUrl, this.stateData);
			},
			forward: function(){
				displayAction (this.changeUrl, this.stateData);
			}
		});

		function goNav(id, data){
			var appState = new ApplicationState(data, id);
			dojo.back.addToHistory(appState);
			displayAction (id, data);
		}
		
		function displayAction (item, action) {

			var path = "";
			var admin = false;
			
			item = item.replace(/.*\:/, "");
			
			// use the url to determine if we are in admin mode
			if (window.location.pathname.search(/\/admin\//) != -1) {
				path = "/admin"
				
				// esnure some specific sections are nulled
				dojo.byId('status') ? dojo.byId('status').innerHTML = "" : false;
				dijit.byId('content') ? dijit.byId('content').attr('content', '') : false;
				dojo.byId('template_content') ? dojo.byId('template_content').innerHTML = "" : false;
					
			    

			}
			
			dojo.xhrGet ( {
				//url: path + '/operations.php?action=' + item + '&mode=' + action,
				url: path + '/apps/' + action + '/' + item,
				handleAs: 'json',
				timeout: 10000,
				load: function (data) {

						// loop round the object and determine if the element exists, if it
						// does then set the contents
						for (elementId in data) {
							if (dojo.byId(elementId)) {
								
								// set the inner content based on the element type
								if (dojo.byId(elementId).tagName == 'img') {
									dojo.byId(elementId).src = data[elementId];
								} else if (elementId == 'content' || elementId == 'leftside_options' || elementId == 'centre') {
									
									// check that any registered widgets are destroyed first
									
									if (dijit.byId('mainTabContainer')) {
										dijit.byId('mainTabContainer').destroy();
									}
									
									if (dijit.byId('articleform')) {
										dijit.byId('articleform').destroy();
									}
									
									if (dijit.byId('articleform')) {
										dijit.byId('articleform').destroy();
									}
									
									if (dijit.byId('startdate')) {
										dijit.byId('startdate').destroy();
									}
									
									if (dijit.byId('expirydate')) {
										dijit.byId('expirydate').destroy();
									}
									
									
									dijit.byId(elementId).attr('content', data[elementId]);
								} else {
									dojo.byId(elementId).innerHTML = data[elementId];
								}
							}
						}
						
						// determine if there is any custom script to run
						if (data.script) {
							eval (data.script);
						}
						
						// set the page title if it is exists int he return
						data.pagetitle ? document.title = data.pagetitle : false;
						
						if (item == "category") {
							toggle(action);
						}
						
						if (admin) {				
							// set the document title
							if (action == 'navigation') action = "page";
							document.title = "Admin Console : " + toProperCase(action) + " : " + toProperCase(item);
						}
					}
				});
				
		}
		
		dojo.require("dojo.back");

		dojo.addOnLoad(function(){
			
			// determine if there is bookmarking information
			if (location.hash) {
				id = location.hash.replace(/\:.*/, "").substr(1);
				data = location.hash.replace(/.*\:/, "");
				
				displayAction(data, id);
				
			} else {
				data = "home";
				id = "";
			}
			
			var appState = new ApplicationState(data, id);
			dojo.back.setInitialState(appState);
		});
 