/**
 * Monta a barra 93
 */

var winW = 1000;
var links = [
    /*{
        label    : '',
        url      : '',
        id       : '',
        title    : ''
    },*/
    {
        label    : '<img src="http://thelema.org.br/barra93/logo-thelema.png" alt="Thelema.org.br" />',
        url      : 'http://thelema.org.br',
        id       : 'thelema',
        title    : 'Faze o que tu queres dever&aacute; ser o todo da Lei'
    },
    {
        label    : 'AstrumArgentum.org.br',
        url      : 'http://astrumargentum.org.br',
        id       : 'aa',
        title    : 'Uma representa&ccedil;&atilde;o brasileira da Ordem A.&middot;.A.&middot;.'
    },
    {
        label    : 'Ocultura.org.br',
        url      : 'http://ocultura.org.br',
        id       : 'ocultura',
        title    : 'Enciclop&eacute;dia virtual sobre Ma&ccedil;onaria, Rosacrucianismo, Gnosticismo, Magia e Thelema'
    },
    {
        label    : 'Hadnu.org',
        url      : 'http://hadnu.org',
        id       : 'hadnu',
        title    : 'Biblioteca virtual com e-books sobre ocultismo, magia, misticismo e religi&atilde;o.'
    },
    {
        label    : 'Collegium.org.br',
        url      : 'http://collegium.org.br',
        id       : 'calen',
        title    : 'Uma moderna escola de Ocultismo &agrave; servi&ccedil;o da Inicia&ccedil;&atilde;o'
    },
    {
        label    : 'Equinocio.ws',
        url      : 'http://equinocio.ws',
        id       : 'equinocio',
        title    : 'Portal com os textos do peri&oacute;dico The Equinox'
    },
    {
        label    : '<img src="http://thelema.org.br/barra93/forum.png" alt="F&oacute;rum" />',
        url      : 'http://thelema.com.br/forum/',
        id       : 'comentarios',
        title    : 'F&oacute;rum thel&ecirc;mico'
    },
    {
        label    : '<img src="http://thelema.org.br/barra93/facebook.png" alt="Facebook" />',
        url      : 'http://www.facebook.com/pages/Collegium-ad-Lux-et-Nox/154960084541330',
        id       : 'facebook',
        title    : 'P&aacute;gina do Collegium ad Lux et Nox no Facebook'
    },
    {
        label    : '<img src="http://thelema.org.br/barra93/yahoo.png" alt="Yahoo" />',
        url      : 'http://br.groups.yahoo.com/group/Thelema-BR/',
        id       : 'yahoo',
        title    : 'Lista de discuss&atilde;o Thelema-BR'
    }
];


/**
 * Monta a Barra93
 */
function montarBarra93()
{
    var html = '<div id="barra93"><ul>';
    
    for (i in links) {
        html += '<li id="barra93-' + links[i].id + '"><a href="' + links[i].url + '" title="' + links[i].title + '">' + links[i].label + '</a></li>';
    }
    
    html += '</ul></div>';
    
    document.write(html);
}

/**
 * Carrega a largura da barra 93
 */
function dimensionarBarra93() {

    if (document.body && document.body.offsetWidth) {
        winW = document.body.offsetWidth;
    }
    if (document.compatMode=='CSS1Compat' &&
        document.documentElement &&
        document.documentElement.offsetWidth ) {
     winW = document.documentElement.offsetWidth;
    }
    if (window.innerWidth) {
     winW = window.innerWidth;
    }
    
    var root= document.compatMode=='BackCompat'? document.body : document.documentElement;
    
    if (root && root.clientWidth) {
        winW = root.clientWidth;
    }

    document.getElementById('barra93').style.width = winW + 'px';
    
    if (document.getElementById('barra93-thelema')) {
        
        var larguraBotaoThelema = (winW - 805);
        
        if (larguraBotaoThelema < 310) {
            larguraBotaoThelema = 310;
        }
        
        document.getElementById('barra93-thelema').style.width = larguraBotaoThelema + 'px';
    }
}

/**
 * Adiciona o CSS da barra 93
 */
function cssBarra93() {
    var headtg = document.getElementsByTagName('head')[0];
    if (!headtg) {
        return;
    }
    var linktg = document.createElement('link');
    linktg.type = 'text/css';
    linktg.rel = 'stylesheet';
    linktg.href = 'http://thelema.org.br/barra93/barra.css';
    headtg.appendChild(linktg);
}

/**
 * Procura se esta em algum dos sites e ativa o bot&atilde;o
 */
function botaoAtivoBarra93()
{
    var url = location.href;
    var id = '';
    var suburl = '';
    
    // Depois de http:// e antes de qualquer URI
    url = url.split('/');
    
    if (url.length >= 4) {
        suburl = url[3];
    }
    
    url = url[2];
    
    // Remove qualquer www. ou .org
    url = url.split('.');
    
    // Localhost
    if (url.length == 1) {
        url = url[0];
    }
    // .org.br
    else if (url[url.length - 1] == 'br') {
        url = url[url.length - 3];
    }
    // .org
    else {
        url = url[url.length - 2];
    }
    
    // Nome da classe
    id = url;
    
    if (url == 'astrumargentum') {
        id = 'aa';
    }
    else if (url == 'collegium') {
        id = 'calen';
    }
    else if (url == 'hadnu' && suburl == 'forum') {
        id = 'comentarios';
    }
    
    id = 'barra93-' + id;
    
    // Seleciona
    if (document.getElementById(id)) {
        document.getElementById(id).className = 'active';
    }
}

// Primeiro carregamento
montarBarra93();
botaoAtivoBarra93();
cssBarra93();
dimensionarBarra93();

// Caso o usu&aacute;rio redimensione a janela, executar&aacute; a fun&ccedil;&atilde;o que encontra a largura da tela
window.onresize = dimensionarBarra93;
