/* vs.js ----------------------------------------------------------*/

//Version: JT02
//V1 of Instrumentation Toolkit Addition


var _JT=new Object();
_JT.protocol=location.protocol;//override "https:"
_JT.v="JT01.02";
_JT.nspace="pt";
_JT.log="1";

//assign site code values
_JT.site=new Array();
_JT.site['sensor']='vs.dmtracker.com/images/zig.gif';
_JT.site['lt']=0;
_JT.site['sensor2']='extended.dmtracker.com/images/zig.gif';

//DM Edit by Mark Pennell
//client side gen'd boone user cookie
_JT.BNUC = (function() {
    var bnuc = null;
    try {
        var arg = "BNUC=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg) {
                var endstr = document.cookie.indexOf(";", j);
                if (endstr == -1)
                    endstr = document.cookie.length;
                bnuc = document.cookie.substring(j, endstr);
                break;
            }
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) break;
        }
    } catch(e) {
        bnuc = null;
    }
    if (bnuc == null) {
        bnuc = new Date().getTime().toString()+Math.random().toString().substring(1,8)+Math.random().toString().substring(1,8);
        try {
            document.cookie = "BNUC=" + bnuc;
        } catch(e) {
            if (!bnuc) {
                bnuc = 'error';
            }
        }
    }
    var pglt = new Date().getTime().toString();
    var pgctr = 0;
    return function() {
        return bnuc+'_'+pglt+'_'+(pgctr = pgctr +1);
    };
})();


//DM Extended functions
//By Aakshi Sehdave (Omniture)

_JT.DM=function(){var m = document.getElementsByTagName("meta");for (z = 0; z < m.length; z++) {if (m[z].getAttribute("scheme") == "DMINSTR2")
{_JT.SET(m[z].getAttribute("name"),_JT.DM_stripChars(m[z].getAttribute("content")));}}}
_JT.DM_Impr=function(){var o = document.getElementsByTagName("div");for (i = 0; i < o.length; i++) {if (o[i].getAttribute("id") == "DMINSTR")
{_JT.DM(); var str = o[i].getAttribute("type") + o[i].getAttribute("name");_JT.SET("ev",str);_JT.SEND_ext();}}}
_JT.DM_Click=function(a){_JT.DM();var s = a.getAttribute("type") + "_click"; var str = s + a.getAttribute("name"); _JT.SET("ev",str);_JT.SEND_ext();}
_JT.DM_stripChars=function(a){var b=new Array(">","<",";","'","`","^","[","]","{","}","#","\\","|","~","&");for(var c=0;c<b.length;c++){a=(a.split(b[c])).join("");}return a;}


//DO NOT EDIT BELOW
_JT.ES=function(a){return (_JT.D(encodeURI))?encodeURI(a):escape(a)}
_JT.NA=function(){return new Array()};_JT.D=function(v){return(typeof v!="undefined")?1:0};_JT.L=function(a){return _JT.D(a)?a.length:0};
_JT.I=function(a,b,c){return a.indexOf(b,c?c:0)};_JT.S=function(a,b,c){return b>_JT.L(a)?"":a.substring(b,c!=null?c:_JT.L(a))};
_JT.Q=function(a,b){if(_JT.hf==1)_JT.hf=0;else{a+="&vid="+Math.random()/*DM Mark Pennell*/+"&bnuc="+_JT.BNUC()/*DM Mark Pennell*/;b=_JT.ii;if(b>=_JT.im)_JT.ii=0;_JT.ia[b]="";
_JT.ia[b]=new Image();a=(a.split('+')).join('%2B');_JT.ia[b].src=a;_JT.last=a;_JT.ii++}};
_JT.EL=function(a,b,c,d){if(a.addEventListener){a.addEventListener(b,c,false)}else if(a.attachEvent){a.attachEvent(((d==1)?"":"on")+b,c)}};
_JT.EI=function(a,b,c){if(typeof eval("window._JTOn"+a)=="function")eval("window._JTOn"+a+"(b)")};
//Edited to add call too _JT.DM by Aakshi Sehdave (Omniture)
_JT.LT=function(e){if((e.which&&e.which==1)||(e.button&&e.button==1)){var a=document.all?window.event.srcElement:this;var x='',y='';
if(e.pageX||e.pageY){x=e.pageX;y=e.pageY}else if(e.clientX||e.clientY){x=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
y=e.clientY+document.body.scrollTop+document.documentElement.scrollTop}_JT.SET('lidx',x);_JT.SET('lidy',y);
if(a.tagName&&a.tagName.toLowerCase()=="a"){_JT.SET("lid",encodeURI(a.href));_JT.SET("ev","link");_JT.DM(); _JT.SEND("Link",a)}}};
_JT.SET=function(a,b,c){_JT.da[a]=(c==0)?b:_JT.ES(b)};_JT.SEND=function(a,b){if(_JT.D(a))_JT.EI(a,b);a="";for(var i in _JT.da){
if(typeof _JT.da[i]!="function"){a+="&"+i+"=";a+=_JT.da[i]}}_JT.Q(_JT.rq+a);_JT.da=_JT.NA()};_JT.HALT=function(){_JT.hf=1}
//Added another Send function for EXtended functions which uses a different sensor by Aakshi Sehdave (Omniture)
_JT.SEND_ext=function(a,b){if(_JT.D(a))_JT.EI(a,b);a="";for(var i in _JT.da){
if(typeof _JT.da[i]!="function"){a+="&"+i+"=";a+=_JT.da[i]}}_JT.Q(_JT.rq2+a);_JT.da=_JT.NA()};_JT.HALT=function(){_JT.hf=1}

