// initialize namespace
var LIB3m5 = LIB3m5 || {}

//var _ = function (_this, func) {
//
//    $.log("arg", func.arguments);
//
//    return function() {
//        func.apply(_this, func.arguments);
//    }
//};

var __hasProp = Object.prototype.hasOwnProperty;

// extending one class from another (borrowed from coffeescript)
var __extends = function(child, parent) {
    for (var key in parent) {
        if ( __hasProp.call(parent, key)) {
            child[key] = parent[key];
        }
        else {
        }
    }
    function Ctor() {
        this.constructor = child;
    }
    Ctor.prototype = parent.prototype;
    child.prototype = new Ctor;

    child.__super__ = parent.prototype;
    return child;
};

var extend = function(child, parent) {
    return __extends(child, parent);
};


(function($) {
	/**
	 * @param {Number} time milliseconds to wait
	 * 
	 * @return {Deferred}
	 */
	$.wait = function(time) {
		return $.Deferred(function(dfd) {
			setTimeout(dfd.resolve, time);
		});
	};
	
	$.xml = function(xmlText) {
        if(window.Document && xmlText instanceof Document) {
            return $(xmlText);
        }
        else if (window.DOMParser) {
            var parser = new DOMParser();
            var xmlDoc = parser.parseFromString(xmlText, "text/xml");
            return $(xmlDoc);
        }
		else if($.browser.msie) {
			var xml = new ActiveXObject("Microsoft.XMLDOM");
            try {
                xml.async = false;
                xml.loadXML(xmlText);
            }
            catch(e) {
                $.log("ERROR", e);
            }
			return $(xml);
		}
		else {
			return $(xmlText);
		}
	};
	
	/**
	 * Logging function
	 * 
	 * @param {Object} msg
	 */
	$.fn.log = function (msg) {
		try {
			console.log("%s: %o", msg, this);
		}
		catch(e) {}
		return this;
	};
	
	/**
	 * Logging function
	 * 
	 * @param {Object} msg
	 * @param {Object} obj
	 */
	$.log = function(msg, obj) {
		try {
			console.log(msg + ": %o",  obj);
		}
		catch(e) {}
	};
	
	/**
	 * create a namespace inside the jQuery root object f.e. "_3m5.zdf.geothek"
	 * 
	 * @param {String} the namespace to be created f.e. "_3m5.zdf.geothek"
	 * @return {Object} the namespace object
	 */
	$.namespace = function(space) {
		if(!$.__namespaces) $.__namespaces = new Object();
		
		var parts = space.split('.');
		var ladder = $.__namespaces;
		for (i = 0; i < parts.length; i++) {
			name = parts[i];
			if(!ladder.hasOwnProperty(name)) {
				ladder[name] = new Object();
			}
			ladder = ladder[name];
		}
		return ladder;
	};

	/**
	 * create a namespace inside the jQuery root object f.e. "_3m5.zdf.geothek"
	 * 
	 * @param {String} the namespace to be created f.e. "_3m5.zdf.geothek"
	 * @return {Object} the namespace object
	 */
	$.use = function(qualifiedClassName) {
		var parts = qualifiedClassName.split('.');
		var className = parts.pop();
		return $.namespace(parts.join('.'))[className];
	};

})(jQuery);

