
SetupParser = (function() {
	/**
	 * use closure to emulate a singleton
	 */
	function Constructor() {
		/*
			private members
			*/
		var _config_path = "./";
		var _types = ["basic"];
		var instance = this;
		var lookup = new Object();
		var loaded = false;
		var loader;
		function trim (str) {
			if (!str) return "";
			for (var i=0; str.charCodeAt(i)<33; i++);
			for (var j=str.length-1; str.charCodeAt(j)<33; j--);
			return str.substring(i, j+1);
		}
		function onData (str) {
			var lines = str.split("\n");
			for (var i=0; i<lines.length; ++i) {
				var line = trim(lines[i]);
				var row = line.split("|");
				var key = row.shift();
				lookup[key] = row;
			}
		}
		function loadNext () {
			var type = _types.shift();
			if (!type) {
				loaded = true;
				instance.broadcastMessage("onLoaded");
				return;
			}
			var path = _config_path + "settings." + type + ".txt";
			loader = new redrocketcms.classes.AjaxConnection();
			loader.load(path);
			loader.onLoad = function () {
				var str = loader.getText();
				if (!str||str=="") {
					alert ("SETUP ERROR: setup file does not exist!");
					return;
				}
				onData(str);
				loadNext();
			}
		}
		function parse () {
			if (loaded) {
				instance.broadcastMessage("onLoaded");
				return;
			}
			if (_types.length==0) {
				alert ("SETUP ERROR: no setup types set!");
				return;
			}
			loadNext();
		}
		/*
			public members
			*/
		this.get = function (key) {
			var row = lookup[key];
			var typestring = row[1];
			var value = row[2];
			typestring = typestring.split(")").join("").split("(");
			var type = trim(typestring[0]);
			var delimiter = trim(typestring[1]) || "x";
			switch (type) {
			 case "color" :
				value = "#" + value;
				break;
			 case "ratio" :
				value = value.split(delimiter);
				value[0] = parseFloat(value[0]);
				value[1] = parseFloat(value[1]);
				break;
			 case "num" :
				value = parseFloat(value);
				break;
			 case "combo" :
				if (value.toLowerCase()=="true") value = true;
				if (value.toLowerCase()=="false") value = false;
				break;
			 case "upload" :
				value = value.split(",");
				value = trim(value[1]);
				break;
			 case "text" :
			 default :
				break;
			}
			return value;
		};
		this.setConfigPath = function (str) {
			_config_path = str;
		};
		this.setTypes = function (path_info) {
			_types = path_info instanceof String ? [path_info] : path_info;
		};
		this.start = function () {
			parse();
		};
	};
	// inherit from the Broadcaster
	Constructor.prototype = new redrocketcms.classes.Broadcaster();
	return new Constructor();
})();
