/***** phone validation method *****/
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please enter a valid phone number");

/***** navigation delay *****/
var nav_ndx = 0;
function stopTrigger() {
	clearTimeout(nav_ndx);
}
function executeNavDrop(obj) {
	$('ul',obj).delay(100).slideDown(100);
	var img = $('img',obj);
		img.attr('src',img.attr('src').replace(/(\.\w+)$/,'_hover$1'));
}
function triggerNavDrop(obj) {
	nav_ndx = setTimeout(function(){executeNavDrop(obj);},500);
}

/***** homepage image rotation *****/
var rot_ntrvl = 0;
var rot_thumbs = null;
var rot_ndx = 0;
var rot_total = 0;

function stopInterval(reset) {
	clearInterval(rot_ntrvl);
	rot_ntrvl = 0;
	if(reset === true) {
		rot_total = 10;
	}
}
function swapHeaderImage() {
	if(rot_total > 1) { rot_total--; }
	else { stopInterval(false); }
	rot_ndx = (rot_ndx + 1) % rot_thumbs.length;
	rot_thumbs.each(function() { $(this).removeClass('active'); });
	$("div#headerimage img.primary").fadeTo(500,.30,function() {
														var thumb = $(rot_thumbs[rot_ndx]);
														var newsrc = thumb.addClass('active').find('img').attr('src').replace(/^(.*?)(\/sized\/images)(.*?)(-\d+x\d+)(\.[a-z]+)$/i,'$1$3$5');
														$(this).parent('a').attr('href',thumb.attr('href'));
														$(this).attr('src',newsrc).fadeTo(250,1);
													});
}
function startRotation() {
	stopInterval(true);
	rot_ntrvl = setInterval(swapHeaderImage,8000);
}
function delayRotation() {
	swapHeaderImage();
	rot_ntrvl = setTimeout(startRotation,20000);
}
function enableRotation() {
	rot_thumbs = $("div#headerimage ul.thumbnails li");
	if(rot_thumbs.length > 0) {
		rot_thumbs.each(function(index) {
				$(this).data('ndx',index).click(function(i) {
						stopInterval(true);
						if(rot_ndx != $(this).data('ndx')) {
							rot_ndx = $(this).data('ndx')-1;
						}
						delayRotation();
					});
			});
		startRotation();
	}
}



$.expr[':'].external = function(o) {
	return !o.href.match(/^mailto\:/) && (o.hostname != location.hostname);
};



/***** when the document is ready... *****/
$(document).ready(function() {
	
	//enable dropdown navigation
	/*
	$('ul#mainnavigation li').each(function(i,e) {
			$(this).hover(
					function() {
						triggerNavDrop(this);
					},
					function() {
						stopTrigger();
						$('ul',this).slideUp(100);
						var img = $('img',this);
							img.attr('src',img.attr('src').replace(/_hover(\.\w+)$/,'$1'));
					}
				);
		});
		*/
	// Primary Nav Menus
	$('ul#mainnavigation > li')
		.hoverIntent({
			over:function() { $('> ul',this).fadeIn(200); },
			out:function() { $('> ul',this).fadeOut(200); }
			});
	$('ul#mainnavigation li ul').each(function() {
			$('li:odd',this).addClass('alt');
		});
	
	
	
	// External Links in New Windows
	$('a:external').attr('target','_blank');
	$('a.notexternal').attr('target','');
	
	
	
	if ($('input[name=start_date],input[name=end_date]',$('form#volunteerform')).length) {
		$('input[name=start_date],input[name=end_date]',$('form#volunteerform')).datepicker({
			showOn:'both',
			buttonImage:'/css/images/calendar-month.png',
			buttonImageOnly:true
		});
	}
	
	
	
	//create rounded corner objects
	$('div[class^=rounded_]').each(function() {
			//$(this).css('background-color','red');
			var box = $(this);
			var color = box.attr('class').replace('rounded_','');
			box.wrap('<div class="cssbox '+color+'box" />').addClass('cssbox_body').addClass(color+'box').removeClass('rounded_'+color);
			var box_heading = $('div.heading',box);
			if(box_heading.length > 0) {
				$('<div class="cssbox_head '+color+'box" />').append(box_heading.detach().addClass(color+'box')).insertBefore(box);
			} else {
				$('<div class="cssbox_head '+color+'box" />').append('<div class="heading2 '+color+'box">&nbsp;</div>').insertBefore(box);
			}
		});
	
	//hunger facts
	$("div.hungerfact div.fact").hide();
	$("div.referred div.fact").show();
	$("div.hungerfact h3").click( // When the h3 is clicked...
			function() {
				$(this).siblings("div.fact").toggle().parent().addClass('active');
				$(this).parent().siblings().removeClass('active').find("div.fact").hide();
			}
		).hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
	
	//homepage image rotator
	enableRotation();
	
});
