window.pfLocationParser = function (loc) {
    /**
     * loc:         http://site.com/foo/bar/index.php?p1=1&p2=2&p3&p4=&p5=5#top
     *
     * site:        site.com
     *
     * pathStr:     /foo/bar/index.php
     * path:        ['foo', 'bar', 'index.php']
     *
     * paramsStr:   p1=1&p2=2&p3&p4=&p5=5
     * params:      {'p1':'1', 'p2':'2', 'p3':null, 'p4':'', 'p5':'5'}
     *
     * anchor:      top
     */

    this.site       = null;

    this.pathStr    = null;
    this.path       = null;

    this.paramsStr  = null;
    this.params     = null;

    this.anchor     = null;

    if (loc == undefined || loc == null) {
        this.loc = '' + document.location;
    }
    else {
        this.loc = '' + loc;
    }

    var buf = this.loc;

    //anchor
    var pos = buf.indexOf('#');
    if (pos != -1) {
        this.anchor = buf.substring(pos+1);
        buf = buf.substring(0, pos);
    }

    //paramsStr + params
    pos = buf.indexOf('?');
    if (pos != -1) {
        this.paramsStr    = buf.substring(pos+1);
        this.params = {};

        //fetching params
        if (this.paramsStr.length) {
            var q = this.paramsStr.split('&');
            for (var j = 0; j < q.length; j++) {
                var pair = q[j];
                var pp = pair.indexOf('=');
                if (pp == -1) {
                    this.params[pair] = null;
                }
                else {
                    this.params[pair.substring(0, pp)] = pair.substring(pp+1);
                }
            }
        }

        buf = buf.substring(0, pos);
    }

    //site + pathStr + path
    pos = buf.indexOf('http://');
    if (pos == -1) {
        //assertion failed
        throw "Can't find http:// in document.location = "+this.loc;
    }
    buf = buf.substring(7);

    pos = buf.indexOf('/');
    if (pos == -1) {
        // url like http://foo.bar
        this.site       = buf;
        this.path       = [];
        this.pathStr    = '/';
    }
    else {
        this.site       = buf.substring(0, pos);
        this.path       = [];
        this.pathStr    = buf.substring(pos);

        //more than just a '/'
        if (this.pathStr.length > 1) {
            this.path = this.pathStr.substring(1).split('/');
        }
    }
}

