var $_basedir = '/';
var $_layout = 'bc';
var $_module = 'site'
var $_layoutPath = $_basedir + $_module + '/layouts/' + $_layout;
var $_defaultPath = $_basedir + 'default';

$(function(){

    $('#facebox').show();
    $('#fechar_banner').click(function(){
        $('#facebox').hide();
    })

    $(window).scroll(function(){
        mudarAlturaFace();
        centralizarBanner();
    })


    mudarAlturaFace();
    centralizarBanner();
   
    /* Fim do Twitter */
    clickLink('/index/home');
    
    getTwitter('#notificacao p',1,'topo');
    setInterval('getTwitter("#notificacao p",1,"topo")',300000);
    
    $('#player').flash(
        {
          src: $_basedir+$_module+'/layouts/'+$_layout+'/som.swf',
          width: 360,
          height: 60,
          wmode: 'transparent'
        },
        {version: 8}
    );

    $('#carregando').flash(
        {
          src: $_basedir+$_module+'/layouts/'+$_layout+'/carregando.swf',
          width: 360,
          height: 60,
          wmode: 'transparent'
        },
        {version: 8}
    );

    $('#menu a:not(a[class="radio-link"]),#menu_rodape a:not(a[rel="externo"]),h1 a').click(function(){
        link_atual = $(this).attr('href');
        clickLink(link_atual);
        return false;
    });

    $('.radio-link').click(function(){
        link_atual = $(this).attr('href');
        window.open(link_atual, "_blank", "scrollbars = no, width = 701, height = 434, status=no, location = no, toolbar = no, menubar=none");
        return false;
    })

    /*Face*/
    $('#bg-agenda-list').append('<li>opa</li>');

});

function mudarAlturaFace(){
    altura = $(document).height();
    $('#facebox').css({'height':altura});
    return altura;
}

function centralizarBanner(){

    largura_banner  = $('#img_banner').width();
    altura_banner   = $('#img_banner').height();

    janela_w = $(window).width();
    janela_h = $(window).height();

    margin_w = (janela_w/2)-(largura_banner/2);
    margin_h = (janela_h/2)-(altura_banner/2);

    $('#banner_home').css({
        'position':'absolute',
        'left':margin_w,
        'top':margin_h
    });
    
}

