	$(document).ready(function() {
	
		//Set Default State of each portfolio piece
		$(".paging").show();
		$(".paging a:first").addClass("active");
			
		//Get size of images, how many there are, then determin the size of the image reel.
		var imageWidth = $(".window").width();
		var imageSum = $(".image_reel img").size();
		var imageReelWidth = imageWidth * imageSum;
		
		//Adjust the image reel to its new size
		$(".image_reel").css({'width' : imageReelWidth});
		
		//Paging + Slider Function
		rotate = function(){	
			var triggerID = $active.attr("rel") - 1; //Get number of times to slide
			var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
			$(".paging a").removeClass('active'); //Remove all active class
			$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
			
			//Slider Animation
			$(".image_reel").animate({ 
				left: -image_reelPosition
			}, 500 );
			
		}; 
		
		//Rotation + Timing Event
		rotateSwitch = function(){		
			play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
				$active = $('.paging a.active').next();
				if ( $active.length === 0) { //If paging reaches the end...
					$active = $('.paging a:first'); //go back to first
				}
				rotate(); //Trigger the paging and slider function
			}, 7000); //Timer speed in milliseconds (3 seconds)
		};
		
		rotateSwitch(); //Run function on launch
		
		//On Hover
		$(".image_reel a").hover(function() {
			clearInterval(play); //Stop the rotation
		}, function() {
			rotateSwitch(); //Resume rotation
		});	
		
		//On Click
		$(".paging a").click(function() {	
			$active = $(this); //Activate the clicked paging
			//Reset Timer
			clearInterval(play); //Stop the rotation
			rotate(); //Trigger rotation immediately
			rotateSwitch(); // Resume rotation
			return false; //Prevent browser jump to link anchor
		});	
		
	});

// Script Thumbs FX

	// If the HTML document is ready to be manipulated
	$(document).ready(function(){
		// Add the hover handler to the link
		$(".fx a").hover(
			function(){ // When mouse pointer is above the link
				// Make the image inside link to be transparent
				$(this).find("img").animate(
					{opacity:"0.5"},
					{duration:300}
				);
			},
			function(){ // When mouse pointer move out of the link
				// Return image to its previous state
				$(this).find("img").animate(
					{opacity:"1"},
					{duration:300}
				);
			}
		);
	});
	
// Funciones para validar formulario de contacto

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must to be a valid e-mail.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('Please, correct the following errors:\n'+errors);
  document.MM_returnValue = (errors == '');
}
