var uaValue;
var isIE;
var isGecko;
var isSafari;
var isKonqueror;
var globallyInited = false;
var dragonX, dragonTimer, dragonDirection, dragonMaxX, dragonCounter;
var flameX, flameY, flameTimer, flameDelay, flameDescent;
var flameDirection = 0;
var flameCounter = 0;

function getRequestObject()
{
    var req = false;
    if (window.XMLHttpRequest)
        req = new XMLHttpRequest();
    else if (window.ActiveXObject)
        req = new ActiveXObject("Microsoft.XMLHTTP");
    return req;
}

var http = getRequestObject();
var adTimer;

function globalInit()
{
    uaValue = navigator.userAgent;
    var ua = uaValue.toLowerCase();
    isIE = (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1);
    isGecko = ua.indexOf("gecko") != -1;
    isSafari = ua.indexOf("safari") != -1;
    isKonqueror = ua.indexOf("konqueror") != -1;
//    initBannerEvents();
    initAdEvents();
    globallyInited = true;
}

function initAdEvents()
{
    var nextButton = document.getElementById('adClickButton');
    if (nextButton)
        adTimer = setInterval('checkCounter()', 1000);
}

function switchBanner(bannerUrl)
{
    document.getElementById("top-center").innerHTML = '<a href="http://www.celebpoker.com/?tracker=ref127966_0&lp=1&b=374"><img src="' + bannerUrl + '" alt="CelebPoker.com" border="0" width="468" height="60" style="margin-top: 10px;"/></a>';
}

function initDragon(direction)
{
    var dragon = document.getElementById("dragon-" + (direction == 1 ? "right" : "left"));
    if (dragon)
    {
        if (window.innerWidth)
            dragonMaxX = window.innerWidth - 100;
        if (document.body && document.body.scrollWidth)
            dragonMaxX = document.body.scrollWidth - 100;
        if (direction == 1)
            dragonX = -100;
        else
        {
            direction = -1;
            dragonX = dragonMaxX;
        }
        dragonCounter = 0;
        dragonDirection = direction;
        flameDelay = 100 + Math.floor(Math.random() * 400);
        flameDescent = 1 + Math.floor(Math.random() * 3);
        dragon.style.left = dragonX + 'px';
        dragon.style.visibility = 'visible';
        dragonTimer = setInterval("animateDragon()", 10);
    }
}

function animateDragon()
{
    var dragon = document.getElementById("dragon-" + (dragonDirection == 1 ? "right" : "left"));
    if (dragon)
    {
        if (dragonCounter == flameDelay && !flameTimer)
        {
            var flameId = "flame-" + (dragonDirection == 1 ? "right" : "left");
            var flame = document.getElementById(flameId);
            if (flame)
            {
                var flameShift = dragonDirection == 1 ? 70 : -40;
                flameTimer = setInterval("animateFlame('" + flameId + "', " + (dragonX + flameShift) + ", 30, " + (dragonDirection * 3) + ")", 10);
            }
        }
        dragonX += dragonDirection;
        dragon.style.left = dragonX + 'px';
        dragonCounter++;
        if (dragonX < -200 || dragonX > dragonMaxX)
        {
            clearInterval(dragonTimer);
            dragon.style.display = 'none';
        }
    }
}

function animateFlame(flameId, x, y, direction)
{
    var flame = document.getElementById(flameId);
    if (flame)
    {
        if (flameDirection == 0)
        {
            flameX = x;
            flameY = y;
            flameDirection = direction;
            flame.style.visibility = 'visible';
        }
        flame.style.left = flameX + 'px';
        flame.style.top = flameY + 'px';
        flameX += flameDirection;
        flameY += flameDescent;
        flameCounter++;
        if (flameCounter > 200)
        {
            clearInterval(flameTimer);
            flame.style.display = 'none';
            flameCounter = 0;
            flameDirection = 0;
        }
    }
}

function isDesignModeAvailable()
{
    return document.getElementById && document.designMode && !isSafari && !isKonqueror;
}

function addInviteBox(parentDiv, userId, aLabel, gameOptionStr, buttonLabel)
{
    var newDivId = parentDiv.id + 'body';
    if (document.getElementById(newDivId))
        parentDiv.removeChild(document.getElementById(newDivId));
    else
    {
        var newDiv = document.createElement('div');
        newDiv.setAttribute('id', newDivId);
        var formCode = '<form action="DefineNewGame" method="get">';
        formCode += '<div class=\"padded-border\">';
        formCode += aLabel + ': ';
        formCode += '<select name="tp" class="inputbox">';
        formCode += gameOptionStr.replace(/\|\|/g, "\"");
        formCode += '</select> ';
        formCode += '<input type="submit" name="submit" value="' + buttonLabel + '" class="inputbutton">';
        formCode += '<input type="hidden" name="op" value="' + userId + '">';
        formCode += '</div>';
        formCode += '</form>';
        newDiv.innerHTML = formCode;
        parentDiv.appendChild(newDiv);
    }
}

