jQuery(document).ready(function(){
    jQuery('#search_form').submit(function(e){
        e.preventDefault();
        jQuery.post(
            'wp-content/plugins/motoboys-search/motoboys-search.php',
            {
                _wpnonce: jQuery('#_wpnonce').val(),
                _wp_http_referer: jQuery("input[name='_wp_http_referer']").val(),
                search_category_type: jQuery("#search_category_type").val(),
				search_user_name: jQuery("#search_user_name").val(),				
                search_user_email: jQuery("#search_user_email").val(),
                search_user_celphone: jQuery("#search_user_celphone").val(),
                search_user_telphone: jQuery("#search_user_telphone").val(),
				search_user_age: jQuery("#search_user_age").val(),
				search_user_term_id: jQuery("#search_cidades").val(),
				search_user_info_extra: jQuery("#search_user_info_extra").val(),
                search_captcha_code: jQuery("#search_captcha_code").val()
            },
            function(e){
                if(e.length != 0){
                    jQuery('#search_user').before(e);
                }
                else{
                    jQuery('#search_form').html('<p>Obrigado por cadastra seus dados. Click <a href="'+window.location.href+'" id="search_reload_form">aqui</a> para voltar para p&aacute;gina de cadastro.</p>');
                    jQuery('#search_msg').show();
                }
            }
        );
    });
    jQuery('#search_form').ajaxError(function(e, xhr, settings, exception){
        alert(xhr);
    });

    jQuery('a#search_reload_form').click(function(event){
        event.preventDefault();
        alert(';stiuf');
		if(this.className == 'form_motoboys')
        jQuery('#search_form').text('[search_show_form_motoboys /]')
		else jQuery('#search_form').text('[search_show_form_empresas /]')
    });

    jQuery('#search a').click(function(event){
        if(this.className == 'publish' || this.className == 'delete'){
            event.preventDefault();
            jQuery.post(
                '../wp-content/plugins/motoboys-search/motoboys-search.php',
                {
                    action: this.className,
                    id: this.id
                },
                function(e){
                    jQuery('#search_msg').html(e);
                    jQuery('#search_msg').show();
                    var row = jQuery(event.target).parents('tr');
                    row.each(function(){
                        jQuery(this).fadeOut('slow', function(e){
                            jQuery(this).remove();
                        });
                    });
                    jQuery(row).fadeOut('slow', function(e){
                        jQuery(row).remove();
                    });
                    setTimeout(function(e){
                        jQuery('#search_msg').fadeOut('slow');
                    }, 5000);
                }
            );
        }
    });
});
