// JavaScript Document

$(function() {
	
	/*
	$(".feed_item .feed_preview_date").each(function() {
		var thisDay = parseInt($(this).children(".day").text(), 10);
		var thisMonth = parseInt($(this).children(".month").text(), 10) - 1;
		var thisYear = parseInt($(this).children(".year").text(), 10) + 2000;
		$(this).text($.datepicker.formatDate('MM dd, yy', new Date(thisYear,thisMonth,thisDay)));
	});
	
	$(".upcoming-event .upcoming-event-date").each(function() {
		var thisDay = parseInt($(this).children(".ued-day").text(), 10);
		var thisMonth = parseInt($(this).children(".ued-month").text(), 10) - 1;
		var thisYear = parseInt($(this).children(".ued-year").text(), 10);
		$(this).text($.datepicker.formatDate('M dd, yy', new Date(thisYear,thisMonth,thisDay)));
    });
	*/
	
	//AJAX to populate carousel image text elements
	$.ajax({
		type: "GET",
		url: "/media/slideshow-text.xml",
		dataType: "xml",
		success: parseXml
	});
	
	function  parseXml(xml) {
		$(xml).find("image-text").each(function(intIndex)	{
			$("#img" + (intIndex+1) + "txt").append($(this).find("content").text());
		});
	}

	//jQuery Carousel
    if ($("#carousel").length>0) {
		jQuery("#carousel").jcarousel({
			scroll: 1,
			auto: 5,
			wrap: "last",
			initCallback: carousel_initCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			itemFirstInCallback: carousel_itemFirstInCallback
		});	
	}
});

function carousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
	
	// Slidehow stop and resume buttons
	jQuery('#carousel-stop').bind('click', function() {
        carousel.stopAuto();
		return false;
    });
	
	jQuery('#carousel-play').bind('click', function() {
		carousel.startAuto();
		return false;
	});
	
	// Slideshow next and previous buttons
    jQuery('#carousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#carousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
	
	// Pause slideshow if mouse is hovered over image, resume when mouse is moved off of image
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

// jCarousel active image class for jCarousel-scroll element
function carousel_itemFirstInCallback(carousel, item, idx, state) {
	$('.activeImg').removeClass('activeImg');
    $('.jcarousel-control a:eq('+(idx-1)+')').addClass('activeImg');
};

function isZIP(strValue) {
	// US ONLY - Validates US ZIPS - 99999 or 99999-9999
	return /^\d{5}([\-]\d{4})?$/.test(strValue); 
}

function isPhone(strValue) {
	return /^\d{3}-\d{3}-\d{4}$/.test(strValue);
}
			
function isEmail(strValue) {
	return /^[-!#\$%\*\+\/\?\|\^&{}`~\w]+(\.[-!#\$%\*\+\/\?\|\^&{}`~\w]+)*@[-\w]+(\.[-\w]+)+$/.test(strValue);
} 

function validateContact(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";

	//if(objForm.salutation.value.replace(/\s+/g, "") == "")
	//{ strReqMsg += "    - Salutation\n"; }

	if(objForm.name.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Name\n"; }
	
	if(objForm.email.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Email\n"; }
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\n"; }
	
	if(objForm.phone.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Phone\n"; }
	
	if(objForm.comments.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Comments\n"; }
	
	// Assemble all of the error messates together to display to the user
	if(strReqMsg.length || strValidationMsg.length)
	{
		var strDisplay = "";
		if(strReqMsg.length)
		{ strDisplay += "The following fields are required to be completed:\n\n" + strReqMsg; }

		if(strValidationMsg.length)
		{ strDisplay += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}

}
