//prevent conflicts by wrapping plugin
(function($) {
    //private function (by scope)
    function QueryStringParser() {
        this.Values = new Object();
        this.load();
    }

    //add methods to the QueryStringParser class
    $.extend(QueryStringParser.prototype, {
        load: function() {
            //no query string...no problem
            if (window.location.search.length <= 1) {
                return;
            }

            //get the raw query string without the ?
            var queryString = window.location.search.substring(1);
            //split into pairs
            var pairs = queryString.split('&');
            //foreach pair
            for (var i = 0; i < pairs.length; i++) {
                //set the value (decode string just in case)
                this.Values[pairs[i].split('=')[0].toLowerCase()] = decodeURIComponent(pairs[i].split('=')[1]);
            }
        },
        get: function(key) {
            return (this.Values[key.toLowerCase()]) ? this.Values[key.toLowerCase()] : '';
        },
        set: function(key, value) {
            this.Values[key.toLowerCase()] = value;
            //chain...in true jQuery fashion
            return this;
        },
        remove: function(key)
        {
             var x;
             var tmpArray = new Array();
             for(x in this.Values)
             {
                if(x!=key) { tmpArray[x] = this.Values[x]; }
             }
             this.Values = tmpArray;
             return this;
        }
    });

    //the $.extend method doesn't appear to allow overriding toString on prototypes
    QueryStringParser.prototype.toString = function() {
        var params = [];

        for (var prop in this.Values) {
            //add the encoded value
            params.push(prop + "=" + encodeURIComponent(this.Values[prop]));
        }

        //return a complete query string
        return '?' + params.join('&');
    };

    //set the global property
    $.Params = new QueryStringParser();
})(jQuery);

