/**
 * @fileoverview URL parser.
 * @name URL
 * Requires Prototype version 1.6.0 or greater.
 */

/**
 * URL class.
 * @class URL
 * @author Ed Jenkins
 * @example
 * Usage examples:
 * Example 1:
 * var url = new URL("http://www.autotrader.com/research/compare/index.jsp?Compare=Compare&styles=&vehicle_number1=287117&vehicle_number2=293148&vehicle_number3=&vehicle_number4=");
 * var vehicle_number1 = url.getParameter("vehicle_number1");
 * Example 2:
 * var vehicle_number2 = ATC.cs.research.ctr.url.getParameter("vehicle_number2");
 */
var URL = Class.create();

/**
 * URL prototype.
 * @scope URL.prototype
 */
URL.prototype =
{

    /**
     * An array of parameters (name/value pairs).
     * It's like a List of Map of String and String.
     * @private
     */
    parameters: [],

    /**
     * Constructor.
     * Parses the URL to get a list of map of request parameters.
     * @param {string} url the URL to parse.
     * @throws exception if Prototype 1.6.0 or greater is not loaded.
     */
    initialize: function(url)
    {
        // Make sure Prototype is included.
        var exception = "Prototype version 1.6.0 or greater is required.";
        if(typeof Prototype == "undefined")
        {
            throw(exception);
        }
        // Make sure it is version 1.6.0 or greater.
        var ver = Prototype.Version.split(".");
        var major = ver[0];
        var minor = ver[1];
        if(major < 1)
        {
            throw(exception);
        }
        if(minor < 6)
        {
            throw(exception);
        }
        // Verify parameters.
        // Use current URL if one was not specified.
        var u = url;
        if(u == null)
        {
            u = window.location.href;
        }
        // Split into an array of 2 strings (before and after the "?").
        var url_args = u.split(/\?/);
        // Stop parsing if there are no request parameters.
        if(url_args.length != 2)
        {
            return;
        }
        var args = url_args[1];
        if(args == "")
        {
            return;
        }
        // Split request parameters part of URL into an array of strings.
        var a = args.split(/&/);
        // Loop through the array of parameters.
        var x = 0;
        var y = a.length;
        var z = 0;
        for(x=0; x<y; x++)
        {
            // Get a parameter in "name=value" format.
            var o = a[x];
            // Translate into an array of name/value pairs.
            var p = o.split(/=/);
            // Skip if there is a name with no value.
            if(p.length != 2)
            {
                continue;
            }
            if(p[1] == "")
            {
                continue;
            }
            // Decode values.
            var name = p[0];
            var value = unescape(p[1]);
            var nvp = [name, value];
            // Add the name/value pair to the list of parameters we found.
            this.parameters[z++] = nvp;
        }
    },

    /**
     * Gets a copy of the parameters.
     * @return {array} a copy of the parameters.
     */
    getParameters: function()
    {
        // Create return variable.
        var r = [];
        // Loop through the parameters.
        var x = 0;
        var y = this.parameters.length;
        for(x=0; x<y; x++)
        {
            // Get a parameter.
            var nvp = this.parameters[x];
            // Make a deep copy.
            var name = new String(nvp[0]);
            var value = new String(nvp[1]);
            var new_nvp = [name, value];
            r[x] = new_nvp;
        }
        // Return result.
        return r;
    },

    /**
     * Gets a parameter value.
     * @param {string} name the name of the parameter to get.
     * @return {string} the value of the named parameter or null if not found.
     */
    getParameter: function(name)
    {
        // Verify parameters.
        if(name == null)
        {
            return null;
        }
        // Loop through the list of parameters we gathered earlier.
        var value = null;
        var x = 0;
        var y = this.parameters.length;
        for(x=0; x<y; x++)
        {
            // Get a name/value pair.
            var nvp = this.parameters[x];
            // See if the name matches the one we're looking for.
            if(nvp[0] == name)
            {
                // If it does, save it and stop searching.
                value = nvp[1];
                break;
            }
        }
        // Return result.
        return value;
    }

}

// Create an instance.
ATC.cs.research.ctr.url = new URL();
