//
// Esse código com cara de gambiarra foi escrito por Guilherme Vinicius
// Se você for um bom rapaz, vai deixar os créditos! hehe é nozes! xD
// http://www.guilhermevinicius.com.br/
//


var largura;
var altura;
var completo;

$(window).resize(function () {
    largura = $(document).width();
    altura = $(document).height();
});



function corrige(porLinha) {
    var v = 0;
    var line = 0;
    $(".pessoa").each(function (i) {

        if (v == porLinha) {
            line++;
            v = 0;
        }
        if (v > (porLinha / 2)) {
            $(this).addClass('direita');
        }
        $(this).css('width', 140);
        $(this).css('left', (v * 140));
        $(this).css('top', (line * 165));
        v++;
    });
}

//aqui está a magia
function magia(porLinha) {
    var v = 0;
    var line = 0;
    $(".pessoa").each(function (i) {

        if (v == porLinha) {
            line++;
            v = 0;
        }
        if (v > (porLinha / 2)) {
            $(this).addClass('direita');
        }
        $(this).css('width', 140);
        $(this).css('left', (v * 140));
        $(this).css('top', (line * 165));
        v++;

        $(this).mouseenter(function () {
            $(this).css({ 'width': '280px', 'z-index': '1' });
            $(this).find('img').removeClass('alpha');

            if ($(this).hasClass('direita')) {
                $(this).find('.legenda').addClass('legendaDireita');
                $(this).find('img').addClass('fotoDireita');

                var novo = ($(this).position().left - 140);
                $(this).css({ 'left': novo });
            }

            //fix: foto do lado
            //$(this).find('img').mouseleave(function () {
            //$(this).parent().mouseleave();
            //});
        });

        $(this).mouseleave(function () {
            $(this).css({ 'width': '140px', 'z-index': '0' });
            $(this).find('img').addClass('alpha');

            if ($(this).hasClass('direita')) {
                $(this).find('.legenda').removeClass('legendaDireita');
                $(this).find('img').removeClass('fotoDireita');

                var novo = ($(this).position().left + 140);
                $(this).css({ 'left': novo });
            }
        });
    });
}
$('#linkUp').live('click', function () {
    if ($('#nome').val() == "" || $('#nome').val() == "nome") {
        AttentionBox.showMessage("Você precisa digitar o seu nome!");
        return;
    } else {

        var nome = $('#nome').val();
        var profissao = ($('#profissao').val() != 'profissão') ? $('#profissao').val() : '';
        var musica = ($('#musica').val() != 'música') ? $('#musica').val() : '';

        $.fancybox({
            'padding': 0,
            'href': 'box.php?nome=' + nome + '&profissao=' + profissao + '&musica=' + musica,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'width': 480,
            'height': 250,
            'type': 'iframe'
        });
    }
});

//Bug Fix Chrome
function fix() {
    var largura = $(document).width();
    var altura = $(document).height();
    var porLinha = Math.ceil((largura / 140));
    corrige(porLinha);
}
$('#POG').mouseleave(function () {
    fix();
    return;
}).mouseenter(function () {
    fix();
    return;
});


$(document).ready(function () {
    //hack
    if (jQuery.browser.msie) {
        $('#linkUp').css({ 'width': '179px' });
    }
    $('#recolhido').hide();

    /*$('#painel').mouseleave(function () {
    var novo = ($(this).position().left - 500);
    $(this).animate({left : novo}, 1500,function(){
    $('#recolhido').show('slow');
    });
    });*/

    /*$('#recolhido').mouseenter(function () {
    $(this).hide('slow', function() {
    $('#painel').animate({left : 0}, 1000);
    });			
    });*/

    largura = $(this).width();
    altura = $(this).height();

    //$('#pelicula').css({ 'width': largura, 'height': altura });

    var porLinha = Math.ceil((largura / 140));
    var porColuna = Math.ceil((altura / 165));

    paraPreencher = (porLinha * porColuna);

    if ($('.pessoa').size() < paraPreencher) {
        //não vai dar pra preencher, e agora?	
        ClonaIsso();
    }

    // Invoca o poder de Merlin
    magia(porLinha);

    $('.pessoa img').addClass('alpha');
    $('.pessoa, footer').addClass('clearfix');

    //Fim do Ready
});

function ClonaIsso() {
    if ($('.pessoa').size() < paraPreencher) {
        //não vai dar pra preencher, e agora?	
        $(".pessoa").clone().prependTo("#rockers");
        ClonaIsso();
    }
}
