/*
 * <%-- $Author: powem $ --%>
 * <%-- $Date: 2009/12/11 21:46:04 $ --%>
 * <%-- $Revision: 1.8.2.6.2.10.2.11 $ --%>
 * <%-- $Name: release_1_6 $ --%>
 *  
 * Based on global.js:
 * <%-- Date: 2009/10/28 17:02:00  --%>
 * <%-- Revision: 1.18  --%>
 * 
 * global.js is now merged with mercktracker.js
 * to reflect external changes, as well.
 *
 * tracking functions for merck.com and Merck manuals
 * These functions are intended to work in tandem with WebTrends code
 * version 8.5 or later.
 *
 */

/**
 *
 * @constructor The class of Merck tracking objects.  This class is intended to be instantiated
 * for customized tracking of activities on the Merck web sites, using the WebTrends
 * Javascript for delivering the information.
 * @version $Revision: 1.8.2.6.2.10.2.11 $
 * @property {String} downloadtypes List of files to be considered as
 * download types.  Currently unused.
 * @property {String} audiotypes List of files to be considered as audio files.  Currently unused.
 * @property {String} videotypes List of files to be considered as video files.  Currently unused.
 * @property {Array} prodSitesArray The array to be filled with product site identifiers.
 * @property {String} searchCookieName The name of the cookie that holds the value
 * of the searches performed on site during the current session.
 * @property {String} searchCookieCrumb The identifier inside <b>searchCookieName</b> that
 * contains the list of search terms.
 * @property {String} mainSearchPage The file name of the on site search results page for the main
 * merck.com site.
 * @property {String} mmSearchPage The file name of the on site search results page for the manuals site.
 * @property {RegExp} mmProPagesCg A regular expression object identifying the paths of the Professional
 * and Geriatrics manuals.
 * @property {RegExp} mmConsPagesCg A regular expression object identifying the paths of the Home
 * and Health & Aging site pages for Manuals.
 * @property {RegExp} mmConsPagesSgHome A regular expression identifying the Home Edition.
 * @property {RegExp} mmConsPagesSgHA A regular expression identifying the Health & Aging Pages.
 * @property {RegExp} mmProPagesSgPhy A regular expression identifying the Professional Edition.
 * @property {RegExp} mmProPagesSgGer A regular expression identifying  the Geriatrics Manual.
 * @property {RegExp} mmPubsCG A regular expression identifying  the <b>/pubs/</b> pages.
 * @property {String} version The cvs revision number of the current code, representing the
 * last checkout.  Currently <b>$Revision: 1.8.2.6.2.10.2.11 $</b>.
 * @property {Object} wt An object for backing up the WebTrends WT object.
 * @property {Object} dcs An object for backing up the WebTrends DCS object.
 * @property {Array} CrcArcTab An array of Hex numbers for generated hashes of page names.
 *
 */

