/**
 * Beacons for page views and event tracking
 */

var cm_track = new Object();


/** Constants **/

cm_track.VERSION_PREFIX = '/v2';
//cm_track.TRACKING_HOST  = '%%TRACK_HOST%%';  // Replaced by Minify class 
cm_track.TRACKING_HOST  = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'track.mom.com';
cm_track.PAGEVIEW_PIXEL = '/view';
cm_track.EVENT_PIXEL    = '/event';

cm_track.LOAD_TIME_MS   = new Date().getTime().toString();
cm_track.LOAD_TIME      = parseInt(cm_track.LOAD_TIME_MS / 1000);


/** Properties **/

cm_track.page_viewed = 0;
cm_track.facebook_app_id = null;
cm_track.site_section = null;
cm_track.sessid = null;
cm_track.dealid = null;

/** Methods **/

cm_track.trackPageView = function () {
    if (this.page_viewed) {
        return;
    }
    var params = this.getStdParams();
    this.send(this.PAGEVIEW_PIXEL, params);
    this.page_viewed = 1;
};

// trackEvent supports (category, action, label (optional)), which are similar
// to the event tracking arguments supported by Google Analytics:
// http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
cm_track.trackEvent = function (category, action, label, extra) {
    if (!this.page_viewed) {
        this.trackPageView();
    }
    var params = this.getStdParams();
    params['evt_cat'] = category;
    params['evt_act'] = action;
    if (label != null) {
        params['evt_lab'] = label;
    }
    if (extra != null) {
        for (var e in extra) {
            params[e] = extra[e];
        }
    }
    this.send(this.EVENT_PIXEL, params);
};

cm_track.getStdParams = function () {
    var p = {'s': 'mc',
             'host': location.host,
             'path': location.pathname + ((location.search.length) ? location.search : ''),
             'refer': document.referrer,
             'isid': this.readCookie('cafemomis'),
             'sid':  this.readCookie('PHPSESSID'),
             'uid':  this.readCookie('huid')
            };
    // optional params
    if (this.facebook_app_id) {
        p['fbaid'] = this.facebook_app_id;
    }
    if (this.site_section) {
        p['section'] = this.site_section;
    }
    if (this.sessid) {
        p['sid'] = this.sessid;
    }
    if (this.dealid) {
        p['did'] = this.dealid;
    }

    return p;
};

cm_track.send = function (pixel, params) {
    var url = this.TRACKING_HOST + this.VERSION_PREFIX + pixel;

    var pix = new Image(1,1);
    var pairs = new Array();
    for (var p in params) {
        pairs.push(p + '=' + ((params[p] != null) ? this.encode(params[p]) : ''));
    }
    url += '?' + pairs.join('&') 
        + '&its=' + this.LOAD_TIME  // Initial page load timestamp
        + '&dms=' + (new Date().getTime().toString() - this.LOAD_TIME_MS);  // Delta millisecs. from page load (and cache-buster)
    pix.src = url;
};


/** Utility Methods **/

cm_track.encode = function (s) {
    s = encodeURIComponent(s);
    return s.replace(/~/g,'%7E').replace(/%20/g,'+');
};

cm_track.readCookie = function (name) {
    // Borrowed from PPK at http://www.quirksmode.org/js/cookies.html
    var name_eq = 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(name_eq) == 0) {
            return c.substring(name_eq.length, c.length);
        }
    }
    return null;
};

cm_track.handleExtStirClick = function(href, section) {
    cm_track.trackEvent('StirLink', 'Click', href, {section:section});
}


