var base_url = $('base').attr('href');

jQuery(document).ready(function(){

    
	
    $(".bt_ler_mais").each(function(){

        $(this).click(function(){

            $(this).parents(".resumo_verbo").slideUp('fast');

            $(this).parents(".resumo_verbo").next(".texto_full").slideDown('slow');

            $(this).parents(".resumo_verbo").prev(".bt_fechar_janelas").fadeIn('slow');

            $(this).closest(".box_questao_2").removeClass("closed");

            $(this).closest(".jcarousel-item").children('.comentarios_questao').children('.last_coment').slideUp('fast');

            $(this).closest(".jcarousel-item").children('.comentarios_questao').children('.todos_coments').slideDown('fast');

            $(this).closest(".jcarousel-item").children('.comentarios_questao').children('.link_comentarios').slideUp('fast');

            return false;

        });

    });
	
	$('.lista_assuntos label').click(function(){
		$('.lista_assuntos label').removeClass('assunto_ativo');
		$(this).addClass('assunto_ativo');
	});
	
	$(".bt_enviar_coment").each(function(){
		 $(this).click(function(){
			if(($(this).prev().prev().val() == 'Digite aqui o seu comentário:') || ($(this).prev().prev().val() == '') ){					
				$(this).prev().prev().addClass('error_post');
				return false;
			}
		 });
	});
		 

    $(".bt_fechar_janelas").each(function(){

        $(this).click(function(){

            $(this).next(".resumo_verbo").slideDown('fast');

            $(this).next().next().slideUp('fast');

            $(this).closest(".box_questao_2").addClass("closed");

            $(this).fadeOut('fast');

            return false;

        });

    });



    $(".bt_comentar_verbo").each(function(){

        $(this).click(function(){

            $(this).closest(".box_questao_2").next().children().fadeIn();
			return false;

        });

    });



    $(".bt_fechar_novo_coment").each(function(){

        $(this).click(function(){

            $(this).parent().fadeOut();

            return false;

        });

    });





    $(".link_comentarios").each(function(){

        $(this).click(function(){

            $(this).next().next().slideUp('fast');

            $(this).next().next().next().slideDown('fast');

            $(this).fadeOut();

            return false;

        });

    });



    $(".bt_resultado").each(function(){

        $(this).click(function(){
            $(this).closest(".item_questao").slideUp('fast');
			$(this).closest(".item_questao").next().slideDown('fast');
			return false;
        });

    });


    abre_login();
    bt_close();
    open_sub_mn();
    selectFake();
    hide_file()
    $(function(){
        $("input:radio").uniform();
        $("input:checkbox").uniform({
            checkboxClass:'radio'
        });
	
    });


    $('.item_footer').hide();
    open_footer();
    //	$('#lista_post').jcarousel();
    $('#lista_post_2').jcarousel();
    $('#lista_box_blog').jcarousel_vertical();
    $('#lista_verbo_home').jcarousel_vertical();

    $('.lista_verbo').jcarousel_vertical();


    jQuery('.hide-input-text').bind({
        click: function(){
            if($(this).val() == $(this).attr('lang'))
                $(this).val('');
        },
        blur: function(){
            if($(this).val() == '')
                $(this).val($(this).attr('lang'));
        }
    });

    jQuery('input[type="text"]').setMask();

    jQuery('.footer .bic_login').click(function() {
        $('#menu .mn_login').click();
        $('html, body').stop();
        $('html, body').animate({
            scrollTop: $('#menu .login').offset().top
        }, "slow");
    });

    jQuery('.hide-input-password').each(function() {
        var $this = $(this);
        $this.parent().css('position', 'relative');

        var span = jQuery('<span class="span_hide_password">'+$this.attr('lang')+'</span>').insertBefore($this);
        span.click(function() {
            $this.focus();
        });

        $this.bind({
            focus: function(){
                span.hide();
            },
            blur: function(){
                if($(this).val() == '')
                    span.show();
            }
        });
    });

    $.Juitter.start({
        searchType:"searchWord", // needed, you can use "searchWord", "fromUser", "toUser"
        searchObject:"%40homembemfeito+OR+%23homembemfeito", // needed, you can insert a username here or a word to be searched for, if you wish multiple search, separate the words by comma.

        // The values below will overwrite the ones on the Juitter default configuration.
        // They are optional here.
        // I'm changing here as a example only
        //		lang:"en", // restricts the search by the given language
        live:"live-99", // the number after "live-" indicates the time in seconds to wait before request the Twitter API for updates.
        placeHolder:"lista_post", // Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>
        loadMSG: "Loading messages...", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on
        imgName: "loader.gif", // Loading image, to enable it, go to the loadMSG var above and change it to "image/gif"
        total: 20, // number of tweets to be show - max 100
        readMore: "Read it on Twitter", // read more message to be show after the tweet content
        nameUser:"image", // insert "image" to show avatar of "text" to show the name of the user that sent the tweet
        openExternalLinks:"newWindow", // here you can choose how to open link to external websites, "newWindow" or "sameWindow"
        filter:"sex->*BAD word*,porn->*BAD word*,fuck->*BAD word*,shit->*BAD word*"  // insert the words you want to hide from the tweets followed by what you want to show instead example: "sex->censured" or "porn->BLOCKED WORD" you can define as many as you want, if you don't want to replace the word, simply remove it, just add the words you want separated like this "porn,sex,fuck"... Be aware that the tweets will still be showed, only the bad words will be removed
    });

    var area_connects = $('#area_connects');
    if (area_connects.length > 0) {
        RedesConnect.onLogin(function(rede) {
            if (rede != 'site') {
                $('#status_redes').html('Conectado com ' + rede).show();
                $('.bt_connector').hide();
            }
        });

        area_connects.find('.bt_connect').click(function(e) {
            e.preventDefault();
            FB.login(function(response) {
                if (response.session) {
                    RedesConnect.setLoginStatus('facebook', true);
                }
            });
        });

        area_connects.find('.bt_twitter').click(function(e) {
            e.preventDefault();
            window.open(base_url + 'login/twitter','','width=806, height=370');
        });
    }

    jQuery('.hands_box .bt_aprovar, .hands_box .bt_reprovar').click(function(e) {
        e.preventDefault();
        var a = this;

        if ($(this).metadata().idpost == null) {
            return;
        }

        if ($(a).parents('.hands_box').find('a.desabilitado').length > 0)
            return;

        var voto = ($(this).hasClass('bt_aprovar')) ? 'aprovou' : 'desaprovou';
        $(a).addClass('desabilitado');

        $.ajax({
            type: "POST",
            url: base_url + 'hands/votar',
            dataType: "json",
            data: {
                idpost: $(this).metadata().idpost,
                voto: voto
            },
            success: function(resp) {
                $(a).parents('.hands_box').find('h3.cont_ap').html(resp.porcentagem_aprovou + "%");
                $(a).parents('.hands_box').find('h3.cont_rep').html(resp.porcentagem_desaprovou + "%");
            }
        });
    });
	
    jQuery('.mini_hands .bt_aprovar, .mini_hands .bt_reprovar').click(function(e) {
        e.preventDefault();
        var a = this;

        if ($(this).metadata().idpost == null) {
            return;
        }

        if ($(a).parents('.mini_hands').find('a.desabilitado').length > 0)
            return;

        var voto = ($(this).hasClass('bt_aprovar')) ? 'aprovou' : 'desaprovou';
        $(a).addClass('desabilitado');

        $.ajax({
            type: "POST",
            url: base_url + 'hands/votar',
            dataType: "json",
            data: {
                idpost: $(this).metadata().idpost,
                voto: voto
            },
            success: function(resp) {
            //$(a).parents('.hands_box').find('h3.cont_ap').html(resp.porcentagem_aprovou + "%");
            //$(a).parents('.hands_box').find('h3.cont_rep').html(resp.porcentagem_desaprovou + "%");
            }
        });
    });

    jQuery('*.votar').click(function(e) {
        e.preventDefault();
        var a = this;

        if ($(this).metadata().idpost == null) {
            return;
        }

        if ($(a).parents('.hands_up_down').find('a.desabilitado').length > 0)
            return;

        var voto = $(this).attr('href');
        $(a).addClass('desabilitado');

        $.ajax({
            type: "POST",
            url: base_url + 'solte_o_verbo/votar',
            dataType: "json",
            data: {
                idpost: $(this).metadata().idpost,
                voto: voto
            },
            success: function(resp) {
                //$(a).parents('.hands_box').find('h3.cont_ap').html(resp.porcentagem_aprovou + "%");
                //$(a).parents('.hands_box').find('h3.cont_rep').html(resp.porcentagem_desaprovou + "%");
            }
        });
    });

})

