/*
 * $Id: acuvue_plugin.js,v 1.7 2010/05/05 13:30:31 powem Exp $
 * $Author: powem $
 * $Date: 2010/05/05 13:30:31 $
 * $Revision: 1.7 $
 *
 */

/**
 * Plugin to add tracking specific to Acuvue.com to create reporting defined in
 * original SOW
 * 4/9/10 7:37 AM
 * 
 */

/**
 * @class This class implements customized tracking for the Johnson&Johnson Acuvue
 * site.  This class provides tracking complementary to the standard tracking created
 * by the standard WebTrends Javascript.  
 * @version $Revision: 1.7 $
 * 
 */

function Acuvue(){
    this.campaignIds=["WT.mc_id","gclid","WT.srch"];
    this.version = "$Revision: 1.7 $";
    this.revisionDate = "$Date: 2010/05/05 13:30:31 $";

}

/**
 * @public
 * @description Sets the parameters for traffic sources and traffic source types.
 * <ul>
 *  <li>WT.z_sourceType</li>
 *  <li>WT.z_source</li>
 * </ul>
 * The following <code>source types</code> are created:
 * <ul>
 *  <li>Direct:  Traffic with no referrer</li>
 *  <li>Campaign: Traffic coming in under one of the defined campaign parameters</li>
 *  <li>Social Media: Traffic coming in from a defined Social Media site</li>
 *  <li>Organic Search: Traffic coming from a recognized organic search</li>
 *  <li>Referrer: All traffic that does not fall into one of the above buckets</li>
 * </ul>
 *
 */
Acuvue.prototype.acSetSource=function(){

    var query = window.location.search;
    var refHost = this.acGetReferringHost();
    // function pointers
    var getCampaignId = this.acGetCampaignValue;
    var getMedia = this.acGetSocialMedia;
    var getSearch = this.acGetOrganicSearch;
    var getReferringUri = this.acGetReferrerUri;

    if (this.acIsDirect() == true){
        _tag.WT.z_sourceType="Direct";
        _tag.WT.z_source="Direct";
    }else if (this.acIsCampaign(query) == true){
        _tag.WT.z_sourceType="Campaign";
        _tag.WT.z_source=getCampaignId();
    }else if (this.acIsSocialMedia(refHost) == true){
        _tag.WT.z_sourceType="Social Media";
        _tag.WT.z_source=getMedia(refHost);
    }else if (this.acIsOrganicSearch(refHost) == true){
        _tag.WT.z_sourceType="Organic Search";
        _tag.WT.z_source=getSearch(refHost);
    } else {
        _tag.WT.z_sourceType="Referrer";
        _tag.WT.z_source=getReferringUri(document.referrer);
    }
}

/**
 * @public
 * @description Finds the defined query parameter containing the campaign identifier and
 * returns its value.  If the parameter is the WebTrends <i>Paid Search</i> parameter,
 * then the return value is <code>Paid Search</code>.
 * @returns {String} The value of the identified campaign parameter.
 */
Acuvue.prototype.acGetCampaignValue=function(){
    var campaign = "";
    var campaigns = this.campaignIds;
    var qry=location.search.substring(1);
    if (qry!=""){
        var pairs=qry.split("&");
        for (var i=0;i<pairs.length;i++){
            var pos=pairs[i].indexOf("=");
            if (pos!=-1){
                for (var j = 0; j < campaigns.length; j++){
                    if (pairs[i].substring(0,pos)==campaigns[j]){
                        if(campaigns[j] == "WT.srch"){
                            campaign = "Paid Search";
                        } else {
                            campaign = pairs[i].substring(pos+1);
                        }
                        break;
                    }
                }
            }
            if (campaign != ""){
                break;
            }
        }
    }
    return campaign;
}

/**
 * @public
 * @description Returns the host portion of the document referrer.  This is used to test
 * whether the referrer is from search engines or social media sites.
 * @returns {String} The referring hostname, e.g. <code>www.google.com</code>
 */

Acuvue.prototype.acGetReferringHost=function(){
    var referrer = window.document.referrer;
    referrer = referrer.indexOf("/",8) == -1 ? referrer.substring(7) : referrer.substring(7,referrer.indexOf("/",8));
    return referrer;
}

/**
 * @public
 * @description Gets the URL of the document referrer without the query string, if
 * it exists, to make the report readable.
 * @param referrer The referring URL
 * @returns {String} The document referrer without a query string
 *
 */
Acuvue.prototype.acGetReferrerUri=function(referrer){
    if(referrer.indexOf("?")!= -1){
        return referrer.split("?")[0];
    } else {
        return referrer;
    }
}

/**
 * @public
 * @description An object containing the list of known social media sites and their
 * string identifiers.
 * @type Object
 *
 */
Acuvue.prototype.acSocialMedia={
    "facebook.com" : "FaceBook",
    "linkedin.com" : "LinkedIn",
    "twitter.com" : "Twitter"
}

/**
 * @public
 * @description Gets the name of the social media site, based on the hostname of
 * the referrer.
 * @param host The hostname of the tested site, e.g. <code>www.facebook.com</code>
 * @returns The looked up identifier for the site, e.g. <code>FaceBook</code>
 *
 */