//Edited to add call too _JT.DM by Aakshi Sehdave (Omniture)
_JT.P=function(v,n,s,r){v=11;n=navigator;s=screen;r=_JT.NA();_JT.SET("t",document.title);_JT.SET("r",eval("document"+".ref"+"errer"));
_JT.SET("l",(n.userLanguage)?n.userLanguage:(n.language)?n.language:"");if(window.screen){v=12;_JT.SET("ss",s.width+"*"+s.height);
_JT.SET("sc",(s.colorDepth)?s.colorDepth:s.pixelDepth)}if(r.toSource||(_JT.I(n.appName,"Microsoft")>-1&&r.shift))v=13;_JT.DM();
eval("try{throw v=14}catch(e){}");if((new Date()).toDateString)v=15;if(r.every)v=16;_JT.SET("jv",v);if(document.body&&document.body.addBehavior){
eval("try{document.body.addBehavior('#default#clientCaps');_JT.SET('ct',document.body.connectionType)}catch(e){}");
eval("try{document.body.addBehavior('#default#homePage');_JT.SET('hp',document.body.isHomePage(location.href)?'y':'n')}catch(e){}")}};
_JT.hf=0;_JT.ii=0;_JT.im=10;_JT.ia=_JT.NA();for(var i=0;i<_JT.im;i++){_JT.ia[i]=new Image()};_JT.da=_JT.NA();_JT.vs=eval("window."+_JT.nspace);
_JT.vs=(_JT.D(_JT.vs))?_JT.vs:new Object();if(_JT.D(_JT.site))for(var i in _JT.site){if(_JT.D(_JT.vs[i])==0)_JT.vs[i]=_JT.site[i]};
if(typeof _JT.vs=="object"){if(_JT.D('Init'))_JT.EI('Init');for(var i in _JT.vs){if(typeof _JT.vs[i]!='function'&&i!='sensor'&&i!='sensor2')_JT.da[i]=_JT.ES(_JT.vs[i])}
_JT.rq=_JT.protocol+"//"+_JT.vs['sensor']+"?Log="+_JT.log+"&v="+_JT.v;
_JT.rq2=_JT.protocol+"//"+_JT.vs['sensor2']+"?Log="+_JT.log+"&v="+_JT.v;
if(_JT.vs['lt']&&_JT.vs['lt']=="1"){_JT.t1=document.links;_JT.t2=0;
for(_JT.t2=0;_JT.t2<_JT.L(_JT.t1);_JT.t2++)_JT.EL(_JT.t1[_JT.t2],'mousedown',_JT.LT)}_JT.P();_JT.SEND("PageView");_JT.EI("PostPageView",_JT.last)}

