/*
	SandBox - Naked Ideas
*/
var Sandbox = function() {
	var notifyList = {};
	var filters = [];

    var parseUri = function (sourceUri){
	    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		    uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		    uri = {};

	    for(var i = 0; i < 10; i++){
		    uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	    }

	    /* Always end directoryPath with a trailing backslash if a path was present in the source URI
	    Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	    if(uri.directoryPath.length > 0){
		    uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	    }

	    return uri;
    }


	// Métodos públicos
	return {

        listen: function(modules, fnHandler, oModule){
            for (var mod in modules) {
                if (notifyList[modules[mod]] != undefined) {
                    notifyList[modules[mod]].push(fnHandler);
                } else {
                    notifyList[modules[mod]] = [fnHandler];
                }
            }
        },

        notify: function(note){
            if (notifyList[note.type] != undefined){
                for (var i in notifyList[note.type]) {
                    //console.log(note.type + ':' + note.data);
                    notifyList[note.type][i](note);
                }
            }
        },

		alert: function(str){
			alert(str);
		},

		console: function(str){
			console.log(str);
		},

		message: function(str){
		    // Reemplazar por un div con mensajes.
            alert(str);
		},

        randNumber: function(){
            return parseInt(Math.random()*99999999);
        },


        hideLoading: function(){
        },

        loading: function(state){

        }

	}

};

// Init
$(document).ready(function() {
    Core.startAll();
});
