var onloads = new Array();

window.onload = function()
{
    for (var i = 0; i < onloads.length; i++)
    {
        onloads[i]();
    }
}

function $(id)
{
    return document.getElementById(id)
}

function popup(url, width, height)
{
    var left = ((screen.width - width) / 2);
    var top = ((screen.height - height) / 2);

    nw = window.open(url, 'popup', 'left='+left+',top='+top+',width='+width+',height='+height+',resizable=yes');

    nw.focus();
    return nw;
}

String.format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ),
                                                arguments[ token + 1 ] );
    }
    return text;
};

function dialogShow(title, msg, buttons)
{
    var d = $('dialog');
    d.hide();
    d.innerHTML = '';

    var div = document.createElement('div');

    var p = document.createElement('h1');
    p.innerHTML = title;
    div.appendChild(p);

    var p = document.createElement('p');
    p.innerHTML = msg;
    div.appendChild(p);

    var p = document.createElement('p');

    p.style.textAlign = 'center';

    for (var i in buttons)
    {
        var button = buttons[i];

        var b = document.createElement('input');

        b.type = 'button';
        b.value = button;
        b.onclick = eval(i);
        b.style.margin = '4px';
        p.appendChild(b);
    }

    div.appendChild(p);

    d.appendChild(div);

    setTimeout('dialogAppear();', 100);
}

function dialogAppear()
{
    var d = $('dialog');
    d.hide();
    d.style.visibility = 'visible';
    Effect.SlideDown(d, {duration : 0.5});
    Effect.Appear(d, {duration : 0.5, to : 0.9});

    var v = $('overlay');
    v.style.height = $('container').getHeight()+'px';
    v.hide();
    v.style.visibility = 'visible';
    Effect.Appear(v, {duration : 0.25, to : 0.5});
}

function dialogHide()
{
    Effect.SlideUp('dialog', {duration : 0.5});
    Effect.Fade('dialog', {duration : 0.5});

    Effect.Fade('overlay', {duration : 0.25});
    setTimeout('dialogHideOverlayImage();', 250);
}

function dialogHideOverlayImage()
{
    //$('overlayimg').style.height = '1px';
}

function cursor(name)
{
    var els = document.getElementsByTagName('*');

    for (var i = 0; i < els.length; i++)
    {
        var el = els[i];
        el.style.cursor = name;
    }
}
