var rootNameSpace = this;
System = (typeof System =='undefined')?(new (function system(){
    var importedModules={};
    var modulesPaths={};
    
	var uuidCounter = {
		value:0,
	    datePrefix:new Date()
	};
	
	var mixIn=function(target,source){
		for (var x in source) {
			target[x]=source[x];
		}
	};
	
    var getNameSpace = function(targetName,namespace){
		namespace = namespace || rootNameSpace;
		nameParts = targetName.split(".");
		// Set each part of a dotted name
		for(var j = 0; j < nameParts.length; j++) {
		    name = nameParts[j];
		
			if(j < nameParts.length - 1) {
			    if(typeof(namespace[name]) == "undefined" || namespace[name] == null) {
					namespace[name] = {};
			    }
				// Iterate through the destination namespace
		        namespace = namespace[name];
		    }
		}
		return namespace[name];
	};
	
    this.hashKeys = function(hash){
    	var y = [];
    	for(var x in hash) {
    		y.push(x);
    	} 
    	return y;
    };
    
	this.getGUID = function (){
		//this is probably good enough; 
		//As long as script never becomes multi-threaded;
		return 'autoId'+(uuidCounter.datePrefix.getTime())+'.'+(++uuidCounter.value);
	};
    
	this.fuse=mixIn;
	
	this.getCoords=function(elem){
		if (elem.getBoundingClientRect){
			this.getCoords=function(elem){
				var box = elem.getBoundingClientRect();
				return {
					x:box.left,
					y:box.top,
					width:box.width,
					height:box.height
				}
			}
		} else if (document.getBoxObjectFor){
			this.getCoords=function(elem){
				return document.getBoxObjectFor(elem);
			}
		} else {
			this.getCoords= function getCoords (element) {
				var coords = { x: 0,
						y: 0, 
						width: element.offsetWidth, 
						height:	element.offsetHeight };
				while (element) {
					coords.x += element.offsetLeft;
					coords.y += element.offsetTop;
					element = element.offsetParent;
				}
				return coords;
			}
		}
		return this.getCoords(elem);
		
	};
	
	this.XMLHttpRequest = (typeof XMLHttpRequest != 'undefined')?
		function(){ return new XMLHttpRequest();}:
		function(){ return new ActiveXObject("Msxml2.XMLHTTP.4.0");
	};
	this.sendAsyncRequest=function(url,data,callback,callbackObject,contentType){
		callback = typeof(callback) == "undefined" ? null : callback;
		var requestType=(data)?"post":"get";
		data = typeof(data) == "undefined" ? null : data;
		contentType = typeof(contentType) == "undefined" ? ((data)?"application/x-www-form-urlencoded":null) : contentType;
		
		var content ='';
		for(var x in data){
				content+=x+'='+encodeURIComponent(data[x])+'&'
		}
		if(content == ''){
			content = data;
		}
		var request =this.XMLHttpRequest();
		
		var stateChangeHandler = function() {
			if (request.readyState == 4) {
				if (callback) {
					callback.call(callbackObject,request, url); 
				}
				
				if(request.close){
					request.close();
				}

				request = null;
				callback=null;
				callbackObject=null;
				url=null;
			}
		};
		
		request.onreadystatechange = stateChangeHandler;
			
		request.open(requestType, url, true);
		if (contentType) {
			request.setRequestHeader("Content-Type", contentType);
		}
		request.setRequestHeader("If-Modified-Since", "Mon, 1 Jan 1900 01:01:01 GMT");
		request.send(content);
	};
	var staticUrlTextRequest =null;
	
	this.getUrlText=function(url){
		
		if(!staticUrlTextRequest){
			staticUrlTextRequest=this.XMLHttpRequest();
		}
		staticUrlTextRequest.open('get', url, false);
		staticUrlTextRequest.send(url);
		return staticUrlTextRequest.responseText;
	};
	this.getUrlJs=function(url){
		
		try {
			eval(this.getUrlText(url))
		} catch(e){
			//Don't know what to do here;
			//debugger;
		}
	};
	this.getUrlXml=function(url){
		this.importModule('/cfapps/common/js/xml/sarissa');
		this.importModule('/cfapps/common/js/xml/sarissa_ieemu_xpath');
		this.importModule('/cfapps/common/js/xml/sarissa_extensions');
		try {
			return (new DOMParser()).parseFromString(this.getUrlText(url), "text/xml");
		} catch(e){
			//Don't know what to do here;
			//debugger;
		}
	};
	
	this.declareModuleObject =getNameSpace;
	
	this.registerModulePath = function(mod,path){
		modulesPaths[mod]=path;
	};
	this.importModule = function(moduleName){
		if(importedModules[moduleName]) {
			return;
		}
		importedModules[moduleName]=true;
		// Try loading the module from each of the include paths
	    var modulePathName = moduleName.replace(/\./g, "/");
	    var syms = moduleName.split(".");
		for(var i = syms.length; i>0; i--){
			var parentModule = syms.slice(0, i).join(".");
			if((i==1) && !modulesPaths[parentModule]){		
				// Support default module directory (sibling of) for top-level modules 
				syms[0] = "../" + syms[0];
			}else{
				var parentModulePath = modulesPaths[parentModule];
				if(parentModulePath&&parentModulePath != parentModule){
					syms.splice(0, i, parentModulePath);
					modulePathName = syms.join('.').replace(/\./g, "/");
					break;
				}
			}
		} 
		this.getUrlJs(modulePathName+'.js');
	};
	
	this.createObject=this.defineObject=function(name,sup,source){
		var names =name.split(".");
		var thisName =names.pop();
		var pacKage = getNameSpace(names.join('.'));
		var o=pacKage[thisName]=function(){
			var that = this;
			this.superConstructor=function(){
				sup.apply(that,arguments);
			}
			if(source && source.initializer){
				source.initializer.apply(this,arguments);
			} else if (sup){
				sup.apply(that,arguments);
			}
		}
		var supPro
		if(sup && sup.prototype){
			supPro=	sup.prototype;
		}
		o.prototype.superclass=supPro;
		o.superclass=supPro;
		mixIn(o.prototype,supPro);
		mixIn(o.prototype,source);
	}
	//Giant hack to make this backwards compatible with dojo.
	if(typeof dojo != 'undefined' && dojo.lang && dojo.lang.declare){
		this.registerModulePath = dojo.registerModulePath;
		this.declareModuleObject = dojo.provide;
		this.importModule = dojo.require;
		//Make sure that the new way to superConstructor is available.
		this.createObject=this.defineObject = function(){
			mixIn(arguments[2],{
				superConstructor:function(){
					this.constructor.superclass.initializer.apply(this,arguments);
				}
			});
			dojo.declare.apply(this,arguments);
		}
		this.registerModulePath( "org.piercecountywa.common", "/cfapps/common/js/common" );
		this.registerModulePath( "org.piercecountywa.dojo.extension", "/cfapps/common/dojo/extension" );
		this.registerModulePath( "org.piercecountywa.isdb.timeTrak", "/cfapps/isdb/timeTrak/js" );
		this.registerModulePath( "org.piercecountywa.isdb.projectTrak", "/cfapps/isdb/projectTrak/js" );
		this.registerModulePath( "org.piercecountywa.isdb.common", "/cfapps/isdb/common/js" );
		this.registerModulePath( "org.piercecountywa.charm.request", "/cfapps/charm/request/js" );
	}
})()):System;
System.declareModuleObject('org.piercecountywa.common.System');
org.piercecountywa.common.System = System;

