// JavaScript Document

$(function() {	

	$(".subheader-tab").click(function() {
		$(".subheader-tab.selected").removeClass("selected");
		$(this).addClass("selected");
		if($(".subheader-tab").index(this) == 0)
		{
			$("#contact-content").show();
			$("#news-content").hide();
		}
		else
		{
			$("#news-content").show();
			$("#contact-content").hide();
		}
	});
	
	/*imageCount = 0;
	
	var t=setTimeout("rotateHome()",5000);*/
	
	$(".home-window-brands li").hover(function () {
		$(this).find(".home-window-brand-img").hide();
		$(this).find(".home-window-brand-txt").show();
	}, function() {
		$(this).find(".home-window-brand-txt").hide();
		$(this).find(".home-window-brand-img").show();
	});
	
	$("img.hover, input.hover").hover(function() {
		$(this).attr("src", $(this).attr("src").split(".").join("-hover."));
	}, function() {
		$(this).attr("src", $(this).attr("src").split("-hover.").join("."));
	});
	
	if($("#tabs").text().length > 1)
	{
		$("#tabs li:eq(0)").addClass("selected");
		$("#tabs-content > div:eq(0)").show();
	}
		
	$("#tabs li").click(function() {
		var clicked = $("#tabs li").index(this);
		$("#tabs li.selected").removeClass("selected");
		$(this).addClass("selected");
		$("#tabs-content > div:visible").hide();
		$("#tabs-content > div:eq("+clicked+")").show();
	});
	
	/*$(".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);
		$(this).text($.datepicker.formatDate('mm.dd.y', 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)	{
			alert("$(this).find('content').text()");
			$("#img" + (intIndex+1) + "txt").append($(this).find("content").text());
		});
	}*/
	
	if($("#subheader-interior-carousel").length)
	{
		
		$.get("/media/slideshow-interior.xml", function (xml) {
			$(xml).find("image-item").each(function(i) {
			
				output = '';
				if(($(this).find("image-link").text()) == "") {
					output += '<li><div><img src="'+$(this).find("image-path").text()+'" width="698" height="291" alt="'+$(this).find("image-title").text()+'"></div>';
				}
				else {
					output += '<li><div><a href="'+$(this).find("image-link").text()+'"><img src="'+$(this).find("image-path").text()+'" width="698" height="291" alt="'+$(this).find("image-title").text()+'"></a></div>';
				}
				output += '<p id="img1txt">'+$(this).find("content").text()+'</p></li>';
				$("#subheader-carousel").append(output);
			 
			});	
			
			var slideSpd = $(xml).find("slideshow-speed").text();
			
			//jQuery Carousel
			if ($("#subheader-carousel").length) {
			jQuery("#subheader-carousel").jcarousel({
				scroll: 1,
				auto: slideSpd,
				wrap: 'circular',
				initCallback: carousel_initCallback,
				buttonNextHTML: null,
				buttonPrevHTML: null
				//itemFirstInCallback:  carousel_itemFirstInCallback
			});	
		}
		});
	}
	else if($("#carousel").length)
	{
		$.get("/media/slideshow-home.xml", function (xml) {
			$(xml).find("image-item").each(function(i) {
			
				output = '';
				if(($(this).find("image-link").text()) == "") {
					output += '<li><div><img src="'+$(this).find("image-path").text()+'" width="698" height="291" alt="'+$(this).find("image-title").text()+'"></div>';
				}
				else {
					output += '<li><div><a href="'+$(this).find("image-link").text()+'"><img src="'+$(this).find("image-path").text()+'" width="698" height="291" alt="'+$(this).find("image-title").text()+'"></a></div>';
				}				
				output += '<p id="img1txt">'+$(this).find("content").text()+'</p></li>';
				$("#subheader-carousel").append(output);
			 
			});	
			
			var slideSpd = $(xml).find("slideshow-speed").text();
			
			//jQuery Carousel
			if ($("#carousel").length) {
			jQuery("#carousel").jcarousel({
				scroll: 1,
				auto: slideSpd,
				wrap: 'circular',
				initCallback: carousel_initCallback,
				buttonNextHTML: null,
				buttonPrevHTML: null
				//itemFirstInCallback:  carousel_itemFirstInCallback
			});	
		}
		});
	}
	
	if($("#modules-content").length)
	{
		$("#modules-content").css("height",$("#main").outerHeight() - 115);
	}
	
	var blockHts = new Array();
	blockHts[0] = $("#home-block2").outerHeight();
	blockHts[1] = $("#home-block3").outerHeight();
	blockHts[2] = $("#home-testimonials").outerHeight();
	var largestHt = 0;
	
	for(var i=0; i < 3; i++)
	{
		if(blockHts[i] > largestHt)
		{
			largestHt = blockHts[i];
		}
	}
	
	$("#home-block2").css("height",largestHt);
	$("#home-block3").css("height",largestHt);
	$("#home-testimonials").css("height",largestHt);
	
	
	
});

/*function rotateHome()
{
	imageCount++;
	imageIndex = imageCount % 5;
	$("#subheader-image img").attr("src","/media/home-main"+imageIndex+".jpg");
	
	var t=setTimeout("rotateHome()",5000);
}*/

function isPhone(strValue) { return /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?|\.?))[2-9]\d{2}[- \.]?\d{4}$/.test(strValue); } //^\d{3}-\d{3}-\d{4}$
function isEmail(strValue) { return /^[-!#\$%\*\+\/\?\|\^&{}`~\w]+(\.[-!#\$%\*\+\/\?\|\^&{}`~\w]+)*@[-\w]+(\.[-\w]+)+$/.test(strValue); } 
function isZIP(strValue) { return /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(strValue); } 

function validateContact(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";
	
	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.phone.value.length && !isPhone(objForm.phone.value))
	{ strValidationMsg += "    - Phone must be in the format xxx-xxx-xxxx\n"; }
	
	//if(objForm.interest.value == "-Choose One-")
	//{ strReqMsg += "    - Interest\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 += "\nThe following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}
}

function validateLongContact(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";
	
	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.city.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - City\n"; }
	
	if(objForm.state.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - State\n"; }
	
	if(objForm.zip.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - ZIP\n"; }
	
	if(objForm.zip.value.length && !isZIP(objForm.zip.value))
	{ strValidationMsg += "    - ZIP must be in the format xxxxx or xxxxx-xxxx\n"; }
	
	if(objForm.phone.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Phone\n"; }
	
	if(objForm.phone.value.length && !isPhone(objForm.phone.value))
	{ strValidationMsg += "    - Phone must be in the format xxx-xxx-xxxx\n"; }
	
	if(objForm.call.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Best Time to Call\n"; }
	
	if(objForm.interest.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Product name of interest:\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;
	}
}

function carousel_initCallback(carousel) {
	
	// Slidehow stop and resume buttons
	jQuery('#carousel-stop').bind('click', function() {
				carousel.stopAuto();
    });
	
	jQuery('#carousel-play').bind('click', function() {
		carousel.startAuto();
	});
	
	// Slideshow next and previous buttons
    jQuery('#carousel-next').bind('click', function() {
        carousel.next();
    });

    jQuery('#carousel-prev').bind('click', function() {
        carousel.prev();
    });
	
};

// jCarousel active image class for jCarousel-scroll element
function carousel_itemFirstInCallback(carousel, item, idx, state) {
	$('.activeImg').removeClass('activeImg').filter('.jcarousel-control a:eq('+(idx-1)+')').addClass('activeImg');
};
