/*jsLint configuration*/
/*global $: false, document: false, omni_log: false, window: false, $siteRoot: false, cto: false*/
//
// // Modified DCBB version to allow media tracking of both audio and video types and links/downloads from the Flash
// Mikea-MadeByPi-17/11/2009

// called from the flash for 'page' views

function flashCall_pageView(pageName, siteSection) {
    omni_log("pageView pageName=" + pageName + " siteSection=" + siteSection);
    omni_log("site = " + $siteRoot + ":" + siteSection);

    if (window["cto"]) {
        cto.initFlashPV(); // Let the DIMG analytics layer know that we are going to send a flash PV
        //Set new values for CTO variables
        if (siteSection !== "") {
            siteSection = ":" + siteSection;
        }
        if ($siteRoot === null) {
            $siteRoot = "";
        }

        cto.siteSection = $siteRoot + siteSection;
        cto.pageName = pageName + ".flash";
        cto.trackFlashPV(); // Submit the PV data along with updated values for the properties.
    } else {
        omni_error("flashCall_pageView::Error with analytics cto object is not defined on the page");
    }
}

// called from the flash for link tracking (download or other)

function flashCall_linkTrack(link, name, type) {
    omni_log("linkTrack link=" + link + " name=" + name + " type=" + type);
    if (window["cto"]) {
        cto.FlashTrackLink(link, name, type);
    } else {
        omni_error("flashCall_linkTrack::Error with analytics cto object is not defined on the page");
    }
}

// media tracking functions

function flashCall_mediaOpen(name, mediaLength, playerName) {
    omni_log("flashCall_mediaOpen name=" + name + " mediaLength=" + mediaLength + " playerName=" + playerName);
    if (window["cto"]) {
        cto.MediaOpen(name, mediaLength, playerName);
    } else {
        omni_error("flashCall_mediaOpen::Error with analytics cto object is not defined on the page");
    }
}

function flashCall_mediaPlay(name, mediaOffset, mediaType) {
    omni_log("flashCall_mediaPlay name=" + name + " mediaOffset=" + mediaOffset + " mediaType=" + mediaType);
    if (window["cto"]) {
        cto.MediaPlay(name, mediaOffset, mediaType);
    } else {
        omni_error("flashCall_mediaPlay::Error with analytics cto object is not defined on the page");
    }
}

function flashCall_mediaStop(name, mediaOffset, mediaType) {
    omni_log("flashCall_mediaStop name=" + name + " mediaOffset=" + mediaOffset + " mediaType=" + mediaType);
    if (window["cto"]) {
        cto.MediaStop(name, mediaOffset, mediaType);
    } else {
        omni_error("flashCall_mediaStop::Error with analytics cto object is not defined on the page");
    }
}

function flashCall_mediaClose(name) {
    omni_log("flashCall_mediaClose name=" + name);
    if (window["cto"]) {
        cto.MediaClose(name);
    } else {
        omni_error("flashCall_mediaClose::Error with analytics cto object is not defined on the page");
    }
}

/**
 a little logging...
 */

function getConsole() {
    //need to check for console this way, testing console==null throws a reference error if it's not defined - and that stops the script running
    if (window["console"] === null) {
        try { //fix for missing console functions when firebug is not present...
            window["console"] = {
                log: function (value) {},
                error: function (value) {},
                info: function (value) {},
                warn: function (value) {}
            };
        } catch (error) {}
    }
    return window["console"];
}

function showLogging() {
    //should the firebug log be visible?
    var host = window.location.host;
    var hasDebugParam = window.location.href.indexOf("debug") > 0;
    var isDevPlatform = (host.indexOf("dev.") >= 0) || (host.indexOf("localhost.") >= 0) || (host.indexOf("127.0.0.1") != -1);
    return (hasDebugParam || isDevPlatform);
}

function omni_log(msg) {
    if (showLogging()) {
        getConsole().log("Omniture JS:" + msg);
    }
}

function omni_error(msg) {
    getConsole().error("Omniture JS:" + msg);
}

omni_log("EMEA Omniture code loaded ok javascript version");
