///////////////////////////////////////////////////////////
//  Copyright 2009, 2010 Usability Sciences Corporation  //
///////////////////////////////////////////////////////////
if (typeof WebIQ === "undefined")
{
    var WebIQ = {};
}

if (typeof WebIQ.PageTag === "undefined")
{
    WebIQ.PageTag = {};
}

(
    function
    (
        ns,
        config
    )
    {
        var fnEmpty;
        var fnDateToOLEUTCString;
        var fnCreateEventHandler;
        var fnPollDomInit;
        var fnOnDomContentLoaded;
        var fnPollInitialRequestComplete;

        fnEmpty = function()
        {
        };

        fnDateToOLEUTCString = function(date)
        {
            var h,m,s;

            h = date.getUTCHours();
            m = date.getUTCMinutes();
            s = date.getUTCSeconds();

            return (
                (date.getUTCMonth() + 1) + "/" +
                date.getUTCDate() + "/" +
                date.getUTCFullYear() + " " +
                ((h<10)?"0":"") + h + ":" +
                ((m<10)?"0":"") + m + ":" +
                ((s<10)?"0":"") + s
            );
        };

        ns = ns || {};
        config = config || {};
        
        ns.Version = 1;

        ns.Enabled = (typeof config.Enabled === "boolean") ? config.Enabled : true;
        ns.BaseUrl = (typeof config.BaseUrl === "string" && config.BaseUrl) || "webiq005.webiqonline.com/WebIQ/DataServer/";
        ns.IsDomInit = false;
        ns.IsDocLoaded = false;
        ns.PageID = "";
        ns.PageLoadStartTime = new Date();
        ns.PageLoadSequence = ns.PageLoadStartTime.valueOf();

        ns.OnAfterInit = (typeof config.OnAfterInit === "function" && config.OnAfterInit) || fnEmpty;
        ns.OnAfterHooked = (typeof config.OnAfterHooked === "function" && config.OnAfterHooked) || fnEmpty;
        ns.OnDOMInit = (typeof config.OnDOMInit === "function" && config.OnDOMInit) || fnEmpty;
        ns.OnDocLoad = (typeof config.OnDocLoad === "function" && config.OnDocLoad) || fnEmpty;

        ns.SendRequest = function()
        {
            if (
                !/WebIQSessionActive=(\d)/i.test(document.cookie)
                ||
                RegExp.$1 === "1"
            )
            {
                var src;
                var script;

                src = document.location.protocol + "//" + ns.BaseUrl + "HandlePageTag.srf?U=" + encodeURIComponent(document.location.href) + "&T=" + encodeURIComponent(document.title) + "&ver=" + ns.Version.toString();

                if (ns.PageID)
                {
                    src += "&I=" + encodeURIComponent(ns.PageID) + "&S=" + (new Date().valueOf().toString()) + "&E=" + encodeURIComponent(fnDateToOLEUTCString(new Date()));
                }
                else
                {
                    src += "&S=" + ns.PageLoadSequence.toString() + "&E=" + encodeURIComponent(fnDateToOLEUTCString(ns.PageLoadStartTime));
                }

                script = document.createElement("script");
                script.setAttribute("type", "text/javascript");
                script.setAttribute("language", "JavaScript");
                script.setAttribute("src", src);
                document.getElementsByTagName("head")[0].appendChild(script);
            }
        };

        fnCreateEventHandler = function(evt, fn)
        {
            if (window.attachEvent)
            {
                window.attachEvent("on" + evt, fn);
            }
            else if (window.addEventListener)
            {
                window.addEventListener(evt, fn, false);
            }
        };

        fnPollInitialRequestComplete = function()
        {
            if (ns.IsDocLoaded && ns.PageID)
            {
                ns.SendRequest();
                ns.OnDocLoad();
            }
            else
            {
                window.setTimeout(fnPollInitialRequestComplete, 100);
            }
        };

        fnOnDomContentLoaded = function()
        {
            if (!ns.IsDomInit)
            {
                ns.IsDomInit = true;

                window.setTimeout(fnPollInitialRequestComplete, 100);

                ns.SendRequest();
                ns.OnDOMInit();
            }
        };

        fnPollDomInit = function()
        {
            if (!ns.IsDomInit)
            {
                if (/<\/body>/i.test(document.getElementsByTagName("html")[0].innerHTML))
                {
                    fnOnDomContentLoaded();
                }
                else
                {
                    window.setTimeout(fnPollDomInit, 1);
                }
            }
        };

        if (
            ns.Enabled
            &&
            (
                !/WebIQSessionActive=(\d)/i.test(document.cookie)
                ||
                RegExp.$1 === "1"
            )
        )
        {
            ns.OnAfterInit();

            fnCreateEventHandler("load", function(){ns.IsDocLoaded = true;});
            fnCreateEventHandler("unload", fnEmpty);
            fnCreateEventHandler("DOMContentLoaded", fnOnDomContentLoaded);

            window.setTimeout(fnPollDomInit, 100);

            ns.OnAfterHooked();
        }
    }
)
(WebIQ.PageTag, {
    Enabled : true,
    BaseUrl : "webiq005.webiqonline.com/WebIQ/DataServer/",

    OnAfterInit     : function() {},
    OnAfterHooked   : function() {},
    OnDOMInit       : function() {},
    OnDocLoad       : function() {}
});

