jQuery(function() {
	
	//jQuery('#nav ul ul').hide();
	
	jQuery('#nav ul li').hover(
	function () {
		jQuery(this).find('ul').show();
		jQuery(this).addClass('relative');
		jQuery(this).find('.level1').addClass('over');
	}, 
	function () {
		jQuery(this).find('ul').hide();
		jQuery(this).removeClass('relative');
		jQuery(this).find('.level1').removeClass('over');
	}
	);
	
	jQuery('.page-item-22 > a').click(function() {	
		return false; 
	});	
	
	jQuery('.menu > ul > li > a').addClass('level1');
	
	jQuery(".home .menu li:first").addClass('active');
	jQuery(".menu > ul").find('.current_page_item').find('a').addClass('active');
	//jQuery(".menu > ul").find('.current_page_ancestor').find('a').addClass('active');
	//jQuery(".menu > ul").find('.current_page_parent').find('a').addClass('active');
	jQuery(".menu li:last").addClass('no_margin');
	jQuery("#nav li li a:first").addClass('first');
	
	 jQuery("#nav li").each(function (i) {
        jQuery(this).find('ul li a:first').addClass('first');
      });

	
	jQuery('.target').attr('target','_blank');
	
	jQuery("#loading").bind("ajaxSend", function(){
		jQuery(this).show();
		jQuery('#contact_form fieldset').addClass('layer');
		}).bind("ajaxComplete", function(){
		jQuery(this).hide();
		jQuery('#contact_form fieldset').removeClass('layer');
	});
	
	var action_val = jQuery('#contact_form').attr('action'); 

	jQuery(".submit").click(function() {
		
	 var name = jQuery("#name").val();
		if (name == "") {
      jQuery("#name_error").show();
      jQuery("#name").focus();
      return false;
    }
	
	 var name = jQuery("#name").val();
		if (name == "*Name") {
      jQuery("#name_error").show();
      jQuery("#name").focus();
      return false;
    }
	
	var email = jQuery("#email").val();
		if (email == "") {
      jQuery("#email_error").show();
      jQuery("#email").focus();
      return false;
    }
	
	var email = jQuery("#email").val();
		if (email == "*Email") {
      jQuery("#email_error").show();
      jQuery("#email").focus();
      return false;
    }
	
	var email = jQuery("#email").val();
	if( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) ){
	jQuery("#email_error").show();
	jQuery("#email").focus();
	return (false);
	}
	
	var phone = jQuery("#phone").val();
		if (phone == "") {
      jQuery("#phone_error").show();
      jQuery("#phone").focus();
      return false;
    }
	
	var phone = jQuery("#phone").val();
		if (phone == "Phone") {
      jQuery("#phone_error").show();
      jQuery("#phone").focus();
      return false;
    }
	
	var message = jQuery("#message").val();
		if (message == "") {
      jQuery("#message_error").show();
      jQuery("#message").focus();
      return false;
    }
	
	var message = jQuery("#message").val();
		if (message == "Message") {
      jQuery("#message_error").show();
      jQuery("#message").focus();
      return false;
    }
		
		var dataString = 'name='+ name + '&phone=' + phone + '&email=' + email + '&message=' + message;
		//alert (dataString);return false;
		jQuery.ajax({
		type: "POST",
		url: action_val,
		data: dataString,
		success: function() {
			jQuery('#contact_form').html("<div id='message_text'></div>");
			jQuery('#message_text').html("<p>Contact form submitted!</p>")
			.append("<p>We will be in touch soon.</p>");
      }
     });
    return false;
	});
	
	//Set Default State of each portfolio piece
	jQuery(".paging").show();
	jQuery(".paging a:first").addClass("active");
		
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = jQuery(".window").width();
	var imageSum = jQuery(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	jQuery(".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

		jQuery(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		jQuery(".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 = jQuery('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = jQuery('.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
	jQuery(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	jQuery(".paging a").click(function() {	
		$active = jQuery(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
	});	
  
});