/*
=head1 NAME

ALBase - URI generation

=head1 DESCRIPTION

Provide functionality similar to Catalyst's URI generating methods.  

=head1 FUNCTIONS

=cut
*/

var ALBase;

if (!ALBase) // only load once
{
    ALBase = {};

    // Generic validating setter for prepended URI component.
    ALBase._set = function(var_name, val)
    {
        if (   ALBase[var_name] != null 
            && ALBase[var_name] != val)
        {
            throw("Can't set " + var_name + " to " + val + " since it was "
                + "already set to " + ALBase[var_name]);
        }
        if (val == undefined) {
            throw("Can't set " + var_name + " to undefined");
		}
        val.replace(/\/$/, "");  // strip trailing slash
        ALBase[var_name] = val;
    };

    // Generic validating getter.
    ALBase._get = function(var_name) 
    {
        if (ALBase[var_name] === null) {
            throw(var_name + " not set yet");
		}
        return ALBase[var_name];
    };

/* 
=head2 set_base( base_uri ) / get_base( )

Set/get a base directory which make_uri() will prepend to URI strings.  If the
application is deployed in the root directory, it should be set to ""; if it
is deployed in the directory "/training", it should be set to "/training".

=cut
*/
    ALBase.base = null;
    ALBase.set_base = function(val) { ALBase._set("base", val); };
    ALBase.get_base = function()    { ALBase._get("base"); };

/* 
=head2 set_static( static_base_uri ) / get_static( )

Set/get a base directory which make_static_uri() will prepend to URIs.

=cut
*/
    ALBase.stat = null;  // "static" is a reserved word
    ALBase.set_static = function(val) { ALBase._set("stat", val); };
    ALBase.get_static = function()    { ALBase._get("stat"); };

/* 
=head2 set_static_img( static_img_base_uri ) / get_static_img( )

Set/get a base directory which make_static_img_uri() will prepend to URIs.

=cut
*/
    ALBase.static_img = null;
    ALBase.set_static_img = function(val) { ALBase._set("static_img", val); };
    ALBase.get_static_img = function()    { ALBase._get("static_img"); };

/*
=head2 make_uri(path)

Create a URI string by prepending a base string.  Similar to, but less
sophisticated than the Catalyst uri_for() method.  The supplied path must
begin with a slash.

=cut
*/
    ALBase.make_uri = function(path) 
    {
        if (ALBase.base == null) {
            throw("base not yet set");
		}
        if (path.charAt(0) != '/') {
            throw("path '" + path + "' doesn't start with a slash");
		}
        return ALBase.base + path; 
    };

/*
=head2 make_static_uri(path)

As with make_uri(), but uses value from set_static().

=cut
*/
    ALBase.make_static_uri = function(path) 
    {
        if (ALBase.stat == null) { 
            throw("static dir not yet set");
		}
        if (path.charAt(0) != '/'){ 
            throw("path '" + path + "' doesn't start with a slash");
		}
        return ALBase.stat + path; 
    };

/*
=head2 make_static_img_uri(path)

As with make_uri(), but uses value from set_static_img().

=cut
*/
    ALBase.make_static_img_uri = function(path) 
    {
        if (ALBase.static_img == null) {
            throw("static_img not yet set");
		}
        if (path.charAt(0) != '/'){ 
            throw("path '" + path + "' doesn't start with a slash");
		}
        return ALBase.static_img + path; 
    };
}

/* 
=head1 COPYRIGHT

Copyright 2007 Atomic Learning Inc.

=cut
*/
