Shadowbox.init();

$.fn.tipsy.defaults = {
    delayIn: 0,
    delayOut: 0,
    fade: true,
    gravity: "s",
    html: true,
    offset: 0,
    opacity: 0.8,
    trigger: 'focus',
    title: 'title',
    fallback: ''
};



$.validator.setDefaults({
    debug: false,
    ignoreTitle: true,
    errorClass: "invalid",
    validClass: "accept",
    unhighlight: function(element, errorClass, validClass) {
        $(element).attr("original-title",'');
        $(element).attr("title",'');
        $('.tipsy').hide();
        $(element).removeClass(errorClass).addClass(validClass);
        $(element).parent().parent().prev().removeClass(errorClass);
    },
    errorPlacement: function(error, element) {
        $(element).attr("title", error.html());
        $(element).tipsy({
            trigger: 'focus'
        });
        $('.tipsy').show();
    }
});

hoverOut = function(element){element.animate({opacity: 0.70}, 500)};

hoverIn = function(element){element.animate({opacity: 1}, 500)};

showFormProfissional = function(codigo) {
    $.ajax({
        type: "POST",
        url: "/servicos/get-form-profissional/",
        data: 'id='+codigo,
        success: function(retorno){  
            Shadowbox.open({
                content:        retorno,
                player:         "html",
                title:          "",
                height:         380,   
                width:          620,
                options: {
                    enableKeys:     false,
                    handleOversize: "none",                
                    modal:          false,
                    onFinish: bindContatoForm()
                }
            });
            bindContatoForm();
        },
        beforeSend: function(){
            $('#loading').fadeIn('slow');
        },
        complete: function(){
            $('#loading').fadeOut('slow');
        }
    });
}

bindContatoForm = function() {
    $('form[name="envia-contato"]').validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "/contato/enviar",
                data: $(form).serialize(),
                success: function(retorno) {        
                    $(form).children().children('input').val('');
                    $(form).children().children('textarea').val('');
                    $('#dialog').html(retorno);
                    $('#dialog').dialog({
                        title: 'Aviso',
                        modal: true,
                        draggable: false,
                        resizable: false,
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                                $('#dialog').html('');
                            }
                        }
                    });                     
                },
                beforeSend: function(){
                    $('#loading').fadeIn('slow');
                },
                complete: function(){
                    $('#loading').fadeOut('slow');
                }
            });
        },
        rules: {
            nome: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
            telefone: {
                required: true,
                minlength: 8
            },
            mensagem: {
                required: true,
                minlength: 10
            }
        }, 
        messages: {
            nome: {
                required: "Informe seu nome!",
                minlength: "Este campo deve ter no mínimo 5 caracteres!" 
            },
            email: "Email inválido",
            telefone: { 
                required: "Informe seu telefone!",
                minlength: "Este campo deve ter no mínimo 8 caracteres!" 
            },
            mensagem: {
                required: "Digite sua mensagem!",
                minlength: "Este campo deve ter no mínimo 10 caracteres!" 
            }
        }
    });    
}

$(document).ready(
    function() {
        if($('#cycle').length > 0) {
            $('#cycle').cycle({
                pager:  '#cycle-nav'
            });
        }
        
        if($('.interna-wrapper .banner').length > 0) {
            $('.interna-wrapper .banner').cycle();
        }
        
        if($('#servico-home-select').length > 0) {
            $('#servico-home-select').change(
                function() {
                    if($(this).val() != '-1') {
                        $('form[name="ir-servico"]').submit();
                    }
                }
            );
        }
        
        if($('#servico-especialidade-select').length > 0) {
            $('#servico-especialidade-select').change(
                function() {
                    $('#resultados-profissionais').html('');
                    
                    if($(this).val() != '-1') {
                        $.ajax({
                            type: "POST",
                            url: "/servicos/get-profissionais/",
                            data: $(this).serialize(),
                            success: function(retorno){  
                                $('#resultados-profissionais').html(retorno);
                            },
                            beforeSend: function(){
                                $('#loading').fadeIn('slow');
                            },
                            complete: function(){
                                $('#loading').fadeOut('slow');
                            }
                        });
                    }
                }
            );
        }
        
        if($('form[name="envia-contato"]').length > 0) {
           bindContatoForm();
        }
        if($('form[name="contato-import-export"]').length > 0) {
            $('form[name="contato-import-export"]').validate({
           submitHandler: function(form) {
               $.ajax({
                   type: "POST",
                   url: "/import-export/enviar",
                   data: $(form).serialize(),
                   success: function(retorno) {        
                       $(form).children().children('input').val('');
                       $(form).children().children('textarea').val('');
                       $('#dialog').html(retorno);
                       $('#dialog').dialog({
                           title: 'Aviso',
                           modal: true,
                           draggable: false,
                           resizable: false,
                           buttons: {
                               Ok: function() {
                                   $( this ).dialog( "close" );
                                   $('#dialog').html('');
                               }
                           }
                       });                     
                   },
                   beforeSend: function(){
                       $('#loading').fadeIn('slow');
                   },
                   complete: function(){
                       $('#loading').fadeOut('slow');
                   }
               });
           },
           rules: {
               nome: {
                   required: true,
                   minlength: 5
               },
               email: {
                   required: true,
                   email: true
               },
               telefone: {
                   required: true,
                   minlength: 8
               },
               pais: {
                   required: true
               },
               uf: {
                   required: true,
                   minlength: 2
               },
            cidade: {
                   required: true
               },
               nome: {
                   required: true,
                   minlength: 5
               },
               mensagem: {
                   required: true,
                   minlength: 10
               }
           }, 
           messages: {
               nome: {
                   required: "Informe seu nome!",
                   minlength: "Este campo deve ter no mínimo 5 caracteres!" 
               },
               email: "Email inválido",
               telefone: { 
                   required: "Informe seu telefone!",
                   minlength: "Este campo deve ter no mínimo 8 caracteres!" 
               },
                pais: {
                   required: "Informe seu país!"
               },
               uf: {
                   required: "Informe seu UF!",
                   minlength: "Este campo deve ter no mínimo 2 caracteres!" 
               },
                cidade: {
                   required: "Informe sua cidade!"
               },
               mensagem: {
                   required: "Digite sua mensagem!",
                   minlength: "Este campo deve ter no mínimo 10 caracteres!" 
               }
           }
       });
    }
    }
);




this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});
