var gcap = window.gcap || {};

gcap.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=gcap;
        for (j=(d[0] == "gcap") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

gcap.asset_path = function(path) {
    // Find containing directory of jQuery by inspecting <script> elements
    var jsdir = jQuery(
        'script[src*=jquery]:first'
    ).attr('src').split('/').slice(0, -1).join('/');
    // Assuming it was served from a/common/js/, knock off three parent dirs
    var asset_path = jsdir.split('/').slice(0, -2).join('/') + '/';
    // This is a self-replacing function, to memoize the discovered path
    gcap.asset_path = function(path) {
        return asset_path + (path || '');
    };
    return asset_path + (path || '');
};

