var game = null;
var numberOfPlayers = 0;
var numberOfRounds = 0;
var skillLevel = "original";
var callback = new AsyncCallback();
var round = null;


/**
 * Reveal a layer
 */
function showLayer(layerName)
{
    if (layerName.style != null)
    {
        layerName.style.display = "";
    }
}

/**
 * Hide a layer
 */
function hideLayer(layerName)
{
    if (layerName.style != null)
    {
        layerName.style.display = "none";
    }
}

/**
 * Reveal a layer
 */
function showLayerByName(layerName)
{
    var layer = document.getElementById(layerName);
    if (layer != undefined)
    {
        showLayer(layer);
    }
}

/**
 * Hide a layer
 */
function hideLayerByName(layerName)
{
    var layer = document.getElementById(layerName);
    if (layer != undefined)
    {
        hideLayer(layer);
    }
}

/**
 * Reveal a div
 */
function showDiv(div)
{
    if (div.style != null)
    {
        div.style.visibility = "visible";
    }
}

/**
 * Hide a div
 */
function hideDiv(div)
{
    if (div.style != null)
    {
        div.style.visibility = "hidden";
    }
}

/**
 * Reveal a div
 */
function showDivById(id)
{
    var div = document.getElementById(id);
    if (div != undefined)
    {
        showDiv(div);
    }
}

/**
 * Hide a div
 */
function hideDivById(id)
{
    var div = document.getElementById(id);
    if (div != undefined)
    {
        hideDiv(div);
    }
}

function assert(expr, msg)
{
    if (!expr)
    {
        jslog(msg);
    }
}

/**
 *  Log a message
 */
function jslog(msg)
{
    try
    {
        if (parent && parent.dialog)
        {
            parent.dialog.append(msg);
        }
        else
        {
            alert(msg);
        }
    }
    catch (e)
    {
    }
}

function introduceGame()
{
    // position the anchor to the upper left hand corner of the
    //

    // place and show the box and some links
    moveDiv('decknote', 300, 68, 'anchor', true);
    moveDiv('introduction', 30, 100, 'anchor', true);
}

function beginGame()
{
    hideDivById('introduction');
    hideDivById('decknote');

    var numPlayers = document.getElementById('numPlayers');
    numberOfPlayers = numPlayers.value;

    var numRounds = document.getElementById('numRounds');
    numberOfRounds = numRounds.value;

    var chosenSkillLevel = document.getElementById('skillLevel');
    skillLevel = chosenSkillLevel.value;

    game = new Game(parseInt(numberOfPlayers), parseInt(numberOfRounds));
    game.playNextRound();
}


function moveDiv(id, left, top, anchor, isVisible, zOrder)
{
    var element = document.getElementById(id);
    var theStyle = element.style;

    if (anchor != null)
    {
        var pos = getElementPosition(anchor);
        left = parseInt(pos.left) + left;
        top = parseInt(pos.top) + top;
    }

    theStyle.left = left;
    theStyle.top = top;
    if (zOrder != null)
    {
        if (!isNaN(zOrder))
        {
            theStyle.zIndex = zOrder;
        }
    }

    if (isVisible == true)
    {
        showDivById(id);
    }
    else
    {
        hideDivById(id);
    }
}


function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}


/**
 * Open a daughter window with the specified url
 * @param thePage the relative url of the page
 */
function popup(thePage, target, width, height, parms){
    try
    {
        if (!width)
        {
            width = "620";
        }
        if (!height)
        {
            height = "380";
        }
        if (!parms)
        {
            parms = "resizable=yes";
        }

        // get dimensions to center the popup
        var screenWidth = screen.availWidth;
        var screenHeight = screen.availHeight;
        var leftPos = (screenWidth - width) / 2;
        var topPos  = (screenHeight - height) /2;

        var newWindow = window.open(thePage, target,"width=" + width + ",height=" + height + ",top=" + topPos + ",left=" + leftPos + "," + parms);
        newWindow.focus();
    }
    catch(exception)
    {
    }
    return false;
}


function chartZoom(zoomIn)
{
    if (zoomIn)
    {
        moveDiv('chartZoom', 230, 270, 'anchor', true, 1000);
    }
    else
    {
        moveDiv('chartZoom', 230, 270, 'anchor', false, 0);
    }
}

function flyCard(card, left, top, anchor, zOrder, rotateCardAtDestination)
{
    if (rotateCardAtDestination == null)
    {
        rotateCardAtDestination = false;
    }

    var id = card.getDivId();

    var element = document.getElementById(id);
    var theStyle = element.style;

    if (anchor != null)
    {
        var pos = getElementPosition(anchor);
        left = parseInt(pos.left) + left;
        top = parseInt(pos.top) + top;
    }

    // while flying the zorder is 10 more than while not flying
    if (zOrder == null)
    {
        zOrder = theStyle.zIndex + 10;
    }
    else
    {
        zOrder += 10;
    }

    theStyle.zIndex = zOrder;

    moveCardTo(id, card.getColor(), left, top, rotateCardAtDestination);
}

function moveCardTo(divId, color, left, top, rotateCardAtDestination)
{
    showDivById(divId);

    var currentPos = getElementPosition(divId);
    var tl = parseInt(currentPos.left);
    var tt = parseInt(currentPos.top);

    if (tl < left)
    {
        tl++;
        if (tl < (left-3)) { tl += 4; }
    }
    else if (tl > left)
    {
        tl--;
        if (tl > (left+3)) { tl -= 4; }
    }

    if (tt < top)
    {
        tt++;
        if (tt < (top-3)) { tt += 4; }
    }
    else if (tt > top)
    {
        tt--;
        if (tt > (top+3)) { tt -= 4; }
    }

    var element = document.getElementById(divId);
    var theStyle = element.style;

    theStyle.left = tl;
    theStyle.top = tt;

    if ((tl == left) && (tt == top))
    {
        // card has arrived at the target location
        // decrease the artificial zOrder bump during flight
        var zOrder = theStyle.zIndex;
        theStyle.zIndex = zOrder - 10;

        // leave it as is unless the 'r'otated form of the card
        // is to be shown
        if (rotateCardAtDestination)
        {
            if ((color == 'wild') || (color == 'plus2'))
            {
                hideDivById(divId);
                divId = 'r'+divId;
                var element = document.getElementById(divId);
                var theStyle = element.style;

                theStyle.left = tl;
                theStyle.top = tt;
                theStyle.zIndex = zOrder;
                showDivById(divId);
            }
        }

        callback.decrement();

        return;
    }
    else
    {
        setTimeout("moveCardTo('" + divId + "','" + color + "'," + left + "," + top + "," + rotateCardAtDestination + ");",1);
    }
}


