Event.observe(window, 'load', init, false);

var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
var bMoz = (navigator.appName == 'Netscape');

function init() {
    refreshplayerbox();
    refreshchatbox();
    refreshplayarea();
    refreshsidebar();
}

//Loading
var loaded = false;
var enteringdice = false;

function startLoading() {
    loaded = false;
    window.setTimeout('showLoadingImage()', 5000);
}

function showLoadingImage() {
    var el = document.getElementById("loadingbar");
    if (el && !loaded) {
        el.style.display = 'block';
    }
}

function stopLoading() {
    var el = document.getElementById("loadingbar");
    el.style.display = 'none';
    loaded = true;
}
//End Loading

function refreshplayarea() {
    if (document.getElementById('playarea').innerHTML.substring(0,16) == 'It is your turn!') {
        setTimeout("refreshplayarea()",2000);
    } else {
        var url = 'server.php';
        var pars = 'fetch=playarea';
        var target = 'playarea';
        var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
        setTimeout("refreshplayarea()",4000);
    }
}

function refreshchatbox() {
    var url = 'server.php';
    var pars = 'fetch=chatbox';
    var target = 'chatbox';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
    setTimeout("refreshchatbox()",5000);
}

function addchat() {
    var url = 'server.php';
    var pars = 'action=addchat&message=' + escape(document.getElementById('chat_message').value);
    var myAjax = new Ajax.Updater('', url, {method: 'post', parameters: pars});
    document.getElementById('chat_message').value = '';
}

function refreshsidebar() {
    var url = 'server.php';
    var pars = 'fetch=sidebar';
    var target = 'sidebar';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
    setTimeout("refreshsidebar()",4000);
}

function checkmessagekeyup(key) {
    if (key == 13) {
        addchat();
    }
}

function creategame() {
    document.getElementById('creategame').style.display = 'block';
}

function hidecreategame() {
    document.getElementById('creategame').style.display = 'none';
}

function joingame(id) {
    var url = 'server.php';
    var pars = 'fetch=joingame&gameid=' + id;
    var target = 'playarea';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function leavegame(id) {
    var url = 'server.php';
    var pars = 'fetch=leavegame';
    var target = 'playarea';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function startgame() {
    var url = 'server.php';
    var pars = 'fetch=startgame';
    var target = 'playarea';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function refreshplayerbox(getform) {
    var url = 'server.php';
    if (getform=='login') {
        var pars = 'fetch=playerboxlogin';
    } else if (getform=='signup') {
        var pars = 'fetch=playerboxsignup';
    } else {
        var pars = 'fetch=playerbox';
    }
    var target = 'playerbox';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function playerlogout() {
    var url = 'server.php';
    var pars = 'fetch=logout';
    var target = 'playerbox';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function scrolldice(whichtype,direction) {
    if (document.getElementById('scrolldice_amount').innerHTML.lastIndexOf('">')>0) {
        var dice_amount_current = document.getElementById('scrolldice_amount').innerHTML.substring(document.getElementById('scrolldice_amount').innerHTML.lastIndexOf('">')+2);
    } else {
        var dice_amount_current = document.getElementById('scrolldice_amount').innerHTML.substring(document.getElementById('scrolldice_amount').innerHTML.lastIndexOf('2>')+2);
    }
    dice_amount_current = dice_amount_current.toLowerCase();
    dice_amount_current = dice_amount_current.substring(0,dice_amount_current.indexOf('</font>'));

    var dice_number_current = document.getElementById('scrolldice_number').innerHTML.substring(document.getElementById('scrolldice_number').innerHTML.lastIndexOf('dice_')+5);
    dice_number_current = dice_number_current.substring(0,1);


    if (whichtype=='amount') {
        if (direction=='up') {
            if (dice_amount_current<40) {
                dice_amount_current++;
            }
        } else if(direction=='down') {
            if (dice_amount_current>1) {
                dice_amount_current--;
            }
        }
    } else if (whichtype=='number') {
        if (direction=='up') {
            if (dice_number_current<6) {
                dice_number_current++;
            }
        } else if(direction=='down') {
            if (dice_number_current>1) {
                dice_number_current--;
            }
        }
    }

    document.getElementById('scrolldice_amount').innerHTML = '<font size="+2" color="black">' + dice_amount_current + '</font>';
    document.getElementById('scrolldice_number').innerHTML = '<img src="dice_' + dice_number_current + '.gif" />';
}

//Games

function taketurn(act) {
    if (document.getElementById('scrolldice_amount').innerHTML.lastIndexOf('">')>0) {
        var dice_amount_current = document.getElementById('scrolldice_amount').innerHTML.substring(document.getElementById('scrolldice_amount').innerHTML.lastIndexOf('">')+2);
    } else {
        var dice_amount_current = document.getElementById('scrolldice_amount').innerHTML.substring(document.getElementById('scrolldice_amount').innerHTML.lastIndexOf('2>')+2);
    }
    dice_amount_current = dice_amount_current.toLowerCase();
    dice_amount_current = dice_amount_current.substring(0,dice_amount_current.indexOf('</font>'));

    var dice_number_current = document.getElementById('scrolldice_number').innerHTML.substring(document.getElementById('scrolldice_number').innerHTML.lastIndexOf('dice_')+5);
    dice_number_current = dice_number_current.substring(0,1);

    var url = 'server.php';
    if (act == 'liar') {
        var pars = 'fetch=taketurn&act=liar';
    } else {
        var pars = 'fetch=taketurn&amount=' + dice_amount_current + '&dice=' + dice_number_current;
    }
    var target = 'playarea';
    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

//Misc

function showrules() {
    alert("I. Each pirate -- including you -- starts with 5 dice under their cup.\n\nII. The first player chooses a dice face (1-6) and makes a bid on how many of these he thinks are on the table underneath all the cups.\n\nIII. The next player then either bids on the same face but a higher amount, a higher amount of a different face, or if they suspect the previous player is lying they call 'LIAR'.\n\nIV. The bidding continues, player after player, until 'LIAR' is called, at which point all cups are raised and the amount of dice in the last bid are tallied.\n\nV. If less dice are showing the face of the bid, the Pirate that called 'LIAR' is correct and the bidder loses a dice.\n\nVI. If the same amount or more dice are showing the face of the bid, the Pirate that called 'LIAR' is wrong and loses a dice.\n\nVII. If a Pirate loses all their dice he is out.  Play continues until only one Pirate remains.");
}