/*
 *  Funções Gerais
 *
 *----------------------------------------------------*/
menu = {
    elem: '#menu li',
    load: function(){
        $(this.elem).each(function(){
            var el = $(this).find('ul:eq(0)');
            //el.parent("li").addClass("subm");
            $(this).hover(function() {
                //$(this).addClass("hover").children("a:eq(0)").addClass("hover");
                $(this).addClass("menu_hover").children("a:eq(0)").addClass("hover");
//                el.parent(this).addClass("hoverseta");
                src = $(this).children('img').attr('src');
                ext         = src.substring(src.lastIndexOf(".")).toLowerCase();
                newImg      = src.replace(ext,'');
                src = $(this).children('img').attr('src',newImg + '_hover' + ext);

                el.fadeIn(0);
            },
            function(){
                $(this).removeClass("hover").children("a:eq(0)").removeClass("hover");
                el.parent(this).removeClass("menu_hover");
                src = $(this).children('img').attr('src');
                src.replace('_hover', '');
                src = $(this).children('img').attr('src',src.replace('_hover', ''));
                el.fadeOut('fast');
            });
        });
    }
}


jQuery(document).ready(function() {
    if ($("#menu").length > 0 ) {
         menu.load();
    }

    jquery_hover.init();
    $('.exp_login').toggle(
        function() {$(".box_login").animate({'height': '143'} , 500 );$('.exp_login img').attr('src','./assets/site/img/btn_loginud_click.png');return false;},
        function() {$(".box_login").animate({'height': '3'} , 500 );$('.exp_login img').attr('src','./assets/site/img/btn_loginud.png');return false;}
    );
    if ($('#hdestaque').length > 0 ) {
        $('.destaque_control li:eq(0)').addClass('active');
        function onAfter(curr, next, opts) {
            var index = opts.currSlide;
            $('.destaque_control li').removeClass('active');
            $('.destaque_control li:eq('+index+')').addClass('active');
        }
        $('#hdestaque ul').cycle({fx: 'fade', speed:  2000, timeout: 8000, after: onAfter});
        $('.destaque_control li').mouseover(function() {$('#hdestaque ul').cycle($(this).index());$('#hdestaque ul').cycle('pause');return false;});
        $('.destaque_control li').mouseout(function() {$('#hdestaque ul').cycle('resume');return false;});
        $('.box_intro').animate({'margin-left' : '-169px'} );
        $('#hdestaque ul').find('.event').mouseenter(function() {
            $(this).children('.box_intro').animate({'margin-left' : '0'} );$('#hdestaque ul').cycle('pause');
        });
        $('#hdestaque ul').find('.event').mouseleave(function() {
            $(this).children('.box_intro').animate({'margin-left' : '-169px'} );$('#hdestaque ul').cycle('resume');
        });

    }
    if ($(".input_pesquisa").length > 0 )
    {
        $(".input_pesquisa").keypress(function (e) {if (e.which == 13) {$(".btn_pesquisa").trigger('click');}});

        $(".btn_pesquisa").click(function() {
            if ( ($(".termo").val() != "Busca") && ( $(".input_pesquisa").val() != "") ) {
                $(location).attr('href', $("base").attr("href")+'pesquisa/'+$.URLEncode($(".input_pesquisa").val()));
            }
            else return false;
        });
    }
//    if ($(".btn_news").length > 0) {
//        $(".btn_news").colorbox({iframe:true, innerWidth: "520px", innerHeight: "200px"});
//    };
    
    if ($(".box_twitter").length > 0) {
        while($(".box_twitter ul").height() > 234) $(".box_twitter ul").children('li').last().remove();
    }

    /* noticias */

    generateImgs = function() {

        if($('.ins_img_left').length >=1)
        {
            $('.ins_img_left').each(function() {
                    $(this).wrap('<span class="insimg"></span>');
                    if ($(this).attr('src')  == $(this).parents('div.txt').find('img:eq(0)').attr('src')) {nclass=' marginTop4';} else {nclass='';}
                    pai = $(this).parent().wrap('<span class="insimg_left gnot'+nclass+'"></span>');
                    leg = $(pai).find('img').attr('alt');
                    $(pai).append('<strong class="insimg_leg">'+leg+'</strong>');
            });
        }
        if($('.ins_img_center').length >=1)
        {
            $('.ins_img_center').each(function() {
                    $(this).wrap('<span class="insimg"></span>');
                    if ($(this).attr('src')  == $(this).parents('div.txt').find('img:eq(0)').attr('src')) {nclass=' marginTop4';} else {nclass='';}
                    pai = $(this).parent().wrap('<span class="insimg_center gnot'+nclass+'"></span>');
                    leg = $(pai).find('img').attr('alt');
                    $(pai).append('<strong class="insimg_leg">'+leg+'</strong>');
            });
        }
        if($('.ins_img_right').length >=1)
        {
            $('.ins_img_right').each(function() {
                    $(this).wrap('<span class="insimg"></span>');
                    if ($(this).attr('src')  == $(this).parents('div.txt').find('img:eq(0)').attr('src')) {nclass=' marginTop4';} else {nclass='';}
                    pai = $(this).parent().wrap('<span class="insimg_right gnot'+nclass+'"></span>');
                    leg = $(pai).find('img').attr('alt');
                    $(pai).append('<strong class="insimg_leg">'+leg+'</strong>');
            });
        }
    }
    if ($(".paginacao_nav").length >=1 ) {
        $('.pag_controlprevnext').live('mousedown mouseup mouseover mouseout', function(event) {$(this).find('.hclick').attr('src',jquery_hover.Change($(this).find('img').attr('src'),event.type));} ) ;
    }

    applyGalImg = function() {
        $('.insimg').each(function() {
            var img = $(this).children("img");
            var title= $(img).attr('alt').replace('"','');
            var href= $("base").attr('href')+$(img).attr('src').replace('_259.','.').replace('_660.JPG','.').replace('./','');
            $(img).wrap('<a rel="gnf" href="'+href+'" title="'+title+'" ></a>');
        });
        $("a[rel='gnf']").colorbox({'maxHeight' : 700});


        $('.ginc').each(function() {
            var href= $("base").attr('href')+$(this).attr('src').replace('_110.','.').replace('./','');
            var title= $(this).attr('title').replace('"','');
            $(this).wrap('<a rel="nrg" href="'+href+'" title="'+title+'" ></a>');
            $("a[rel='nrg']").colorbox({'maxHeight' : 700});
        });
    }
    
    $('.gisc').colorbox({'maxHeight' : 700});
    if ( ($(".insimg").length >= 1) || ($('.ginc').length >= 1) || ($('.ins_img_left').length >= 1) || ($('.ins_img_right').length >= 1) || ($('.ins_img_center').length >= 1) ) {
        generateImgs();
        applyGalImg();
    }
//
    $(".submit").click(function() {$(this).parents("form").submit();});

    if ($("#lista_perguntas").length >0 )
    {
            $(".tt_pergunta").click(function () { $(this).siblings('div').slideToggle(); });
    }

    if ($("#form_comentar").length > 0) {
        $("#form_comentar").validate({
            rules: {
                nome: {required: true},
                email: {required: true, email:true},
                comentario: {required: true}
            },
            messages: {
                nome: {required: "Preecha com o seu nome."},
                email: {required: "Preencha com o seu e-mail.", email:"Formato inválido."},
                comentario: {required: "Preencha com o comentário."}
            }
        });
    }
//
//    if ($("#form_comentar").length > 0) {
//        $("#form_comentar").validate({
//            rules: {
//                nome: {required: true},
//                email: {required: true, email:true},
//                comentario: {required: true}
//            },
//            messages: {
//                nome: {required: "Preecha com o seu nome."},
//                email: {required: "Preencha com o seu e-mail.", email:"Formato inválido."},
//                comentario: {required: "Preencha com o comentário."}
//            }
//        });
//    }
//    if ($("#form_contato").length > 0) {
//        $("#form_contato").validate({
//            rules: {
//                nome: {required: true},
//                email: {required: true, email:true},
//                contato: {required: true}
//            },
//            messages: {
//                nome: {required: "Preecha com o seu nome."},
//                email: {required: "Preencha com o seu e-mail.", email:"Formato inválido."},
//                contato: {required: "Preencha com a Mensagem."}
//            }
//        });
//    }

    if ($("#form_seja_socio").length > 0) {
        $("#form_seja_socio").validate({
            rules: {
                nome: {required: true},
                telefone: {required: true}
            },
            messages: {
                nome: {required: "*Preecha com o seu nome."},
                telefone: {required: "*Preencha com o telefone<br/> para contato"}
            }
        });
    }

    
    if ($("#galeria").length > 0 ) {
        $("#galeria").carousel({
                dispItems:1,
                nextBtn: "<div> </div>",
                prevBtn: "<div> </div>"
        });
    }
    /* Galerias */
    tornar_individual = function(el) {
        if( el.index() ==  ($("#lista_fotos li").length)) {
            return false;
        }
        if( el.index() == -1) {
            return false;
        }
        if( el.index() ==  ($("#lista_fotos li").length -1)) {
//            $('.nextPic').children('a').hide();
            $('.nextPic').fadeTo(0,0.5);
        }
        else {$('.nextPic').fadeTo(0,1);}
        if( el.index() ==  0) {
            $('.prevPic').fadeTo(0,0.5);
//            $('.prevPic').children('a').toggle();
        }
        else {$('.prevPic').fadeTo(0,1);}

        $(".active").removeClass("active");
        $(el).addClass('active');
        el = $(el).children('a');
        img = $(el).attr('href');
        legenda = $(el).children('img').attr('alt');
        id = $(el).attr('id');
        $('#gfhideid').val(id);
        var newimg = false;
        $(function () {
            var newimg = new Image();
            $(newimg).load(function () {
                $('.box_imagem').html(this);
                $("#foto_individual").find('p').html(legenda);
//                if($(this).height() > 400) {
//                    $(this).height(400);
//                }
                $("#gfcomentarios").children().remove();
                $("#gfcomentarios").load($('base').attr('href')+'galerias/comentarios/'+id);
                return false;
            }).error(function () { }).attr('src', $(el).attr('href')).attr('alt',$(el).children('img').attr('alt'));
        });
//        atualizarTwitter();
    }
    
});

