/**
 * @author José Luis Granados Reyes
 */
//Function to make the main menu
function sprite_menu()
{
	$('.menu').kwicks({
	    duration: 300,
	    max: 259,
	    spacing:  0
	});
}
//Function to hide the pointed margin on a clicked link
function no_blur()
{
	$('a').click(function(){
		$(this).blur();
		//return false;
	});
}
//Function to create the slide show
function slideSwitch() 
{
    var $active_img = $('#slideshow DIV.active');
	var $active_text = $('#information_box DIV.active');
	
    if ($active_img.length == 0) {
		//alert($active_img.length);
		$active_img = $('#slideshow DIV:last');
		$active_text = $('#information_box DIV:last');
	}
    // use this to pull the divs in the order they appear in the markup
	
	if($active_img.next().length == true){
		
		var $next_img = $active_img.next();
		var $next_text = $active_text.next();
	}
	else{
		var $next_img = $('#slideshow DIV:first');
		var $next_text = $('#information_box DIV:first');
	}
	
    $active_img.addClass('last_img-active');
	$active_text.addClass('last_text-active');
	
    $next_img.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() 
		{
            $active_img.removeClass('active last-active');
        });
		
	$next_text.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() 
		{
            $active_text.removeClass('active last-active');
        });	
}

$(function() 
{
    setInterval( "slideSwitch()", 6500 );
});

$(function show_login()
{
	var browser = navigator.appName;
	
	if(browser =="Microsoft Internet Explorer")
	{
		$("a.fancybox").fancybox(
		{ 
		'hideOnContentClick': false,
		'frameWidth': 360,
		'frameHeight':276,
		'padding': 0,
		'overlayShow': true,
		'overlayOpacity': 0.6
		}); 
	}
	else
	{
		$("a.fancybox").fancybox(
		{ 
		'hideOnContentClick': false,
		'frameWidth': 330,
		'frameHeight':220,
		'padding': 0,
		'overlayShow': true,
		'overlayOpacity': 0.6
		}); 
	}
	
	
});

$(document).ready(function() 
{
	
	sprite_menu();//Call to the main menu function
	no_blur();//Call to the no blur in links function
	
	//This is for the main body rounded corners, first I must check if the element exists, because if not, it sends an error message
	if ( $('#white_body').length > 0 ) 
	{
		var white_body = RUZEE.ShadedBorder.create({ corner:6, shadow:16, edges:"tlr" });
		white_body.render('white_body');
	}
	
	if ( $('#information_box_bottom').length > 0 ) 
	{
		var information_box_bottom = RUZEE.ShadedBorder.create({ corner:6, edges:"blr" });
		information_box_bottom.render ('information_box_bottom');
	} 
	
	if ( $('#footer').length > 0 ) 
	{
		var footer = RUZEE.ShadedBorder.create({ corner:6, shadow:16, edges:"blr" });
		footer.render('footer'); 
	} 
	
	//This is for the alphanumeric validation
	if($(".no-special-chars").length > 0)
	{
	    $(".no-special-chars").alphanumeric({allow:".@¿?!¡, "});
	}

	//This is for the slide up effect in the search box tabs.
	$("a.tab").click(function ()
	{
		// slide all elements with the class 'content' up
		$(".content").slideUp();
		
		// Now figure out what the 'title' attribute value is and find the element with that id.  Then slide that down.
		var content_show = $(this).attr("title");
		$("#"+content_show).slideDown();
		
		//This is to avoid the relocation of the window when the link is clicked.
		return false;

	});
	
	$("#enviar").click(function ()
	{
		var nombre = $("#nombre").val();
		var email = $("#email").val();
		var asunto = $("#asunto").val();
		var mensaje = $("#mensaje").val();
		var flag = 0;//condicion inicial
		var regex = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		
		$(".validar").each(function()
		{
		    if($(this).val() == "")
		    {
                $(this).css({'background-color':'red'}); 
                flag = 1;//hay campos sueltos
		    }
		});
		
		if(flag != 1)
		{
		    if(regex.test(email))
	        {
                var cliente = encodeURI(nombre);
	            var from = encodeURI(email);
	            var subject = encodeURI(asunto);
	            var body = encodeURI(mensaje);
	            
	            $.ajax({
	                type: "GET",
	                url: "acciones.php",
	                data:"accion=enviar_correo&cliente="+cliente+"&from="+from+"&subject="+subject+"&body="+body,
	                success: function(responseMsg)
	                {
	                    if(responseMsg == "sent")
	                    {
	                        alert("Su correo ah sido enviado exitosamente");
                            window.location.href="index.php";
	                    }
	                    if(responseMsg == "fail")
	                    {alert("Su correo no pudo ser enviado, reintente nuevamente");}
	                },
		            error: function(msg)
		            {
			            alert("Se produjo el siguiente error: "+msg);
		            }
	            });    
	        }
	        else
	        {
	            flag = 2;
	            $('#email').css({'background-color':'red'}); 
	        }
		}
		
		if(flag == 1)
		{alert("Error: Debe completar todos los campos para poder enviar un correo");}
		if(flag == 2)
		{alert("Error: Ah introducido una dirección de email no válida");}
		
	});


});
