﻿/**
* A simple querystring parser.
* Example usage: var q = $.parseQuery(); q.foo returns  "bar" if query contains "?foo=bar"; 
* multiple values are added to an array. 
* Values are unescaped by default and plus signs replaced with spaces, 
* or an alternate processing function can be passed in the params object .
* http://actingthemaggot.com/jquery
*
* Copyright (c) 2008 Michael Manning (http://actingthemaggot.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
**/
jQuery.parseQuery = function(qs, options) {
    var q = (typeof qs === 'string' ? qs : window.location.search), o = { 'f': function(v) { return unescape(v).replace(/\+/g, ' '); } }, options = (typeof qs === 'object' && typeof options === 'undefined') ? qs : options, o = jQuery.extend({}, o, options), params = {};
    if (q.indexOf('?', 0) > -1)
        q = q.substring(q.indexOf('?') + 1);
    jQuery.each(q.match(/^\??(.*)$/)[1].split('&'), function(i, p) {
        p = p.split('=');
        p[1] = o.f(p[1]);
        params[p[0]] = params[p[0]] ? ((params[p[0]] instanceof Array) ? (params[p[0]].push(p[1]), params[p[0]]) : [params[p[0]], p[1]]) : p[1];
    });
    return params;
}

// Create a new Querystring using the passed in parameters array
jQuery.createQuery = function(params) {
    var q = '';
    for (prop in params) {
        if (params[prop] instanceof Array) {
            for( val in params[prop] )
                q += escape(prop) + '=' + escape(val) + '&';
        }
        else
            q += escape(prop) + '=' + escape(params[prop]) + '&';
        
    }
    if (q.length > 0)
        return q.substring(0, q.length - 1);
    return q;
}
