var Spool = {
	
	baseUrl: null,						//> reference to the base url of the application
	javascriptBase: null,				//> reference to the base url of this file
	
	sessionTimerId: null,				//> timer reference for session timer
	sessionTimerInterval: 60000,		//> how often to check how much time is left in the session
	sessionTimerAlertThreshold: 120,	//> within how much time to alert the user of a session that's about to expire
	
	/*
		Any initial setup stuff goes here.
		Think of this as the constructor.
	*/
	init: function() {
		//> find the right value for Spool.javascriptBase, for use with the require function below
		var js = /\/spool\.js/;
		$$('head script[src]').findAll(function(s) {
			return s.src.match(js);
		}).each(function(s) {
			Spool.javascriptBase = s.src.replace(js, '');
		});
		
		//> call Spool.setup() if it has been defined
		document.observe("dom:loaded", function() {
			if(Spool.setup && typeof Spool.setup == "function") {
				Spool.setup();
			}
		});
		
		Spool.require("ext/array.js");
	},
	
	/*
		Include a JS file. Path is relative to the location of this file.
	*/
	require: function(path, callback) {
		// document.write('<script type="text/javascript" src="'+Spool.javascriptBase+'/'+path+'"><\/script>');
		
		//> Generate a script tag dynamically
		var s = document.createElement('script');
		s.type = "text/javascript";
		s.src = Spool.javascriptBase+'/'+path;
		
		//> append script tag to head element
		var head = document.getElementsByTagName("head")[0];
		head.appendChild(s);
		
		if(callback && typeof callback == "function") {
			s.onload = callback;
		}
	},
	
	/*
		Include a CSS file. Path is relative to the location of this file.
	*/
	requireCSS: function(path) {
		// <link href="" rel="stylesheet" type="text/css" />
		
		//> Generate a script tag dynamically
		var s = document.createElement('link');
		s.rel = "stylesheet";
		s.type = "text/css";
		s.href = Spool.javascriptBase+'/'+path;
		
		//> append script tag to head element
		var head = document.getElementsByTagName("head")[0];
		head.appendChild(s);
	},
	
	/*
		Set the base url for the site (eg http://localhost/project or http://project.com)
		This will typically be called via an onload event in the body tag
	*/
	setBaseUrl: function(url) {
		Spool.baseUrl = url;
	},
	
	/*
		Initialize Spool's click tracking functionality
	*/
	initClickTracking: function() {
		Spool.require("spool/clicks.js");
	},
	
	/*
		Use a confirm dialog to warn the user of a destructive action
	*/
	confirmNav: function(url, message) {
		if(confirm(message || "Are you sure?")) {
			if(url != "" && url != "undefined" && url != null) {
				window.location = url;
			}
			else {
				return true;
			}
		}
	},
	
	/*
		Start the timer that hold the amount of time left in the session
	*/
	startSessionTimer: function() {
		Spool.sessionTimerId = setInterval("Spool.checkSessionTimer()", Spool.sessionTimerInterval);
	},
	
	/*
		Refresh the session timer
	*/
	checkSessionTimer: function() {
		new Ajax.Request(Spool.baseUrl + "/ajax/checkSessionTime", {
			method: 'post',
			onSuccess: function(transport) {
				var response = transport.responseText;
				if(response <= Spool.sessionTimerAlertThreshold && !$('alert')) {
					Spool.popup("alert", Spool.baseUrl + "/ajax/sessionAlert");
				}
				else if(response < 0) {
					
				}
			}
		});
	},
	
	/*
		Clear the session timer
	*/
	clearSessionTimer: function() {
		clearInterval(Spool.sessionTimerId);
	},
	
	/*
		Extend the session by making a bogus Ajax call
	*/
	extendSession: function() {
		new Ajax.Request(Spool.baseUrl + "/ajax/nothing", {
			method: 'get'
		});

		Spool.closePopup('alert');
	},
	
	/*
		Given a string of XML data, return a real XML document
	*/
	parseXmlString: function(xmlString) {
		var parser;
		var xmlDoc;
		try {
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(xmlString);
		}
		catch(e) {
			try {
				parser = new DOMParser();
				xmlDoc = parser.parseFromString(xmlString, "text/xml");
			}
			catch(e) {
				alert(e.message);
				return false;
			}
		}
	
		return xmlDoc;
	},
	
	popup: function(targetDiv, file, callBack) {
		Spool.setOpac();
	
		new Ajax.Request(file, {
			method: 'get',
			onSuccess: function(transport) { 
				// $('loading-indicator').style.display = "none";
				if(transport.responseText.length == 0) {
					return;
				}
			
				var scrollX = document.viewport.getScrollOffsets().left;
				var scrollY = document.viewport.getScrollOffsets().top;
			
				var element = document.createElement('div');
				element.setAttribute("id", targetDiv);
				element.innerHTML = transport.responseText;
			
				WIDTH = "100%";
				element.style.width = WIDTH;
				element.style.position = "absolute";
				element.style.left = (scrollX) + "px";
				element.style.top = (scrollY + 100) + "px";
				element.style.zIndex = 1000;
				element.style.margin = "0 auto";
			
				document.body.appendChild(element);
			
				if(callBack) {
					callBack();
				}
			}
		});
	},
	popupText: function(targetDiv, text) {
		Spool.setOpac();
		var scrollX = document.viewport.getScrollOffsets().left;
		var scrollY = document.viewport.getScrollOffsets().top;
		
		var element = document.createElement('div');
		element.setAttribute("id", targetDiv);
		element.innerHTML = text;
		
		WIDTH = "100%";
		element.style.width = WIDTH;
		element.style.position = "absolute";
		element.style.left = (scrollX) + "px";
		element.style.top = (scrollY + 100) + "px";
		element.style.zIndex = 1000;
		element.style.margin = "0 auto";
	
		document.body.appendChild(element);
	},
	closePopup: function(divName) {
		var div = $(divName);
		if(div) {
			div.parentNode.removeChild(div);
			Spool.restoreOpac();
		}
	},
	
	opacDiv: null,
	setOpac: function() {
		var scrollX = document.viewport.getScrollOffsets().left;
		var scrollY = document.viewport.getScrollOffsets().top;
		
		opacDiv = document.createElement('div');
		opacDiv.style.position = "absolute";
		opacDiv.style.left = "0px";
		opacDiv.style.top = "0px";
		opacDiv.style.width = document.body.clientWidth + scrollX + "px";
		opacDiv.style.height = document.body.clientHeight + scrollY + "px";
		opacDiv.style.backgroundColor = "#000000";
		opacDiv.style.zIndex = "999";
		opacDiv.style.opacity = 0;
		opacDiv.style.mozOpacity = 0;
		document.body.appendChild(opacDiv);
		
		$(opacDiv).morph('opacity:.5; -moz-opacity: .5', { duration: .3 });
		
		window.onresize = function() {
			opacDiv.style.width = document.body.clientWidth + document.viewport.getScrollOffsets().left + "px";
			opacDiv.style.height = document.body.clientHeight + document.viewport.getScrollOffsets().top + "px";
		};
	},
	restoreOpac: function() {
		opacDiv.parentNode.removeChild(opacDiv);
		window.onresize = null;
	}
	
};

Spool.init();