//abre box de login
function abre_login(){
    jQuery('.mn_login').click(function(){
        jQuery('.login').slideUp();
        jQuery(this).next('.login').toggle('slow');
        return false;
    });
	jQuery('.login_bt').click(function(){
        jQuery('*.login').slideUp();
        jQuery('.login_baixo').toggle('slow');
        return false;
    });

    $('.link_lembrar').click(function(){
        $('.frm_login').fadeOut('slow');
        $('.title_login').fadeOut('slow');
        $('.title_login').hide();
        $('.frm_login').hide();
        $('.frm_lembrar_senha').fadeIn('slow');
        $('.title_lembrar').fadeIn('slow');
        return false;
    });

    $('.voltar_login').click(function() {
        $('.frm_lembrar_senha').fadeOut('slow');
        $('.frm_lembrar_senha').hide();
        $('.frm_login').fadeIn('slow');
        $('.title_login').fadeIn('slow');
        $('.title_lembrar').hide();
        return false;
    });

	$('.link_lembrar2').click(function(){
		$('.frm_login2').fadeOut('slow');
        $('.title_login2').fadeOut('slow');
        $('.title_login2').hide();
        $('.frm_login2').hide();
        $('.frm_lembrar_senha2').fadeIn('slow');
        $('.title_lembrar2').fadeIn('slow');
        return false;
    });

    $('.voltar_login2').click(function() {
        $('.frm_lembrar_senha2').fadeOut('slow');
        $('.frm_lembrar_senha2').hide();
		$('.frm_login2').fadeIn('slow');
        $('.title_login2').fadeIn('slow');
        $('.title_lembrar2').hide();
        return false;
    });
}

