/**
 * Loader 1.0
 * 
 * Tom Goodsun 2011.01.30
 */
var SELF_TOP_DIR = 'assets/js/';

var ScriptLoader = function(basepath, charset, localDir) {
	this.basepath = ScriptLoaderManager.createBasePath(localDir) + basepath;
	this.charset = charset;
}

ScriptLoader.prototype = {
	/**
	 * Base path
	 */
	basepath: null,

	/**
	 * Character set
	 */
	charset: 'UTF-8',

	/**
	 * Loaded files
	 */
	loadedFiles: [],

	/**
	 * Check file is loaded
	 * 
	 * @param file
	 */
	isLoaded: function(file) {
		for (var i = 0; i < this.loadedFiles.length; i++) {
			if (this.loadedFiles[i] == file) {
				return true;
			}
		}
		return false;
	},

	/**
	 * Prepare importing path and register it to stack
	 * 
	 * @param scriptFile
	 */
	createImportPath: function(scriptFile) {
		var scriptFile = this.basepath + scriptFile;
		if (!this.isLoaded(scriptFile)) {
			this.loadedFiles.push(scriptFile);
			return scriptFile
		}
		return null;
	},

	/**
	 * Get charset
	 */
	getCharset: function() {
		return this.charset;
	}
}

/**
 * ScriptLoader instance manager
 */
var ScriptLoaderManager = {
	/**
	 * Instances
	 */
	instances: {},

	/**
	 * Instances
	 * 
	 * @param id
	 * @param path
	 * @param charset
	 * @param localDir
	 */
	getInstance: function(id, path, charset, localDir) {
		if (this.instances[id]) {
			return this.instances[id];
		}
		this.instances[id] = new ScriptLoader(path, charset, localDir);
		return this.instances[id];
	},

	/**
	 * Get self script file path
	 */
	getSelfPath: function() {
		var scripts = document.getElementsByTagName('script');
		var i = scripts.length;
		while (i--) {
			var match = scripts[i].src.match(/(^|.*\/)Loader\.js$/);
			if (match) return match[1];
		}
	},

	/**
	 * Create base parh
	 * 
	 * @param selfLocalDir
	 */
	createBasePath: function(selfLocalDir) {
		var selfPath = ScriptLoaderManager.getSelfPath();
		var siteDomainRoot = document.URL.replace(/^(http|https)(\:\/\/.*?\/).+$/, '$1$2');
		return '/' + selfPath.replace(selfLocalDir, '').replace(siteDomainRoot, '');
	}
}

/**
 * Write script tag
 * 
 * @param path
 */
function loadScript(path) {
	var sl = ScriptLoaderManager.getInstance('script', 'assets/js/', 'UTF-8', SELF_TOP_DIR);
	var path = sl.createImportPath(path);
	if (path != null) {
		document.write('<script type="text/javascript" src="' + path + '" charset="' + sl.getCharset() + '"></script>');
	}
}


/**
 * Write style tag
 * 
 * @param path
 */
function loadStyle(path) {
	var sl = ScriptLoaderManager.getInstance('style', 'assets/ccs/', 'UTF-8', SELF_TOP_DIR);
	var path = sl.createImportPath(path);
	if (path != null) {
		document.write('<link rel="stylesheet" type="text/css" href="' + path + '" charset="' + sl.getCharset() + '" />');
	}
}


/**
 * Write script and style tag of plugin
 * 
 * @param path
 */
function loadPlugin(path) {
	var extension = path.substring(path.lastIndexOf('.') + 1);
	var sl = ScriptLoaderManager.getInstance('plugin', 'assets/scripts/', 'UTF-8', SELF_TOP_DIR);
	var path = sl.createImportPath(path);
	if (path != null) {
		switch(extension) {
			case 'js':
				document.write('<script type="text/javascript" src="' + path + '" charset="' + sl.getCharset() + '"></script>');
				return;
			case 'css':
				document.write('<link rel="stylesheet" type="text/css" href="' + path + '" charset="' + sl.getCharset() + '" />');
				return;
			default:
				return;
		}
	}
}

