var uaValue;
var isIE;
var isGecko;
var isSafari;
var isKonqueror;
var globallyInited = false;

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();
    globallyInited = true;
}

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 += '<table border="1" cellpadding="5"><tr valign="top"><td>';
        formCode += aLabel + ': ';
        formCode += '<select name="tp">';
        formCode += gameOptionStr.replace(/\|\|/g, "\"");
        formCode += '</select>';
        formCode += '<input type="submit" name="submit" value="' + buttonLabel + '">';
        formCode += '<input type="hidden" name="op" value="' + userId + '">';
        formCode += '</td></tr></table>';
        formCode += '</form>';
        newDiv.innerHTML = formCode;
        parentDiv.appendChild(newDiv);
    }
}

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)
            {
                // alert(getLeftOffset(frame, 8) + ", " + getTopOffset(frame, 8) + ", " + frame.width + ", " + frame.height);
                frame.addEventListener('focus', chitikaCapture, false);
            }
            else if (frame.attachEvent)
                frame.attachEvent('onfocus', chitikaCapture);
        }
        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 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);
}
