﻿/* INSTALLATION SECTION */

function install() {
    // This section is responsible for attaching trackers.  If you want to extend a function separate
    // of tracking, do it outside of this testing block but with a similar methodology.
    if (window.console) {
        // Try an early binding.  This is necessary for those pesky "onload" tracking events
        try {
            fireSpotlight = attachTracker(fireSpotlight, spotlightTracker);
        }
        catch (err) {
            // On a failure of early load, attempt to wait until the DOM is ready.
            // This will handle scripts included later in the page and may not be defined yet.
            window.onload = function() {
                fireSpotlight = attachTracker(fireSpotlight, spotlightTracker);
            }
        }

        // Attaches must be separated, since one can work while the other fails.
        // Failure to separate can cause functions to execute more than once.
        try {
            dcsMultiTrack = attachTracker(dcsMultiTrack, dcsTracker);
        }
        catch (err) {
            window.onload = function() {
                dcsMultiTrack = attachTracker(dcsMultiTrack, dcsTracker);
            }
        }
    }
}
install();
/* END INSTALLATION */

// Attach the tracker to the actual function.  This serves as a wrapper and intercepts the call to the function.
function attachTracker(target, tracker) {
    return function() {
        // Execute the tracker and output results to the console log.
        try {
            if (getCookie("alertme") != null && eval(getCookie("alertme")))
                alert(tracker(arguments, target));
            else {
                console.log(tracker(arguments, target));
            }
        }
        catch (err) {
            console.log("[Installer]: Failure");
        }
    }
}

/* TRACKERS -- These are broken out to allow for customization in argument passing. */

// Spotlight Tracker -- Only uses the first argument to pass on to the spotlight function.
function spotlightTracker(argList, target) {
    var output = "";
    try {
        output += "[Spotlight Tracker]\n"
        output += "Arg List: " + outputArray(argList) + "\n"
        target(argList[0]);
        output += "Complete";
    }
    catch (err) {
        output += "Failure"
    }
    return output;
}

// Web Trends Tracker -- Simply passes on the entire argument list.
function dcsTracker(argList, target) {
    var output = "";
    try {
        output += "[Web Trends Tracker]\n"
        output += "Arg List: " + outputArray(argList) + "\n"
        target(argList);
        output += "Complete";
    }
    catch (err) {
        output += "Failure"
    }
    return output;
}

/* END TRACKERS */

/* HELPER FUNCTIONS */

function outputArray(arr) {
    var str = "";
    for (var i = 0; i < arr.length; i++) {
        str += arr[i] + ", ";
    }
    return str.slice(0, str.length - 2);
}

function useAlert() {
    setCookie("alertme", true, 365);
    install();
}
function useLog() {
    setCookie("alertme", false, 365);
    install();
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

/* END HELPER FUNCTIONS */

