/**
 * Gestionnaire des fonctions d'initialisation
 */
var initFunctions = new Array;

function registerInitFunction(_function)
{
    initFunctions[initFunctions.length] = _function;
}

function fireInitFunctions()
{
    for(i = 0; i < initFunctions.length; i++)
    {
        initFunctions[i]();
    }
}

window.onload = fireInitFunctions;

/**
 * Fonctions utilitaires pour les images
 */
var swappedImg = new Array;

function imgPreload()
{
    var d = document;
    if(d.images){
        var i, j = 0, p = new Array, a = imgPreload.arguments;
        for(i = 0; i < a.length; i++)
        {
            p[j] = new Image;
            p[j++].src = a[i];
        }
    }
}

function imgSwap()
{
    var i, j = swappedImg.length, x, a = imgSwap.arguments;
    for(i = 0; i < a.length - 1; i += 2)
    {
        x = document.getElementById(a[i]);
        if(x != null){
            swappedImg[j++] = x;
            if(!x.o) x.o = x.src;
            x.src = a[i + 1];
        }
    }
}

function imgRestore()
{
    var i, x, a = swappedImg;
    for(i = 0; i < a.length; i++)
    {
        x = a[i];
        if(x.o) x.src = x.o;
    }
}

/**
 * Fonctions du POST-it
 */
var runner = null;

function initPostIt()
{
    if(document.getElementById('postItContainer') && document.getElementById('postItChild'))
    {
        var container = document.getElementById('postItContainer');
        container.style.height = container.offsetHeight + 'px';
        
        var child = document.getElementById('postItChild');
        child.style.top = '0px';
        child.style.left = container.offsetWidth + 'px';
        
        startPostIt();
    }
}

function animPostIt()
{
    var child = document.getElementById('postItChild');
    var position = parseInt(child.style.left);
    if(position <= -1 * child.offsetWidth)
    {
        child.style.left = document.getElementById('postItContainer').offsetWidth + 'px';
    }
    else
    {
        child.style.left = (position - 1) + 'px';
    }
}

function startPostIt()
{
    runner = setInterval('animPostIt()', 18);
}

function stopPostIt()
{
    clearInterval(runner);
}

registerInitFunction(initPostIt);

/**
 * Fonctions du menu
 */
over = -1;
delay = 10;

function initMenu()
{
    imgPreload("img/menu_sep_over.gif");
}

function overMenu(id)
{
    over = id;
    document.getElementById("menu" + id).style.background = "#193244";
    imgSwap("sep" + (id - 1), "img/menu_sep_over.gif");
    imgSwap("sep" + (id + 1), "img/menu_sep_over.gif");
    if(document.getElementById("sub" + id))
    {
        document.getElementById("sub" + id).style.visibility = "visible";
    }
}

function outMenu(id)
{
    over = -1;
    window.setTimeout("realOut(" + id + ")", delay);
}

function overSubMenu(id)
{
    over = id;
}

function outSubMenu(id)
{
    over = -1;
    window.setTimeout("realOut(" + id + ")", delay);
}

function realOut(id)
{
    if(over != id)
    {
        document.getElementById("menu" + id).style.background = "#7FA5B2";
        if(over != id - 2)
        {
            imgSwap("sep" + (id - 1), "img/menu_sep_out.gif");
        }
        if(over != id + 2)
        {
            imgSwap("sep" + (id + 1), "img/menu_sep_out.gif");
        }
        if(document.getElementById("sub" + id))
        {
            document.getElementById("sub" + id).style.visibility = "hidden";
        }
    }
}

registerInitFunction(initMenu);