function findPosition(object)
{
    if (object.offsetParent)
    {
        for (var posX = 0, posY = 0; object.offsetParent; object = object.offsetParent)
        {
            posX += object.offsetLeft;
            posY += object.offsetTop;
//            alert(posX + ", " + posY);
        }
        return [posX, posY];
    }
    else
    {
        return [object.x, object.y];
    }
}

function getLeftOffset(elm, parents_up)
{
    var mOffsetLeft = elm.offsetLeft;
    var mOffsetParent = elm.offsetParent;
    if (!parents_up)
        parents_up = 10000; // arbitrary big number
    while (parents_up > 0 && mOffsetParent)
    {
        mOffsetLeft += mOffsetParent.offsetLeft;
        mOffsetParent = mOffsetParent.offsetParent;
        parents_up--;
    }
    return mOffsetLeft;
}

function getTopOffset(elm, parents_up)
{
    var mOffsetTop = elm.offsetTop;
    var mOffsetParent = elm.offsetParent;
    if (!parents_up)
        parents_up = 10000; // arbitrary big number
    while (parents_up > 0 && mOffsetParent)
    {
        mOffsetTop += mOffsetParent.offsetTop;
        mOffsetParent = mOffsetParent.offsetParent;
        parents_up--;
    }
    return mOffsetTop;
}

function initBannerEvents()
{
    var iframes = document.all ? document.all.tags('iframe') : document.getElementsByTagName('iframe');
    for (var i = 0; i < iframes.length; i++)
    {
        var frame = iframes[i];
        if (frame.src.indexOf('googlesyndication.com') > -1)
        {
            if (frame.addEventListener)
                frame.addEventListener('focus', adSenseCapture, false);
            else if (frame.attachEvent)
                frame.attachEvent('onfocus', adSenseCapture);
        }
        else if (frame.src.indexOf('chitika.net') > -1)
        {
            if (frame.addEventListener)
                frame.addEventListener('focus', chitikaCapture, false);
            else if (frame.attachEvent)
                frame.attachEvent('onfocus', chitikaCapture);
        }
        else if (frame.src.indexOf('adtegrity.net') > -1)
            {
                if (frame.addEventListener)
                    frame.addEventListener('focus', adtegrityCapture, false);
                else if (frame.attachEvent)
                    frame.attachEvent('onfocus', adtegrityCapture);
            }
            else if (frame.name == 'googleSearchFrame')
                {
                    if (frame.addEventListener)
                        frame.addEventListener('focus', adSearchCapture, false);
                    else if (frame.attachEvent)
                        frame.attachEvent('onfocus', adSearchCapture);
                }
    }
}

function adSenseCapture()
{
    if (window.status)
    {
        var bug = new Image();
        bug.src = 'http://BrainKing.com/en/AJAXAdSenseClick?ref=' + escape(document.location.href) +
                  '&url=' + escape(window.status);
    }
}

function chitikaCapture()
{
    if (window.status)
    {
        var bug = new Image();
        bug.src = 'http://BrainKing.com/en/AJAXChitikaClick?ref=' + escape(document.location.href) +
                  '&url=' + escape(window.status);
    }
}

function adtegrityCapture()
{
    if (window.status)
    {
        var bug = new Image();
        bug.src = 'http://BrainKing.com/en/AJAXAdtegrityClick?ref=' + escape(document.location.href) +
                  '&url=' + escape(window.status);
    }
}

function adSearchCapture()
{
    if (window.status)
    {
        var bug = new Image();
        bug.src = 'http://BrainKing.com/en/AJAXAdSearchClick?ref=' + escape(document.location.href) +
                  '&url=' + escape(window.status);
    }
}

function initBlogPageEvents()
{
    var bug = new Image();
    bug.src = 'http://BrainKing.com/en/AJAXBlogPage?ref=' + escape(document.location.href) +
              '&url=' + escape(uaValue);
}

function initBrainRookPageEvents()
{
    var bug = new Image();
    bug.src = 'http://BrainKing.com/en/AJAXBrainRookPage?ref=' + escape(document.location.href) +
              '&url=' + escape(uaValue);
}

function textLinkClickCapture(targetUrl)
{
    var bug = new Image();
    bug.src = 'http://BrainKing.com/en/AJAXAdSenseClick?ref=' + escape(document.location.href) +
              '&url=' + escape(targetUrl);
}