function bt_close(){
    jQuery('.close').click(function(){
        jQuery('.login').hide();
        return false;
    })
}

function open_sub_mn(){
    $("#menu a, li a").removeAttr('title');
    $("#menu ul:not(.no_sub)").css({
        display: 'none'
    });
    // Opera Fix

    $("#menu li").each(function(){
        var $sublist = $(this).find('ul:not(.no_sub)');//var $sublist = $(this).find(‘ul:first’); original


        $(this).hover(function(){
		
            $sublist.css({
                overflow:"hidden",
                height:"auto",
                display:"none"
            }).show(100, function(){
                if($(this).parent().hasClass('mn_hotsite')){
                    $(this).parent().addClass('mn_hotsite_ativo');
                }
                if($(this).parent().hasClass('mn_redes')){
                    $(this).parent().addClass('mn_redes_ativo');
                }
                if($(this).parent().hasClass('mn_confort')){
                    $(this).parent().addClass('mn_confort_ativo');
                }
				
				
                $(this).css({
                    overflow:"visible",
                    height:"auto",
                    display:"block",
                    margin:"53px 0 0 -4px",
                    padding:"20px 0 0 20px",
                    opacity:"1"
                });
				
            });
			
			
        },
        function(){
            $sublist.stop().hide(1, function(){
				
                $(this).css({
                    display:"none",
                    overflow:"hidden"
                });
                $(this).parent().removeClass('mn_hotsite_ativo');
                $(this).parent().removeClass('mn_confort_ativo');
                $(this).parent().removeClass('mn_redes_ativo');
				
            });
        });
    });
}

