function viewFrame(Url)
{
    // Désactivation des object qui peuvent merder avec les z-index
    var objects = document.getElementsByTagName('object');
    var objects_length = objects.length;
    for (i = 0; i < objects_length; i++)
    {
        objects[i].parentNode.removeChild(objects[i]);
    }

    var body = document.getElementsByTagName('body')[0];

    var frame = document.createElement('iframe');
    frame.setAttribute('id', 'nkFrame');
    frame.setAttribute('scrolling', 'auto');
    frame.setAttribute('frameborder', '0');
    frame.setAttribute('src', Url);
    frame.style.width = document.getElementsByTagName('html')[0].offsetWidth;

    var link = document.createElement('a');
    link.setAttribute('href', '#');
    link.setAttribute('id', 'nkFrameClose');
    link.setAttribute('onclick', 'hideFrame(); return false;');
    link.innerHTML = '<span class="x">X</span> <span class="text">Fermer</span>';

    body.appendChild(link);
    body.appendChild(frame);
}

function hideFrame()
{
    var body = document.getElementsByTagName('body')[0];
    document.getElementById('nkFrameClose').style.visibility = "hidden";
    body.removeChild(document.getElementById('nkFrameClose'));
    body.removeChild(document.getElementById('nkFrame'));
}

function displaySessionRefresh()
{
    var _RIMAGE = new Image(1,1);
    _RIMAGE.src = web_path + "admin/keepsession.php?" + Math.round(Math.random()*1000000000);
    window.setTimeout('displaySessionRefresh()', 15 * 60 * 1000);
}

function createToolbarLink(elm_class, url, text, inFrame)
{
    var li = document.createElement('li');
    li.className = elm_class;

    var link = document.createElement('a');
    link.setAttribute('href', url);

    if (inFrame)
    {
        link.onclick = function () {
            viewFrame(this.href + '&inFrame');
            return false;
        }
    }

    link.innerHTML = text;

    li.appendChild(link);
    return li;
}

function initToolbar()
{
    if (is_logged)
    {
        var div = document.createElement('div');
        div.id = 'nkToolbar';

        var list = document.createElement('ul');

        list.appendChild(createToolbarLink('edit', web_path + 'admin/edit.php?page=' + current_page, 'Editer cette page', true));
        list.appendChild(createToolbarLink('pages', web_path + 'admin/pages.php?base=' + current_page, 'Gestion des pages', true));
        list.appendChild(createToolbarLink('documents', web_path + 'admin/documents.php?1', 'Documents', true));
        list.appendChild(createToolbarLink('config', web_path + 'admin/config.php?1', 'Configuration', true));
        list.appendChild(createToolbarLink('logout', web_path + 'admin/login.php?logout=1', 'Déconnexion', true));

        div.appendChild(list);
        document.getElementsByTagName('body')[0].appendChild(div);

        window.setTimeout('displaySessionRefresh()', 15 * 60 * 1000);
    }
    else
    {
        var div = document.createElement('div');
        div.id = "nkLoginContainer";

        var link = document.createElement('a');
        link.setAttribute('href', web_path + 'admin/login.php');
        link.id = 'nkLogin';
        link.innerHTML = 'Connexion';
        link.onclick = function () {
            viewFrame(this.href + '?inFrame');
            return false;
        }

        div.appendChild(link);
        document.getElementsByTagName('body')[0].appendChild(div);
    }
}

function reinitToolbar()
{
    var body = document.getElementsByTagName('body')[0];

    if (document.getElementById('nkToolbar'))
    {
        body.removeChild(document.getElementById('nkToolbar'));
    }
    if (document.getElementById('nkLoginContainer'))
    {
        body.removeChild(document.getElementById('nkLoginContainer'));
    }

    initToolbar();
}