var Tracking = {
    cookiePrefix: '__p2v_',
    
    getParam: function(name) {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
    
        if( results == null ) {
                return null;
        } else {
                return results[1];
        }
    },
    
    load: function() {
        var source = this.getParam("source");
        if (source != null) {
            this.updateCookie("src", source);
        } else {
            source = this.get("src");
        }
    
        if (source == null) {
            source = "";
        }
        var asource = this.getParam("addsrc");
        if (asource != null && asource != "") {
            if (source.indexOf(asource) == -1) {
            source = asource + "|" + source;
            this.updateCookie("src", source);
            }
        }
    },

    init: function() {
        this.initDt = ((new Date()).valueOf());
        var dcs = document.domain.split('.');
        var sdomain = dcs[dcs.length - 1];
        if (dcs.length >= 2) {
            sdomain = dcs[dcs.length - 2] + '.' + sdomain;
        }
        this.superDomain = sdomain;
        if (dcs.length > 2) {
            this.cookieDomain = '.' + sdomain;
        } else {
            this.cookieDomain = sdomain;
        }
        this.options = { expires: 999999, path: '/', domain: this.cookieDomain };
        },
        get: function(name) {
        var prefix = this.cookiePrefix + name;
        return jQuery.cookie(prefix);
    },
    
    setCookie: function(name, value, options)  {
        jQuery.cookie('' + name, '' + value, options);
    },
    
    updateCookie: function(name, value) {
        var prefix = this.cookiePrefix + name;
        var options = this.options;
        this.setCookie(prefix, '' + value, options);
    },
    
    recordPageView: function() {
        var prevCount = this.get('pv');
        if (prevCount != null && prevCount != "") {
            var count = int(prevCount) + 1;
            this.updateCookie('pv', '' + count);
        } else {
            var count = 1;
            this.options = { expires: 999999, path: '/', domain: this.cookieDomain };
            this.setCookie('pv', '' + count, this.options);
        }
    }
};

