$(document).ready(function() {    //Document Ready
 	 	
 	 
 	 	// Función del portafolio.
		$("a.portafolio").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false,
			'titlePosition' : 'over'
		});




//Servicios FadeIn and Fade Out...

		$(".web").fadeIn(); //we show this thing ^^
		$("#button1").css ('color','#393939');
    	$("#button1").click(function () 
    		{
    			$("#button1").css ('color','#393939');
   				$("#button2").css ('color','#909090');
				$("#button3").css ('color','#909090');
      			$(".web").fadeIn(500);
	  			$(".ilustracion").hide();
	  			$(".anuncios").hide();
	  
			});
		$("#button2").click(function () 
			{
				$("#button2").css ('color','#393939');
   				$("#button1").css ('color','#909090');
				$("#button3").css ('color','#909090');
      			$(".ilustracion").fadeIn(500);
	  			$(".web").hide();
	  			$(".anuncios").hide();
	  
			});
		$("#button3").click(function () 
			{
				$("#button3").css ('color','#393939');
   				$("#button1").css ('color','#909090');
				$("#button2").css ('color','#909090');
    	  		$(".anuncios").fadeIn(500);
		  		$(".ilustracion").hide();
		  		$(".web").hide();
	  
			});

// Funciones de jFlow 

if (screen.width<=480)
	{
		$("#men").jFlow({
		slides: "#slides",
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		auto: true,		//auto change slide, default true
		width: "100%", //610px
		height: "800px", //410
		duration: 500,
		prev: ".jFlowPrev", // must be class, use . sign  
        next: ".jFlowNext" // must be class, use . sig
		});		
		
		$("#galeria").css ({'width':'310px', 'height':'500px'});
		$("#galeria li").css ({'width':'310px', 'height':'500px'});
		$("#prevBtn").css ('margin-left', '30px');

		$("#galeria").easySlider({
			prevText:'Página Anterior',
			nextText:'| Página Siguiente',
			speed: 900,
			orientation:'horizontal'			
		});
	}
	else
	{
		$("#men").jFlow({
		slides: "#slides",
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		auto: true,		//auto change slide, default true
		width: "100%", //610px
		height: "410px", //410
		duration: 500,
		prev: ".jFlowPrev", // must be class, use . sign  
        next: ".jFlowNext" // must be class, use . sig
		});
		
		$("span #prevBtn").css ('margin-left', '150px');
		$("#galeria").easySlider({
			prevText:'Página Anterior',
			nextText:'| Página Siguiente',
			speed: 900,
			orientation:'horizontal'			
		});
	}
	
	

//Galería del portafolio
		
		
// Placeholder para IE y otros...

	if (!Modernizr.input.placeholder) 
		{
			$("#placer").focus(function() 
				{
					if ($("#placer").val() == $("#placer").attr('placeholder'))
						{
							$('#placer').val("");
						}
				});
			$("#placer").blur(function() 
				{
					if($("#placer").val()=="") 
						{
							$("#placer").val($("#placer").attr('placeholder'));;
				
						}
				});
			$("#placer").blur();
		}
		
		
});


// google script
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5773560-4']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


//  Validación del Formulario de contacto

//name - email - asunto - mensaje

function validar(form)
	{
		fail = validarNombre(form.name.value)
		fail += validarMail(form.email.value)
		fail += validarAsunto(form.asunto.value)
		fail += validarMensaje(form.mensaje.value)
	
		if(fail =="") 
			{ 
				name = form.name.value
				email = form.email.value
				asunto = form.asunto.value
				mensaje = form.mensaje.value
				enviarmail(name,email,asunto,mensaje) 
				return false 
			
			}
		else { addPopup("<p>" + fail + "</p>"); //utilizamos la función para sacar el pop up
			 return false;
 			}
	
	}

	function validarNombre(field)
		{
			if(field=="") return "Error: Olvidó colocar su nombre <br />"
			return ""
		}
	function validarMail(field)
		{
			if(field=="") return "Error: Olvidó colocar su Correo <br />"
			else if (!( (field.indexOf(".") > 0) ) && (!(field.indexOf("@") > 0) ) || /[^a-zA-Z0-9.@_-]/.test(field) )
				return "Error: El formato de su correo es inválido <br />";
			return ""
		}
	function validarAsunto(field)
		{
			if(field=="") return "Error: Olvidó colocar el asunto  <br />"
			return ""
		}
	function validarMensaje(field)
		{
			if(field=="") return "Error: Olvidó colocar el mensaje <br />"
			else if (field.length < 10) return "Necesitamos más datos en el mensaje para poder responder adecuadamente  <br />" 
			return ""
		}

//Ajax Call para validad el formulario

 //Geting the data from the ajaxRequest for responseText or responseXML <-- chanching any of those things
 
 
 

function enviarmail(name,email,asunto,mensaje)
{
	

nocache = "&nocache=" + Math.random() * 1000000
request = new ajaxRequest()
request.open("GET","mail.php?" + "name=" + name + "&email=" + email + "&asunto=" + asunto + "&mensaje=" + mensaje + nocache,true)

request.onreadystatechange = function()
 	{
 		if(this.readyState == 4)
 			{
 				if(this.status == 200)
 					{
 						if(this.responseText != null)
 							{
 								addPopup("<p> Su mensaje se ha enviado correctamente, muchas gracias =)<p>"); // Mensajtio
 							}
 						else alert("Ajax Error: No se recibieron los datos." )
 					}
 				else alert("Ajax Error: " + this.statusText)
 			}
 	}
 	
 	// after his we do
 	
 	request.send(null)  // if we are using get, if we use post, we put the string with the value name
 	
}
 function ajaxRequest()
 	{
 		try // for real browsers (non IE)
 			{
 				var request = new XMLHttpRequest()
 			}
 		catch(e1) // for IE6 + suckwers 
 			{
 				try
 					{
 						request = new ActiveXOBject("Msxml2.XMLHTTP")
 					}
 				catch(e2)
 					{
 						request = false
 					}
 			}
 		return request
 	}
 	

 	
// Pop-up para mensaje enviado


function addPopup(notice) {
			$('<div class="notice"></div>')
				.append('<div class="skin"></div>')
				.append('<a href="#" class="x"></a>')
				.append($('<div class="content"></div>').html($(notice)))
				.hide()
				.appendTo('#pop')
				.fadeIn(1000);
			$('#pop')  //busca el objeto que nos sirve de locker para el popup
				.find('.x')  // buscamos el botón de cerrar por su clase
				.live('click', function() {  // on click...
					$(this)
						.closest('.notice')
						.animate({
							border: 'none',
							height: 0,
							marginBottom: 0,
							martinTop: '-6px',
							opacity: 0,
							paddingBottom: 0,
							paddingTop: 0,
							queue: false
						}, 500, function(){
							$(this).remove();
						});
				})
		}