Acuvue.prototype.acGetSocialMedia=function(host){
    var media = this.acSocialMedia;
    var sMedia = "";
    for (site in media){
        if (host.search(site) != -1){
            sMedia = media[site];
        }
    }
    return sMedia;
}

/**
 * @public
 * @type Object
 * @description A JSON object containing the known search engines.  This data is generated
 * by a script from the WebTrends keywords.ini file.  The format is:
 * <code>
 * siteHostname : [site Name, [site query parameter array]]
 * </code>
 * Where <code>siteHostname</code> may be a semicolon-delimited list of multiple hostnames
 * that all point to a single site.  
 *
 */
Acuvue.prototype.acSearchEngines={
    // added in a separate file
    }

/**
 * @public
 * @description Tests whether or not the given host is an organic search engine.
 * The test is based on the list of search engines in the <code>acSearchEngines</code>
 * object.
 * @param host The hostname of the document referrer.
 * @returns {Boolean} True if the given host is coming from a search results page,
 * False otherwise.
 *
 */
Acuvue.prototype.acIsOrganicSearch=function(host){
    var searchEngines = this.acSearchEngines;
    var isRecognized = false;
    var query = "";
    for (engine in searchEngines){
        if(engine.search(host) != -1){
            var referrer = window.document.referrer;
            if (referrer.indexOf("?") != -1){
                query = referrer.split("?")[1];
            } else {
                continue;
            }            
            var searchParams = searchEngines[engine][1];
            for (var i = 0; i < searchParams.length; i++){
                if (query.search(searchParams[i]+"=")!= -1){
                    isRecognized = true;
                    break;
                }
            }
            if(isRecognized == true){
                break;
            }
        }
    }
    return isRecognized;
}
/**
 * @public
 * @description Returns the name of the identified organic search engine.  The name
 * is looked up from the <code>acSearchEngines</code> object, based on the hostname
 * of the referrer.
 * @param host The hostname of the referring site.
 * @returns {String} The looked up name of the search engine.
 *
 */
Acuvue.prototype.acGetOrganicSearch=function(host){
    var searchEngines = this.acSearchEngines;
    var search = "";
    for (engine in searchEngines){
        if(engine.search(host) != -1){
            search = searchEngines[host][0];
            break;
        }
    }
    return search;
}

/**
 * @public
 * @description Determines if a given referrer is from a recognized Social Media
 * site.
 * @param host The hostname of the referring URL.
 * @returns {Boolean} True if the referrer is a social media site, False otherwise.
 *
 */
Acuvue.prototype.acIsSocialMedia=function(host){
    
    var media = this.acSocialMedia;
    var isRecognized = false;
    for (medium in media){
        if(host.search(medium) != -1){
            isRecognized = true;
            break;
        }
    }
    return isRecognized;
}

/**
 * @public
 * @description Determines if a given request is in response to a campaign.  The
 * query string of the request is searched for one of the identified campaign
 * parameters.  These parameters are stored in the <code>campaignIds</code> property
 * of the instantiated Acuvue object.
 * @param query The query string of the incoming request.
 * @returns {Boolean} True if it is a campaign hit, False otherwise.
 * 
 */
Acuvue.prototype.acIsCampaign=function(query){
    var campaigns = this.campaignIds;
    var isRecognized = false;
    for(var i = 0; i < campaigns.length; i++){
        if (query.search(campaigns[i]) != -1){
            isRecognized = true;
            break;
        }
    }
    return isRecognized;
}

/**
 * @public
 * @description Determines if the hit has no referrer, i.e. is a "Direct" hit.
 * @returns {Boolean} True if the hit has no referrer (an empty string), False
 * otherwise.
 * 
 */
Acuvue.prototype.acIsDirect=function(){
    return window.document.referrer == "" ? true : false;
}

/*
* Run after WT code is loaded and has run.
* This function depends on the previous running of WebTrends code,
* in order to set the object property WT.vt_f_s
* Tracks whether or not a page loaded is the first page of visit or subsequent page
*/

/**
 * @public
 * @description Determines whether the hit is the first hit of a new visit.  Sets
 * <code>WT.z_bounces=1</code> for the first hit, and <code>WT.z_bounces=0</code>
 * for all other hits.
 * @returns {void}
 *
 */
Acuvue.prototype.acIsBounce=function(){

    // check for first page bounce
    if(typeof _tag.WT.vt_f_s == "undefined") {
        _tag.WT.z_bounces = "0";
    } else if (_tag.WT.vt_f_s == "1" ) {
        _tag.WT.z_bounces = "1";
    } else if (_tag.WT.vt_f_s == "" || _tag.WT.vt_f_s == 0 ) {
        _tag.WT.z_bounces = "0";
    }
}
// =============================================================================
//                      End Custom Code For Acuvue
// =============================================================================
/**
 * @description This redefines the dcsCollect function so that we can add our alterations without
 * touching the original WebTrends code from webtrends.js
 *
 */
WebTrends.prototype.dcsCollect=function(AcuvueTrackingObject){
    if (this.enabled){
        this.dcsVar();
        this.dcsMeta();
        this.dcsAdv();
        AcuvueTrackingObject.acIsBounce();
        AcuvueTrackingObject.acSetSource();
        this.dcsTag();
    }
}
