jQuery(function($){

    $('#telefone').mask('(99) 9999 - 9999');

    $('#newsletter-form').submit(function(e){
            e.preventDefault();
            $('#msg_news').html('');
            if ($('#newsletter').val() == '' || $('#newsletter').val() == $('#newsletter').attr('placeholder')) {
                $('#msg_news').html('<span style="color:red">! E-mail :: Obrigatório</span>');
            } else if (!valid_email($('#newsletter').val())) {
                $('#msg_news').html('<span style="color:red">! E-mail :: Inválido</span>');
            } else {
                    $.ajax({
                            url: get_site() + 'envia_newsletter',
                            type: 'POST',
                            data: $('#newsletter-form').serialize(),
                            success: function(data) {
                                    if (data == 'true') {
                                            $('#msg_news').html('<span style="color:green">Email cadastrado com sucesso!</span>');
                                            $('#newsletter-form')[0].reset();
                                    } else {
                                            this.error();
                                    }
                            },
                            error: function(){
                                    $('#msg_news').html('Ocorreu um erro, tente novamente.');
                            }
                    });
            }
    });

    $('#form_contato').submit(function(e){
		e.preventDefault(); //Prevevir o envio do formulario
		$('#msg_contato').html('');
		if ($('#nome').val() == '' || $('#nome').val() == $('#nome').attr('placeholder')) {
			$('#msg_contato').html('! Campo Nome :: Obrigatório');
		} else if ($('#mail').val() == '' || $('#mail').val() == $('#mail').attr('placeholder')) {
			$('#msg_contato').html('! Campo E-mail :: Obrigatório');
		} else if (!valid_email($('#mail').val())) {
			$('#msg_contato').html('! Campo E-mail :: Inválido');
                } else if ($('#telefone').val() == '' || $('#telefone').val() == $('#telefone').attr('placeholder')) {
			$('#msg_contato').html('! Campo Telefone :: Obrigatório');
		} else if ($('#cidade').val() == '' || $('#cidade').val() == $('#cidade').attr('placeholder')) {
			$('#msg_contato').html('! Campo Cidade :: Obrigatório');
		} else if ($('#mensagem').val() == '' || $('#mensagem').val() == $('#mensagem').attr('placeholder')) {
			$('#msg_contato').html('! Campo Mensagem :: Obrigatório');
		} else {
			$.ajax({
				url: get_site() + 'envia_contato',
				type: 'POST',
				data: $('#form_contato').serialize(),
				success: function(data) {
					if (data == 'true') {
						$('#msg_contato').html('<strong style="color:#693562">Mensagem enviada com sucesso!</strong>');
                                                $('#form_contato')[0].reset();
                                                $('#form_contato input,#form_contato textarea').blur();
					} else {
						this.error();
					}
				},
				error: function(){
					$('#msg_contato').html('<strong style="color: #db653b;">Erro: A mensagem não pode ser enviada.</strong>');
				}
			});
		}
    });
});

var email_regexp = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

function valid_email(email) {
    return email_regexp.test(email);
}

jQuery(function($){
    
    jQuery.placeholderize();

});

(function($){

    $.placeholderize = function(){

        // Verifica se o browser suporta o atributo placeholder
        if ( true ) {//!$('<input placeholder="1" />')[0].placeholder ) {

            // Fallback para o atributo placeholder
            $('[placeholder]').each(function(){

                var input = $(this);

                input

                    .bind('focus, focus.mask', function(){
                        if (input.val() == input.attr('placeholder')) {
                            input.val('');
                        }
                    })

                    .bind('blur, blur.mask', function(){
                        if (input.val() == '') {
                            input.addClass('placeholder').val(input.attr('placeholder'));
                        }
                    })

                    .bind('focus, focus.mask', function(){
                        input.removeClass('placeholder');
                    })

                    // Invoca o evento, para aplicar a função de fallback
                    .blur();
            });

        }
    };
})(jQuery);

jQuery(function($){ //Quando a pagina estiver pronta
    $('.lightbox').lightBox();
    $('.gallery').lightBox();

    $("#sidebar-news .pagination a").live("click",function(e){ //Seleciona , fica escutando, muda
        e.preventDefault();
        $.ajax({
            url:this.href,
            success:function(resp){
                $("#sidebar-news").html(
                    $("#sidebar-news",resp).html()
                );
            }
        });
    });

    $("#oi a").live("click",function(e){
        e.preventDefault();
        $.ajax({
            url:this.href,
            success:function(resp){
                $("#new_p").html(
                    $("#new_p",resp).html()
                );
                $('.gallery').lightBox();
            }
        });
    });

    $("#menu-tratamentos a").live("click",function(e){
        e.preventDefault();
        $.ajax({
            url:this.href,
            success:function(resp){
                $("#texto-tratamentos").html(
                    $("#texto-tratamentos",resp).html()
                );
                $('.gallery').lightBox();
                galeria();
                $('.lightbox').lightBox();
            }
            
        });
    });
    galeria();
    function galeria(){
        $(".ver").click(function(){
            $('.lightbox:first').trigger('click');
        });
    }
    
});