function MerckTracker(){
    /*
     * Modify these if necessary
     */

    this.downloadtypes="xls,doc,pdf,txt,csv,zip";
    this.audiotypes="wma,mp3,m4a";
    this.videotypes="wmv,flv";
    this.WT = this.DCS = this.DCSext = null;
    this.prodSitesArray = new Array();
    this.searchCookieName = "OSS";
    this.searchCookieCrumb = "term";
    this.mainSearchPage = "userSearch.do";
    this.mmSearchPage = "search.html";
    this.mmProPagesCg = new RegExp(/\/mmpe\/|\/mkgr\//);
    this.mmConsPagesCg = new RegExp(/\/mmhe\/|\/mmanual_ha\//);
    this.mmConsPagesSgHome = new RegExp(/\/mmhe\//);
    this.mmConsPagesSgHA = new RegExp(/\/mmanual_ha\//);
    this.mmProPagesSgPhy = new RegExp(/\/mmpe\//);
    this.mmProPagesSgGer = new RegExp(/\/mkgr\//);
    this.mmPubsCG = new RegExp(/\/pubs\//);

    /*
     * =========================================================================
     * End user modifiable options
     * =========================================================================
     */

    this.version = "$Revision: 1.8.2.6.2.10.2.11 $";

    /**
     * @public
     * @fieldOf MerckTracker#
     * @description A pointer to the instantiated object, useful when <b>this</b>
     * points to something else in context.
     */
    var that = this;

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Adds a meta element to the page.  This is a "privileged" function,
     * meaning it is not a public method but is available to any public instance
     * methods and can see private data.
     * @param {String} name The name of the variable in the meta element.
     * @param {String} content The value of the meta element assigned to the variable in name.
     *
     */
    this.mtAddMeta=function(name,content){

        var mtMeta = document.createElement('meta');
        mtMeta.name = name;
        mtMeta.content = content;
        document.getElementsByTagName('head').item(0).appendChild(mtMeta);
    }

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Determines if an href belongs to an identified product site.
     * @param {Array} prodSitesArray  An array of product site names.
     * @param {String} site The site name to be looked up. <b>site</b> is expected to be
     * in the format <i>www.site.com</i>.
     * @returns {Boolean} True if the <b>site</b> is found in <b>prodSitesArray</b>.  False otherwise.
     *
     */
    this.mtIsProductSiteLink=function(prodSitesArray,site){
        var sites = prodSitesArray;
        var isSite = false;
        for(var i=0;i<sites.length;i++){
            if(site.indexOf(sites[i]) != -1){
                isSite=true;
                break;
            }
        }
        return isSite;
    }

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Determines if an href belongs to an identified video site.
     * @param {Array} videoSitesArray An array of names of sites identified as originating Merck video.
     * @param {String} site The site to be looked up.
     * @returns {Boolean} True if the site is a video site.  False otherwise.
     *
     */
    this.mtIsVideoSiteLink=function(videoSitesArray,site){
        var sites = videoSitesArray;
        var isSite = false;
        for(var i=0;i<sites.length;i++){
            if(site.indexOf(sites[i]) != -1){
                isSite=true;
                break;
            }
        }
        return isSite;
    }

    this.wt = {};
    this.dcs = {};

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Reads the specified XML file to get the collection of sites
     * that are to be considered Merck product sites.  This method has been supplanted
     * by <code>mtGetProductSitesJSON</code>.
     * @deprecated
     * @see mtGetProductSitesJSON
     * @returns {void}
     *
     */

    this.mtGetProductSitesXML=function(){
        var i = 0;
        var siteId;
        var site;
        var domain;
        var ps = new Object();
        $.ajax({
            type: "GET",
            url: productsXML,
            dataType: "xml",
            success: function(xml) {
                $(xml).find('site').each(function(){
                    siteId = $(this).attr('id');
                    site = $(this).find('name').text();
                    domain = $(this).find('domain').text();
                    that.prodSitesArray[i++] = domain;
                });

            }

        }); // end ajax

    }

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Retrieves a list of sites to be considered <i>product sites</i> for
     * Merck products.  Replaces <code>mtGetProductSitesXML</code> because Ajax call was too slow.
     * @see mtGetProductSitesXML
     * @returns {void}
     *
     */
    this.mtGetProductSitesJSON=function(){
        var i = 0;
        for (var site in productSites){
            if(!productSites.hasOwnProperty(site)){
                continue;
            }
            this.prodSitesArray[i] = productSites[site];
            i++;
        }

    }

    // these are for the distinct pages tracking, to construct hashed identifiers
    // written by David Smith, Technology Leaders, LLC

    this.CrcArcTab = new Array(
        0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,0xC601,0x06C0,0x0780,0xC741,0x0500,
        0xC5C1,0xC481,0x0440,0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,0xCE81,0x0E40,0x0A00,0xCAC1,
        0xCB81,0x0B40,0xC901,0x09C0,0x0880,0xC841,0xD801,0x18C0,0x1980,0xD941,0x1B00,0xDBC1,0xDA81,
        0x1A40,0x1E00,0xDEC1,0xDF81,0x1F40,0xDD01,0x1DC0,0x1C80,0xDC41,0x1400,0xD4C1,0xD581,0x1540,
        0xD701,0x17C0,0x1680,0xD641,0xD201,0x12C0,0x1380,0xD341,0x1100,0xD1C1,0xD081,0x1040,0xF001,
        0x30C0,0x3180,0xF141,0x3300,0xF3C1,0xF281,0x3240,0x3600,0xF6C1,0xF781,0x3740,0xF501,0x35C0,
        0x3480,0xF441,0x3C00,0xFCC1,0xFD81,0x3D40,0xFF01,0x3FC0,0x3E80,0xFE41,0xFA01,0x3AC0,0x3B80,
        0xFB41,0x3900,0xF9C1,0xF881,0x3840,0x2800,0xE8C1,0xE981,0x2940,0xEB01,0x2BC0,0x2A80,0xEA41,
        0xEE01,0x2EC0,0x2F80,0xEF41,0x2D00,0xEDC1,0xEC81,0x2C40,0xE401,0x24C0,0x2580,0xE541,0x2700,
        0xE7C1,0xE681,0x2640,0x2200,0xE2C1,0xE381,0x2340,0xE101,0x21C0,0x2080,0xE041,0xA001,0x60C0,
        0x6180,0xA141,0x6300,0xA3C1,0xA281,0x6240,0x6600,0xA6C1,0xA781,0x6740,0xA501,0x65C0,0x6480,
        0xA441,0x6C00,0xACC1,0xAD81,0x6D40,0xAF01,0x6FC0,0x6E80,0xAE41,0xAA01,0x6AC0,0x6B80,0xAB41,
        0x6900,0xA9C1,0xA881,0x6840,0x7800,0xB8C1,0xB981,0x7940,0xBB01,0x7BC0,0x7A80,0xBA41,0xBE01,
        0x7EC0,0x7F80,0xBF41,0x7D00,0xBDC1,0xBC81,0x7C40,0xB401,0x74C0,0x7580,0xB541,0x7700,0xB7C1,
        0xB681,0x7640,0x7200,0xB2C1,0xB381,0x7340,0xB101,0x71C0,0x7080,0xB041,0x5000,0x90C1,0x9181,
        0x5140,0x9301,0x53C0,0x5280,0x9241,0x9601,0x56C0,0x5780,0x9741,0x5500,0x95C1,0x9481,0x5440,
        0x9C01,0x5CC0,0x5D80,0x9D41,0x5F00,0x9FC1,0x9E81,0x5E40,0x5A00,0x9AC1,0x9B81,0x5B40,0x9901,
        0x59C0,0x5880,0x9841,0x8801,0x48C0,0x4980,0x8941,0x4B00,0x8BC1,0x8A81,0x4A40,0x4E00,0x8EC1,
        0x8F81,0x4F40,0x8D01,0x4DC0,0x4C80,0x8C41,0x4400,0x84C1,0x8581,0x4540,0x8701,0x47C0,0x4680,
        0x8641,0x8201,0x42C0,0x4380,0x8341,0x4100,0x81C1,0x8081,0x4040);

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Creates a hash value for the given string
     * @param {String} str The string to be hashed.
     * @returns {String} The hashed value of the input string.
     */
    this.mtCrc16=function (str)   {
        var c;
        var crc=0;
        for (var n=0; n< str.length; n++)  {
            c = str.charCodeAt(n);
            crc= ((crc>>8)&0xFF)^this.CrcArcTab[(crc^c)&0xFF];
        }
        return crc.toString(32).toUpperCase();
    }

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Backs up current WT object properties.  This is used to prevent
     * calls to MultiTrack from inserting object properties that get carried forward
     * on subsequent calls from the same page.
     * @param {Object} obj The object to be backed up.
     * @param {Object} bu The object to which the properties are to be copied for backup.
     * @returns {void}
     *
     */
    MerckTracker.prototype.mtSaveWT=function(obj,bu){
        for(var prop in obj){
            bu[prop] = obj[prop];
        }
    }

    /**
     * @private
     * @methodOf MerckTracker#
     * @description Restores WT object properties from the object used in <b>mtSaveWT</b> as backup.
     * @param {Object} obj The object to which to restore the backed up properties.
     * @param {Object} bu The object containing the backup.
     * @returns {void}
     *
     */
    MerckTracker.prototype.mtRestoreWT=function(obj,bu){
        obj = {}; // zero out everything to get rid of newly-created properties
        for(var prop in bu){
            obj[prop] = bu[prop];
        }
    }


    /**
     * @private
     * @methodOf MerckTracker#
     * @description A copy of the dcsMultiTrack function, altered for use with Merck
     * tracking.
     * @returns {void}
     */
    this.mtMultiTrack=function(){

        var args = arguments;
        that.wt = {};
        that.dcs = {};
        that.mtSaveWT(_tag.WT,that.wt);
        that.mtSaveWT(_tag.DCS,that.dcs);

        if (args.length%2==0){
            for (var i=0;i<args.length;i+=2){
                if (args[i].indexOf('WT.')==0){
                    _tag.WT[args[i].substring(3)]=args[i+1];
                }
                else if (args[i].indexOf('DCS.')==0){
                    _tag.DCS[args[i].substring(4)]=args[i+1];
                }
                else if (args[i].indexOf('DCSext.')==0){
                    _tag.DCSext[args[i].substring(7)]=args[i+1];
                }
            }
            var dCurrent=new Date();
            _tag.DCS.dcsdat=dCurrent.getTime();
            _tag.dcsFunc(_tag.dcsFPC());
            _tag.dcsTag();
        }

        that.mtRestoreWT(_tag.WT,that.wt);
        that.mtRestoreWT(_tag.DCS,that.dcs);
        that.wt = {};
        that.dcs = {};
    } // end multitrack

    /*
     * Private method calls
     */
    this.mtGetProductSitesJSON();


} // end constructor




/**
 * @public
 * @methodOf MerckTracker#
 * @description This is a special category of offsite click that is a site that
 * belongs to Merck but not identifiable by the Merck name. Sets <b>WT.dl=31</b>
 * and <b>WT.pi=Product Site Click</b>.  The sites are looked up in the
 * <code>mtProductSitesArray</code> created at the time the MerckTracker object is
 * instantiated.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagProductSiteClick=function(){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    var ps = this.prodSitesArray;
    var site = "";
    for(var i = 0; i < ps.length; i++){
        var h = "a[href*="+ps[i]+"]";
        $().find(h).unbind('click');
        $().find(h).bind('click',function(){

            save(_tag.WT,wt);
            save(_tag.DCS,dcs);

            var site = $.url.attr("host");
            _tag.dcsMultiTrack("DCS.dcssip",site,'DCS.dcsqry','','DCS.dcsuri',this.pathname,
                'WT.ti',"Link to Product Site: " +site,'WT.dl','31','WT.pi','Product Site Click');
            restore(_tag.WT,wt);
            restore(_tag.DCS,dcs);

        });
    }
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Tests the given site name to see if it is in the <i>merck.com</i> domain.
 * @param {String} site The name of the site to be tested, e.g. www.merck.com
 * @returns {Boolean} Returns <code>true</code> if the hostname has <i>merck.com</i>
 * in it, <code>false</code> otherwise.
 * 
 */
MerckTracker.prototype.mtIsMerckSite=function(site){
    var isSite = false;
    if(site.indexOf("merck.com") != -1){
        isSite = true;
    }
    return isSite;
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Tracks clicks out of manuals back into main merck site.  Sets
 * <b>WT.z_merck=1</b> and <b>WT.pi=Merck Main Site</b> if the click goes back to the main site.
 * @returns {void}
 *
 */
MerckTracker.prototype.mtOnSiteClick=function(){

    var siteRegex = new RegExp(/pubs|mmpe|mmhe|mmanual_ha|mkgr|redirector.pl/);

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    // for absolute URLs
    $('a[href*=merck.com]').each(function(){
        if(siteRegex.test($(this).attr('href')) == false){
            $(this).click(function(){
                save(_tag.WT,wt);
                save(_tag.DCS,dcs);
                _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',this.pathname,'WT.ti',this.innerHTML,
                    'WT.z_merck','1','WT.pi','Merck Main Site');
                WT.z_merck="";
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });
        }
    });
    // for relative URLs
    $('a[href^=/]').each(function(){
        if(siteRegex.test($(this).attr('href')) == false){
            $(this).click(function(){
                save(_tag.WT,wt);
                save(_tag.DCS,dcs);
                _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',this.pathname,'WT.ti',this.innerHTML,
                    'WT.z_merck','1','WT.pi','Merck Main Site');
                WT.z_merck="";
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });
        }
    });

}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Tracks downloads of the given file types.  Should be run after the
 * WebTrends code has loaded.  A download is identified by <b>WT.dl=20</b> and
 * <b>WT.pi=Downloads</b>.
 * @returns {void}
 *
 */
MerckTracker.prototype.mtFlagDownloadClick=function(){
    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;
    var multitrack = this.mtMultiTrack;

    $('a[pathname$=.pdf],a[pathname$=.doc],a[pathname$=.zip]').unbind('click', _tag.dcsDownload);
    $('a[pathname$=.pdf],a[pathname$=.doc],a[pathname$=.zip]').click(function(){
        //        save(_tag.WT,wt);
        //        save(_tag.DCS,dcs);
        var hn = this.hostname;
        if(typeof _tag.WT.oss != "undefined" && _tag.WT.oss.length > 0){
            _tag.WT.oss_s='1';
            _tag.WT.oss="";
            _tag.WT.oss_r="";
        }

        multitrack("DCS.dcssip",hn,'DCS.dcsqry','','WT.dl','20','DCS.dcsuri',this.pathname,'WT.ti',
            "Download: "+this.innerHTML,'WT.pi','Downloads');

    //        _tag.dcsMultiTrack("DCS.dcssip",hn,'DCS.dcsqry','','WT.dl','20','DCS.dcsuri',this.pathname,'WT.ti',
    //            "Download: "+this.innerHTML,'WT.pi','Downloads');

    //        restore(_tag.WT,wt);
    //        restore(_tag.DCS,dcs);
    });
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Tracks clicks to identified audio files.  Should be run after
 * WT code loads. An audio play is defined by <b>WT.dl=33</b> for <i>merck.com</i>
 * and <b>WT.dl=35</b> for <i>manuals</i>.
 * @param {String} site the name of the site calling the method.  <i>merck</i> for
 * the main site or <i>manuals</i> for the Merck Manuals site.
 * @returns {void}
 *
 */
MerckTracker.prototype.mtFlagAudioClick=function(site){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    switch(site){

        case "merck" :

            $('a[href$=.mp3],a[href$=.m4a]').click(function(){
                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                var hr = this.pathname;
                var hn = this.hostname;
                var tName = hr.split("/");
                var title = "Audio: "+ tName[tName.length-1];
                _tag.dcsMultiTrack("DCS.dcssip",hn,'DCS.dcsqry','','WT.dl','33','DCS.dcsuri',hr,'WT.ti',title,
                    'WT.pi','Audio Play');

            });
            break;

        case "manuals" :

            $('a[href$=.mp3],a[href$=.m4a]').click(function(){

                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                var hr = this.pathname;
                var hn = this.hostname;
                var tName = hr.split("/");
                var title = "Audio: "+ tName[tName.length-1];
                _tag.dcsMultiTrack("DCS.dcssip",hn,'DCS.dcsqry','','WT.dl','35','DCS.dcsuri',hr,'WT.ti',title,
                    'WT.pi','Audio Play');

                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });

            if(window.location.pathname.indexOf('/resources/multimedia/name/audio.html') != -1){
                jQuery('a[href$=x.html]').click(function(){
                    save(_tag.WT,wt);
                    save(_tag.DCS,dcs);
                    _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',this.pathname,'WT.ti','Audio: '+this.innerHTML,
                        'WT.dl','35','WT.pi','Audio Play');
                    restore(_tag.WT,wt);
                    restore(_tag.DCS,dcs);
                });
            }
            break;
    }

}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Tracks clicks to subscribe to Merck Manuals podcasts via iTunes.
 * An iTunes click is identified by <b>WT.dl=39</b> and <b>WT.pi=Subscribe</b>.
 * @returns {void}
 *
 */
MerckTracker.prototype.mtFlagItunesClick=function(){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    $('a:contains("Subscribe via iTunes")').click(function(){
        save(_tag.WT,wt);
        save(_tag.DCS,dcs);
        _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',this.href.split("url=")[1],'WT.ti',this.innerHTML,
            'WT.dl','39','WT.pi','Subscribe');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags loads of pages supplied by Lexi-Comp.  The pages are identified
 * by <b>WT.z_lexi=1</b>.  This method should run before WebTrends code runs because it sets
 * a meta tag.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagLexiCompPage=function(){
    if(window.location.pathname.indexOf("lexicomp") == -1){
        return;
    }
    this.mtAddMeta("WT.z_lexi", "1");
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags clicks to identified video files. Run after WT code loads
 * Video clicks are identified by <b>WT.dl=34</b> and <b>WT.pi=Video Play</b>.
 * @returns {void}
 *
 */
MerckTracker.prototype.mtFlagVideoClick=function(){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    $().find('a[href^=http://www.careertv.com][href*=VideoID]').click(function(){

        save(_tag.WT,wt);
        save(_tag.DCS,dcs);

        var hn = this.hostname;
        _tag.dcsMultiTrack("DCS.dcssip",hn,'DCS.dcsqry','','WT.dl','34','DCS.dcsuri',this.pathname,'WT.ti',
            "Video: "+this.innerHTML,'WT.pi','Video Play');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });

    $().find('a[href^=http://get.adobe.com/flashplayer]').click(function(){

        save(_tag.WT,wt);
        save(_tag.DCS,dcs);

        var title = $(this).find('img').attr('alt');
        var hn = this.hostname;
        _tag.dcsMultiTrack("DCS.dcssip",hn,'DCS.dcsqry','','WT.dl','34','DCS.dcsuri',this.pathname,'WT.ti',"Video: "+title,
            'WT.pi','Video Play');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });


    $('a[href$=.flv],a[href$=.wmv]').click(function(){

        save(_tag.WT,wt);
        save(_tag.DCS,dcs);

        _tag.dcsMultiTrack('DCS.dcsqry','','WT.dl','34','DCS.dcsuri',this.pathname,'WT.ti',"Video: "+this.innerHTML,
            'WT.pi','Video Play');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });

    if(window.location.pathname.indexOf('/resources/multimedia/name/video.html') != -1 ||
        window.location.pathname.indexOf('/resources/multimedia/name/animation.html') != -1){
        $('a[href$=x.html]').click(function(){

            save(_tag.WT,wt);
            save(_tag.DCS,dcs);
            var query = this.search != '' ? this.search.substring(1) : "";
            _tag.dcsMultiTrack('DCS.dcsqry',query,'DCS.dcsuri',this.pathname,'WT.ti','Video: '+this.innerHTML,
                'WT.dl','36','WT.pi','Video Play');
            restore(_tag.WT,wt);
            restore(_tag.DCS,dcs);
        });
    }

}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags clicks to email a page.  Should be run after WT code loads.
 * @param {String} site The site to be tracked, <i>merck</i> for the main site or
 * <i>manuals</i> for the Merck Manuals site. For manuals, email a topic page.
 * Sets <b>WT.cont=1</b>, <b>WT.z_email=1</b> and <b>WT.pi=Content Email</b>.
 * @param {String} site The site to be tracked, <i>merck</i> for the main site or
 * <i>manuals</i> for the Merck Manuals site.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagEmailClick=function(site){
    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    var multitrack = this.mtMultiTrack;

    switch(site){
        case "manuals" :
            $().find('a[href*=emailpage]').unbind('click');
            $().find('a[href*=emailpage]').click(function(){

                save(_tag.WT,wt);
                save(_tag.DCS,dcs);
                multitrack('DCS.dcsqry','','DCS.dcsuri',window.location,'WT.ti',
                    "Email: " + document.title,'WT.z_cont','1','WT.z_email','1','WT.pi','Content Email');
                _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',window.location,'WT.ti',
                    "Email: " + document.title,'WT.z_cont','1','WT.z_email','1','WT.pi','Content Email');
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });
            break;
        case "merck" :
            $('a:contains("Share")').click(function(){

                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                multitrack('DCS.dcsqry','','DCS.dcsuri',window.location,'WT.ti',document.title,
                    'WT.z_email','1','WT.z_cont','1','WT.pi','Content Email');

                //                _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',window.location,'WT.ti',document.title,
                //                    'WT.z_email','1','WT.z_cont','1','WT.pi','Content Email');
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });
            break;
    }
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags topic pages in Merck Manuals.  Sets <b>WT.z_topic=1</b> for a topic page.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagTopicPage=function(){

    var topic = $('p:contains("Topics")').length > 0 ? true : false;

    if(topic == false){
        return;
    }
    this.mtAddMeta("WT.z_topic", "1");
}


/**
 * @public
 * @methodOf MerckTracker#
 * @description Sets content groups and subgroups as meta tags.  Should be run
 * before WT code loads.  Also, sets <b>WT.z_manual</b> to the name of a manual
 * if we are in a manual page.  On the Merck main site, content groups
 * and subgroups are created by parsing the <code>window.location.pathname</code> structure.
 * @param {String} site The site being tracked, <i>merck</i> for the main site or
 * <i>manuals</i> for the Merck Manuals site.
 * @returns {void}
 */
MerckTracker.prototype.mtSetContentGroups=function(site){

    var cg = "";
    var sg = "";
    var href = window.location.href;

    switch(site){

        case "merck" :

            cg = ($.url.segment(0) == null) ? "Home" : $.url.segment(0);
            sg = ($.url.segment(1) == null) ? "" : $.url.segment(1);

            break;

        case "manuals" :
            var mmCgConName = "Patients & Caregivers";
            var mmCgPhyName = "Healthcare Professionals";

            var mmSgHome = "Merck Manual: Home Edition";
            var mmSgHA = "Merck Manual: Health & Aging";
            var mmSgPro = "Merck Manual of Diagnosis and Therapy";
            var mmSgGr = "Merck Manual of Geriatrics";

            var manualParam = "WT.z_manual";

            if(this.mmConsPagesCg.test(href) == true){
                cg = mmCgConName;
                sg = (this.mmConsPagesSgHome.test(href) == true) ? mmSgHome : "";
                if(sg==""){
                    sg = (this.mmConsPagesSgHA.test(href) == true) ? mmSgHA : "";
                }
            } else if(this.mmProPagesCg.test(href) == true){
                cg = mmCgPhyName;
                if (this.mmProPagesSgPhy.test(href) == true){
                    sg = mmSgPro;
                } else if(this.mmProPagesSgGer.test(href) == true){
                    sg = mmSgGr;
                } else{
                    sg = "";
                }
            } else if(this.mmPubsCG.test(href) == true){
                cg = "Pubs";
                sg = ($.url.segment(1) == null) ? "" : $.url.segment(1);
            }
            
            // if the subgroup is nonempty, then it's the name of a manual, so
            // set the name here
            if(cg != "Pubs" && sg != ""){
                this.mtAddMeta(manualParam, sg);
            }
            break;
    } // end switch

    this.mtAddMeta('WT.cg_n', cg);
    this.mtAddMeta('WT.cg_s', sg);
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Sets content navigation flags <b>WT.svl</b>.  Intended to run just
 * on the home page.  Modifies the page structure by adding query strings to links
 * on the page.  Sets the following values to <be>WT.svl</b>.
 * <ul>
 *  <li>footer</li>
 *  <li>content</li>
 *  <li>topnav</li>
 *  <li>mainnav</li>
 * </ul>
 * @returns {void}
 */
MerckTracker.prototype.mtSetNavigation=function(){

    var isHome = $.url.segment(0) == null ? true : false;

    if (isHome == false){
        return;
    }
    $('ul.sitemap  a').each(function(m){
        $(this).attr('href', $(this).attr('href')+"?WT.svl=footer");
    });

    $('ul.homeCols a:not(.arrow)').each(function(a){
        $(this).attr('href', $(this).attr('href')+"?WT.svl=content&WT.pi=content+Views");
    });

    $('a.arrow').each(function(m){
        $(this).attr('href', $(this).attr('href')+"?WT.svl=content");
    });


    $( "ul.topNav a").each(function(m){
        $(this).attr('href', $(this).attr('href')+"?WT.svl=topnav");
    });

    $("div.globalNav a").each(function(m){
        $(this).attr('href',$(this).attr('href')+"?WT.svl=mainnav");
    });
}

/*
 * Modified to add section name along with subject and also
 * modified to set section name on the section link that clicks back to the top
 * level page.  mp - 11/4/09
 *
 * Modified to set section name even when subject is not present.  Needed for deep
 * link clicking into section head pages, WT.z_section is not passed in for those
 * cases and subject does not exist.  mp - 11/25/09
 */

/**
 * @public
 * @methodOf MerckTracker#
 * @description Sets the parameter <b>WT.z_subject</b> with the subject specified
 * on a topic page.  Also, sets the section <b>WT.z_section</b> as well.  Always
 * sets section, even if subject is not present.
 * @returns {void} 
 */
MerckTracker.prototype.mtSetSubject=function(){

    var isSubject = $('td.bcrumbTitleCell:contains("Subject")').length > 0? true : false;

    var sectionParam = "WT.z_section";
    var sectionName  = $('td.bcrumbTitleCell:contains("Section")').next('td').next('td').find('a').html();

    if(sectionName != null && sectionName != 'undefined'){
        this.mtAddMeta(sectionParam, sectionName);
    }

    if(isSubject == false){
        return;
    }
    var subjectParam = "WT.z_subject";
    var subjectName = $('td.bcrumbTitleCell:contains("Subject")').next('td').next('td').find('a').html();

    $('td.bcrumbTitleCell:contains("Section")').next('td').next('td').find('a').each(function(){
        if($(this).attr('href') != ''){
            $(this).attr('href', $(this).attr('href')+"?WT.z_section="+$(this).html())
        }
    });

    if (subjectName != null && subjectName != 'undefined'){
        this.mtAddMeta(subjectParam, subjectName);
    }
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Sets the parameter <b>WT.z_resourceSection</b> to the name of the
 * topic, type or drawing specified as 'current' on a resources page.  Needs to run
 * before the WebTrends code because it sets a meta tag.  Also, modifies the page
 * to set <b>WT.z_resource</b> on the resource topic links.
 * @returns {void}
 */
MerckTracker.prototype.mtSetResourceTopic=function(){

    var locRe = new RegExp(/\/mm(h|p)e\/(resources|appendices|appendixes|drugnames-index|about)\//);
    if(locRe.test(window.location.href) == false){
        return;
    }
    var currResourceParam = "WT.z_resource";
    var currResource = ($('td.bcrumbTitleCell').children('p').html() != null) ? $('td.bcrumbTitleCell').next().next().children('p').children('a').html() : "";

    if($('a.plsLink') != null){
        $('a.plsLink').each(function(){
            $(this).attr('href',$(this).attr('href')+'?WT.z_resource='+$.query()['WT.z_resource'])
        });
    }

    var resourceTopicParam = "WT.z_resourceSection";
    var isTopic = $('td.bcrumbTitleCell:contains("Topics")').length > 0 ? true : false;
    var isType  = $('p.bcrumbLabel:contains("Types")').length > 0 ? true : false;
    var isDrawing = $('td.bcrumbTitleCell:contains("Drawings")').length > 0 ? true : false;
    var topic = "";
    if (isTopic == true){
        topic = $('td.bcrumbTitleCell:contains("Topics")').next('td').next('td').find('a.current').html();
        $('td.bcrumbTitleCell:contains("Topics")').next('td').next('td').find('a.bcLink').each(function(){
            $(this).attr('href', $(this).attr('href')+"?WT.z_resource="+$.query()['WT.z_resource']);
        });
        $('p.bcrumbLabel:contains("Resource")').parent().next().next().find('a').each(function(){
            $(this).attr('href', $(this).attr('href')+"?WT.z_resource="+$.query()['WT.z_resource']);
        });
    } else if(isType == true) {
        topic = $('p.bcrumbLabel:contains("Types")').parent().next().next().find('a.current').html();
        $('p.bcrumbLabel:contains("Type")').parent().next().next().children().find('a.bcLink').each(function(){
            $(this).attr('href', $(this).attr('href')+"?WT.z_resource="+$.query()['WT.z_resource']);
        });

        $('a.chapterLnk').each(function(){
            $(this).attr('href', $(this).attr('href')+"?WT.z_resource="+$.query()['WT.z_resource']);
        });

        if(topic == "Photographs"){
            $('a.topicLnk').click(function(){
                var qry = this.search != '' ? this.search.substring(1) : '';
                _tag.dcsMultiTrack('DCS.dcsuri',this.pathname,'DCS.dcssip',this.hostname,'WT.ti',
                    'Photograph: '+this.innerHTML,'DCS.dcsqry',qry);
            });
        } else {
            $('a.topicLnk').each(function(){
                $(this).attr('href', $(this).attr('href')+"?WT.z_resource="+$.query()['WT.z_resource']);
            });
        }

    } else if (isDrawing == true){
        topic = $('td.bcrumbTitleCell:contains("Drawings")').next('td').next('td').find('a.current').html();

        currResource = $('td.bcrumbTitleCell:contains("Resource")').next('td').next().children('p').children('a').html();

        // flag bread crumb links on the individual topic pages so that clicks carry the resource title
        // with them
        $('td.bcrumbTitleCell:contains("Drawings")').next('td').next('td').find('a.bcLink').each(function(){
            $(this).attr('href',$(this).attr('href')+'?WT.z_resource='+currResource);
        });
    } else {
        topic = "";
    }
    this.mtAddMeta(resourceTopicParam, topic);
    this.mtAddMeta(currResourceParam,currResource);
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags clicks to print a topic or page with <b>WT.z_print=1</b>
 * and <b>WT.pi=Content Print</b>.  Should run after WT code is loaded.
 * @param {String} site The site to be tracked, either <i>merck</i> for the main
 * site or <i>manuals</i> for the Merck Manuals site.
 * @returns {void} 
 */
MerckTracker.prototype.mtFlagPrintClick=function(site){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;
    var multitrack = this.mtMultiTrack;
    switch(site){

        case "merck" :

            if (location.pathname.indexOf("printer-friendly.html") != -1){
                this.mtAddMeta("WT.z_print", "1");
                this.mtAddMeta("WT.pi","Content Print");
                this.mtAddMeta("WT.z_cont","1");
                return;
            }

            $('a:contains("Print")').click(function(){

                //                save(_tag.WT,wt);
                //                save(_tag.DCS,dcs);

                multitrack('DCS.dcsqry','','DCS.dcsuri',window.location,
                    'WT.ti',"Print Topic: " + document.title,'WT.z_cont','1',
                    'WT.z_print', '1', 'WT.pi','Content Print');

            //                _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',window.location,
            //                    'WT.ti',"Print Topic: " + document.title,'WT.z_cont','1',
            //                    'WT.z_print', '1', 'WT.pi','Content Print');
            //                restore(_tag.WT,wt);
            //                restore(_tag.DCS,dcs);
            });

            break;

        case "manuals" :
            $('a[href*=/print/]').click(function(){

                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',this.pathname,'WT.ti',
                    "Print Topic: " + document.title,'WT.z_print','1','WT.z_cont','1',
                    'WT.z_print','1','WT.pi','Content Print');
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });

            break;
    }

}

/* 
 * This may be out of date -- mp 11/8/2009
 * 
 */
/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags clicks to the PDA version of the Merck Manuals. These clicks
 * actually go off site to merckbooks via a redirector.  Sets <b>WT.z_dl=35</b>
 * and <b>WT.pi=Merck Book Click</b>.
 * @returns {void}
 *
 */
MerckTracker.prototype.mtFlagPdaClick=function(){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    $('a[href$=PDA.html]').click(function(){

        save(_tag.WT,wt);
        save(_tag.DCS,dcs);

        _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',this.pathname,'WT.ti',"PDA: "+document.title,
            'WT.dl','38','WT.pi','Merck Book Click');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });
}

/*
 * WT.z_alert=1
 *
 */
/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags clicks to the subscription to email alerts.  Sets <b>WT.z_alert=1</b>
 * and <b>WT.pi=Alert Subscribe</b>.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagAlertSubscribeClick=function(){
    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;
    $('a[href$=email_alert.html]').click(function(){
        save(_tag.WT,wt);
        save(_tag.DCS,dcs);
        _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',this.pathname,'WT.ti',"Alert:  Subscription",
            'WT.z_alert','1','WT.pi','Alert Subscribe');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });
    $(':image[name=Subscribe]').click(function(){
        save(_tag.WT,wt);
        save(_tag.DCS,dcs);
        _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',window.location,'WT.ti',"Alert:  Subscription",
            'WT.z_alert','1','WT.pi','Alert Subscribe');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });

}

/*
* 
* This function depends on the previous running of WebTrends code,
* in order to set the object property WT.vg_f_s
* 
*/
/**
 * @public
 * @methodOf MerckTracker#
 * @description  Tracks whether or not a page loaded is the first page of visit or
 * subsequent page. 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
 * <b>WT.vg_f_s</b>.  Sets the parameter <b>WT.z_bounces=1</b> if <b>WT.vt_f_s=1</b>,
 * which signifies the first hit of the visit.  Otherwise, sets <b>WT.z_bounces=0</b>.
 * The call to <code>mtBounceCheck</code> is embedded in the WebTrends function
 * <code>dcsCollect</code>.
 * @example
 * <code>
 * WebTrends.prototype.dcsCollect=function(mTrackObj){
    if (this.enabled){
        this.dcsVar();
        this.dcsMeta();
        this.dcsFunc(this.dcsAdv());
        mTrackObj.mtBounceCheck();
        this.dcsTag();
    }
}</code>
 * @returns {void}
 */
MerckTracker.prototype.mtBounceCheck=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";
    }
}

/**
 * @private
 * @methodOf MerckTracker#
 * @description Determines if a given page is the search results page of either
 * the main site or of Merck Manuals.  The test is based on the search pages
 * identified in the constructor.  If the page is a search results page, the function
 * sets <b>WT.pi=Searches</b> as a meta tag.
 * @returns {Boolean} <b>true</b> if one of the search pages is identified,
 * <b>false</b> otherwise.
 */
MerckTracker.prototype.mtIsSiteSearch=function(){
    var isSearch = false;
    if((location.href).indexOf(this.mainSearchPage) != -1 ||
        (location.href).indexOf(this.mmSearchPage) != -1){
        isSearch = true;
        this.mtAddMeta("WT.pi", "Searches");
    }
    return isSearch;
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags search results clicks with <b>WT.oss_s=1</b> and
 * <b>WT.pi=Successful Searches</b> on the main site.  Uses a session cookie to
 * monitor onsite search terms and restricts the clickthrough counting to the
 * first click for a given search term.  Should be run after the WebTrends code
 * is loaded.
 * @returns {void}
 *
 */
MerckTracker.prototype.mtMainTrackSearchClick=function(){
    if(!this.mtIsSiteSearch()){
        return;
    }
    var downloadRegex = new RegExp(/pdf|doc|zip/);

    _tag.WT.oss=$.query()['strSearchTerm'];

    if (navigator.userAgent.indexOf('Firefox') != -1){
        _tag.WT.oss_r=$('div#searchcontainer').next('b').html();
    } else if ($('div#searchcontainer').next().next().html() != null) {
        _tag.WT.oss_r= $('div#searchcontainer').next().next().html().indexOf("No results")==-1 ?
        $('div#searchcontainer').next().next().html() : null;
    }

    _tag.WT.oss_r = (_tag.WT.oss_r == null) ? "0" : _tag.WT.oss_r;

    var ossSuccess = "1";

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    var sCName = this.searchCookieName;
    var sCCrumb = this.searchCookieCrumb;


    $.Jookie.Initialise(sCName, "-1");
    var searchCookie = $.Jookie.Get(sCName,sCCrumb);

    if ((searchCookie == null || typeof searchCookie == 'undefined') || searchCookie.indexOf(_tag.WT.oss)== -1){

        $('div#columnCenter table a[href]').click(function(){

            var hr = this.pathname;
       
            if (downloadRegex.test(hr) == true){
                _tag.WT.pi="Successful Searches";
                _tag.WT.oss_s=ossSuccess;
                return;
            }
        
            save(_tag.WT,wt);
            save(_tag.DCS,dcs);

            _tag.dcsMultiTrack('DCS.dcsqry','','WT.oss','','WT.oss_r','','WT.oss_s',ossSuccess,
                'DCS.dcsuri',this.pathname,'WT.ti',this.innerHTML,'WT.pi','Successful Searches');
            restore(_tag.WT,wt);
            restore(_tag.DCS,dcs);
        
        });
    }
    // set the cookie after links are flagged initially
    // then check for cookie on subsequent page loads

    $.Jookie.Initialise(sCName, "-1");
    var cookieValue = $.Jookie.Get(sCName,sCCrumb);
    if(cookieValue == null || cookieValue == "undefined"){
        $.Jookie.Initialise(sCName, "-1");
        $.Jookie.Set(sCName, sCCrumb, _tag.WT.oss);
    }else if(cookieValue.indexOf(_tag.WT.oss) == -1){
        cookieValue += ";" + _tag.WT.oss;
        $.Jookie.Set(sCName, sCCrumb, cookieValue);
    } else {
// nothing
}

}

/*
* Run after WT code is loaded
* Flags result clicks so we can track successful searches as ones in which a
* result was clicked
* 
*
*/
/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags search results clicks with <b>WT.oss_s=1</b> and
 * <b>WT.pi=Successful Searches</b> on the Merck Manuals site.  Uses a session
 * cookie to monitor onsite search terms and restricts the clickthrough counting
 * to the first click for a given search term.  Should be run after the WebTrends
 * code is loaded.  Note that the multitrack calls unset <b>WT.oss</b> and <b>WT.oss_r</b> so
 * they won't be resent as part of the data collection.
 * @returns {void} 
 */
MerckTracker.prototype.mtManualsTrackSearchClick=function(){

    if(!this.mtIsSiteSearch()){
        return;
    }

    var ossSuccess = ($('.textNoEntries').html() == null) ? true : false;
    var ossResults = (ossSuccess == true) ? $.trim($('p.searchResults').html().split('of')[1]) : '0';

    _tag.WT.oss=$.query()['qt'];

    _tag.WT.oss_r= (ossSuccess == true) ? ossResults : "0";

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    var sCName = this.searchCookieName;
    var sCCrumb = this.searchCookieCrumb;

    $.Jookie.Initialise(sCName, "-1");
    var searchCookie = $.Jookie.Get(sCName,sCCrumb);
    
    if ((searchCookie == null || searchCookie == 'undefined') || searchCookie.indexOf(_tag.WT.oss)== -1){

        $().find('.searchTable:contains("Subject titles")').each(function(x){
            var headerText = $(this).find('.bigHeader').html();
            $(this).find('a.MMchapterLnk').each(function(y){
                var hr = $(this).attr('pathname');
                var title = headerText + ": " + $(this).html();
                $(this).click(function(){

                    save(_tag.WT,wt);
                    save(_tag.DCS,dcs);

                    _tag.dcsMultiTrack('DCS.dcsqry','','WT.oss','','WT.oss_r','','DCS.dcsuri',hr,
                        'WT.ti',title,'WT.oss_s','1','WT.pi','Successful Searches');
                    restore(_tag.WT,wt);
                    restore(_tag.DCS,dcs);

                });
            });

            $(this).find('a.MMtopicLnk').each(function(y){
                var hr = $(this).attr('pathname');
                var title = headerText + ": " + $(this).html();
                $(this).click(function(){

                    save(_tag.WT,wt);
                    save(_tag.DCS,dcs);

                    _tag.dcsMultiTrack('DCS.dcsqry','','WT.oss','','WT.oss_r','','DCS.dcsuri',hr,'WT.ti',title,
                        'WT.oss_s','1','WT.pi','Successful Searches');
                    restore(_tag.WT,wt);
                    restore(_tag.DCS,dcs);
                });
            });

        });

        $().find('.searchTable:contains("Topic titles")').each(function(y){
            var headerText = $(this).find('.bigHeader').html();
            $(this).find('a.MMtopicLnk').each(function(y){
                var hr = $(this).attr('pathname');
                var title = headerText + ": " + $(this).html();
                $(this).click(function(){

                    save(_tag.WT,wt);
                    save(_tag.DCS,dcs);

                    _tag.dcsMultiTrack('DCS.dcsqry','','WT.oss','','WT.oss_r','','DCS.dcsuri',hr,'WT.ti',title,
                        'WT.oss_s','1','WT.pi','Successful Searches');
                    restore(_tag.WT,wt);
                    restore(_tag.DCS,dcs);
                });
            });
        });

        $().find('.searchTable:contains("Index entries")').each(function(y){
            var headerText = $(this).find('.bigHeader').html();
            var anchors = $(this).find('a[pathname]');

            $(anchors).click(function(){

                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                var hr = $(this).attr('pathname');
                var title = headerText + ": " + $(this).html().replace(/\s{2,}/g, " ");
                _tag.dcsMultiTrack('DCS.dcsqry','','WT.oss','','WT.oss_r','','DCS.dcsuri',hr,'WT.ti',title,
                    'WT.oss_s','1','WT.pi','Successful Searches');
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });
        });

        // Pages section
        var pageHeader = "Pages";

        $('td > p[class="searchTopic"] a').each(function(m){

            var hr = $(this).attr('pathname');
            var title = pageHeader + ": " + $(this).html();
            $(this).click(function(){

                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                _tag.dcsMultiTrack('DCS.dcsqry','','WT.oss','','WT.oss_r','','DCS.dcsuri',hr,'WT.ti',title,
                    'WT.oss_s','1','WT.pi','Successful Searches');
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });

        });

        $('p[class="searchTopicDesc"] a').each(function(n){
            var hr = $(this).attr('pathname');
            var title = pageHeader + ": " + $(this).html();
            $(this).click(function(){

                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                _tag.dcsMultiTrack('DCS.dcsqry','','WT.oss','','WT.oss_r','','DCS.dcsuri',hr,'WT.ti',title,
                    'WT.oss_s','1','WT.pi','Successful Searches');
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });
        });
    }
    // set the cookie after links are flagged initially
    // then check for cookie on subsequent page loads

    $.Jookie.Initialise(sCName, "-1");
    var cookieValue = $.Jookie.Get(sCName,sCCrumb);
    if(cookieValue == null || cookieValue == "undefined"){
        $.Jookie.Initialise(sCName, "-1");
        $.Jookie.Set(sCName, sCCrumb, _tag.WT.oss);
    }else if(cookieValue.indexOf(_tag.WT.oss) == -1){
        cookieValue += ";" + _tag.WT.oss;
        $.Jookie.Set(sCName, sCCrumb, cookieValue);
    } else {
        // nothing
        var f = "";
    }
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags a page as being other than a tier one page.  Should run
 * before WebTrends data collection because it sets a meta tag.  Sets <b>WT.z_tier2=1</b>.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagPageTier=function(){
    var tier = $('meta[name="WT.z_tier"]').attr('content');
    if(tier == '1'){
        return;
    } else {
        var name="WT.z_tier2";
        var content="1";

        this.mtAddMeta(name, content);
    }
}

/*
* Run after WT code is loaded
* WT.dl=30 flags clicks to the worldwide Merck sites
*/
/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags clicks to the Merck Worldwide sites.  Intended to track the
 * individual sites, but at this time not possible because the clickthrough is done
 * through a popup overlay and the worldwide site links are not in the text of the
 * containing pages.  Sets <b>WT.dl=30</b> and <b>WT.pi=Worldwide Merck Site Click</b>.
 * @returns {void} 
 */
MerckTracker.prototype.mtFlagWorldwideClick=function(){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    $('a.btn-worldwide').click(function(){

        save(_tag.WT,wt);
        save(_tag.DCS,dcs);

        _tag.dcsMultiTrack('DCS.dcsqry','','DCS.dcsuri',document.location,'WT.ti',
            'Link to Worldwide Site:  '+document.title,'WT.dl','30','WT.pi','Worldwide Merck Site Click');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Modifies the page structure to attach section or resource names to
 * the appropriate links on the pages as parameters in the query string.  Only runs
 * in <i>/mmpe/</i> and <i>/mmhe/</i> paths.  Values are passed in paramters <b>WT.z_resource</b>
 * and <b>WT.z_section</b>.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagSectionsResources=function(){

    if((location.href.indexOf("/mmhe/") == -1) && (location.href.indexOf("/mmpe/")) == -1){
        return;
    }

    $('div div.MMnavTitleBar:contains("Sections")').next().find('li a').each(function(){
        $(this).attr('href', $(this).attr('href')+"?WT.z_section="+$(this).html());

    });

    $('div div.MMnavTitleBar:contains("Resources")').next().find('li a').each(function(){
        $(this).attr('href', $(this).attr('href')+"?WT.z_resource="+$(this).html());

    });

}

/*
* Run after WT code is loaded
* Flags links for offsite clicks (everything not merck.com)
* This excludes merckbooks.com also (any link that does not contain the string
* 'merck' in the URL is flagged)
*
*/
/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags links for offsite clicks.  Offsite click is defined as any
 * site that is not
 * <ul>
 *  <li>A known product site</li>
 *  <li>A known video site</li>
 *  <li>merck.com</li>
 * </ul>
 * Sets <b>WT.dl=24</b> and <b>WT.pi=Non-Merck Site Click</b>.
 * @returns {void}
 */
MerckTracker.prototype.mtFlagOffsiteClick=function(){
    var isProductSiteCheck = this.mtIsProductSiteLink;
    var isVideoSiteCheck = this.mtIsVideoSiteLink;
    var videoSites=["www.careertv.com","get.adobe.com"];
    var sites = this.prodSitesArray;
    var isMerckSite = this.mtIsMerckSite;

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    $('a[href^=http]').each(function(){
        var isProductSite = isProductSiteCheck(sites,this.href);
        var isVideoSite = isVideoSiteCheck(videoSites,this.href);
        var isOnSite = isMerckSite(this.href);
        if(isProductSite == false && isVideoSite == false && isOnSite == false){
            $(this).click(function(){
                save(_tag.WT,wt);
                save(_tag.DCS,dcs);

                var hn = this.hostname;
                _tag.dcsMultiTrack("DCS.dcssip",hn,'DCS.dcsqry','','DCS.dcsuri',this.pathname,
                    'WT.ti','Link to Offsite Page: '+this.innerHTML,'WT.dl','24',
                    'WT.pi','Non-Merck Site Click');
                restore(_tag.WT,wt);
                restore(_tag.DCS,dcs);
            });
        }
    });
}

/**
 * @public
 * @methodOf MerckTracker#
 * @description Flags clicks to <i>merckbooks.com</i>.  Should be run after the
 * WebTrends code is loaded.  Sets <b>WT.dl=37</b> and <b>WT.pi=Merck Book Click</b>.
 * @returns {void}
 */

MerckTracker.prototype.mtFlagMerckBookClick=function(){

    var save = this.mtSaveWT;
    var restore = this.mtRestoreWT;
    var wt = this.wt;
    var dcs = this.dcs;

    $('a[href*=merckbooks.com]').click(function(){

        save(_tag.WT,wt);
        save(_tag.DCS,dcs);

        var hn=this.hostname;
        _tag.dcsMultiTrack("DCS.dcssip",hn,'DCS.dcsqry','','DCS.dcsuri',this.pathname,'WT.ti',this.innerHTML,
            'WT.dl','37','WT.pi','Merck Book Click');
        restore(_tag.WT,wt);
        restore(_tag.DCS,dcs);
    });
}

/*
* This should run after WT code is loaded and before WT data collector is called
* collector for the main site
*
*/
/**
 * @public
 * @methodOf MerckTracker#
 * @description The data collector wrapper for the main site.  This wrapper calls
 * the following methods for collecting data on <i>merck.com</i>.
 * <ul>
 *   <li>mtFlagAudioClick("merck")</li>
 *   <li>mtFlagVideoClick()</li>
 *   <li>mtFlagDownloadClick()</li>
 *   <li>mtFlagEmailClick("merck")</li>
 *   <li>mtFlagPrintClick("merck")</li>
 *   <li>mtSetNavigation()</li>
 *   <li>mtMainTrackSearchClick()</li>
 *   <li>mtSetContentGroups("merck")</li>
 *   <li>mtFlagPageTier()</li>
 *   <li>mtFlagWorldwideClick()</li>
 *   <li>mtFlagProductSiteClick()</li>
 *   <li>mtFlagOffsiteClick()</li>
 *   <li>mtUniquePages()</li>
 * </ul>
 * @returns {void}
 */
MerckTracker.prototype.mtMainCollect=function(){
    this.mtFlagAudioClick("merck");
    this.mtFlagVideoClick();
    this.mtFlagDownloadClick();
    this.mtFlagEmailClick("merck");
    this.mtFlagPrintClick("merck");
    this.mtSetNavigation();
    this.mtMainTrackSearchClick();
    this.mtSetContentGroups("merck");
    this.mtFlagPageTier();
    this.mtFlagWorldwideClick();
    this.mtFlagProductSiteClick();
    this.mtFlagOffsiteClick();
    this.mtUniquePages();
}

/*
* This should run after WT code and before WT data collector is called
* collector for the manuals
* This is for Merck Manuals
*
*/
/**
 * @public
 * @memberOf MerckTracker#
 * @description The data collection wrapper for the manuals site.  This wrapper calls
 * the following methods.
 * <ul>
 *  <li>this.mtFlagAudioClick("manuals")</li>
 *  <li>this.mtFlagVideoClick()</li>
 *  <li>this.mtFlagDownloadClick()</li>
 *  <li>this.mtFlagEmailClick("manuals")</li>
 *  <li>this.mtFlagPrintClick("manuals")</li>
 *  <li>this.mtFlagMerckBookClick()</li>
 *  <li>this.mtSetSubject()</li>
 *  <li>this.mtFlagSectionsResources()</li>
 *  <li>this.mtSetResourceTopic()</li>
 *  <li>this.mtManualsTrackSearchClick()</li>
 *  <li>this.mtFlagAlertSubscribeClick()</li>
 *  <li>this.mtFlagPdaClick()</li>
 *  <li>this.mtFlagItunesClick()</li>
 *  <li>this.mtFlagOffsiteClick()</li>
 *  <li>this.mtSetContentGroups("manuals")</li>
 *  <li>this.mtFlagLexiCompPage()</li>
 *  <li>this.mtFlagTopicPage()</li>
 *  <li>this.mtFlagProductSiteClick()</li>
 *  <li>this.mtOnSiteClick()</li>
 *  <li>this.mtUniquePages()</li>
 * </ul>
 *
 */
MerckTracker.prototype.mtManualsCollect=function(){
    this.mtFlagAudioClick("manuals");
    this.mtFlagVideoClick();
    this.mtFlagDownloadClick();
    this.mtFlagEmailClick("manuals");
    this.mtFlagPrintClick("manuals");
    this.mtFlagMerckBookClick();
    this.mtSetSubject();
    this.mtFlagSectionsResources();
    this.mtSetResourceTopic();
    this.mtManualsTrackSearchClick();
    this.mtFlagAlertSubscribeClick();
    this.mtFlagPdaClick();
    this.mtFlagItunesClick();
    this.mtFlagOffsiteClick();
    this.mtSetContentGroups("manuals");
    this.mtFlagLexiCompPage();
    this.mtFlagTopicPage();
    this.mtFlagProductSiteClick();
    this.mtOnSiteClick();
    this.mtUniquePages();
}

MerckTracker.prototype.mtDebug=function(){

    this.mtAddMeta("TestName", "TestContent");
    var ps = this.prodSitesArray;

}

// function for tracking/counting unique pages viewed by visitors
// written by David Smith
// updated 11/11/09 to fix problems on live site by DTS
/**
 * @public
 * @memberOf MerckTracker#
 * @description Counts unique pages viewed by visitors on a monthly and yearly
 * basis.  It does so by hashing the page name and storing the hash in a cookie
 * along with a date stamp.  It sets meta tags with the yearly and monthly counts
 * of unique pages viewed for the visitor.
 * @returns {void}
 * @see mtCrc16
 */
MerckTracker.prototype.mtUniquePages= function() {
    var sCName="mtP";
    var sCPage=this.mtCrc16(location.pathname);
    var d=new Date();
    var dNow = d.getFullYear()*100+d.getMonth();
    $.Jookie.Initialise(sCName, "5256000"); // 10 year cookie in minutes
    $.Jookie.Set(sCName, sCPage, dNow.toString());

    var cookieList = $.Jookie.Data[sCName].oValues; // get the whole array
    var mcount=0;
    var ycount=0;
    for (var page in cookieList) {
        if (d.getFullYear()!=cookieList[page].substring(0,4)){
            $.Jookie.Unset(sCName,page);
            continue;
        }
        if (cookieList[page]==dNow.toString()) {
            mcount++;
        }
        ycount++;
    }
    this.mtAddMeta("WT.z_distinct_m", mcount.toString());
    this.mtAddMeta("WT.z_distinct_y",ycount.toString());
}

/*
 * =============================================================================
 *                         end of code written by TL
 * =============================================================================
 */

// an addition/plugin to jQuery for parsing query strings
// from http://www.nabble.com/method-plugin-for-getting-query-string-vars--td6919130.html#a6919130
jQuery.query = function() {
    var r = {};
    var q = location.search;
    q = q.replace(/^\?/,''); // remove the leading ?
    q = q.replace(/\&$/,''); // remove the trailing &
    jQuery.each(q.split('&'), function(){
        var key = this.split('=')[0];
        var val = this.split('=')[1];
        // convert floats
        if(/^[0-9.]+$/.test(val))
            val = parseFloat(val);
        // ingnore empty values
        if(val)
            r[key] = val;
    });
    return r;
};

/*
 * Added to Merck project 11 October 2009
 * $Id: mercktracker.js,v 1.8.2.6.2.10.2.11 2009/12/11 21:46:04 powem Exp $
 */

// jQuery cookie plugin
// -------------------------------------------------
/*
  License:
  Jookie 1.0 jQuery Plugin

  Copyright (c) 2008 Jon Combe (http://joncom.be)

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * @class This class is a jQuery plugin that manages cookies.
 * @version 1.0
 * @extends jQuery,$
 */
(function($) {

    $.Jookie = {
        Data:         {},
        /**
     * @public
     * @methodOf $.Jookie#
     * @description Displays information about a cookie.
     * @param {String} a The name of a cookie
     * @returns {void}
     */
        Debug:        function(a)     {
            Debug(a)
        },
        /**
     * @public
     * @methodOf $.Jookie#
     * @description Deletes the specified cookie.
     * @param {String} a The name of the cookie to delete.
     * @returns {void}
     */
        Delete:       function(a)     {
            Delete(a)
        },    // delete cookie
        /**
     * @public
     * @methodOf $.Jookie#
     * @description Get the value of a single cookie crumb.
     * @param {String} a The name of the cookie.
     * @param {String} b The name of the cookie crumb for which to extract
     * the value.
     * @returns {String} The extracted value.
     */
        Get:          function(a,b)   {
            return Get(a,b)
        },    // get a single value from a cookie
        /**
         * @public
         * @methodOf $.Jookie#
         * @description Initialize the plugin.
         * @param {String} a The name of the cookie
         * @param {Integer} b The lifetime of the cookie in minutes
         * @returns {void}
         */
        Initialise:   function(a,b)   {
            Initialise(a,b)
        },
        /**
         * @public
         * @methodOf $.Jookie#
         * @description Set the cookie.
         * @param {String} a The name of the cookie.
         * @param {String} b The cookie crumb.
         * @param {String} c The value of the cookie crumb.
         * @returns {void}
         */
        Set:          function(a,b,c) {

            Set(a,b,c)
        },    // set a single value to a cookie
        /**
         * @public
         * @methodOf $.Jookie#
         * @description Remove or delete a cookie crumb.
         * @param {String} a The cookie to be processed.
         * @param {String} b The cookie crumb identifying the value to be removed.
         * @returns {void}
         */
        Unset:        function(a,b)   {
            Unset(a,b)
        }     // remove a single value from a cookie
    }

    // PUBLIC: show debugging information
    
    function Debug(sName) {
        var lsRegExp = /\+/g;
        var sJSON = unescape(String(Extract(sName)).replace(lsRegExp, " "));
        alert("Name: " + sName +
            "\nLifespan: " + $.Jookie.Data[sName].iLifespan +
            " minutes\nCookie Existed Prior to Init: " + $.Jookie.Data[sName].bMadeEarlier + "\n\n" +
            sJSON);
    }

    // PUBLIC: delete a cookie
    
    function Delete(sName) {
        delete $.Jookie.Data[sName];
        document.cookie = (sName + "=; expires=" + (new Date(1990, 6, 3)).toGMTString() + "; path=/");
    }

    // PRIVATE: extract the contents of a cookie
    /**
     * @private
     * @methodOf $.Jookie
     * @param {String} sName The name of the cookie from which to extract the contents
     * @returns {String} The extracted value.
     */
    function Extract(sName) {
        var vValue = null;
        var aContents = document.cookie.split(';');
        sName += "=";

        // loop through cookie strings
        for (var iIndex in aContents) {
            var sString = aContents[iIndex];
          
            while (sString.charAt(0) == " ") {
                sString = sString.substring(1, sString.length);
            }
            if (sString.indexOf(sName) == 0) {
                vValue = sString.substring(sName.length, sString.length);
                break;
            }
        }

        // return extracted value
        return vValue;
    }

    // PUBLIC: retrieve a cookie's value
   
    function Get(sName, sVariableName) {
        return $.Jookie.Data[sName].oValues[sVariableName];
    }

    // PUBLIC: Initialise the plugin
    function Initialise(sName, iLifespanInMinutes) {
        if (typeof $.Jookie.Data[sName] == "undefined") {
            var oRetrievedValues = {};
            var bCookieExists = false;

            // extract cookie value
            var vCookieValue = Extract(sName);
            if (vCookieValue !== null) {
                oRetrievedValues = JSON.parse( unescape(String(vCookieValue).replace(/\+/g, " ")) );
                bCookieExists = true;
            }

            // add cookie details to object
            $.Jookie.Data[sName] = {
                iLifespan    : iLifespanInMinutes,
                bMadeEarlier : bCookieExists,
                oValues      : oRetrievedValues
            };
            Save(sName);
        }
    }

    // PRIVATE: write cookie to user's browser
    /**
     * @private
     * @methodOf $.Jookie
     * @description Writes a given cookie to the browser.
     * @param {String} sName The name of the cookie to write.
     * @returns {void}
     */
    function Save(sName) {
        var sExpires = "";
        if ($.Jookie.Data[sName].iLifespan > 0) {
            var dtDate = new Date();
            dtDate.setMinutes(dtDate.getMinutes() + $.Jookie.Data[sName].iLifespan);
            sExpires = ("; expires=" + dtDate.toGMTString());
        }
        document.cookie = (sName + "=" +
            escape(JSON.stringify($.Jookie.Data[sName].oValues)) +
            sExpires + "; path=/");
    }

    // PUBLIC: set and save a cookie's value
    function Set(sName, sVariableName, vValue) {
        $.Jookie.Data[sName].oValues[sVariableName] = vValue;
        Save(sName);
    }

    // PUBLIC: delete a single variable from a cookie
    function Unset(sName, sVariableName) {
        delete $.Jookie.Data[sName].oValues[sVariableName];
        Save(sName);
    }

})(jQuery);

/*
    http://www.JSON.org/json2.js
    2008-05-25

    Public Domain.

    NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.

    See http://www.JSON.org/js.html

    This file creates a global JSON object containing two methods: stringify
    and parse.

        JSON.stringify(value, replacer, space)
            value       any JavaScript value, usually an object or array.

            replacer    an optional parameter that determines how object
                        values are stringified for objects without a toJSON
                        method. It can be a function or an array.

            space       an optional parameter that specifies the indentation
                        of nested structures. If it is omitted, the text will
                        be packed without extra whitespace. If it is a number,
                        it will specify the number of spaces to indent at each
                        level. If it is a string (such as '\t' or '&nbsp;'),
                        it contains the characters used to indent at each level.

            This method produces a JSON text from a JavaScript value.

            When an object value is found, if the object contains a toJSON
            method, its toJSON method will be called and the result will be
            stringified. A toJSON method does not serialize: it returns the
            value represented by the name/value pair that should be serialized,
            or undefined if nothing should be serialized. The toJSON method
            will be passed the key associated with the value, and this will be
            bound to the object holding the key.

            For example, this would serialize Dates as ISO strings.

                Date.prototype.toJSON = function (key) {
                    function f(n) {
                        // Format integers to have at least two digits.
                        return n < 10 ? '0' + n : n;
                    }

                    return this.getUTCFullYear()   + '-' +
                         f(this.getUTCMonth() + 1) + '-' +
                         f(this.getUTCDate())      + 'T' +
                         f(this.getUTCHours())     + ':' +
                         f(this.getUTCMinutes())   + ':' +
                         f(this.getUTCSeconds())   + 'Z';
                };

            You can provide an optional replacer method. It will be passed the
            key and value of each member, with this bound to the containing
            object. The value that is returned from your method will be
            serialized. If your method returns undefined, then the member will
            be excluded from the serialization.

            If the replacer parameter is an array, then it will be used to
            select the members to be serialized. It filters the results such
            that only members with keys listed in the replacer array are
            stringified.

            Values that do not have JSON representations, such as undefined or
            functions, will not be serialized. Such values in objects will be
            dropped; in arrays they will be replaced with null. You can use
            a replacer function to replace those with JSON values.
            JSON.stringify(undefined) returns undefined.

            The optional space parameter produces a stringification of the
            value that is filled with line breaks and indentation to make it
            easier to read.

            If the space parameter is a non-empty string, then that string will
            be used for indentation. If the space parameter is a number, then
            the indentation will be that many spaces.

            Example:

            text = JSON.stringify(['e', {pluribus: 'unum'}]);
            // text is '["e",{"pluribus":"unum"}]'


            text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
            // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'

            text = JSON.stringify([new Date()], function (key, value) {
                return this[key] instanceof Date ?
                    'Date(' + this[key] + ')' : value;
            });
            // text is '["Date(---current time---)"]'


        JSON.parse(text, reviver)
            This method parses a JSON text to produce an object or array.
            It can throw a SyntaxError exception.

            The optional reviver parameter is a function that can filter and
            transform the results. It receives each of the keys and values,
            and its return value is used instead of the original value.
            If it returns what it received, then the structure is not modified.
            If it returns undefined then the member is deleted.

            Example:

            // Parse the text. Values that look like ISO date strings will
            // be converted to Date objects.

            myData = JSON.parse(text, function (key, value) {
                var a;
                if (typeof value === 'string') {
                    a =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
                    if (a) {
                        return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
                            +a[5], +a[6]));
                    }
                }
                return value;
            });

            myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
                var d;
                if (typeof value === 'string' &&
                        value.slice(0, 5) === 'Date(' &&
                        value.slice(-1) === ')') {
                    d = new Date(value.slice(5, -1));
                    if (d) {
                        return d;
                    }
                }
                return value;
            });


    This is a reference implementation. You are free to copy, modify, or
    redistribute.

    This code should be minified before deployment.
    See http://javascript.crockford.com/jsmin.html

    USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
    NOT CONTROL.
*/

/*jslint evil: true */

/*global JSON */

/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", call,
    charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, getUTCMinutes,
    getUTCMonth, getUTCSeconds, hasOwnProperty, join, lastIndex, length,
    parse, propertyIsEnumerable, prototype, push, replace, slice, stringify,
    test, toJSON, toString
*/

if (!this.JSON) {

    // Create a JSON object only if one does not already exist. We create the
    // object in a closure to avoid creating global variables.

    JSON = function () {

        function f(n) {
            // Format integers to have at least two digits.
            return n < 10 ? '0' + n : n;
        }

        Date.prototype.toJSON = function (key) {

            return this.getUTCFullYear()   + '-' +
            f(this.getUTCMonth() + 1) + '-' +
            f(this.getUTCDate())      + 'T' +
            f(this.getUTCHours())     + ':' +
            f(this.getUTCMinutes())   + ':' +
            f(this.getUTCSeconds())   + 'Z';
        };

        var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        escapeable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        gap,
        indent,
        meta = {    // table of character substitutions
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        rep;


        function quote(string) {

            // If the string contains no control characters, no quote characters, and no
            // backslash characters, then we can safely slap some quotes around it.
            // Otherwise we must also replace the offending characters with safe escape
            // sequences.

            escapeable.lastIndex = 0;
            return escapeable.test(string) ?
            '"' + string.replace(escapeable, function (a) {
                var c = meta[a];
                if (typeof c === 'string') {
                    return c;
                }
                return '\\u' + ('0000' +
                    (+(a.charCodeAt(0))).toString(16)).slice(-4);
            }) + '"' :
            '"' + string + '"';
        }


        function str(key, holder) {

            // Produce a string from holder[key].

            var i,          // The loop counter.
            k,          // The member key.
            v,          // The member value.
            length,
            mind = gap,
            partial,
            value = holder[key];

            // If the value has a toJSON method, call it to obtain a replacement value.

            if (value && typeof value === 'object' &&
                typeof value.toJSON === 'function') {
                value = value.toJSON(key);
            }

            // If we were called with a replacer function, then call the replacer to
            // obtain a replacement value.

            if (typeof rep === 'function') {
                value = rep.call(holder, key, value);
            }

            // What happens next depends on the value's type.

            switch (typeof value) {
                case 'string':
                    return quote(value);

                case 'number':

                    // JSON numbers must be finite. Encode non-finite numbers as null.

                    return isFinite(value) ? String(value) : 'null';

                case 'boolean':
                case 'null':

                    // If the value is a boolean or null, convert it to a string. Note:
                    // typeof null does not produce 'null'. The case is included here in
                    // the remote chance that this gets fixed someday.

                    return String(value);

                // If the type is 'object', we might be dealing with an object or an array or
                // null.

                case 'object':

                    // Due to a specification blunder in ECMAScript, typeof null is 'object',
                    // so watch out for that case.

                    if (!value) {
                        return 'null';
                    }

                    // Make an array to hold the partial results of stringifying this object value.

                    gap += indent;
                    partial = [];

                    // If the object has a dontEnum length property, we'll treat it as an array.

                    if (typeof value.length === 'number' &&
                        !(value.propertyIsEnumerable('length'))) {

                        // The object is an array. Stringify every element. Use null as a placeholder
                        // for non-JSON values.

                        length = value.length;
                        for (i = 0; i < length; i += 1) {
                            partial[i] = str(i, value) || 'null';
                        }

                        // Join all of the elements together, separated with commas, and wrap them in
                        // brackets.

                        v = partial.length === 0 ? '[]' :
                        gap ? '[\n' + gap +
                        partial.join(',\n' + gap) + '\n' +
                        mind + ']' :
                        '[' + partial.join(',') + ']';
                        gap = mind;
                        return v;
                    }

                    // If the replacer is an array, use it to select the members to be stringified.

                    if (rep && typeof rep === 'object') {
                        length = rep.length;
                        for (i = 0; i < length; i += 1) {
                            k = rep[i];
                            if (typeof k === 'string') {
                                v = str(k, value, rep);
                                if (v) {
                                    partial.push(quote(k) + (gap ? ': ' : ':') + v);
                                }
                            }
                        }
                    } else {

                        // Otherwise, iterate through all of the keys in the object.

                        for (k in value) {
                            if (Object.hasOwnProperty.call(value, k)) {
                                v = str(k, value, rep);
                                if (v) {
                                    partial.push(quote(k) + (gap ? ': ' : ':') + v);
                                }
                            }
                        }
                    }

                    // Join all of the member texts together, separated with commas,
                    // and wrap them in braces.

                    v = partial.length === 0 ? '{}' :
                    gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
                    mind + '}' : '{' + partial.join(',') + '}';
                    gap = mind;
                    return v;
            }
        }

        // Return the JSON object containing the stringify and parse methods.

        return {
            stringify: function (value, replacer, space) {

                // The stringify method takes a value and an optional replacer, and an optional
                // space parameter, and returns a JSON text. The replacer can be a function
                // that can replace values, or an array of strings that will select the keys.
                // A default replacer method can be provided. Use of the space parameter can
                // produce text that is more easily readable.

                var i;
                gap = '';
                indent = '';

                // If the space parameter is a number, make an indent string containing that
                // many spaces.

                if (typeof space === 'number') {
                    for (i = 0; i < space; i += 1) {
                        indent += ' ';
                    }

                // If the space parameter is a string, it will be used as the indent string.

                } else if (typeof space === 'string') {
                    indent = space;
                }

                // If there is a replacer, it must be a function or an array.
                // Otherwise, throw an error.

                rep = replacer;
                if (replacer && typeof replacer !== 'function' &&
                    (typeof replacer !== 'object' ||
                        typeof replacer.length !== 'number')) {
                    throw new Error('JSON.stringify');
                }

                // Make a fake root object containing our value under the key of ''.
                // Return the result of stringifying the value.

                return str('', {
                    '': value
                });
            },


            parse: function (text, reviver) {

                // The parse method takes a text and an optional reviver function, and returns
                // a JavaScript value if the text is a valid JSON text.

                var j;

                function walk(holder, key) {

                    // The walk method is used to recursively walk the resulting structure so
                    // that modifications can be made.

                    var k, v, value = holder[key];
                    if (value && typeof value === 'object') {
                        for (k in value) {
                            if (Object.hasOwnProperty.call(value, k)) {
                                v = walk(value, k);
                                if (v !== undefined) {
                                    value[k] = v;
                                } else {
                                    delete value[k];
                                }
                            }
                        }
                    }
                    return reviver.call(holder, key, value);
                }


                // Parsing happens in four stages. In the first stage, we replace certain
                // Unicode characters with escape sequences. JavaScript handles many characters
                // incorrectly, either silently deleting them, or treating them as line endings.

                cx.lastIndex = 0;
                if (cx.test(text)) {
                    text = text.replace(cx, function (a) {
                        return '\\u' + ('0000' +
                            (+(a.charCodeAt(0))).toString(16)).slice(-4);
                    });
                }

                // In the second stage, we run the text against regular expressions that look
                // for non-JSON patterns. We are especially concerned with '()' and 'new'
                // because they can cause invocation, and '=' because it can cause mutation.
                // But just to be safe, we want to reject all unexpected forms.

                // We split the second stage into 4 regexp operations in order to work around
                // crippling inefficiencies in IE's and Safari's regexp engines. First we
                // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
                // replace all simple value tokens with ']' characters. Third, we delete all
                // open brackets that follow a colon or comma or that begin the text. Finally,
                // we look to see that the remaining characters are only whitespace or ']' or
                // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.

                if (/^[\],:{}\s]*$/.
                    test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
                        replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
                        replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

                    // In the third stage we use the eval function to compile the text into a
                    // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
                    // in JavaScript: it can begin a block or an object literal. We wrap the text
                    // in parens to eliminate the ambiguity.

                    j = eval('(' + text + ')');

                    // In the optional fourth stage, we recursively walk the new structure, passing
                    // each name/value pair to a reviver function for possible transformation.

                    return typeof reviver === 'function' ?
                    walk({
                        '': j
                    }, '') : j;
                }

                // If the text is not JSON parseable, then a SyntaxError is thrown.

                throw new SyntaxError('JSON.parse');
            }
        };
    }();
}

/* ===========================================================================
*
* JQuery URL Parser
* Version 1.0
* Parses URLs and provides easy access to information within them.
*
* Author: Mark Perkins
* Author email: mark@allmarkedup.com
*
* For full documentation and more go to http://projects.allmarkedup.com/jquery_url_parser/
*
* ---------------------------------------------------------------------------
*
* CREDITS:
*
* Parser based on the Regex-based URI parser by Steven Levithan.
* For more information (including a detailed explaination of the differences
* between the 'loose' and 'strict' pasing modes) visit http://blog.stevenlevithan.com/archives/parseuri
*
* ---------------------------------------------------------------------------
*
* LICENCE:
*
* Released under a MIT Licence. See licence.txt that should have been supplied with this file,
* or visit http://projects.allmarkedup.com/jquery_url_parser/licence.txt
*
* ---------------------------------------------------------------------------
*
* EXAMPLES OF USE:
*
* Get the domain name (host) from the current page URL
* jQuery.url.attr("host")
*
* Get the query string value for 'item' for the current page
* jQuery.url.param("item") // null if it doesn't exist
*
* Get the second segment of the URI of the current page
* jQuery.url.segment(2) // null if it doesn't exist
*
* Get the protocol of a manually passed in URL
* jQuery.url.setUrl("http://allmarkedup.com/").attr("protocol") // returns 'http'
*
*/

/**
 * @class
 * @version 1.0
 * @property {Object} segments An object containing the segments of the URL path.
 * @property {Object} parsed
 * 
 */
jQuery.url = function()
{
    var segments = {};

    var parsed = {};

    /**
     * Options object. Only the URI and strictMode values can be changed via the setters below.
     */
    var options = {

        url : window.location, // default URI is the page in which the script is running

        strictMode: false, // 'loose' parsing by default

        key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query

        q: {
            name: "queryKey",
            parser: /(?:^|&)([^&=]*)=?([^&]*)/g
        },

        parser: {
            strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,  //less intuitive, more accurate to the specs
            loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
        }

    };

    /**
     * Deals with the parsing of the URI according to the regex above.
     * Written by Steven Levithan - see credits at top.
     */
    var parseUri = function()
    {
        str = decodeURI( options.url );

        var m = options.parser[ options.strictMode ? "strict" : "loose" ].exec( str );
        var uri = {};
        var i = 14;

        while ( i-- ) {
            uri[ options.key[i] ] = m[i] || "";
        }

        uri[ options.q.name ] = {};
        uri[ options.key[12] ].replace( options.q.parser, function ( $0, $1, $2 ) {
            if ($1) {
                uri[options.q.name][$1] = $2;
            }
        });

        return uri;
    };

    /**
     * Returns the value of the passed in key from the parsed URI.
     *
     * @param {String} key The key whose value is required
     * @returns {String} The desired value.
     */
    var key = function( key )
    {
        if ( ! parsed.length )
        {
            setUp(); // if the URI has not been parsed yet then do this first...
        }
        if ( key == "base" )
        {
            if ( parsed.port !== null && parsed.port !== "" )
            {
                return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/";
            }
            else
            {
                return parsed.protocol+"://"+parsed.host+"/";
            }
        }

        return ( parsed[key] === "" ) ? null : parsed[key];
    };

    /**
     * @public
     * @methodOf $.url#
     * @description Returns the value of the required query string parameter.
     * @param {String} item The parameter whose value is required
     * @returns {String} The extracted value.
     */
    var param = function( item )
    {
        if ( ! parsed.length )
        {
            setUp(); // if the URI has not been parsed yet then do this first...
        }
        return ( parsed.queryKey[item] === null ) ? null : parsed.queryKey[item];
    };

    /**
     * @private
     * @description 'Constructor' (not really!) function.  Called whenever the
     * URI changes to kick off re-parsing of the URI and splitting it up into segments.
     */
    var setUp = function()
    {
        parsed = parseUri();

        getSegments();
    };

    /**
     * Splits up the body of the URI into segments (i.e. sections delimited by '/')
     */
    var getSegments = function()
    {
        var p = parsed.path;
        segments = []; // clear out segments array
        segments = parsed.path.length == 1 ? {} : ( p.charAt( p.length - 1 ) == "/" ? p.substring( 1, p.length - 1 ) : path = p.substring( 1 ) ).split("/");
    };

    return {

        /**
         * Sets the parsing mode - either strict or loose. Set to loose by default.
         *
         * @param {String} mode The mode to set the parser to. Anything apart from a
         * value of 'strict' will set it to loose!
         * @returns {Object} this a reference to the current object.
         */
        setMode : function( mode )
        {
            strictMode = mode == "strict" ? true : false;
            return this;
        },

        /**
         * Sets URI to parse if you don't want to to parse the current page's URI.
         * Calling the function with no value for newUri resets it to the current page's URI.
         *
         * @param string newUri The URI to parse.
         * @returns {Object} this A reference to the current object.
         */
        setUrl : function( newUri )
        {
            options.url = newUri === undefined ? window.location : newUri;
            setUp();
            return this;
        },

        /**
         * Returns the value of the specified URI segment. Segments are numbered
         * from 1 to the number of segments.
         * For example the URI http://test.com/about/company/ segment(1) would return 'about'.
         *
         * If no integer is passed into the function it returns the number of segments in the URI.
         *
         * @param int pos The position of the segment to return. Can be empty.
         * @returns {String} The value of the segments array at the specified index.
         */
        segment : function( pos )
        {
            if ( ! parsed.length )
            {
                setUp(); // if the URI has not been parsed yet then do this first...
            }
            if ( pos === undefined )
            {
                return segments.length;
            }
            return ( segments[pos] === "" || segments[pos] === undefined ) ? null : segments[pos];
        },

        attr : key, // provides public access to private 'key' function - see above

        param : param // provides public access to private 'param' function - see above

    };

}();


/*
    http://www.JSON.org/json2.js
    2008-05-25

    Public Domain.

    NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.

    See http://www.JSON.org/js.html

    This file creates a global JSON object containing two methods: stringify
    and parse.

        JSON.stringify(value, replacer, space)
            value       any JavaScript value, usually an object or array.

            replacer    an optional parameter that determines how object
                        values are stringified for objects without a toJSON
                        method. It can be a function or an array.

            space       an optional parameter that specifies the indentation
                        of nested structures. If it is omitted, the text will
                        be packed without extra whitespace. If it is a number,
                        it will specify the number of spaces to indent at each
                        level. If it is a string (such as '\t' or '&nbsp;'),
                        it contains the characters used to indent at each level.

            This method produces a JSON text from a JavaScript value.

            When an object value is found, if the object contains a toJSON
            method, its toJSON method will be called and the result will be
            stringified. A toJSON method does not serialize: it returns the
            value represented by the name/value pair that should be serialized,
            or undefined if nothing should be serialized. The toJSON method
            will be passed the key associated with the value, and this will be
            bound to the object holding the key.

            For example, this would serialize Dates as ISO strings.

                Date.prototype.toJSON = function (key) {
                    function f(n) {
                        // Format integers to have at least two digits.
                        return n < 10 ? '0' + n : n;
                    }

                    return this.getUTCFullYear()   + '-' +
                         f(this.getUTCMonth() + 1) + '-' +
                         f(this.getUTCDate())      + 'T' +
                         f(this.getUTCHours())     + ':' +
                         f(this.getUTCMinutes())   + ':' +
                         f(this.getUTCSeconds())   + 'Z';
                };

            You can provide an optional replacer method. It will be passed the
            key and value of each member, with this bound to the containing
            object. The value that is returned from your method will be
            serialized. If your method returns undefined, then the member will
            be excluded from the serialization.

            If the replacer parameter is an array, then it will be used to
            select the members to be serialized. It filters the results such
            that only members with keys listed in the replacer array are
            stringified.

            Values that do not have JSON representations, such as undefined or
            functions, will not be serialized. Such values in objects will be
            dropped; in arrays they will be replaced with null. You can use
            a replacer function to replace those with JSON values.
            JSON.stringify(undefined) returns undefined.

            The optional space parameter produces a stringification of the
            value that is filled with line breaks and indentation to make it
            easier to read.

            If the space parameter is a non-empty string, then that string will
            be used for indentation. If the space parameter is a number, then
            the indentation will be that many spaces.

            Example:

            text = JSON.stringify(['e', {pluribus: 'unum'}]);
            // text is '["e",{"pluribus":"unum"}]'


            text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
            // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'

            text = JSON.stringify([new Date()], function (key, value) {
                return this[key] instanceof Date ?
                    'Date(' + this[key] + ')' : value;
            });
            // text is '["Date(---current time---)"]'


        JSON.parse(text, reviver)
            This method parses a JSON text to produce an object or array.
            It can throw a SyntaxError exception.

            The optional reviver parameter is a function that can filter and
            transform the results. It receives each of the keys and values,
            and its return value is used instead of the original value.
            If it returns what it received, then the structure is not modified.
            If it returns undefined then the member is deleted.

            Example:

            // Parse the text. Values that look like ISO date strings will
            // be converted to Date objects.

            myData = JSON.parse(text, function (key, value) {
                var a;
                if (typeof value === 'string') {
                    a =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
                    if (a) {
                        return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
                            +a[5], +a[6]));
                    }
                }
                return value;
            });

            myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
                var d;
                if (typeof value === 'string' &&
                        value.slice(0, 5) === 'Date(' &&
                        value.slice(-1) === ')') {
                    d = new Date(value.slice(5, -1));
                    if (d) {
                        return d;
                    }
                }
                return value;
            });


    This is a reference implementation. You are free to copy, modify, or
    redistribute.

    This code should be minified before deployment.
    See http://javascript.crockford.com/jsmin.html

    USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
    NOT CONTROL.
*/

/*jslint evil: true */

/*global JSON */

/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", call,
    charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, getUTCMinutes,
    getUTCMonth, getUTCSeconds, hasOwnProperty, join, lastIndex, length,
    parse, propertyIsEnumerable, prototype, push, replace, slice, stringify,
    test, toJSON, toString
*/

if (!this.JSON) {

    // Create a JSON object only if one does not already exist. We create the
    // object in a closure to avoid creating global variables.
    /**
 * @public
 * @description A JSON object
 *
 */
    JSON = function () {

        function f(n) {
            // Format integers to have at least two digits.
            return n < 10 ? '0' + n : n;
        }

        Date.prototype.toJSON = function (key) {

            return this.getUTCFullYear()   + '-' +
            f(this.getUTCMonth() + 1) + '-' +
            f(this.getUTCDate())      + 'T' +
            f(this.getUTCHours())     + ':' +
            f(this.getUTCMinutes())   + ':' +
            f(this.getUTCSeconds())   + 'Z';
        };

        var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        escapeable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        gap,
        indent,
        meta = {    // table of character substitutions
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        rep;


        function quote(string) {

            // If the string contains no control characters, no quote characters, and no
            // backslash characters, then we can safely slap some quotes around it.
            // Otherwise we must also replace the offending characters with safe escape
            // sequences.

            escapeable.lastIndex = 0;
            return escapeable.test(string) ?
            '"' + string.replace(escapeable, function (a) {
                var c = meta[a];
                if (typeof c === 'string') {
                    return c;
                }
                return '\\u' + ('0000' +
                    (+(a.charCodeAt(0))).toString(16)).slice(-4);
            }) + '"' :
            '"' + string + '"';
        }


        function str(key, holder) {

            // Produce a string from holder[key].

            var i,          // The loop counter.
            k,          // The member key.
            v,          // The member value.
            length,
            mind = gap,
            partial,
            value = holder[key];

            // If the value has a toJSON method, call it to obtain a replacement value.

            if (value && typeof value === 'object' &&
                typeof value.toJSON === 'function') {
                value = value.toJSON(key);
            }

            // If we were called with a replacer function, then call the replacer to
            // obtain a replacement value.

            if (typeof rep === 'function') {
                value = rep.call(holder, key, value);
            }

            // What happens next depends on the value's type.

            switch (typeof value) {
                case 'string':
                    return quote(value);

                case 'number':

                    // JSON numbers must be finite. Encode non-finite numbers as null.

                    return isFinite(value) ? String(value) : 'null';

                case 'boolean':
                case 'null':

                    // If the value is a boolean or null, convert it to a string. Note:
                    // typeof null does not produce 'null'. The case is included here in
                    // the remote chance that this gets fixed someday.

                    return String(value);

                // If the type is 'object', we might be dealing with an object or an array or
                // null.

                case 'object':

                    // Due to a specification blunder in ECMAScript, typeof null is 'object',
                    // so watch out for that case.

                    if (!value) {
                        return 'null';
                    }

                    // Make an array to hold the partial results of stringifying this object value.

                    gap += indent;
                    partial = [];

                    // If the object has a dontEnum length property, we'll treat it as an array.

                    if (typeof value.length === 'number' &&
                        !(value.propertyIsEnumerable('length'))) {

                        // The object is an array. Stringify every element. Use null as a placeholder
                        // for non-JSON values.

                        length = value.length;
                        for (i = 0; i < length; i += 1) {
                            partial[i] = str(i, value) || 'null';
                        }

                        // Join all of the elements together, separated with commas, and wrap them in
                        // brackets.

                        v = partial.length === 0 ? '[]' :
                        gap ? '[\n' + gap +
                        partial.join(',\n' + gap) + '\n' +
                        mind + ']' :
                        '[' + partial.join(',') + ']';
                        gap = mind;
                        return v;
                    }

                    // If the replacer is an array, use it to select the members to be stringified.

                    if (rep && typeof rep === 'object') {
                        length = rep.length;
                        for (i = 0; i < length; i += 1) {
                            k = rep[i];
                            if (typeof k === 'string') {
                                v = str(k, value, rep);
                                if (v) {
                                    partial.push(quote(k) + (gap ? ': ' : ':') + v);
                                }
                            }
                        }
                    } else {

                        // Otherwise, iterate through all of the keys in the object.

                        for (k in value) {
                            if (Object.hasOwnProperty.call(value, k)) {
                                v = str(k, value, rep);
                                if (v) {
                                    partial.push(quote(k) + (gap ? ': ' : ':') + v);
                                }
                            }
                        }
                    }

                    // Join all of the member texts together, separated with commas,
                    // and wrap them in braces.

                    v = partial.length === 0 ? '{}' :
                    gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
                    mind + '}' : '{' + partial.join(',') + '}';
                    gap = mind;
                    return v;
            }
        }

        // Return the JSON object containing the stringify and parse methods.

        return {
            stringify: function (value, replacer, space) {

                // The stringify method takes a value and an optional replacer, and an optional
                // space parameter, and returns a JSON text. The replacer can be a function
                // that can replace values, or an array of strings that will select the keys.
                // A default replacer method can be provided. Use of the space parameter can
                // produce text that is more easily readable.

                var i;
                gap = '';
                indent = '';

                // If the space parameter is a number, make an indent string containing that
                // many spaces.

                if (typeof space === 'number') {
                    for (i = 0; i < space; i += 1) {
                        indent += ' ';
                    }

                // If the space parameter is a string, it will be used as the indent string.

                } else if (typeof space === 'string') {
                    indent = space;
                }

                // If there is a replacer, it must be a function or an array.
                // Otherwise, throw an error.

                rep = replacer;
                if (replacer && typeof replacer !== 'function' &&
                    (typeof replacer !== 'object' ||
                        typeof replacer.length !== 'number')) {
                    throw new Error('JSON.stringify');
                }

                // Make a fake root object containing our value under the key of ''.
                // Return the result of stringifying the value.

                return str('', {
                    '': value
                });
            },


            parse: function (text, reviver) {

                // The parse method takes a text and an optional reviver function, and returns
                // a JavaScript value if the text is a valid JSON text.

                var j;

                function walk(holder, key) {

                    // The walk method is used to recursively walk the resulting structure so
                    // that modifications can be made.

                    var k, v, value = holder[key];
                    if (value && typeof value === 'object') {
                        for (k in value) {
                            if (Object.hasOwnProperty.call(value, k)) {
                                v = walk(value, k);
                                if (v !== undefined) {
                                    value[k] = v;
                                } else {
                                    delete value[k];
                                }
                            }
                        }
                    }
                    return reviver.call(holder, key, value);
                }


                // Parsing happens in four stages. In the first stage, we replace certain
                // Unicode characters with escape sequences. JavaScript handles many characters
                // incorrectly, either silently deleting them, or treating them as line endings.

                cx.lastIndex = 0;
                if (cx.test(text)) {
                    text = text.replace(cx, function (a) {
                        return '\\u' + ('0000' +
                            (+(a.charCodeAt(0))).toString(16)).slice(-4);
                    });
                }

                // In the second stage, we run the text against regular expressions that look
                // for non-JSON patterns. We are especially concerned with '()' and 'new'
                // because they can cause invocation, and '=' because it can cause mutation.
                // But just to be safe, we want to reject all unexpected forms.

                // We split the second stage into 4 regexp operations in order to work around
                // crippling inefficiencies in IE's and Safari's regexp engines. First we
                // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
                // replace all simple value tokens with ']' characters. Third, we delete all
                // open brackets that follow a colon or comma or that begin the text. Finally,
                // we look to see that the remaining characters are only whitespace or ']' or
                // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.

                if (/^[\],:{}\s]*$/.
                    test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
                        replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
                        replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

                    // In the third stage we use the eval function to compile the text into a
                    // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
                    // in JavaScript: it can begin a block or an object literal. We wrap the text
                    // in parens to eliminate the ambiguity.

                    j = eval('(' + text + ')');

                    // In the optional fourth stage, we recursively walk the new structure, passing
                    // each name/value pair to a reviver function for possible transformation.

                    return typeof reviver === 'function' ?
                    walk({
                        '': j
                    }, '') : j;
                }

                // If the text is not JSON parseable, then a SyntaxError is thrown.

                throw new SyntaxError('JSON.parse');
            }
        };
    }();
}

