$(document).ready(function() {
// Validation
	var phoneRegex = /^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
	$('.required').blur(function(e) {
    var error = $(this).next();
		if( this.value == this.defaultValue ) {
			$(this).next().fadeTo(200,0.1,function() { $(this).html('This field is required.').addClass('alert').fadeTo(900,1); });;
		} else {
			$(this).next().fadeTo(200,0.1,function() { $(this).html('*').addClass('alert').fadeTo(900,1); });
		}
	});
	$('.phone').blur(function(e) {
		if (phoneRegex.test($(this).val())) {
			var formatted = $(this).val().replace(phoneRegex, "($1) $2-$3");		
			$(this).val(formatted);
			$(this).next().fadeTo(200,0.1,function() { $(this).html('*').addClass('alert').fadeTo(900,1); });
		} else {
			$(this).val('Phone');
			$(this).next().fadeTo(200,0.1,function() { $(this).html('Please enter a valid number - <em>(123) 456-7890</em>').addClass('alert').fadeTo(900,1); });
		}
	});

// MEGA Dropdown
function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).addClass('show');
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).removeClass('show') 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#navigation2 li .sub").css({'opacity':'0'});
	$("ul#navigation2 li").hoverIntent(config);
	


$('div.accordion> div').hide();  
  $('div.accordion> h2').click(function() {
    var $thisHeading = $(this);
    var $nextDiv = $(this).next();
    var $visibleSiblings = $nextDiv.siblings('div:visible');
    if ($visibleSiblings.length ) {
      $visibleSiblings.slideUp(1000, function() {
        $nextDiv.slideToggle(1000).removeClass("selected");
	    $.scrollTo($thisHeading,1000, {offset:-10} );
      });
    } else {
      $nextDiv.slideToggle(1000).addClass("selected");
	  $.scrollTo($thisHeading,1000, {offset:-10} );
    }
  });

// AJAX For Message Form
$("#ajax-message").submit(function() {
  var dataString = $(this).serialize();
	$('#message-form').html('<img src="http://www.equineconnection.ca/images/ajax-loader.gif" />').hide().show('slow');
	
  $.ajax({
    type: "POST",
    url: "http://www.equineconnection.ca/message.php",
    data: dataString,
    success: function() {
      $('#message-form').html('<p>Thank you for your message, we\'ll be in touch.</p>').hide().show('slow');
    }
  });
  return false;
});

// AJAX For Volunteer Form
$("#ajax-volunteer").submit(function() {
  var dataString = $(this).serialize();
	$('#volunteer-form').html('<img src="http://www.equineconnection.ca/images/ajax-loader.gif" />').hide().show('slow');
	
  $.ajax({
    type: "POST",
    url: "http://www.equineconnection.ca/volunteer.php",
    data: dataString,
    success: function() {
      $('#volunteer-form').html('<p>Thank you for your interest, we\'ll be in touch.</p>').hide().show('slow');
    }
  });
  return false;
});

// AJAX For Newsletter
$("#newsletter").submit(function() {
  var dataString = $(this).serialize();
	$('#news-form').html('<img src="http://www.equineconnection.ca/images/ajax-loader.gif" />').hide().show('slow');
	
  $.ajax({
    type: "POST",
    url: "http://www.equineconnection.ca/newsletter.php",
    data: dataString,
    success: function() {
      $('#news-form').html('<p>Thank you for subscribing.</p>').hide().show('slow');
    }
  });
  return false;
});

// Small nudge for links with the .nudge class
	$('a.nudge').hover(function() { //mouse in
			$(this).animate({ paddingLeft: '10px' }, 400);
		}, function() { //mouse out
			$(this).animate({ paddingLeft: 0 }, 400);
		});

// Clears out input boxes that have values onclick
	$(".filled").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
		}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
		});
	
// Sends rel="external" to a new tab/window
	$('a[rel="external"]').click( function() {
			window.open( $(this).attr('href') );
			return false;
			});  
	
// Hover effects for image rollovers
  if ($.browser.msie && $.browser.version < 7) return;  
  $('#navigation li, #navigation2 li')
    .removeClass('highlight')
    .find('a')
    .append('<span class="hover" />').each(function () {
      var $span = $('> span.hover', this).css('opacity', 0);
      $(this).hover(function () {
        $span.stop().fadeTo(500, 1);
      }, function () {
        $span.stop().fadeTo(1000, 0);
      });
    });

});
