$(document).ready(function(){
    
    /* On load, show the default message (python) */
    $('#python').addClass('img_borders');
    $('div.python').fadeIn();

    $('img.outsourcing').hover(
	function(){
	    /* First hide all the detailed popup-like divs */
	    $('div.detail_popup').hide()
	    $('img.outsourcing').removeClass('img_borders');
	    var current_id = $(this).attr('id');
	    $(this).addClass('img_borders');
	    $('div.'+current_id).fadeIn();
	},
	function(){
	    var current_id = $(this).attr('id');
	    /* $('div.'+current_id).slideUp(); */
	    /* $(this).removeClass('img_borders'); */
	}
    );

    $('#vform').submit(function(event) { 	
	/*
	  Manage messages sent from the contact form
	 */

	// stop form from submitting normally
	event.preventDefault(); 

	$('div.form_container').slideUp();
	$('div.error').hide();
	$('div.correcto').hide();
	$('div.aviso').slideDown();

	var form = $(this);
        var email = form.find( 'input[name="email"]' ).val();
        var nombre = form.find( 'input[name="nombre"]' ).val();
        var telefono = form.find( 'input[name="telefono"]' ).val();
        var mensaje = form.find( 'textarea[name="mensaje"]' ).val();

	$.ajax({
	    type: "POST",
	    url: "/api/notificacion/",
	    data: {email: email, nombre: nombre, telefono: telefono, mensaje: mensaje},
	    success: function(msg){
		$('div.aviso').slideUp();
		$('div.correcto').slideDown();
		$('#vform').each (function(){
		    this.reset();
		});
		$('div.form_container').slideDown();
	    },
	    error: function(err){
		/* alert(err.status); */
		/* alert(err.responseText); */
		$('div.aviso').slideUp();
		$('div.error').slideDown();
		$('div.form_container').slideDown();
	    }
	});

	// !!! Important !!! 
	// always return false to prevent standard browser submit and page navigation 
	return false; 

    }); 

    /* If there is a div with the id blog_widget, load its contents from the 
       blog app */
    
    if ( $('#blog_widget').length ){
	$.ajax({
	    url: '/blog/widget/',
	    success: function(data) {
		$('#blog_widget').html(data);
	    },
	    error: function(err){
		$('#blog_widget').html('<h4 class="grande">No se ha podido conectar con el blog</h4>');
	    }
	});
    }
    
});
