﻿// ==============================================================================
// Sinewave Analytics Stats Tracking Script. http://www.sinewave.net
// Copyright (c) 2009 Quality Cube, LLC All Rights Reserved.
// ==============================================================================
//
// Tracking Page Load
// Pinging "alive" status
// Submitting page-related events (conversions, etc.)

function SinewaveTracker() {
    // public 
    this.setKeyValuePair = function(k, v) {
        if (!_userdata) _userdata = new Array();
        _userdata.push(new _kvp(k, v));
    }
    this.setVar = function(v) {
        this.setKeyValuePair("_", v);
    }
    this.loglead = function(cd, cv) {
        this.logconversion("lead", cd, cv);
    }
    this.logpurchase = function(cd, cv) {
        this.logconversion("purchase", cd, cv);
    }
    this.logdownload = function(cd, cv) {
        this.logconversion("download", cd, cv);
    }
    this.logdemoview = function(cd, cv) {
        this.logconversion("demo", cd, cv);
    }
    this.logsignup = function(cd, cv) {
        this.logconversion("signup", cd, cv);
    }   
    this.logother = function(cd, cv) {
        this.logconversion("other", cd, cv);
    }
    this.logconversion = function(ct, cd, cv) {
        ct = (ct) ? ct : '';
        cd = (cd) ? cd : '';
        cv = (cv) ? cv : '';
        this._send('c', '');
    }
    this.ping = function(updateIdle) {
        var kvpa = new Array();
        kvpa.push(new _kvp("sc", _sessionname));
        kvpa.push(new _kvp("i", _i));
        this._send("/ping.js", "sinewave_ping", kvpa);
        if (updateIdle && updateIdle == true) _i = parseInt(((new Date()) - _la) / 1000);
    }
    this.trackExit = function() {
        // todo..
    }
    this.trackVirtualPageView = function(virtualUrl, pageTitle) {
        var kvpa = new Array();
        kvpa.push(new _kvp("sc", _sessionname));
        kvpa.push(new _kvp("n", _username));
        kvpa.push(new _kvp("e", "vp"));
        kvpa.push(new _kvp("ud", this._getuserdata()));
        kvpa.push(new _kvp("sw", screen.width));
        kvpa.push(new _kvp("sh", screen.height));
        kvpa.push(new _kvp("p", virtualUrl));
        kvpa.push(new _kvp("t", pageTitle));
        kvpa.push(new _kvp("u", escape(document.URL)));
        kvpa.push(new _kvp("dw", _body.scrollWidth));
        kvpa.push(new _kvp("dh", _body.scrollHeight));
        kvpa.push(new _kvp("cw", _body.clientWidth));
        kvpa.push(new _kvp("ch", _body.clientHeight));
        this._send("/virtualpageview.js", "sinewave_virtualpageview", kvpa);
    }
    this.trackPageView = function() {
        var kvpa = new Array();
        kvpa.push(new _kvp("sc", _sessionname));
        kvpa.push(new _kvp("ns", _newsession));
        kvpa.push(new _kvp("n", _username));
        kvpa.push(new _kvp("e", "p"));
        kvpa.push(new _kvp("r", escape(_referrer)));
        kvpa.push(new _kvp("d", document.domain));
        kvpa.push(new _kvp("ud", this._getuserdata()));
        kvpa.push(new _kvp("sw", screen.width));
        kvpa.push(new _kvp("sh", screen.height));
        kvpa.push(new _kvp("o", screen.colorDepth));
        kvpa.push(new _kvp("l", _moz ? navigator.language : navigator.browserLanguage));
        kvpa.push(new _kvp("k", navigator.cookieEnabled));
        kvpa.push(new _kvp("p", _pathname.substring(_pathname.lastIndexOf('\\') + 1)));
        kvpa.push(new _kvp("t", document.title));
        kvpa.push(new _kvp("u", escape(document.URL)));
        kvpa.push(new _kvp("dw", _body.scrollWidth));
        kvpa.push(new _kvp("dh", _body.scrollHeight));
        kvpa.push(new _kvp("cw", _body.clientWidth));
        kvpa.push(new _kvp("ch", _body.clientHeight));
        this._send("/pageview.js", "sinewave_pageview", kvpa);
    }

    function _kvp(_k, _v) {
        this.k = _k;
        this.v = _v;
    }
    this._onmd = function(e) {
        var elem = (e.srcElement) ? e.srcElement : e.target;
        if (elem.tagName == "A") {
            var link = elem;
            var _download = link.pathname.match(/(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&)/);
            if (_download) {
                this.logdownload(link.href);
            }
            if (!_download && link.hostname != location.host && link.hostname.indexOf('javascript') == -1 && link.hostname != '') {
                //this.trackExit();
            }
        }
    }
    this._reseti = function() {
        _la = new Date();
        _i = 0;
    }
    this._onmm = function(e) {
        tRef._reseti();
    }
    this._getc = function(k) {
        try {
            var c = "" + document.cookie;
            var i = c.indexOf(k);
            if (i == -1 || k == "") return "";
            var iv = c.indexOf(';', i);
            if (iv == -1) iv = c.length;
            return unescape(c.substring(i + k.length + 1, iv));
        } catch (ex) {
            return "";
        }
    }
    this._setc = function(k, v, d) {
        var ex, c;
        if (d > 0) {
            var dt = new Date();
            dt.setDate(dt.getDate() + d);
            ex = dt.toGMTString();
        }
        c = k + '=' + v + '; ' + ((ex) ? ('expires=' + ex + ';') : '') + 'path=/';
        document.cookie = c;
        return this._getc(k);
    }
    this._getk = function(d) {
        if (!d) d = new Date();
        return "" + d.getFullYear() + this._pl(d.getMonth() + 1, 2) + this._pl(d.getDate(), 2) + this._pl(d.getHours(), 2) + this._pl(d.getMinutes(), 2) + this._pl(d.getSeconds(), 2) + this._pl(d.getMilliseconds(), 3) + d.getTimezoneOffset();
    }
    this._pl = function(v, l, c) {
        if (!c) c = "0";
        while (("" + v).length < l) { v = c + v + ""; }
        return v;
    }
    this._getuserdata = function() {
        var d = '';
        if (_userdata) {
            for (var i = 0; i < _userdata.length; i++) {
                d += (d == '' ? '' : '&') + encodeURIComponent(_userdata[i].k) + '=' + encodeURIComponent(_userdata[i].v);
            } 
        }
        return escape(d);
    }
    this._send = function(endpoint, scriptTagName, kvpa) {

        var url = _address + endpoint + "?Z=" + this._getk();

        for (var i = 0; i < kvpa.length; i++) {
            url += '&' + kvpa[i].k + '=' + kvpa[i].v;
        }

        var scriptElement = document.getElementById(scriptTagName);
        if (scriptElement)
            scriptElement.src = url;

        else {
            scriptElement = document.createElement('script');
            scriptElement.type = "text/javascript";
            scriptElement.setAttribute("id", scriptTagName);
            scriptElement.src = url;
            document.getElementsByTagName('head')[0].appendChild(scriptElement);
        }

    }

    var _address, _hostname, _pathname, _body, _moz, _referrer, _protocol, tRef, _username, _sessionname, _newsession, _d, _la, _i, _userdata, _dn
    this._init = function() {
        _dn = "tracker.qualitydata.com"
        _d = new Date();
        _protocol = document.location.protocol;
        _hostname = document.location.hostname;
        _pathname = window.location.pathname;
        _body = document.compatMode != "BackCompat" ? document.documentElement : document.body;
        _moz = navigator.appName == 'Netscape';
        try { _referrer = (parent == self) ? window.document.referrer : parent.document.referrer; } catch (ex) { _referrer = window.document.referrer; }
        _username = this._getc("sinewaveVisitor");
        if (!_username) _username = this._setc("sinewaveVisitor", this._getk(_d), 10000);
        _sessionname = this._getc("sinewaveSession");
        if (_sessionname) _newsession = 0; else { _newsession = 1; _sessionname = this._setc("sinewaveSession", this._getk(_d), -1); }
        _address = _protocol + '//' + _dn;
        //if (_protocol.indexOf('s') == -1) _address = _protocol + '//' + ((_hostname.indexOf('www.') < 0) ? _hostname : _hostname.substring(4)) + '.' + _dn;
        tRef = this;
        this._reseti();

        if (document.addEventListener) {
            document.addEventListener("mousedown", this._onmd, false);
            document.addEventListener("mousemove", this._onmm, false);
        }
        else {
            document.attachEvent("onmousedown", this._onmd);
            document.attachEvent("onmousemove", this._onmm);
        }

        this.trackPageView();

    }
}
var sinewaveTracker = new SinewaveTracker();
window.onload = function() { sinewaveTracker._init(); } 