function getInit(){

    idPaiGoogle = $('#div-6156774867193325762').parent().attr('id');
    
    if(idPaiGoogle == 'li-friends-google' || idPaiGoogle == 'li-friends-google-a-dupla'){
        var skin = {};
        skin['BORDER_COLOR'] = '#cccccc';
        skin['ENDCAP_BG_COLOR'] = '#291203';
        skin['ENDCAP_TEXT_COLOR'] = '#cec0b1';
        skin['ENDCAP_LINK_COLOR'] = '#ff9115';
        skin['ALTERNATE_BG_COLOR'] = '#ffffff';
        skin['CONTENT_BG_COLOR'] = '#ffffff';
        skin['CONTENT_LINK_COLOR'] = '#0000cc';
        skin['CONTENT_TEXT_COLOR'] = '#333333';
        skin['CONTENT_SECONDARY_LINK_COLOR'] = '#7777cc';
        skin['CONTENT_SECONDARY_TEXT_COLOR'] = '#666666';
        skin['CONTENT_HEADLINE_COLOR'] = '#333333';
        skin['NUMBER_ROWS'] = '2';
        google.friendconnect.container.setParentUrl('/' /* location of rpc_relay.html and canvas.html */);
        google.friendconnect.container.renderMembersGadget(
         {id: 'div-6156774867193325762',
           site: '15047323715050287452'},
          skin);
    }
    
    mascaras();

    function newCep(objeto){
	var quebra = $(objeto).attr('id').split("_");
	var n = quebra[1];

	// Se o campo CEP n�o estiver vazio
	if($.trim($(objeto).val()) != ""){

		//atribuo o valor de "carregando..." nos campos e desabilito eles
		$("#rua_"+n).val('carregando...').attr('readonly','readonly');
		$("#bairro_"+n).val('carregando...').attr('readonly','readonly');
		$("#cidade_"+n).val('carregando...').attr('readonly','readonly');
		$("#uf_"+n).val('carregando...').attr('readonly','readonly');

		q = $(objeto).val();

		$.getScript("http://www.testedeprojetos.com.br/cep/?cep="+q+"&reqtype=jquery", function(){
                    if (resultadoCEP['uf']!='') {
                        // troca o valor dos elementos
                        $("#rua_"+n).val(unescape(resultadoCEP['logradouro']));
                        $("#bairro_"+n).val(unescape(resultadoCEP ['bairro']));
                        $("#cidade_"+n).val(unescape(resultadoCEP ['cidade']));
                        $("#uf_"+n).val(unescape(resultadoCEP ['uf']));
                        $("#rua_"+n+", #cidade_"+n+", #uf_"+n+", #bairro_"+n+"").css('color','#999');

                    }else{
                        alert('Não foi encontrado nenhum endereço com este CEP, verifique-o.');
                        $("#rua_"+n).val('').removeAttr("readonly");
                        $("#bairro_"+n).val('').removeAttr('readonly');
                        $("#cidade_"+n).val('').removeAttr('readonly');
                        $("#uf_"+n).val('').removeAttr('readonly');
                        $("#cep_"+n).focus();
                    }
                });
        }else{
                $("#rua_"+n).val('').removeAttr("readonly");
                $("#bairro_"+n).val('').removeAttr('readonly');
                $("#cidade_"+n).val('').removeAttr('readonly');
                $("#uf_"+n).val('').removeAttr('readonly');
        }

    }

    $('input[id*="cep"]').blur(function(){
	newCep($(this));
    });//fim do blur

    getTwitter('#tweets',7,'index');
    setInterval('getTwitter("#tweets",7,"index")',300000); // 5 em 5 minutos

    
    $('#papel,.linkAjax,#link_banner_home').click(function(){
        clickLink($(this).attr('href'));
        $('#facebox').hide();
        return false;
    })

    $('#menu li a').bind('mouseover',function(){
       classe = $(this).parent().attr('class');
       $(this).parent().css('backgroundImage','url("site/layouts/bc/imagens/btn_'+classe+'.png")');
       $(this).css('color','#FFF');
    });

    $('#menu li a').bind('mouseout',function(){
        $(this).parent().css('backgroundImage','none');
        $(this).css('color','#cec0b1');
    });

    $("a[rel=externo]").attr('target','_blank');

    $('#redes-sociais li:not(:first)').css({'marginLeft':'42px'});

    
    $('.video-link,.titulo-list-video').click(function(){
        id = $(this).attr('href');
        $.ajax({
            url:'index/video-atual',
            data:{'id':id},
            beforeSend:function(){
              $('.carregando1').show();
            },
            success:function(data){
                $('#video_destaque').html(data);
            },
            complete:function(){
                $('.carregando1').hide();
            }
        })
        return false;
    });
    
    /* MUSICAS */
    
    $('.disco .musicas').hide();
    $('.disco .disc_description').unbind('click').click(function(){
        if($('#musicas-' + $(this).attr('id').split('-').pop()).is(':hidden'))
            $('#musicas-' + $(this).attr('id').split('-').pop()).stop().slideDown('slow');
        else
            $('#musicas-' + $(this).attr('id').split('-').pop()).stop().slideUp('slow');
    });
    
    /* ################ */

    $('.mp3').jmp3({
        showfilename: "false",
        backcolor: "CDC0B1",
        forecolor: "0095BE",
        volume:80
    });
    
    $('.banner-click:not([ref="externo"])').click(function(){
        clickLink($(this).attr('href'));
        return false;
    })

    $('#blog-page a').click(function(){
        link_atual = $(this).attr('href');
        clickBlog(link_atual,$(this));
        return false;
    });

    $('.link-comentarios,.blog-leia').click(function(){
        link_atual = $(this).attr('href');
        clickBlog(link_atual,$(this));
        return false;
    });

    $('.excluir-pic-fa').click(function(){
        id = $(this).attr('href');

        obj = $(this).parent();
        
        $.ajax({
            data:{'id':id},
            type:'post',
            url:'/fa-club/fotos-excluir',
            success:function(resp){
                $('#carregando').hide();
                obj.remove();
                count = $('#my-pics li').size(this);
                if(count == 0){
                    $('#my-pics').prev().remove();
                    $('#my-pics').remove();
                }
                alert(resp);
            },
            ajaxStart:function(){
                $('#carregando').show();
            }
        })
        
        return false;
    });

    /////////////////////////////////////////Formularios/////////////////////////
    //
    // bind form using 'ajaxForm'

    $('#form_fas_fotos').ajaxForm({
        //target:        '#output1',   // target element(s) to be updated with server response
        beforeSubmit:  function (formData, jqForm, options) {
            $('#carregando').show();
        },  // pre-submit callback
        success:function(responseText, statusText, xhr, form)  {
            $('#carregando').hide();
            $('#content_middle').html(responseText);
            getInit();
        },// post-submit callback
        // other available options:
        url:'fa-club/perfil', // override for form's 'action' attribute
        type:'post',        // 'get' or 'post', override for form's 'method' attribute
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        resetForm: true        // reset the form after successful submit
    });

    $('#conteudo_da_home .zend_form').ajaxForm({
        //target:        '#output1',   // target element(s) to be updated with server response
        beforeSubmit:  function (formData, jqForm, options) {
            $('#carregando').show();
        },  // pre-submit callback
        success:function(responseText, statusText, xhr, form)  {
            $('#carregando').hide();
            $('#content_middle').html(responseText);
            getInit();
        },// post-submit callback
        // other available options:
        url:'login',         // override for form's 'action' attribute
        type:'post',        // 'get' or 'post', override for form's 'method' attribute
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        resetForm: true        // reset the form after successful submit
    });

    $('#form_contato').ajaxForm({
        //target:        '#output1',   // target element(s) to be updated with server response
        beforeSubmit:  function (formData, jqForm, options) {
            $('#carregando').show();
        },  // pre-submit callback
        success:function(responseText, statusText, xhr, form)  {
            $('#carregando').hide();
            $('#content_middle').html(responseText);
            getInit();
        },// post-submit callback
        // other available options:
        url:'contato',         // override for form's 'action' attribute
        type:'post',        // 'get' or 'post', override for form's 'method' attribute
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        resetForm: true        // reset the form after successful submit
    });
    
    
    $('#form_gbook').ajaxForm({
        //target:        '#output1',   // target element(s) to be updated with server response
        beforeSubmit:  function (formData, jqForm, options) {
            $('#carregando').show();
        },  // pre-submit callback
        success:function(responseText, statusText, xhr, form)  {
            $('#carregando').hide();
            $('#content_middle').html(responseText);
        },// post-submit callback
        // other available options:
        url:'mural',         // override for form's 'action' attribute
        type:'post',        // 'get' or 'post', override for form's 'method' attribute
        dataType:  'html',        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        resetForm: true        // reset the form after successful submit
    });

    $('#form_comentario').ajaxForm({
        //target:        '#output1',   // target element(s) to be updated with server response
        beforeSubmit:  function (formData, jqForm, options) {
            $('#carregando').show();
        },  // pre-submit callback
        success:function(responseText, statusText, xhr, form)  {
            $('#carregando').hide();
            $('#content_middle').html(responseText);
            getInit();
        },// post-submit callback
        // other available options:
        data: {id: $('#post').val()},
        url:'blog',         // override for form's 'action' attribute
        type:'post',        // 'get' or 'post', override for form's 'method' attribute
        dataType:  'html',        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        resetForm: true        // reset the form after successful submit
    });
    
    $('#form_fa').ajaxForm({
        //target:        '#output1',   // target element(s) to be updated with server response
        beforeSubmit:  function (formData, jqForm, options) {
            $('#carregando').show();
        },  // pre-submit callback
        success:function(responseText, statusText, xhr, form)  {
            $('#carregando').hide();
            $('#content_middle').html(responseText);
            getInit();
        },// post-submit callback
        // other available options:
        url:'fa-club',         // override for form's 'action' attribute
        type:'post',        // 'get' or 'post', override for form's 'method' attribute
        dataType:  'html',        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        resetForm: true        // reset the form after successful submit
    });

    //////////////////////////////////////////////////////////////////////////////
}


