Interface = {
    strReferer: 'posts',
    iniciar: function() {
        // Removendo a tela de loading
        if ($('loading')) {
            $('loading').remove();
        }

        // Criando botões das sessões
        Interface.criarBotoes();

        // jogando para a url padrão
        var strHash = location.hash;
        var strSessao = 'posts';

        if (strHash.length == 0) {
            $(strSessao).show();

            if ($('btn_' + strSessao)) {
                $('btn_' + strSessao).addClassName('ativo');
            }
        }

        // abrindo a chamada
        if (strHash.length > 0) {
            strSessao = strHash.replace('#', '');

            $(strSessao).show();

            if ($('btn_' + strSessao)) {
                $('btn_' + strSessao).addClassName('ativo');
            }
        }

        // Links externos devem abrir em uma nova janela
        Interface.links();

        // Formulário de contato é em ajax
        Interface.contato();
    },

    criarBotoes: function() {
        var arrBotoes  = new Array({
            id: 'posts',
            icone: 'big_twitter.png'
        }, {
            id: 'textos',
            icone: 'big_text.png'
        }, {
            id: 'albuns',
            icone: 'big_picasa.png'
        }, {
            id: 'portifolio',
            icone: 'big_portifolio.png'
        }, {
            id: 'curriculo',
            icone: 'big_pdf.png'
        }, {
        //    id: 'links',
        //    icone: 'big_digg.png'
        //}, {
            id: 'shared',
            icone: 'big_g_reader.png'
        }, {
            id: 'network',
            icone: 'big_network.png'
        }, {
            id: 'desenhos',
            icone: 'big_desenhos.png'
        }, {
            id: 'contato',
            icone: 'big_email.png'
        });

        // Criando a div pra inserir os botões
        var divBotoes = Builder.node('div', {
            id: 'botoes'
        });

        document.body.appendChild(divBotoes);

        $('botoes').addClassName('botoes');

        // Criando os botões
        arrBotoes.each(function(e) {
            var imgContent = Builder.node('img', {
                src: '/img/icons/' + e.icone,
                alt: e.id
            });

            var lnkContent = Builder.node('a', {
                id: 'btn_' + e.id,
                href: 'index.php#' + e.id
            }, e.id);

            lnkContent.appendChild(imgContent);

            divBotoes.appendChild(lnkContent);

            $(e.id).hide().style.visibility = 'visible';
        });

        // Criando os eventos de clique dos botões
        Interface.criarEventos();
    },

    criarEventos: function() {
        // Percorrendo os botões criados
        $$('div.botoes a').each(function(e) {
            var destino = e.id.replace('btn_', '');

            // Capturando o clique de cada botão
            Event.observe(e, 'click', function() {
                $('btn_' + destino).addClassName('ativo');

                setTimeout(function() {
                    $(destino).show();
                }, 600);

                if (location.hash.length > 0) {
                    Interface.strReferer = location.hash.replace('#', '');
                }

                if (Interface.strReferer != destino) {
                    $('btn_' + Interface.strReferer).removeClassName('ativo');
                }

                $(Interface.strReferer).fade({
                    duration: 0.5,
                    from: 1,
                    to: 0
                });
            });
        });
    },

    links: function() {
        var externos = new Array({
            id: 'shared'
        }, {
            id: 'albuns'
        }, {
            id: 'network'
        //}, {
        //    id: 'links'
        }, {
            id: 'posts'
        }, {
            id: 'textos'
        }, {
            id: 'desenhos'
        }, {
            id: 'portifolio'
        });

        externos.each(function(area) {
            $$('div.' + area.id.toString() + ' a').each(function(objLink) {
                Event.observe($(objLink), 'click', function(e) {
                    window.open(objLink.href);

                    Event.stop(e);

                    return false;
                })
            });
        });
    },

    contato: function() {
        // criando o div de retorno
        var retornos = Builder.node('div', {
            id: 'retornos'
        });

        document.body.appendChild(retornos);

        $('retornos').addClassName('boxMsg').hide();

        // Limpando as áreas de texto
        var arrTxtOriginal = new Array();

        $$('form.contato input').each(function(objLink) {
            arrTxtOriginal[ objLink.id ] = $F(objLink);

            // limpando os campos, somente se o conteúdo for diferente do original
            if (objLink.type == 'text') {
                Event.observe(objLink, 'focus', function() {
                    if (arrTxtOriginal[ objLink.id ] == $F(objLink)) {
                        $(objLink).value = '';
                    }
                });
            }
        });

        // Limpando o conteúdo sempre que o mesmo ganhar foco
        arrTxtOriginal['ContatoConteudo'] = $F('ContatoConteudo');

        Event.observe('ContatoConteudo', 'focus', function() {
            if (arrTxtOriginal['ContatoConteudo'] == $F('ContatoConteudo')) {
                $('ContatoConteudo').value = '';
            }
        });

        Event.observe( $('contato'), 'submit', function(e) {
            var strMensagem = null;

            $('hdnBooAjax').value = 1;

            new Ajax.Request('loadlib/contato.php', {
                parameters: Form.serialize('contato'),
                requestHeaders:['X-Update', 'retornos'],
                method:'post',
                asynchronous:true,
                evalScripts:true,
                onLoading: function() {
                    strMensagem  = '<b class="titulo">Contato</b>';
                    strMensagem += 'aguarde...';

                    $('retornos').show().update(strMensagem);
                },
                onComplete: function(t) {
                    strMensagem  = '<b class="titulo">Contato</b>';
                    strMensagem += t.responseText;

                    $('retornos').show().update(strMensagem);
                },
                onFailure: function() {
                    strMensagem  = '<b class="titulo">Erro</b>';
                    strMensagem += 'Erro enviando mensagem.';

                    $('retornos').show().update(strMensagem);

                    $('ContatoEnviar').show().update(arrTxtOriginal['ContatoEnviar']);
                }
            });

            Event.stop(e);

            return false;
        });

        setInterval(function() {
            Effect.BlindUp('retornos', {
                duration: 0.5
            });
        }, 8000);
    }
}

Event.observe(window, 'load', function() {
    Interface.iniciar();

    try {
        var pageTracker = _gat._getTracker("UA-1337986-1");
        pageTracker._initData();
        pageTracker._trackPageview();
    } catch(err) {}
});
