var VITrack = function(page) {
    this.baseUrl = 'http://admetrics.buyerads.com/track/hit/';
    this.cookieName = '_ab_track';
    this.version = '123123';
    this.page = page;
    this.qsObj = {};
};

VITrack.prototype = {
    buildQs: function (obj) {
        qs = "?"
        for(k in obj) {
           qs += k + "=" + obj[k] + "&";
        }
        return qs;
    },
    init: function() {
        this.qsObj['version'] = this.version;
        this.qsObj['tag'] = this.page;
        this.qsObj['referrer'] = escape(document.referrer);
        var sess = this.getSessId();
        if(sess) {
            this.createCookie(this.cookieName, sess, 7);
        } else {
            sess = this.readCookie(this.cookieName);
            if(!sess) {
                sess = 'e6712f8d8955bffc78a258ac24ee8636';
            }
        }
        this.qsObj['sessionid'] = sess;
        var img = new Image();
        img.src = this.baseUrl + this.buildQs(this.qsObj);
    },
    getSessId: function() {
        var match = /_ab_sessid=([a-z0-9]+)/.exec(window.location.search);
        if(match != null && match.length > 1) {
            return match[1];
        }
        return false;
    },
    createCookie: function(name, value, days) {
        if(days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    },
    readCookie: function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while(c.charAt(0)==' ') c = c.substring(1,c.length);
            if(c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },
    eraseCookie: function(name) {
	    this.createCookie(name,"",-1);
    }
};

