// Dependancy Loader

function depLoad(list, callback, ignoreCache) {
	
	var ignoreCache = ignoreCache || false;
	var callback = callback || function(){};
	var n = 0;
	var loadCount = 0;
	var thisFunction = this;
	this.callbackCount = 0;
	
	if(ignoreCache == true) {
		var cacheStopper = '?' + Math.floor(Math.random()*1000001);
	} else {
		var cacheStopper = '';
	}
	
	loadCount = list.length;
	
	jQ.each(list, function() {
		
		n++;
		var currentFile = this + cacheStopper;
		
		ajaxWrapper(n, currentFile, loadCount);
		
		function ajaxWrapper(n, currentFile, loadCount) {
			
			
			jQ.get(currentFile, function(fileData) { 
			
				var fileName = currentFile;
								
				if( fileName.split('.')[fileName.split('.').length -1].match('js')) {
										
					var newElement = document.createElement('script');
					newElement.setAttribute("type","text/javascript");
					
					cb.appendScript(document.getElementsByTagName('head')[0], newElement);
					cb.setScriptHTML(newElement, fileData);
					
					

				} else if( fileName.split('.')[fileName.split('.').length -1].match('css') ) {
					
					var newElement=document.createElement("style");
					newElement.setAttribute("type", "text/css");
					
					cb.setStyleHTML(newElement, fileData);
					document.getElementsByTagName('head')[0].appendChild(newElement);
					
					
				}
				
				callbackCounter(document.body, loadCount, callback);
			
			});
		}	
				
	});
	
}



function callbackCounter(node, number, callback) {
	node.callbackCount = node.callbackCount + 1 || 1;
	//alert(node.callbackCount + ' of ' + number );
	if(node.callbackCount == number) { callback(); }
}



//cross browser junk
var cb = {};
cb.setScriptHTML = function(script, text){
	if (typeof(script.text) != 'undefined') {
		script.text = text;
	} else {
		script.innerHTML = text;
	}
	return script;
}	

cb.setStyleHTML = function(style, text){
	//cb.console('cb.setStyleHTML');
	if (typeof(style.styleSheet) != 'undefined') {
		if (typeof(style.styleSheet.cssText) != 'undefined') {
			style.styleSheet.cssText = text;
		}
	} else {
		jQ(style).empty();
		style.appendChild(document.createTextNode(text));
	}
	//cb.console('cb.setStyleHTML Success');
	return style;
}

cb.appendScript = function(elem, script){
	//cb.console('cb.appendScript');
	elem.appendChild(script);
	cb.evalScript(script);
}

cb.evalScript = function(script) {
	//cb.console('cb.evalScript: '+script.id);
	if (typeof(script.text) != 'undefined') {
		eval(script.text);
	} else {
		eval(script.innerHTML);
	}
}

