/*********************************
Javascript Document
©2008 Ukoh William @ http://www.williamukoh.com. All rights reserved. Forget it, I will sue!!!
Scope: Global

************
META-DATA
************
@ date created :: 30 May 2008
@ last modified :: 2 June 2008
@ description :: Contains client-side logic for all pages :) requires jQuery javascript library
*/
var com;

if(!com)
	com = {};
else if(typeof com != "object")
	throw new Error("Variable {com} is defined, but it isn't of type object");

if(!com.williamukoh)
	com.williamukoh = {};
else if(typeof com.williamukoh != "object")
	throw new Error("Namespace {com.williamukoh} is already defined, but it isn't of type object");

if(com.williamukoh.Core)
	throw new Error("Module {com.williamukoh.Core} is already defined");


com.williamukoh.Core = {
	
	/***********
	init()
	************/
	init:function(){
		
	},
	
	/********************************
	loader object [returns an object]
	********************************/
	loader:function () {
	  
	  var pending = null;
	  var queue = [];
	  var _urls = [];
	  
	  
	  return {
		
		xhrObject:null,

		loadScript: function (urls, callback, obj, scope) {
		  
			 // _urls.push(urls);
			  var core = com.williamukoh.Core;
			  var request = {urls: urls, callback: callback, obj: obj, scope: scope};
		
			  // If a previous load request is currently in progress, we'll wait our
			  // turn.
			  if (pending) {
				queue.push(request);
				return;
			  }
		
			  pending = request;
				
			  // Cast urls to an Array.
			  urls = urls.constructor === Array ? urls : [urls];
			  
			  // Load the scripts at the specified URLs.
			  var script;
		
			  for (var i = 0; i < urls.length; i += 1) {

				script = document.createElement('script');
				script.src = urls[i];
				document.body.appendChild(script);
			  }
			  
			  if (!script) {
				return;
			  }
		
			  if ((/msie/i).test(navigator.userAgent) &&
				  !(/AppleWebKit\/([^ ]*)/).test(navigator.userAgent) &&
				  !(/opera/i).test(navigator.userAgent)) {

				// If this is IE, watch the last script's ready state.
				script.onreadystatechange = function () {

				  if (this.readyState === 'loaded' || this.readyState === 'complete') {
 					core.loader.requestComplete();
				  }
				};
				
			  } else {
				// If this is a browser that doesn't suck, append a scriptlet after the
				// last script.
				
				script = document.createElement('script');
				script.appendChild(document.createTextNode(
					'com.williamukoh.Core.loader.requestComplete();'));
				document.body.appendChild(script);
			  }
		},
	
		loadCSS:  function(url){
			var newCSS = document.createElement('link');
			   newCSS.href = url; //path to CSS file
			   newCSS.type = 'text/css';
			   newCSS.rel = 'stylesheet';

				//Add CSS to Document
			   var head = document.getElementsByTagName('head')[0];
			   if(!head) return;
			   head.appendChild(newCSS);
		},
		
		preloadImages: function(){
			
			var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
			var i,j=d.MM_p.length,a=this.preloadImages.arguments; for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}	
		}
		,
		
		loadScriptOnce: function (urls, callback, obj, scope, force) {
		  
			  var core = com.williamukoh.Core;
			  var newUrls = [];
			  var scripts = document.getElementsByTagName('script');
			  
		
			  urls = urls.constructor === Array ? urls : [urls];
		
			  for (var i = 0; i < urls.length; i += 1) {
				var loaded = false,
					url    = urls[i];
		
				for (var j = 0; j < scripts.length; j += 1) {
				  if (url === scripts[j].src) {
					loaded = true;
					break;
				  }
				}
		
				if (!loaded) {
				  newUrls.push(url);
				}
			  }
		
			  if (newUrls.length > 0) {
				this.load(newUrls, callback, obj, scope);
			  } else if (force) {
				if (obj) {
				  if (scope) {
					callback.call(obj);
				  } else {
					callback.call(window, obj);
				  }
				} else {
				  callback.call();
				}
			  }
			},

		requestComplete: function () {
		  // Execute the callback.
		  
		  if (pending.callback) {
			if (pending.obj) {
			  if (pending.scope) {
				pending.callback.call(pending.scope,pending.obj);
			  } else {
				pending.callback.call(window, pending.obj);
			  }
			} else {
			  pending.callback.call();
			}
		  }
	
		  pending = null;
	
		  // Execute the next load request on the queue (if any).
		  if (queue.length > 0) {
			var request = queue.shift();

			this.loadScript(request.urls, request.callback, request.obj, request.scope);
		  }
		},
		
		//function loads data from server
		loadData: function (_url,_params,_callback){
		
			/*if(this.xhrObject)
				this.xhrObject.abort();
				*/
				
			if(_params instanceof Object)
				_params["cb"] = Math.random();
			else
				_params = {"param":_params,"cb":Math.random()};
				
			this.xhrObject = $.get(_url, _params, function(r,s){ 
													//console.dir(this);
													_callback.apply(null,arguments);
												  },"html");
		},
		
		//function that posts data to server
		postData: function (_url,_params,_callback){
		
			$.post(_url, _params, _callback,"html");
		}
	  };
	}(),
	
	/***********
	pages object [stores reference to data/objects that will be used by various pages in the future
	************/
	pages:{}

}