/**
File: omniture-hitbox.js

The reason for the function names is to avoid having to rewrite our existing code for flash files 
that are/were calling hitbox
**/
(function($) {
    /* Pages that should be excluded from the pageName (according to Phil) */
    var defaultPages = "index,default";

    /** String extensions used within the parser **/
    $.extend(String,{
        //whether or not a string is null,blank or only whitespace
        isNullOrEmpty: function(stringValue) {
            return (stringValue == null || stringValue.match(/^\s*$/g));
        },//whether or not this string starts with the prefix
        startsWith: function(value,prefix) {
            //if null or empty...no good
            if (String.isNullOrEmpty(value) || String.isNullOrEmpty(prefix))
                return false;

            //if the prefix is longer than the string...no good
            if (prefix.length > value.length)
                return false;

            return (value.substring(0,prefix.length) == prefix);
        },inArray: function(value,arr) {
            //no array....not found
            if (arr == null || arr.length == 0) {
                return false;
            }

            var found = false;
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].toString().toLowerCase() == value.toLowerCase()) {
                    found = true;
                    break;
                }
            }

            return found;
        }
    });

    function OmnitureUrlParser(virtualPath) {
        //omniture properties
        this.channel = "";
        this.prop1 = "";
        this.prop2 = "";
        this.prop3 = "";
        this.prop22 = "";
        this.prop23 = "";
        this.prop24 = "";
        this.pageName = "";
        this.virtualPath = "";

        //remove preceeding slash /
        if (String.startsWith(virtualPath,'/')) {
            virtualPath = virtualPath.substring(1);
        }

        //remove query string
        if (virtualPath.indexOf('?') > -1) {
            virtualPath = virtualPath.substring(virtualPath.indexOf('?'));
        }

        //the only case is blank or slash (the main home page)
        if (String.isNullOrEmpty(virtualPath)) {
            virtualPath = "home";
        }

        this.virtualPath = virtualPath;

        //process the values
        this.processUrlAndSetProperties();
    };

    $.extend(OmnitureUrlParser.prototype,{
        processUrlAndSetProperties: function() {
            //split the path by slash
            var urlParts = this.virtualPath.split('/');
            var lastIndex = urlParts.length - 1;

            //remove file extension from pages
            if (urlParts[lastIndex].indexOf('.') > -1) {
                urlParts[lastIndex] = urlParts[lastIndex].substring(0,urlParts[lastIndex].lastIndexOf('.'));
            }

            //remove any default names. Phil says these shouldn't report,//which makes sense,/blog/ and /blog/index.aspx should report the same way
            if (String.inArray(urlParts[lastIndex],defaultPages.split(','))) {
                urlParts[lastIndex] = "";
            }

            //hierarchy values for omniture 
            //THESE ARE SPECIFIC TO OUR IMPLEMENTATION - CHANGE AS NEEDED
            var omniData = [
                "",//channel
                "",//prop1
                "",//prop2
                "",//prop3
                "",//prop22
                "",//prop23
                "",//prop24
                ""  //pageName
            ];

            if (urlParts.length > omniData.length) {
                alert("There are not enough hierarchies available to report this URL to omniture");
            }
            else {
                //copy everything we can
                for (var i = 0; i < urlParts.length; i++) {
                    omniData[i] = urlParts[i];
                }

                //starting at 1 ensure properties
                for (var i = 1; i < omniData.length; i++) {
                    omniData[i] = this.ensureProperty(omniData[i],omniData[i - 1]);
                }

                //copy values to this object (Phil would like all reporting done in lowercase)
                this.channel = omniData[0].toLowerCase();
                this.prop1 = omniData[1].toLowerCase();
                this.prop2 = omniData[2].toLowerCase();
                this.prop3 = omniData[3].toLowerCase();
                this.prop22 = omniData[4].toLowerCase();
                this.prop23 = omniData[5].toLowerCase();
                this.prop24 = omniData[6].toLowerCase();
                this.pageName = omniData[7].toLowerCase();
            }
        },ensureProperty: function(prop,prefixValue) {
            var str = "";
            if (String.isNullOrEmpty(prop))
                str = prefixValue;
            else if (prop.indexOf(":") < 0)
                str = prefixValue + ":" + prop;

            return str;
        }
    });

    //jQuery bug: can't override toString with extend
    OmnitureUrlParser.prototype.toString = function() {
        var msg = [];
        msg.push("Omniture Settings:\n\n");
        msg.push("s.channel = '" + this.channel + "';\n");
        msg.push("s.prop1 = '" + this.prop1 + "';\n");
        msg.push("s.prop2 = '" + this.prop2 + "';\n");
        msg.push("s.prop3 = '" + this.prop3 + "';\n");
        msg.push("s.prop22 = '" + this.prop22 + "';\n");
        msg.push("s.prop23 = '" + this.prop23 + "';\n");
        msg.push("s.prop24 = '" + this.prop24 + "';\n");
        msg.push("s.pageName = '" + this.pageName + "';\n");

        return msg.join('');
    };

    //make this function publically visible
    $.createOmniturePageView = function(urlToReport) {
        return new OmnitureUrlParser(urlToReport);
    };
})(jQuery);

