if (typeof (TicTacTi) == 'undefined') {
    TicTacTi = {
        globals: { widgets: [], widgetCount: 0, t3cdn: "http://cdn.tictacti.com/", pageLoaded: false, scriptLoaded: false },
        init: function () { try { document.write("<script type=\"text/javascript\" src=\"http://cdn.tictacti.com/widgets/js/swfobject.js\"></" + "script>"); } catch (e) { } },
        renderWidget: function (publisherId, tagType, width, height, t3cfg)
        {
            try
            {
                if (this.undef(publisherId) || this.undef(tagType) || this.undef(width) || this.undef(height) || this.undef(t3cfg)) return;
                document.write("<div id=\"t3widget_" + this.globals.widgetCount.toString() + "\" ></div>");
                TicTacTi.globals.widgets.push({ "publisherId": publisherId, "tagType": tagType, "width": width, "height": height, "t3cfg": t3cfg, "countNum": TicTacTi.globals.widgetCount });
                TicTacTi.globals.widgetCount++;
            }
            catch (e) { }
        },
        adEvent: function (elm, type, listener, useCapture)
        {
            if (elm.addEventListener) { elm.addEventListener(type, listener, false); }
            else if (elm.attachEvent) { elm.attachEvent("on" + type, listener); }
        },
        remvoeEvent: function (elm, type, listener, useCapture)
        {
            if (elm.removeEventListener) { elm.removeEventListener(type, listener, false); }
            else if (elm.detachEvent) { elm.detachEvent("on" + type, listener); }
        },
        onPageLoaded: function ()
        {
            var flashvars, params, attributes, widget, origHeight, origWidth;
            for (var i = 0; i < TicTacTi.globals.widgets.length; i++)
            {
                widget = TicTacTi.globals.widgets[i];
                flashvars = { publisherId: widget.publisherId, tagType: widget.tagType, engineId: "gamePlayer_" + i.toString(), widgetName: widget.t3cfg.widgetName, enableDM: widget.t3cfg.config.enableDM, playerId: widget.countNum };
                if (!TicTacTi.undef(widget.t3cfg.config.isGameFullPath))
                {
                    if (widget.t3cfg.config.isGameFullPath.toString().toLowerCase() == "true") { flashvars.TTTWrapperGameUrl = widget.t3cfg.gameUrl; }
                    else { flashvars.TTTWrapperGameUrl = TicTacTi.globals.t3cdn + widget.t3cfg.gameUrl; }
                }
                else
                    flashvars.TTTWrapperGameUrl = TicTacTi.globals.t3cdn + widget.t3cfg.gameUrl;
                if (widget.t3cfg.gameUrl == null || widget.t3cfg.gameUrl == "undefined") flashvars.TTTWrapperGameUrl = "";

                if (!TicTacTi.undef(widget.t3cfg.config.origHeight)) flashvars.origHeight = widget.t3cfg.config.origHeight;
                if (!TicTacTi.undef(widget.t3cfg.config.origWidth)) flashvars.origWidth = widget.t3cfg.config.origWidth;
                if (!TicTacTi.undef(widget.t3cfg.config.externalId)) flashvars.externalId = widget.t3cfg.config.externalId;
                if (!TicTacTi.undef(widget.t3cfg.config.enableCache)) flashvars.enableCache = widget.t3cfg.config.enableCache;
                if (!TicTacTi.undef(widget.t3cfg.config.tttPreloader)) flashvars.tttPreloader = widget.t3cfg.config.tttPreloader;
                if (!TicTacTi.undef(widget.t3cfg.config.engineConnectorType)) flashvars.engineConnectorType = widget.t3cfg.config.engineConnectorType;

                for (var p in widget.t3cfg.config) { if (p.indexOf('widget') > -1) { flashvars[p] = widget.t3cfg.config[p]; } }

                params = { allowfullscreen: "true", allowScriptAccess: "always", wmode: "opaque", base: widget.t3cfg.config.gameBaseUrl };
                if (!TicTacTi.undef(widget.t3cfg.config.bgcolor))
                    params.bgcolor = widget.t3cfg.config.bgcolor;
                attributes = { id: "gamePlayer_" + i.toString(), name: "gamePlayer_" + i.toString() };
                swfobject.embedSWF(TicTacTi.globals.t3cdn + widget.t3cfg.wrapperUrl + '?wildKey=' + (new Date()).getMilliseconds(), "t3widget_" + i.toString(), widget.width, widget.height, "9.0.0", false, flashvars, params, attributes);
                if (flashvars.engineConnectorType == 7 && widget.width < 320 && widget.height < 270) { widget.t3cfg.displayT3Logo = true; }
            }
        },

        undef: function (obj) { return (typeof (obj) == 'undefined'); },
        dispatchEngineEvent: function (eventName, widgetId)
        {
            if (TicTacTi.globals.widgets.length > widgetId)
            {
                if (TicTacTi.globals.widgets[widgetId].t3cfg.displayT3Logo == true && eventName == "onDisplay") { this.displayLogo(widgetId); }
                if (typeof (TicTacTi.globals.widgets[widgetId].t3cfg[eventName]) == "function") { TicTacTi.globals.widgets[widgetId].t3cfg[eventName](); }
            }
        },
        displayLogo: function (widgetId)
        {
            var i = 0; var imgWidth = 21; var imgHeight = 16; var ad = { "w": 0, "h": 0, "top": 0, "left": 0, "z": 1 };
            while (document.getElementById('tttFrame' + i) != null) { i++; }
            iframeTag = document.getElementById("tttFrame" + (i - 1));
            if (iframeTag == null)
            {
                var objectTag = document.getElementById("gamePlayer_" + widgetId);
                ad.w = parseInt(objectTag.width); ad.h = parseInt(objectTag.height);
                if (!isNaN(parseInt(objectTag.style.zIndex))) ad.z = parseInt(objectTag.style.zIndex) + 1;
                while (objectTag) { ad.top += objectTag.offsetTop; ad.left += objectTag.offsetLeft; objectTag = objectTag.offsetParent; }
            }
            else
            {
                ad.w = parseInt(iframeTag.style.width); ad.h = parseInt(iframeTag.style.height);
                ad.top = parseInt(iframeTag.style.top); ad.left = parseInt(iframeTag.style.left);
                ad.z = parseInt(iframeTag.style.zIndex) + 1;
            }
            if (isNaN(ad.w) || isNaN(ad.h)) { setTimeout("TicTacTi.displayLogo(" + widgetId + ")", 10); return; }
            logoDiv = document.createElement("div"); logoDiv.style.position = "absolute";
            logoDiv.innerHTML = '<a href="http://tictacti.com" target="_blank"><img id="t3Logo' + widgetId + '" src="http://t3cdn.tictacti.com/adserver/tictacti_com/transparent.gif" style="border:0px;width:' + imgWidth + 'px;height:' + imgHeight + 'px;background:url(http://t3cdn.tictacti.com/adserver/tictacti_com/adLogoLeft.png) no-repeat scroll 100% top" alt="Powerd by TicTacTi" onmouseover="this.animateDirection=1;TicTacTi._animateDispatch(this);" onmouseout="this.animateDirection=-1;TicTacTi._animateDispatch(this);" /></a>';
            logoDiv.style.top = ad.top + "px"; logoDiv.style.left = ad.left + "px"; logoDiv.baseL = ad.left + ad.w; logoDiv.style.zIndex = ad.z;
            document.body.appendChild(logoDiv);

        },
        _animateDispatch: function (imgObj){if (!imgObj.intervalId){imgObj.intervalId = setInterval('TicTacTi.animateLogo("' + imgObj.id + '");', 20);} },
        animateLogo: function (imgObjId)
        {
            var imgObj = document.getElementById(imgObjId);
            var logoDiv = imgObj.parentNode.parentNode; var w = parseInt(imgObj.style.width) + (imgObj.animateDirection * 5); imgObj.style.width = w + "px";
            if (w <= 21 || w >= 116)
            {
                var d = w <= 21 ? 21 - w : w - 116; imgObj.style.width = (w + d) + "px";
                imgObj.intervalId = clearInterval(imgObj.intervalId);
            }
        }
    };
    TicTacTi.adEvent(window, "load", TicTacTi.onPageLoaded, false);
    TicTacTi.init();
}
function onTTTAdDisplayed(widgetId) { setTimeout("TicTacTi.dispatchEngineEvent('onDisplay','" + widgetId + "')", 2); }
function onTTTAdClosed(widgetId) { setTimeout("TicTacTi.dispatchEngineEvent('onClose','" + widgetId + "')", 2); }
function onTTTAdFailed(widgetId) { setTimeout("TicTacTi.dispatchEngineEvent('onEmpty','" + widgetId + "')", 2); }
function onTTTNoPreRollFound(widgetId) { setTimeout("TicTacTi.dispatchEngineEvent('onEmpty','" + widgetId + "')", 2); }
