var tracker = (function() {
    // App creds for Nars Integrated Try-On
    // var api_key = '8c82e90ae16e481bb57594cd51ba7eaa';
    // var shared_secret = '2984986b0b12420d';
    // var trackUrl = 'http://ecf.facecake.com/facecake/?api_key=' + api_key + '&';
    // var appID = '1604';

    //FOR TESTING
    var api_key = '6d8ff1b4cef247cb90bce898130a6f3f';
    var shared_secret = '2215b03e24dc42c6';
    var trackUrl = 'http://ecf.facecake.com/facecake/?api_key=' + api_key + '&';
    var appID = '1609';

    var currIP = -1;
    var userEmail = -1;

    if (!localStorage.getItem("user email")) {
        userEmail = -1;
    } else {
        userEmail = localStorage.getItem("user email");
    }

    var getParentUrl = function() {
        var isInIframe = (parent !== window),
            parentUrl = null;

        if (isInIframe) {
            parentUrl = document.referrer;
        } else {
            parentUrl = window.location.href;
        }

        var urlIndex = parentUrl.indexOf('#');
        var shortURL;

        if (urlIndex == -1) {
            shortURL = parentUrl;
        } else {
            shortURL = parentUrl.substring(0, urlIndex);
        }

        return shortURL;
    };

    var getCookie = function(name) {
        var re = new RegExp(name + "=([^;]+)");
        var value = re.exec(document.cookie);
        return (value !== null) ? unescape(value[1]) : null;
    };

    var SetCookieValue = function(key, value) {
        // console.log(key + "=" + value);
        document.cookie = key + "=" + value;
        GUID = value;

        return value;
    };

    var getGUID = function() {
        if (getCookie("userGUID") === null) {
            SetCookieValue("userGUID", Math.uuid());
        } else {
            GUID = getCookie("userGUID");

            return GUID;
        }
    };

    var imgTrack = function(action, actionDesc, appliedID, currentView, userID) {

        var buildURL = function() {
            var strVal = {
                action: action,
                actionDescriptor: actionDesc,
                appliedAssetID: appliedID,
                email: userEmail,
                ipAddr: currIP,
                pageName: currentView,
                referrer: getParentUrl(),
                userGuid: getGUID(),
                tempSecret: shared_secret,
                userID: userID
            };

            var strMD5 = $().crypt({
                method: "md5",
                source: strVal
            });

            var trackImg = new Image(1, 1);

            trackImg.src = trackUrl + 'action=' + action + '&actionDescriptor=' + actionDesc + '&appID=' + appID + '&appliedAssetID=' + appliedID + '&appliedAssetIDList=-1&appliedToImageID=-1&appliedValue=-1&associate=-1&bannerID=0&defaultPage=1&email=' + userEmail + '&impID=1&ipAddr=' + currIP + '&machineName=-1&pageName=' + currentView + '&referrer=' + getParentUrl() + '&userGuid=' + getGUID() + '&api_sig=' + strMD5 + '&userID=' + userID + '';

            // console.log(trackImg.src);
        };

        if (currIP == -1 || currIP === undefined) {
            $.getJSON("//www-dev.facecake.com/nars-test/get-ip.php", function(data) {
                currIP = data.ip;
            }).done(function(json) {
                buildURL();
            });
        } else {
            buildURL();
        }
    };

    // Public API
    return {
        track: imgTrack,
        refURL: getParentUrl,
        GUID: getGUID
    };
})();
