/****************************************
 * Numeric Analytics tagging framework
 * v 1.0  Jan 5th 2009
 ****************************************/
try {
    var _numeric_ = new Object;

    // ****************** START CONFIG *********************************
    _numeric_.config = new Object;
    _numeric_.config.openScrpt = '<scr' + 'ipt language="JavaScript" type="text/javascript" src="';
    _numeric_.config.closeScrpt = '"></scr' + 'ipt>';
    _numeric_.config.baseUrl ="//narservices.realtor.org/ctsengine/webanalytics/";    //prod
    //_numeric_.config.baseUrl = "//wstest.realtor.org/ctsengine/webanalytics/";    //test
    //_numeric_.config.baseUrl ="//"+location.hostname+":8080/ctsengine/webanalytics/";   //local

    if (! _numeric_.config.pnPrefix) _numeric_.config.pnPrefix = "";

    // ****************** END CONFIG *********************************

    // ****************** START UTILS *********************************
    _numeric_.utils = new Object;
    _numeric_.setProperty = function(pName, pValue) {
        this[pName] = pValue;
    }
    _numeric_.getProperty = function(pName) {
        if (this[pName]) {
            return this[pName];
        } else {
            return "";
        }
    }
    _numeric_.utils.getEventTarget = function(e) {
        try {
            var targ;
            if (!e) {
                var e = window.event;
            }
            if (e.target) {
                targ = e.target;
            }
            else if (e.srcElement) {
                targ = e.srcElement;
            }
            if (targ.nodeType == 3) {
                targ = targ.parentNode;
            }
            return targ;
        } catch(e) {
        }
    }
    _numeric_.utils.getEventTargetName = function(e) {
        try {
            var targ;
            if (!e) {
                var e = window.event;
            }

            if (e.target) {
                targ = e.target;
            }
            else if (e.srcElement) {
                targ = e.srcElement;
            }

            if (targ.nodeType == 3) {
                targ = targ.parentNode;
            }
            return targ.tagName;
        } catch(e) {
        }
    }
    _numeric_.utils.addOnClickEvent = function(func, El) {
        try {
            if (El.attachEvent) {
                El.attachEvent("onclick", func);
            }
            if (El.addEventListener) {
                El.addEventListener("click", func, false);
            }
            return this;
        } catch(e) {
        }
    }
    _numeric_.utils.addOnSubmitEvent = function(func, El) {
        try {
            if (El.attachEvent) {
                El.attachEvent("onsubmit", func);
            }
            if (El.addEventListener) {
                El.addEventListener("submit", func, false);
            }
            return this;
        } catch(e) {
        }
    }
    _numeric_.utils.loadScriptLibrary = function(src, targetElement, olFunction) {
        try {
            if (_numeric_.scriptElement == null) {
                _numeric_.scriptElement = document.createElement('script');

                _numeric_.scriptElement.setAttribute('type', 'text/javascript');
                _numeric_.scriptElement.setAttribute('language', 'javascript');
                _numeric_.scriptElement.setAttribute('src', src);
                _numeric_.scriptElement.setAttribute('id', src);

            }
            if (targetElement == null) {
                if (_numeric_.headElement == null)
                    _numeric_.headElement = document.getElementsByTagName('body')[0];
                targetElement = _numeric_.headElement;
            }
            var library = _numeric_.scriptElement.cloneNode(true);
            library.src = src;

            if (typeof olFunction == 'function') {
                if (! _numeric_.isIE) {
                    library.addEventListener("load", olFunction, false);
                } else {
                    olFunIE = olFunction.toString();
                    olFunIE = olFunIE.substr('function '.length);
                    olFunIE = olFunIE.substr(0, olFunIE.indexOf(')') + 1);

                    library.onreadystatechange = function () {
                        if (library.readyState == 'complete' || library.readyState == 'loaded') {
                            eval(olFunIE);
                        }
                    }
                }
            }

            targetElement.appendChild(library);
        } catch(e) {
        }
    }
    _numeric_.utils.addOnloadEvent = function(func, El) {
        try {
            if (typeof El == "undefined") {
                El = window;
            }
            if (El.attachEvent) {
                El.attachEvent("onload", func);
            }
            if (El.addEventListener) {
                El.addEventListener("load", func, false);
            }
            return this;
        } catch(e) {
        }
    }
    _numeric_.utils.queryStringToHash = function(str) {
        try {
            var qs = [];
            var a = str.indexOf("?") > -1 ? str.split("?")[1].split("&") : str.split("&");
            for (var x = 0; x < a.length; x++) {
                var b = a[x].split("=");
                qs[b[0]] = b[1];
            }
            return qs;
        } catch(e) {
        }
    }
    _numeric_.utils.readCookie = function(name) {
        try {
            var name = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1, c.length)
                }
                ;
                if (c.indexOf(name) == 0) {
                    return c.substring(name.length, c.length)
                }
                ;
            }
            return null;
        } catch(e) {
        }
    }
    _numeric_.utils.setCookie = function(name, value) {
        try {
            document.cookie = name + "=" + value + "; path=/";
        } catch(e) {
        }
    }
    _numeric_.utils.decode = function($str) {
        try {
            var $entities = [ ['%f8','%20'], ['%a0', '%20'],  ['%3e','%20'], ['%3c','%20'],  ['%26','%20'],  ['%22','%20'] ];
            var $ii = 0;
            var $estr = escape($str);
            $estr = $estr.toLowerCase();
            while ($ii < $entities.length) {
                $estr = $estr.replace(new RegExp($entities[$ii][0], "g"), $entities[$ii][1]);
                $ii++;
            }
            return unescape($estr);
        } catch(e) {
            return $str;
        }
    }
    _numeric_.utils.getText = function(nodeOrig) {
        try {
            var rtn = "";
            var t = 0;
            var fcn = function(node) {
                if (t < 100) {
                    for (var c = 0; c < node.childNodes.length; c++) {
                        var childnode = node.childNodes[c];
                        switch (childnode.nodeType) {
                            case 3:
                                rtn += _numeric_.utils.decode(childnode.nodeValue);
                                break;
                            case 1:
                                t++;
                                fcn(childnode);
                                break;
                        }
                        ;
                    }
                }
            }
            fcn(nodeOrig);
            return rtn;
        } catch(e) {
            return "";
        }
    }


    _numeric_.isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);
    if (!  _numeric_.isIE) {
        HTMLElement.prototype.__defineGetter__("innerText",
                function () {
                    return(this.textContent);
                });
        HTMLElement.prototype.__defineSetter__("innerText",
                function (txt) {
                    this.textContent = txt;
                });
    }

    _numeric_.utils.sendEvent = function(el, event, vars, vals, type) {
        try {
            s = s_gi(s_account);
            var tmp = new Array;
            if (typeof vars == "undefined") {
                s.linkTrackVars = "pageName,channel,eVar1, prop1,prop2,prop5,prop6,prop7,prop8,events";
                s.linkTrackEvents = event;
                s.events = event;
            } else {
                s.linkTrackVars = "pageName,channel,eVar1, prop1,prop2,prop5,prop6,prop7,prop8,events," + vars;
                s.linkTrackEvents = event;
                if (event != "None") {
                    s.events = event;
                }
                tmp = vars.split(',');
                var tmp1 = vals.split(',');
                for (i = 0; i < tmp.length; i++) {
                    s[tmp[i]] = tmp1[i];
                }
            }
            if (typeof type == "undefined") {
                s.tl(el, 'o');
            } else {
                s.tl(el, type);
            }
            for (i = 0; i < tmp.length; i++) {
                s[tmp[i]] = "";
            }
            s.linkTrackVars = "None";
            s.linkTrackEvents = "None";

        } catch(e) {
        }
    };

    // ******************  END UTILS *********************************

    //Call vender code
    //Omniture
    _numeric_.utils.loadScriptLibrary(_numeric_.config.baseUrl + "s_code.js", document.getElementsByTagName('head')[0], metricTranslation);

    //Google Analytics
    //_numeric_.utils.loadScriptLibrary(_numeric_.config.baseUrl +"ga.js",  document.getElementsByTagName('head')[0]);


    _numeric_.analyticsCallBack = function() {
        try {
            //load vendor widgets
            //Omniture
            _numeric_.utils.loadScriptLibrary(_numeric_.config.baseUrl + "omniWidget.js", document.getElementsByTagName('head')[0]);
            //Google Analytics
            //_numeric_.utils.loadScriptLibrary(_numeric_.config.baseUrl + "gaWidget.js",  document.getElementsByTagName('head')[0]);

        } catch(e) {
        }
    }

} catch(e) {
}
