var displayError = function( displayableTitle, displayableText ) {
	alert( displayableTitle + '\n\n' + displayableText );
};

var STATECRAFT = function ( ) {
	var mapCurrentSelection;
	
	var mapChangeSelection = function( pageElement ) {
		if( mapCurrentSelection ) {
			mapCurrentSelection.style.backgroundColor = '#FFFFFF';
		}
		
		pageElement.style.backgroundColor = pageElement.style.borderColor;
		mapCurrentSelection = pageElement;
	};
	
	/*var mapShowContextMenu = function( ) {
		var click = function (e) {
			if( document.all ) {
				if( event.button === 2 ) {
					return false;
				}
			}
			if (document.layers) {
				if (e.which === 3) {
					return false;
				}
			}
		}
		
		if( document.layers ) {
			document.captureEvents(Event.MOUSEDOWN);
		}
		document.onMouseDown=click;
	}*/
	
	var xmlHttp;
	
	var ajaxQueue = new Array();
	var continentID;
	var ajaxIsActive = false;
	var readyStateCatcher, ajaxNext, ajaxMonitorUpdatesForContinentID, ajaxQueuePostableDataForURL, currentRequestIsHighPriority, ajaxInterval, ajaxSmartDelay;

	var xmlHttpObject = function() {
		if( window.XMLHttpRequest ){ // Firefox, Safari, Opera...
			return new XMLHttpRequest();
		}
		else if( window.ActiveXObject ) { // Internet Explorer 5+
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		else {
			alert('Statecraft cannot function properly because there was a problem creating the XMLHttpRequest object. You might need to use a newer web browser.');
			return false;
		}
	};
	
	readyStateCatcher = function() {
		if( xmlHttp.readyState === 4 || xmlHttp.readyState === "complete" ) {
			ajaxIsActive = false;
			try {
				//alert( 'Got response text…' );
				if( xmlHttp.status === 200 || xmlHttp.status === 201 || xmlHttp.status === 204 || xmlHttp.status === 304 ) {
					//alert( 'Trying to evaluate…' );
					eval( xmlHttp.responseText );
				}
				ajaxNext( );
			}
			catch( evalError ) {
				reportError( 'JavaScript Eval:\n' + xmlHttp.responseText + '\n\nError during eval: ' + evalError.message, evalError.location, evalError.line );
				ajaxNext( );
			}
		}
	};
	
	var initializeMyXmlHttpObject = function() {
		xmlHttp = xmlHttpObject();
		xmlHttp.onreadystatechange = readyStateCatcher;
	};
	
	initializeMyXmlHttpObject();
	
	ajaxNext = function( ) {
		if( !ajaxIsActive ) {
			ajaxIsActive = true;
			if( ajaxQueue.length > 0 ) {
				currentRequestIsHighPriority = true;
				var nextAjaxRequest = ajaxQueue.shift( );
				
				xmlHttp.open( "POST", nextAjaxRequest[ 'destinationURL' ], true );
				xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlEncoded' );
				
				xmlHttp.send( nextAjaxRequest[ 'dataToPost' ] );
			}
			else if( continentID !== null ) {
				currentRequestIsHighPriority = false;
				xmlHttp.open( "POST", "updater.php5?continentid=" + continentID, true );
				xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlEncoded' );
				
				xmlHttp.send( '' );
			}
			else {
				ajaxIsActive = false;
			}
			ajaxSmartDelay = ajaxSmartDelay + 500;
			if( ajaxInterval ) {
				clearInterval( ajaxInterval );
			}
			ajaxInterval = setInterval( 'STATECRAFT.AJAX.doNext( );', ajaxSmartDelay );
		}
	};
	
	ajaxMonitorUpdatesForContinentID = function (newContinentID) {
		continentID = newContinentID;
		ajaxSmartDelay = 1000;
		ajaxNext( );
		if( !ajaxInterval ) {
			ajaxInterval = setInterval( 'STATECRAFT.AJAX.doNext( );', ajaxSmartDelay );
		}
	};
	
	ajaxQueuePostableDataForURL = function (newDataToPost, newDestinationURL) {
		if( ajaxInterval ) {
			clearInterval( ajaxInterval );
		}
		
		ajaxQueue.push( { dataToPost : newDataToPost, destinationURL: newDestinationURL } );
		
		/*if( currentRequestIsHighPriority !== true && xmlHttp.readyState !== 3 ) {
			if( ajaxIsActive === true ) {
				//alert( 'shouldAbort' );
				xmlHttp.onreadystatechange = function () {};
				xmlHttp.abort();
				//xmlHttp.close;
				//alert( 'doneAborting' );
				delete xmlHttp;
				initializeMyXmlHttpObject();
			}
			ajaxIsActive = true;
			currentRequestIsHighPriority = true;
			
			var nextAjaxRequest = ajaxQueue.shift( );
			//alert( 'nextRequest' );
			
			xmlHttp.open( "POST", nextAjaxRequest[ 'destinationURL' ], true );
			xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlEncoded' );
			
			xmlHttp.send( nextAjaxRequest[ 'dataToPost' ] );
		}*/
		
		ajaxSmartDelay = 1000;
		ajaxInterval = setInterval( 'STATECRAFT.AJAX.doNext( );', ajaxSmartDelay );
	};
	
	return {
		MAP : {
			changeSelection : mapChangeSelection
		},
		
		AJAX : {
			monitorUpdatesForContinentID : ajaxMonitorUpdatesForContinentID,
			queuePostableDataForURL : ajaxQueuePostableDataForURL,
			doNext : ajaxNext
		}
	};
}();