/** 
for flash page view calls 
usage: _hbPageView('/thefull/path/to/report','pageName.ext');

NOTE: if a variable called omni_debug is declared anywhere on 
the page before this call is made,a dialog box will be 
displayed showing the user what will be sent

**/
function _hbPageView(urlToReport,pageName) {
    
    if(!String.isNullOrEmpty(pageName)) {
        //if urlToReport ends with a slash
        if(urlToReport.match(/\/$/g)) {
            urlToReport += pageName;
        } else {
            urlToReport += "/"+ pageName;
        }
    }

    var data = jQuery.createOmniturePageView(urlToReport);

    if (typeof (omni_debug) != 'undefined') {
        alert(data.toString());
    }

    s.channel = data.channel;
    s.prop1 = data.prop1;
    s.prop2 = data.prop2;
    s.prop3 = data.prop3;
    s.prop22 = data.prop22;
    s.prop23 = data.prop23;
    s.prop24 = data.prop24;
    s.pageName = data.pageName;

    s.t();
}

/** 
for flash link calls 
usage: _hbLink('/thefull/path/to/report');

NOTE: if a variable called omni_debug is declared anywhere on 
the page before this call is made,a dialog box will be 
displayed showing the user what will be sent

P.S. A page view must be sent before this call can be made. 
**/
function _hbLink(action, not, used) {

    //contains /play_activity means event9 everything else is misc
    var events = action.match(/\/play_activity/gi) ? "event9" : "event11";
    var eventName = action.substring(action.lastIndexOf('/') + 1).replace(/_/gi,' ');


    
    //markup click event
    s.prop18 = s.eVar18 = eventName; 
    s.events = events;

    //specify which variables to track
    s.linkTrackVars = "prop16,eVar16,prop17,eVar17,prop18,eVar18,events";
    s.linkTrackEvents = "event9,event11";

    if (typeof (omni_debug) != "undefined") {
        alert("Sending action: " + eventName);
    }

    if (typeof (appendToAction) != "undefined") {
        s.prop16 = appendToAction;
    }

    //send image request
    if (events == "event9") {
        s.prop15 = s.prop16;
        s.linkTrackVars = "prop15,prop16,eVar16,prop17,eVar17,prop18,eVar18,events";
    }
   
    s.tl(true, "o", s.prop16 + ":" + eventName);

    //clear dynamic variables
    s.linkTrackVars = "None";
    s.linkTrackEvents = "None";
    s.prop15 = "";
    s.prop16 = s.eVar16 = "";
    s.prop18 = s.eVar18 = "";
    s.events = "";
}

function reportFlashEvent(pLocation, pEvent, pGame) {
    //markup click event
    s.prop18 = s.eVar18 = pLocation.split(':')[1];
    s.events = 'event11';

    //specify which variables to track
    s.linkTrackVars = "prop16,eVar16,prop17,eVar17,prop18,eVar18,events";
    s.linkTrackEvents = "event9,event11";

    s.prop16 = pLocation.split(':')[0];

    //send image request
    s.tl(true, "o", s.prop16 + ":" + s.prop18);

    //clear dynamic variables
    s.linkTrackVars = "None";
    s.linkTrackEvents = "None";
    s.prop16 = s.eVar16 = "";
    s.prop18 = s.eVar18 = "";
    s.events = "";
}
