/**
 * meltmedia Prototype core namespace
 * @author Nick Crohn nick@meltmedia.com
 */

(function(){
    // Check to see if the latest version of Prototype is available
    if(Prototype) {
	if(parseFloat(Prototype.Version) < 1.6) {
	    throw new Error("meltmedia mocha requires Prototype Framework Ver 1.6 or higher.");
	}
    }

    if(typeof meltmedia == "undefined") {
	meltmedia = Class.create({
		Version: "1.1.0",
		toString: function() {
		    return "meltmedia mocha ver: " + this.Version;
		}
	    });
    }
}());

/**
 * Global function that creates javascript namespace with validation to append to a namespace and avoid overwritting.<br />
 * There is also reserved word validation for present and future words.
 * @parameter {string} ns The namespace that is to be created separated by "."
 */
var $NS = function(ns) {
    /** List of reserved words in javascript current and future */
    var a = ['abstract', 'as', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'continue', 'const',
	     'debugger', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'final',
	     'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int',
	     'interface', 'is', 'long', 'namespace', 'native', 'new', 'null', 'package', 'private' , 'protected',
	     'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws',
	     'transient', 'true', 'try', 'typeof', 'use', 'var', 'void', 'volatile', 'while', 'with'];
    var valid = function(str) {
	for(var j=0;j<a.length;j++) {
	    if(a[j] == str) return false;
	}
	return true;
    };
    var ns=ns.split(/\./), x=0, w=window, p=w[ns[x]], u="undefined";
    while(p) {
	x++;
	if(!p[ns[x]]) break;
	p=p[ns[x]];
    }
    for(var i=x; i<ns.length; i++) {
	if(valid(ns[i])) {
	    if(typeof p==u && x==0) {
		if(typeof w[ns[i]] == u) p = w[ns[i]] = {ns:ns[i]};
	    } else {
		if(typeof p[ns[i]] == u) p = p[ns[i]] = {ns:p.ns+"."+ns[i]};
	    }
	} else {
	    throw new Error("Invalid Namespace: Matched reserved word: "+ns[i]);
	}
    }
};

/**
 * @ignore
 */
(function() {
    $NS("meltmedia.gizmo");
    $NS("meltmedia.widget");
    $NS("meltmedia.component");
    $NS("meltmedia.module");
    $NS("meltmedia.cog");
    $NS("meltmedia.util");
    $NS("meltmedia.flash");
})();
