(function($){
    $(document).ready(function(){
        $('#news_capa a.tab').click(function(event){
            event.preventDefault();
            var content_show = $(this).attr('title');
            if(content_show == 'texto_noticia')
            {
                $('#texto_evento').fadeOut(400, function() {
                    $('#texto_noticia').fadeIn(400);
                });
                }
                else
                {
                    $('#texto_noticia').fadeOut(400, function() {
                        $('#texto_evento').fadeIn(400);
                    });
                }
        });
    });

    $(document).ready(function(){
        $("a[rel='thumb']").colorbox();
        $("#acompanhamento").colorbox({width:"80%", height:"80%", iframe:true});
    });

    $(document).ready(function() {
        $('form.required-form').simpleValidate({
            errorElement: 'em',
            ajaxRequest: true,
            completeCallback: function($el) {
                var formData = $el.serialize();
                envia(formData);
            }
        });
    });

    $(document).ready(function() {
        $('#ccurriculo').uploadify({
            'uploader'  : 'uploadify/uploadify.swf',
            'script'    : 'uploadify/uploadify.php',
            'cancelImg' : 'uploadify/cancel.png',
            'folder'    : 'includes/trabalhe_conosco/arquivos',
            'auto'      : true,
            'scriptData' : {'curriculo':'curriculo'},
            'removeCompleted': false,            
            'fileExt'        : '*.pdf;*.doc;*.docx',
            'fileDesc'       : 'Documentos (.PDF, .DOC, .DOCX)',
            'onComplete'	 : function(event, queueID, fileObj, response, data) {
                var filename = fileObj.name;
                $('#fale_conosco').append('<input type="hidden" name="arquivo" value="' + filename + '"/></p>')
            },
            'onAllComplete'  : function(event, queueID, fileObj, reposnse, data){
				$("#enviar").show();
			}
        });
    });

    $(document).ready(function(){
        $("#resposta").hide();
        $('#enviar').hide();
        $("#fale_conosco").simpleValidate({
            errorElement: "em",
            ajaxRequest: false,
            completeCallback: function($el) {
			    $('#fale_conosco').fadeOut('slow').hide();
                $('#resposta').fadeIn('slow').show();
			    //Do AJAX request with formData variable
		    }
        });
    });

    $(document).ready(function(){
        $("#contato_resposta").hide();
        $("#contato").simpleValidate({
            errorElement: "em",
            ajaxRequest: false,
            completeCallback: function($el) {
			    $('#contato').fadeOut('slow').hide();
                $('#contato_resposta').fadeIn('slow').show();
		    }
        });
    });

    $(document).ready(function() {
        $("form#busca a").click(function(event) {
            event.preventDefault();
            var strToSearch = $("#textobusca").val();
            if(strToSearch.length > 0) {
                $(window.location).attr('href', 'index.php?secao=search&str=' + strToSearch);
            }
        });
        $('#textobusca').keyup(function(e) {
            e.preventDefault();
            var code = (e.keyCode ? e.keyCode : e.which);
            if(code == 13) { //Enter keycode
                var strToSearch = $("#textobusca").val();
                if(strToSearch.length > 0) {
                    $(window.location).attr('href', 'index.php?secao=search&str=' + strToSearch);
                }
            }
        });
    });


    /*
     *
     * Carregamos do conteúdo com ajax na seção "Atuação"
     *
     */
    $(document).ready(function() {
        var hash = window.location.hash.substr(1);
        var href = $('#navcontent li a').each(function(){
            var href = $(this).attr('href');
            if(hash==href.substr(0,href.length-4)){
                var toLoad = 'includes/atuacao/' + hash+'.php #texto';
                $('#texto').load(toLoad)
            }
        });
        $('#navcontent li a').click(function(event){
            var toLoad = 'includes/atuacao/' + $(this).attr('href')+' #texto';
            $('#texto').hide('fast',loadContent);
            $('#load').remove();
            $('#conteudo_interno').append('<span id="load">Carregando...</span>');
            $('#load').fadeIn('normal');
            window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
            function loadContent() {
                $('#texto').load(toLoad,'',showNewContent())
            }
            function showNewContent() {
                $('#texto').show('normal',hideLoader());
            }
            function hideLoader() {
                $('#load').fadeOut('normal');
            }
            event.preventDefault();
        });
    });


    /*
     *
     * Janela Modal para se inscrever nos newsletter
     *
     */
    $(document).ready(function() {
        // load the modal window
        $('a.modal').click(function(event){

            // scroll to top
            $('html, body').animate({scrollTop:0}, 'fast');

            // before showing the modal window, reset the form incase of previous use.
            $('.success, .error').hide();
            $('form#contactForm').show();


            //show the mask and contact divs
            $('#mask').show().fadeTo('', 0.7);
            $('div#contact').fadeIn();

            // stop the modal link from doing its default action
            event.preventDefault();
        });

        // close the modal window is close div or mask div are clicked.
        $('div#close, div#mask').click(function() {
            $('div#contact, div#mask').stop().fadeOut('slow');
        });
    });

    /*
     *
     * Envia os dados cadastrados no formulário do newsletter
     *
     */
    function envia(dados) {
        $.ajax({
            type: "post",
            url: "includes/pg_newsletter.php",
            data: dados,
            error: function() {
                $('.error').hide();
                $('#sendError').slideDown('slow');
            },
            success: function () {
                $('.error').hide();
                $('.success').slideDown('slow');
                $('form#contactForm').fadeOut('slow');
            }
        });
    }
})(jQuery);