//abre/fecha Footer
function open_footer(){
    $('.footer').mouseenter(function(){
        $('*.menu_bottom ul').animate({
            "margin-top":"0px"
        },300);
        $('.area_anime').animate({
            "height":"158px"
        },300);
        scrollDown('.footer');
        $('.item_footer').show();

    });
    $('.footer').mouseout(function(){
        $('.footer').stop();
        $('html,body').stop();
    });
    $('.content').hover(function(){
        $('.area_anime').animate({
            "height":"103px"
        },300);
        $('*.menu_bottom ul').animate({
            "margin-top":"17px"
        },300);
        $('.item_footer').hide();

    },function(){
        $('.footer').stop();
        $('html,body').stop();
        $('.area_anime').stop();
        $('*.menu_bottom ul').stop();

    });
    return false;
}

function scrollDown(s){
    $('html, body').animate({
        scrollTop: $(s).offset().top
    }, "slow");
    return false;
}

//select Fake
function selectFake(start){

    if(!start){
        start = "select";
    }

    jQuery(start).each(function(){

        var el  = jQuery(this).find('option:first-child').html();
        var color = jQuery('.'+jQuery(this).attr('class')).css('color');
        var w = jQuery(this).width();
        jQuery(this).addClass('select-hide');
        jQuery(this).before('<div class="borda-left"></div><div class="select_fake centro_select" style="color:'+color+'">'+el+'</div><div class="clear"></div>');

        jQuery(this).change(function(){
            var str = jQuery(this).children('option:selected').text();
            jQuery(this).parent('li').find('.select_fake').html(str);
        });

        /*up*/
        var str = jQuery(this).children('option:selected').text();
        jQuery(this).parent('li').find('.select_fake').html(str);
    });
}

function hide_file(){

    $('.fake').change(function(){
        $('.content-campo-file').text($(this).val());
    });
}

var RedesConnect = {
    loginStatus : {
        twitter:false
    },
    names : {
        twitter: 'Twitter',
        facebook: 'Facebook',
        site: 'site'
    },

    events : {
        onlogin: null
    },
    onLogin : function(fnc) {
        this.events.onlogin = fnc;
    },

    setLoginStatus : function(rede, status) {
        this.loginStatus[rede] = status;

        if (status == true)
            if (this.events.onlogin != null)
                this.events.onlogin(rede);
    },

    getLoginStatus : function(fnc) {
        // site
        if (RedesConnect.loginStatus.site) {
            fnc(true, 'site');
            return;
        }

        // twitter
        if (RedesConnect.loginStatus.twitter) {
            fnc(true, 'twitter');
            return;
        }

        // facebook
        FB.getLoginStatus(function(response) {
            if (response.session) {
                fnc(true, 'facebook');
                return;
            } else {
                fnc(false);
            }
        });
    }
};

function clearForm(form) {
    clear(form);

    jQuery(form).find('input, select, textarea').each(function() {
        var $this = jQuery(this);
        var type = $this.attr('type');

        if (type == 'radio') {
            $this.removeAttr('checked');
        }
        else if (type == 'password') {
            var span = $this.prev();
            if (span.hasClass('span_hide_password')) {
                span.html($this.attr('lang')).show();
            }
            $this.val('');
        }
        else {
            $this.val($this.attr('lang'));
        }
    });
}

function get_cidades_from_estado(estado_input, cidade_input)
{
    jQuery(cidade_input).html('<option value="">Carregando...</option>').change();
    var id_estado = $(estado_input).val();
	
    if (id_estado.length > 0) {
        $(cidade_input).load("util/get_cidades_from_estado_option/" + id_estado, '', function() {
            $(cidade_input).change();

        });
    }
    else {
        $(cidade_input).html('<option value="">Selecione</option>').change();
    }
    
    
}

function abrir(strMensagem)
{
    jQuery(document).ready(function(){
        jQuery.facebox(strMensagem);
    })
}

function fbs_click(u, t){
    if (t == null)
        t=u;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function muda_action_pular_sempre() {
    var url = ($('#pular_pagina:checked').length == 0) ? 'foursquare/acessar' : 'foursquare/acessar/pular_sempre';
    $('#frm_acessar').attr('action', url);
}

function scrollTo(seletor) {
    $('html, body').animate({
        scrollTop: $(seletor).offset().top
    }, "slow");
}


function buscar_home_foursquare() {
    if($("#busca").val() == $('#busca').attr('lang')){
        $('#busca').addClass('error');
        return false;
    }

    $('#frm_busca').submit();
}