function getTwitter(obj,quant,tipo){
    $.ajax({
        url:'/index/twitter-atualiza',
        data:{'quantidade':quant,'tipo':tipo},
        success:function(data){
            $(obj).html(data);
            $("a[rel=externo]").attr('target','_blank');
        }
    });
}


/*links config*/
function clickLink(link_atual){
    $.ajax({
        url:link_atual,
        type:'GET',
        beforeSend:function(){
            $('#carregando').show();
        },
        success:function(data){
            $('#content_middle').html(data);
            getInit();

            $('#mycarousel').jcarousel();
            
            $('.paginationControl a,#list-galeria-fotos a,#agenda-home a').click(function(){
                clickLink($(this).attr('href'));
                return false;
            })

            $('#box-videos a').click(function(){
                id = $(this).attr('href');
                $.ajax({
                    url:'index/video-atual',
                    data:{'id':id},
                    beforeSend:function(){
                        $('#carregando').show();
                    },
                    success:function(data){
                        $('#tv-video').html(data);
                    },
                    complete:function(){
                        $('#carregando').hide();
                }
                })
                return false;
            });

            $('#gal_foto a, #carousel-fotos a').lightBox({
                imageLoading:	'/site/modulos/jquery-lightbox/images/lightbox-ico-loading.gif',
                imageBtnPrev:	'/site/modulos/jquery-lightbox/images/lightbox-btn-prev.gif',
                imageBtnNext:	'/site/modulos/jquery-lightbox/images/lightbox-btn-next.gif',
                imageBtnClose:	'/site/modulos/jquery-lightbox/images/lightbox-btn-close.gif',
                imageBlank:	'/site/modulos/jquery-lightbox/images/lightbox-blank.gif'
            });

        },
        complete:function(){
            $('#carregando').hide();
           /* Links ajax */
        }
    })
}

/*Link do blog*/
function clickBlog(link_atual,obj){

    //vejo se o link é do menu
    if(obj){
        tipo = obj.parent().attr('class');
    }else{
        tipo = false;
    }

    $.ajax({
        url:link_atual,
        type:'GET',
        beforeSend:function(){
           $('#carregando').show();
        },
        success:function(data){
                $('#content_middle').html(data);
                getInit();
        },
        complete:function(){
            $('#carregando').hide();
           /* Links ajax */
        }
    })
}
