/** @constructor */

function Bysshe(cookie) {
    this.cookie = cookie;
};

Bysshe.prototype.getUserId = function() {
    if (!this.cookie.get("userid")) { 
        this.cookie.set(
            "userid", 
            (Math.random() + Math.pow(10, -9)).toString().substr(2, 8), 
            60*60*24*365*5
        ); 
    }
    return this.cookie.get("userid");
};

Bysshe.prototype.getSessionId = function() {
    
    // Google Analytics defines a session to be 30 minutes by default, so let's copy that:
    //
    // http://code.google.com/apis/analytics/docs/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._setSessionTimeout

    if (!this.cookie.get("sessionid")) { 
        this.cookie.set(
            "sessionid", 
            (Math.random() + Math.pow(10, -9)).toString().substr(2, 8).toString(16), 
            60*30
        ); 
    } else {
        this.cookie.set("sessionid", this.cookie.get("sessionid"), 60*30); 
    }

    return this.cookie.get("sessionid");

};

Bysshe.prototype.setPathnameData = function(pathname, time, region) {
    
    var bysshe = this.cookie.getObj("bysshe", { });
    
    bysshe[pathname] = {
        t0: time,
        region: region
    };
    
    this.cookie.setObj("bysshe", bysshe, 30);
    
};

Bysshe.prototype.getPathnameData = function(pathname) {
    var bysshe = this.cookie.getObj("bysshe", { });
    return bysshe[pathname] ? bysshe[pathname] : { t0: null, region: null };
};

Bysshe.prototype.resetPathnameData = function(pathname) {
    var bysshe = this.cookie.getObj("bysshe", { });
    delete bysshe[pathname];
    return this.cookie.setObj("bysshe", bysshe, 30);
};

Bysshe.prototype.getLocation = function() {
    
    var location = {
        latitude: null,
        longitude: null,
        city: null,
        country: null
    };
    
    if ((typeof google != 'undefined') && google.loader.ClientLocation) {
        var l = google.loader.ClientLocation;
        location.latitude = l.latitude;
        location.longitude = l.longitude;
        location.city = l.address.city;
        location.country = l.address.country;
    }
    
    return location;
    
};

Bysshe.prototype.getInfo = function() {
    
    var location = this.getLocation();
    var pathname = this.getPathnameData(document.location.pathname);
    var t0 = pathname.t0;
    var t1 = window.t1;
    var now = new Date();
    
    return {
        url: document.URL,
        title: document.title,
        referrer: document.referrer,
        status: document.status ? document.status : 200,
        userid: this.getUserId(),
        sessionid: this.getSessionId(),
        localtime: (now.getTime() - (now.getTimezoneOffset() * 60 * 1000)),
        width: $(document.body).getWidth(),
        region: pathname.region,
        loadtime0: t0 ? now.getTime() - t0 : null,
        loadtime1: t1 ? now.getTime() - t1 : null,
        latitude: location.latitude,
        longitude: location.longitude,
        city: location.city,
        country: location.country
    };
    
};