//DM Onload functions
//By Aakshi Sehdave (Omniture)
function addToWindowOnLoad(funct){var oldOnload = window.onload;if (typeof window.onload != 'function') {window.onload = funct;}
else {window.onload = function() {oldOnload();funct();};}}
addToWindowOnLoad(_JT.DM_Impr);

/* boone.js ----------------------------------------------------------*/

//===============================================================

// BOONE script to track time on page and scrolling behavior
// for now, we don't suppot SSL pages

if (location.protocol.indexOf('https') < 0) (function() {

    try {

        // bail if we are disabled
        if (window._BOONE_OFF) {
            return;
        }

        // depends on _JT script
        if ("undefined" == typeof(_JT)) {
            return;
        }
        if (!_JT.rq) {
            return;
        }
        if (!_JT.BNUC) {
            return;
        }

        var supported = false;
        var a = navigator.userAgent.toString().toLowerCase();
        var b = a.match(/(msie) (\d{1,2})/) || a.match(/(firefox)\/(\d+)/) || a.match(/(chrome)\/(\d+)/) || a.match(/(safari)\/(\d+)/) || a.match(/[(](iphone)[;]/) || a.match(/[(](ipod)[;]/) || a.match(/[(](ipad)[;]/);
        if (b) {
            if (b[1] == 'msie') supported = parseInt(b[2]) >= 6;
            else if (b[1] == 'firefox') supported = parseInt(b[2]) >= 3;
            else if (b[1] == 'chrome') supported = parseInt(b[2]) >= 3;
            else if (b[1] == 'safari') supported = a.match(/version\/[3-9]\./) != null;
            else if (b[1] == 'ipad' || b[1] == 'ipod' || b[1] == 'iphone') supported = true;
        }
        (function(is_supported) {

            var version = '1.9'; // version of this script

            var prelimTimer = 15;
            var eventsSent = 0;
            var pageUnloadSent = false;


            // default options
            var options = {
                'debug': false
            };

            // can override any of them by defining _BOONE_OPTIONS
            if (window._BOONE_OPTIONS) {
                for (option in window._BOONE_OPTIONS) {
                    if (window._BOONE_OPTIONS.hasOwnProperty(option)) {
                        options[option] = window._BOONE_OPTIONS[option];
                    }
                }
            }

            // allow boonedebug=(true|false|alert) in query params to override options
            var qp_debug = location.search.toLowerCase().match(/[\?&]boonedebug=(true|false|alert)/);
            if (qp_debug) {
                options.debug = qp_debug[1] == 'alert' ? 'alert' : qp_debug[1] == 'true';
            }

            // log function
            function log(s, allowAlert) {
                if (options.debug) {
                    if (window.console) {
                        window.console.log('boone: ' + s);
                    }
                    if (allowAlert && options.debug == 'alert') {
                        alert('boone: ' + s);
                    }
                }
            }

            // common handler to add events
            function listen(a, b, d, c) {
                c = (c) ? true : false;
                if (a.addEventListener) {
                    a.addEventListener(b, d, c)
                } else {
                    if (a.attachEvent) {
                        a.attachEvent("on" + b, d)
                    } else {
                        if (a["on" + b]) {
                        } else {
                            a["on" + b] = d
                        }
                    }
                }
            }

            var tracking = {
                // starting timestamp (script load)
                lt: new Date().getTime(),
                // scrolling tracker
                si: {
                    ms: 0,  // ms since page load for last scroll event
                    x: 0, // max x scroll position recorded
                    y: 0, // max y scroll position recorded
                    t: 0 // total number of scroll events
                },
                // number of clicks while on page
                cl: 0,
                // data of last click
                lc: ''
            }

            // builds the query param for scrolling info
            //  st = total number of scroll events
            //  sms = ms since page load for last scroll event
            //  sx = max x scroll position recorded
            //  sy = max y scroll position recorded
            //  dh = height of the document

            function getScrollingParam() {
                return "st=" + tracking.si.t + "&sms=" + tracking.si.ms + "&sx=" + tracking.si.x + "&sy=" + tracking.si.y + "&dw=" + getDocumentWidth() + "&dh=" + getDocumentHeight() + "&cw=" + getClientWidth() + "&ch=" + getClientHeight();
            }

            // scroll event handler
            function handler_trackScrolling(e) {
                try {
                    if (window._BOONE_OFF) return;
                    // update the scrolling info if tracking
                    log('scrollingHandler', false)
                    tracking.si.t = tracking.si.t + 1;
                    // get the scroll x and y
                    var x = Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
                    var y = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
                    // if either is larger than what we've seen before
                    if (x > tracking.si.x || y > tracking.si.y) {
                        // update the values
                        tracking.si.ms = new Date().getTime() - tracking.lt;
                        tracking.si.x = x;
                        tracking.si.y = y;
                    }
                } catch (e) {
                    log('handler_trackScrolling: '+e.toString(), false);
                }
            }

            // from old boone
            function getParentByTagName(obj, tag) {
                if (!obj || obj.tagName == undefined) return false;
                if (obj.tagName.toLowerCase() == tag) return obj;
                else return getParentByTagName(obj.parentNode, tag);
            }

            // from old boone
            function cssPath(el) {
                try {
                    if (Node.ELEMENT_NODE != 1) {
                        throw true;
                    }
                } catch (e) {
                    window.Node = {
                        ELEMENT_NODE: 1,
                        ATTRIBUTE_NODE: 2,
                        TEXT_NODE: 3
                    };
                }
                if (el == undefined) return;
                var path = [];
                while (el.nodeType === Node.ELEMENT_NODE) {
                    var selector = el.nodeName.toLowerCase();
                    if (el.id) {
                        selector += '#' + el.id;
                    } else {
                        for (var nth = 1, sib = el; sib.previousElementSibling; sib = sib.previousElementSibling,nth++) {
                        }
                        selector += ":nth-child(" + nth + ")";
                    }
                    path.unshift(selector);
                    el = el.parentNode;
                }
                var selectorPath = path.join(" > ");
                return selectorPath;
            }

            // from old boone
            function getParentList(listItem) {
                var list = getParentByTagName(listItem, 'ul');
                if (!list) list = getParentByTagName(listItem, 'ol');
                if (!list) return false;
                return list;
            }

            // from old boone
            function getListPosition(listItem) {
                var el = listItem;
                while (el.parentNode) {
                    for (var c = 1, e = el; e.previousElementSibling; e = e.previousElementSibling,c++) {
                    }
                    if (el.tagName.toLowerCase() == "li") {
                        return c;
                    } else {
                        el = el.parentNode;
                    }
                }
                return false;
            }

            function getClientWidth() {
                try {
                    return Math.max(document.body.clientWidth, document.documentElement.clientWidth);
                } catch(e) {
                    log('getClientWidth: '+e.toString(), false);
                }
                return 0;
            }

            function getClientHeight() {
                try {
                    return Math.max(document.body.clientHeight, document.documentElement.clientHeight);
                } catch(e) {
                    log('getClientHeight: '+e.toString(), false);
                }
                return 0;
            }

            function getDocumentWidth() {
                try {
                    return Math.max(
                            document.documentElement.clientWidth,
                            document.body.scrollWidth, document.documentElement.scrollWidth,
                            document.body.offsetWidth, document.documentElement.offsetWidth
                    )
                } catch(e) {
                    log('getDocumentWidth: '+e.toString(), false);
                }
                return 0;
            }

            function getDocumentHeight() {
                try {
                    return Math.max(
                            document.documentElement.clientHeight,
                            document.body.scrollHeight, document.documentElement.scrollHeight,
                            document.body.offsetHeight, document.documentElement.offsetHeight
                    )
                } catch(e) {
                    log('getDocumentHeight: '+e.toString(), false);
                }
                return 0;
            }

            function handler_trackMouseClick(e) {
                try {
                    if (window._BOONE_OFF) return;
                    log("tracking mouse click", true);
                    tracking.cl = tracking.cl + 1;
                    if (!e) e = window.event;
                    var ev = { }; //createEvent("click");
                    ev.ct = new Date().getTime() - tracking.lt;
                    var target = (e.target) ? e.target : e.srcElement;
                    if (e.pageX || e.pageY) {
                        ev.x = e.pageX;
                        ev.y = e.pageY;
                    } else if (e.clientX || e.clientY) {
                        ev.x = e.clientX + Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
                        ev.y = e.clientY + Math.max(document.body.scrollTop, document.documentElement.scrollTop);
                    }
                    if (target) {
                        ev.el = encodeURIComponent(cssPath(target));
                        var anchor = getParentByTagName(target, 'a');
                        if (anchor) {
                            ev.url = encodeURIComponent(anchor.href); //TODO URI.normalize(anchor.href); ??
                        }
                        var image = getParentByTagName(target, 'img');
                        if (image) {
                            ev.img = encodeURIComponent(image.src);
                        }
                        var listItem = getParentByTagName(target, 'li');
                        if (listItem) {
                            var parentList = getParentList(listItem);
                            var position = getListPosition(listItem);
                            if (parentList && position) {
                                ev.li = encodeURIComponent(cssPath(parentList));
                                ev.lp = position;
                            }
                        }
                    }
                    var tx = target.textContent ? target.textContent : target.innerText;
                    if (tx) {
                        tx = tx.replace(/[\t\n\r]/g,' ').replace(/ {2,}/g,' ').trim();
                        if (tx) {
                            ev.tx = encodeURIComponent(tx.substring(0, 300));
                        }
                    }
                    var sev = '';
                    for (var v in ev) {
                        if (ev.hasOwnProperty(v)) {
                            if (sev) {
                                sev = sev + '&';
                            }
                            sev = sev + v + "=" + ev[v];
                        }
                    }
                    tracking.lc = sev;
                    sendEvent("bev=cl&"+sev);
                } catch(e) {
                    log('handler_trackMouseClick: '+e.toString(), true);
                }
            }

            // send event to omniture
            function sendEvent(ev) {
                if (eventsSent < 50) {
                    sendEventAlways(ev);
                }
            }
            function sendEventAlways(ev) {
                eventsSent = eventsSent + 1;
                ev = "bv=" + version + "&" + getPageInfo() + "&" + ev;
                log(ev, true);
                ev = "ev=boone&bnuc=" + _JT.BNUC() + "&boone=" + encodeURIComponent(ev);
                // create the image and set the url to include the event
                var r = new Image();
                var imgUrl = _JT.rq  + (_JT.rq.indexOf('?') == -1 ? '?' : '&') + ev;
                r.src = imgUrl;
                log(imgUrl, true);
            }

            function getPageInfo() {
                //  lt = page load timestamp
                //  pt = total time on page in ms
                // (scrolling info param)
                return "lt=" + tracking.lt + "&pt=" + (new Date().getTime() - tracking.lt) + "&" + getScrollingParam() + "&cl" + "=" + tracking.cl;
            }

            // handle the page unload event
            function handler_timer(e) {
                try {
                    if (window._BOONE_OFF) return;
                    // send still on page timer event
                    if (tracking.lc) {
                        sendEvent("bev=op&"+tracking.lc);
                    } else {
                        sendEvent("bev=op");
                    }
                    prelimTimer = prelimTimer * 2;
                    // only accumulating up to an hour of these
                    if (prelimTimer <= 960) {
                        setTimeout(handler_timer, prelimTimer * 1000);
                    }
                } catch (e) {
                    log('handler_timer: '+e.toString(), true);
                }
            }

            // handle the page unload event
            function handler_pageUnload(e) {
                try {
                    if (window._BOONE_OFF) return;
                    if (pageUnloadSent) return;

                    pageUnloadSent = true;
                    // build and send the event
                    //  lt = page load timestamp
                    //  pt = total time on page in ms
                    // (scrolling info param)
                    if (tracking.lc) {
                        sendEventAlways("bev=pu&"+tracking.lc);
                    } else {
                        sendEventAlways("bev=pu");
                    }
                } catch (e) {
                    log('handler_pageUnload: '+e.toString(), true);
                }
            }

            if (is_supported) {
                // track clicks
                listen(document, "click", handler_trackMouseClick, false);

                // track unload
                listen(window, "beforeunload", handler_pageUnload, false);

                // track scrolling
                listen(window, "scroll", handler_trackScrolling, false);

                // track still on page via timer
                setTimeout(handler_timer, prelimTimer * 1000);
                log('timer set');

                // log our version
                log('version: ' + version, true);
            } else {
                sendEvent("bev=ns");
            }
        })(supported);

    } catch(e) {
        if (window.console && window.console.log) {
            window.console.log('boone: '+e.toString());
        }
    }
})();
