// include.js v1.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// I don't care what you think about the file size...
//   Be a pro: 
//		http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

/*-----------------------------------------------------------------------------------------------*/

var include = {
	masterFile : new Array(),
	afterLoadFunctions : new Array(),
	xmlhttp : new Array(),
	count : null,
	files : function(fileList) {
		var list = fileList.split(',');
		include.count = list.length;
		for (var x = 0; x < list.length; x++) {
			include.getFile(list[x], x);
		}	
	},
	buildFile : function(file, order) {
		include.masterFile[order] = file;
		include.count--;
		if (include.count == 0) {
			var output = '';
			for (var x = 0; x < include.masterFile.length; x++) {
				output += include.masterFile[x];
			}
			var head = document.getElementsByTagName("head")[0];
			var masterInclude = document.createElement('script');
			masterInclude.type = 'text/javascript';
			masterInclude.text = output;
			head.appendChild(masterInclude);
			setTimeout("include.runLoad();", 1);	
		}
	},
	getFile : function(file, order) {
		var loadError = false;
		try {
			include.xmlhttp[order] = include.getXMLHttpClient();
		} catch (e) {
			loadError = true;	
		}
		if (!loadError) {
			include.xmlhttp[order].onreadystatechange = function(){
				include.handler(order);
			}
			include.xmlhttp[order].open('GET', file, true);
			include.xmlhttp[order].send(null);
		} else {
			throw("There was a problem loading your file list, check to make sure all of the files exist.");
		}
	},
	handler : function(order) {
		if (this.xmlhttp[order].readyState == 4) {
			include.buildFile(this.xmlhttp[order].responseText, order);
		}
	},
	getXMLHttpClient : function() {
		var xmlhttp;
		try {
			var xmlhttpOptions = new Array('MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP');
			var success = false;
			for (var i = 0; i < xmlhttpOptions.length && !success; i++) {
				try {
					xmlhttp = new ActiveXObject(xmlhttpOptions[i]);
					success = true;
				} catch (e) {}
			}
			if (!success) {
				throw("Unable to create XMLHttpRequest Object.");
			}
		} catch (e) {
			xmlhttp = new XMLHttpRequest();
		}
		return xmlhttp;
	},
	onLoad : function() {
		var f = arguments;
		for(var x = 0; x < f.length; x++) {
			if(typeof f[x] === 'function') {
				include.afterLoadFunctions.push(f[x]);
			}
		}
	},
	runLoad : function() {
		for(var x = 0; x < include.afterLoadFunctions.length; x++) {
			include.afterLoadFunctions[x]();
		}
	}
}