System.registerModulePath( "org.piercecountywa.common", "/cfapps/common/js/common" );
System.registerModulePath( "org.piercecountywa.dojo.extension", "/cfapps/common/dojo/extension" );
System.registerModulePath( "org.piercecountywa.isdb.timeTrak", "/cfapps/isdb/timeTrak/js" );
System.registerModulePath( "org.piercecountywa.isdb.projectTrak", "/cfapps/isdb/projectTrak/js" );
System.registerModulePath( "org.piercecountywa.isdb.common", "/cfapps/isdb/common/js" );
System.registerModulePath( "org.piercecountywa.charm.request", "/cfapps/charm/request/js" );

System.importModule("org.piercecountywa.common.event.ObjectConnecter");
System.importModule("org.piercecountywa.common.event.ObjectPublisher");
System.importModule("org.piercecountywa.common.html.HTMLElement");

//Value add hacks:please enjoy.
//Please don't extend Object or Array. Ever...
//Should be put into String.js Number.js and Date.js and be included.

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

Number.prototype.zf=function(num) {
	var temp = "0000000"+this.toString();
	return temp.substring(temp.length-num);
}
Date.prototype._dayNames=[
	'Sunday',
	'Monday',
	'Tuesday',
	'Wednesday',
	'Thursday',
	'Friday',
	'Saturday']
		
Date.prototype._monthNames=[
	'January',
	'February',
	'March',
	'April',
	'May',
	'June',
	'July',
	'August',
	'September',
	'October',
	'November',
	'December']
Date.prototype.format = function(f) {
    if (!this.valueOf())
        return '';

    var d = this;

    return f.replace(/(yyyy|yy|MMMM|MMM|MM|dddd|ddd|dd|hh|mm|ss|a)/gi,
        function($1)
        {
        	switch ($1)
            {
	            case 'yyyy': return d.getFullYear();
	            case 'yy': return d.getFullYear().toString().substr(2, 2);
	            case 'MMMM': return d._monthNames[d.getMonth()];
	            case 'MMM': return d._monthNames[d.getMonth()].substr(0, 3);
	            case 'MM': return (d.getMonth() + 1).zf(2);
	            case 'dddd': return d._dayNames[d.getDay()];
	            case 'ddd': return d._dayNames[d.getDay()].substr(0, 3);
	            case 'dd': return d.getDate().zf(2);
	            case 'hh': return ((h = d.getHours() % 12) ? h : 12).zf(2);
	            case 'mm': return d.getMinutes().zf(2);
	            case 'ss': return d.getSeconds().zf(2);
	            case 'a': return d.getHours() < 12 ? 'AM' : 'PM';
            }
        }
    );
}

//Back space hack;
var globalBackSpaceInteval = 2000;
var globalBackSpaceTime = new Date((new Date()).getTime-globalBackSpaceInteval); 


var readyMe =function (){
	if(document.readyState == 'complete'){
		new ObjectConnecter(document.body,"onkeydown",window,"logBackspace"); 
	}
}

var confirmUnload =function (e){
		e = e || window.event;
		//if backspace was used within the interval they probably are unloading the page with the backspace
		var dt = new Date();
		if(globalBackSpaceTime.getTime() > new Date().getTime()-globalBackSpaceInteval){
			//confirm this is the users intention
			//if(!confirm("Are you sure you want to go Back?")){
				//cancel unload if user changes their mind
				if(e){
					e.cancelBubble = true;
					e.returnValue = ''; 
					//return false; 
				}
			//}
		}
}

var logBackspace = function (e){
		e = e || window.event;
		if(e.keyCode==8) 
		{
			//log last time backspace was used
			globalBackSpaceTime = new Date();
		}
}

new ObjectConnecter(window,"onbeforeunload",this,"confirmUnload");
new ObjectConnecter(document,"onreadystatechange",window,"readyMe");


