/*
 Elements Javascript Framework
 Version: 0.1
 Last Update: 11.07.2008
 */


/* a part out of prototypejs library fuer creating classes */

function $A(iterable) {
	if (!iterable) return [];
	if (iterable.toArray) return iterable.toArray();
	var length = iterable.length || 0, results = new Array(length);
	while (length--) results[length] = iterable[length];
	return results;
}

var Class = {
	create: function() {
		var parent = null, properties = $A(arguments);
		if (Object.isFunction(properties[0]))
			parent = properties.shift();

		function klass() {
			this.initialize.apply(this, arguments);
		}

		Object.extend(klass, Class.Methods);
		klass.superclass = parent;
		klass.subclasses = [];

		if (parent) {
			var subclass = function() {
			};
			subclass.prototype = parent.prototype;
			klass.prototype = new subclass;
			parent.subclasses.push(klass);
		}

		for (var i = 0; i < properties.length; i++)
			klass.addMethods(properties[i]);

		if (!klass.prototype.initialize)
			klass.prototype.initialize = Prototype.emptyFunction;

		klass.prototype.constructor = klass;

		return klass;
	}
};

Class.Methods = {
	addMethods: function(source) {
		var ancestor = this.superclass && this.superclass.prototype;
		var properties = Object.keys(source);

		if (!Object.keys({ toString: true }).length)
			properties.push("toString", "valueOf");

		for (var i = 0, length = properties.length; i < length; i++) {
			var property = properties[i], value = source[property];
			if (ancestor && Object.isFunction(value) &&
					value.argumentNames().first() == "$super") {
				var method = value, value = Object.extend((function(m) {
					return function() {
						return ancestor[m].apply(this, arguments)
					};
				})(property).wrap(method), {
					valueOf:  function() {
						return method
					},
					toString: function() {
						return method.toString()
					}
				});
			}
			this.prototype[property] = value;
		}

		return this;
	}
};


Object.extend = function(destination, source) {
	for (var property in source)
		destination[property] = source[property];
	return destination;
};

Object.extend(Object, {
	keys: function(object) {
		var keys = [];
		for (var property in object)
			keys.push(property);
		return keys;
	},

	isFunction: function(object) {
		return typeof object == "function";
	}
});


/*
 create a namespace out of a point separated string
 the namespace is available at window instance
 */
var Namespace = Class.create({

	initialize: function(namespace) {
		var spaces = namespace.split(".");

		// create main space
		if (typeof window[spaces[0]] != "object") {
			window[spaces[0]] = {};
		}
		var currentLevel = window[spaces[0]];

		// create all subspaces
		for (var i = 1; i < spaces.length; i++) {
			if (typeof currentLevel[spaces[i]] != "object") {
				currentLevel[spaces[i]] = {};
			}
			currentLevel = currentLevel[spaces[i]];
		}
		return currentLevel;
	}
});


var Loader = Class.create({

	initialize: function() {
		this.scriptsToLoad = [];
	},

	setScriptPath : function (path) {
		this.scriptPath = path;
	},

	addClass : function (classname) {
		var realpath = classname.replace(/\./g, "/") + ".js";
		this.scriptsToLoad.push(realpath);
	},

	addFile : function (script) {
		this.scriptsToLoad.push(script);
	},

	load : function () {
		for (var i = 0; i < this.scriptsToLoad.length; i++) {
			document.write('<script type="text/javascript" src="' + this.scriptPath + this.scriptsToLoad[i] + '"></script>');
		}
	},

	loadProxy : function (proxyAdress) {
		var queryString = "?scriptPath=" + this.urlEncode(this.scriptPath) + "&scripts=" + this.urlEncode(this.scriptsToLoad.join(",")) + "&type=js";
		document.write('<script type="text/javascript" src="' + proxyAdress + queryString + '"></script>');
	},

	urlEncode : function (s) {
		if (window.encodeURIComponent) {
			return encodeURIComponent(s);
		}
		else if (window.escape) {
			return escape(s);
		}
		return s;
	}

});







