﻿var cookieName = "MetaverseAnalytics";
var pageName = '';
var valueID = '';

function uVoid() {
    return;
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function getCurrentPageName() {
    if (pageName != '') {
        return pageName;
    }
    var currentPage = document.location.href.substring(document.location.href.length, document.location.href.lastIndexOf('/') + 1);
    var indexOfQuestionMark = currentPage.indexOf('?');
    if (indexOfQuestionMark == -1) {
        return currentPage;
    }

    return currentPage.substring(0, indexOfQuestionMark);
}

function formatDateTime(date) {
    var monthArray = new Array(12);
    monthArray[0] = "January";
    monthArray[1] = "February";
    monthArray[2] = "March";
    monthArray[3] = "April";
    monthArray[4] = "May";
    monthArray[5] = "June";
    monthArray[6] = "July";
    monthArray[7] = "August";
    monthArray[8] = "September";
    monthArray[9] = "October";
    monthArray[10] = "November";
    monthArray[11] = "December";
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();

    var month = monthArray[date.getMonth()];
    var day = date.getDate();
    var year = date.getFullYear();
    return month + " " + day + " " + year + " " + hour + ":" + minute + ":" + second;
}

function createCookie(sessionGuid, domainGuid, clickCount, firstVisit) {
    var date = new Date();
    var formattedDate = formatDateTime(date);

    var expDate = new Date(date.setDate(date.getDate() + 14));

    var cookieData = { sessionGuid: sessionGuid, timestamp: formattedDate, clickCount: clickCount, firstVisit: firstVisit };
    var options = { expiresAt: expDate };
    jaaulde.utils.cookies.set(cookieName + domainGuid, cookieData, options);
}

function getCookie(name) {
    return jaaulde.utils.cookies.get(name);
}

function createUUID() {
    // http://www.ietf.org/rfc/rfc4122.txt
    var s = [];
    var hexDigits = "0123456789ABCDEF";
    for (var i = 0; i < 32; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[12] = "4";
    s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);

    var uuid = s.join("");
    return uuid;
}

function getScreenResolution() {
    return screen.width + ' x ' + screen.height;
}

function getColorDepth() {
    return screen.colorDepth + '-bit';
}

function mvTrack(domainGuid) {
    //use combination of cookie name and domain guid as cookie name, 
    //to ensure tracking occurs for each individual site
    var cookiesDisabled = !(jaaulde.utils.cookies.test());
    var cookieNameLocal = cookieName + domainGuid;
    var cookie = getCookie(cookieNameLocal);

    var isFirstVisit = cookie == null;
    var requestUrl = "http://siteanalytics.metaverse.com/track.gif";
    requestUrl = requestUrl + "?DomainGuid=" + encodeURIComponent(domainGuid);
    if (document.referrer != '' && document.referrer != null) {
        requestUrl = requestUrl + "&Referrer=" + encodeURIComponent(document.referrer);
    }
    requestUrl = requestUrl + "&IsFirstVisit=" + encodeURIComponent(isFirstVisit);
    if (isFirstVisit && !cookiesDisabled) {
        createCookie(createUUID(), domainGuid, 1, formatDateTime(new Date()));
        cookie = getCookie(cookieNameLocal);
    }

    var sessionGuid = cookiesDisabled ? createUUID() : cookie.sessionGuid;
    var timestamp = cookiesDisabled ? formatDateTime(new Date()) : cookie.timestamp;
    requestUrl = requestUrl + "&SessionGuid=" + encodeURIComponent(sessionGuid);
    requestUrl = requestUrl + "&Timestamp=" + encodeURIComponent(timestamp);
    requestUrl = requestUrl + "&TimeNow=" + encodeURIComponent(formatDateTime(new Date()));
    requestUrl = requestUrl + "&CurrentPage=" + encodeURIComponent(getCurrentPageName());
    requestUrl = requestUrl + "&ScreenResolution=" + encodeURIComponent(getScreenResolution());
    requestUrl = requestUrl + "&ColorDepth=" + encodeURIComponent(getColorDepth());
    requestUrl = requestUrl + "&IsCookiesDisabled=" + encodeURIComponent(cookiesDisabled);
    requestUrl = requestUrl + "&CookieText=" + encodeURIComponent(document.cookie);

    if (valueID == '') {
        valueID = getQuerystring("id");
        if (valueID == "") {
            valueID = getQuerystring("pid");
        }
    }

    requestUrl = requestUrl + "&ValueID=" + encodeURIComponent(valueID);

    var i = new Image(1, 1);

    i.onload = function () {
        uVoid();
    };

    i.src = encodeURI(requestUrl);

    createCookie(sessionGuid, domainGuid, cookie.clickCount + 1, cookie.firstVisit);
    valueID = '';
}


