/* 
Author : Bindu Varma
Name of the file: ga_setup.js
Purpose:code to implent basic GA 
*/

//Global Variables section****************
var ga_prependValue = "us::en_us::about::";
var ga_pageName = GetPageName(location.pathname);
var ga_sectionName = "untitled";
var ga_subsectionName = "untitled";
var ga_subsubsectionName = "untitled";
var pathObj = AnalyzeURLPathName(location.pathname.toLowerCase());
var ga_Environment = "PROD";
var gatid;
var ga_delim = String(".");
//var gatid = 'UA-15151886-1'; /*Google Analytics Tracking ID. Set to dev as default.*/
var pattern = /(keynote|ktxn|khte|gomez|watchfire|webxm)/g; /*Regex pattern to find robots that fire Javascript*/
//End Global vars section*************************

//***************NOTE****NOTE***************************************************************
//please make sure to set the environment to TEST for all environments otHer than Production
//ga_Environment = "TEST";
//*******************************************************************************************  
ga_sectionName = GetSectionName(0);
ga_subsectionName = GetSectionName(1);
ga_subsubsectionName = GetSectionName(2);


//**********************************************************************************************************************
//Basic GA implementation
//**********************************************************************************************************************


if (pattern.test(navigator.userAgent)) {
    gatid = 'UA-16395459-1';
} /* If a robot is found, send the data to the Filter Graveyard (for QA of User Agent filter) */
else if (ga_Environment == "TEST") {
    //gatid = 'UA-15151886-1';
    //  gatid = 'UA-17614561-1';
    gatid = 'UA-15151886-1';
} /*proto-cps is dev, use bindu's test id during testing for now*/
else if (ga_Environment == "PROD") {
    gatid = 'UA-15153107-1';
} /* if not a robot and on production server, set to prod.*/



//GA documentation
//GA governance Ga quality assuarance
var _gaq = _gaq || [];
_gaq.push(['wap._setAccount', gatid]);

//cross domain, sub domain links
_gaq.push(['wap._setDomainName', '.intel.com']);
_gaq.push(['wap._setAllowHash', false]);

_gaq.push(['wap._setCustomVar', 1, 'Geo', 'asmo-na', 3]);
_gaq.push(['wap._setCustomVar', 2, 'Local_Code', 'en_us', 3]);

if (ga_sectionName.toLowerCase() != 'nofolder') {
    _gaq.push(['wap._setCustomVar', 3, 'Section', ga_sectionName.toLowerCase(), 3]);

}

if (ga_subsectionName.toLowerCase() != 'nofolder') {
    _gaq.push(['wap._setCustomVar', 4, 'Sub-Section-1', ga_sectionName.toLowerCase() + ga_delim + ga_subsectionName.toLowerCase(), 3]);

}

if (ga_subsubsectionName.toLowerCase() != 'nofolder') {
    _gaq.push(['wap._setCustomVar', 5, 'Sub-Section-2', ga_sectionName.toLowerCase() + ga_delim + ga_subsectionName.toLowerCase() + ga_delim + ga_subsubsectionName.toLowerCase(), 3]);

}


_gaq.push(['wap._trackPageview', ga_pageName.toLowerCase()]);


(function() {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
} ());


//**********End Basic GA implemetation***************************************************************************************
function GetSectionName(postion) {

    var sectionName = (typeof pathObj.dir[postion] === "undefined") ? "nofolder" : pathObj.dir[postion].toLowerCase();
    if (sectionName === "corporateresponsibility") {
        sectionName = "corpres";
    }
    else if (sectionName === "companyinfo") {
        sectionName = "compinfo";
    }
    return unescape(sectionName);
}




function GetPageName(pagename) {

    var searchParam = GetSearchParam("wapkw");


    if ((pagename.indexOf(".") > -1) && (pagename.charAt(pagename.length - 1) === "/")) {
        pagename = pagename.substring(0, pagename.length - 1);
    }
    else {
        pagename = pagename.replace(/(\/)$/, "/index.htm");
    }
    if (searchParam != undefined) {
        pagename = pagename + "?wapkw=" + searchParam;
    }
    return unescape(pagename);
}

function GetSearchParam(param) {
    var searchStr = window.location.search.substring(1);
    var delim = searchStr.split("&");
    for (i = 0; i < delim.length; i++) {
        queryVal = delim[i].split("=");
        if (queryVal[0] == param) {
            return queryVal[1];
        }
    }
}

function AnalyzeURLPathName(strURL) {
    var pathObject = new Object();
    pathObject.dir = new Array();

    // Assumed input string format:  
    // <URL_path><file_name> i.e. <dir1><dir2>...<dirN><file_name>
    //
    // /^             // match at beginning of string
    //   (.+?)        // non-greedily match and capture all.  Targets <dir1>
    //   (?:\/)+      // match zero or more "/" without capture
    //   (.*)         // greedily match all.  Targets <dir2>...<dirN><file_name>
    // /
    //
    // Note:  This is a much more complicated approach than strURL.split("/"),
    // but I thought it necessary for the rare case when multiple forward-slashes
    // are included in the URL path, e.g. a/b//c/d//foo.htm
    //
    i = 0;
    pathObject.fullDir = "/";
    strURL = strURL.replace(/^\//, "");  // Remove any beginning / in our URL
    while ((node = strURL.match(/^(.+?)(?:\/)+(.*)/)) && node[0].length) {
        pathObject.dir[i++] = node[1];
        pathObject.fullDir += node[1] + "/";
        strURL = node[2];
    }
    pathObject.fileName = strURL;
    return pathObject;
}



