
/************ 
FORM VALIDATION
*************/

function addError(parent, errorText) {
	parent.addClass("form-field-error");
	$(".error", parent).html(errorText);
}

function removeErrors() {
	$(".form-field-error div.error").html("");
	$(".form-field-error").removeClass("form-field-error");
}

function validateName(val) {
	if (jQuery.trim(val).length == 0)
		return "Please enter your name.";
	else
		return "";
}

function validatePhone(val) {
	if (jQuery.trim(val).length == 0)
		return "Please enter your phone number.";
	else
		return "";
}

function validateEmail(val) {
	val = jQuery.trim(val);
	if (val.length == 0)
		return "Please enter an email address.";
	else if (val.indexOf("@") <= 0 || val.indexOf(" ") > -1 || val.length < 5)
		return "Please enter a valid email address.";		
	else
		return "";
}

function validateRequestForm(event) {
	event.preventDefault();
	removeErrors();//reset messaging
	var hasErrors = false;
	var errorText = "";

	var fld = $("#realname");
	errorText = validateName(fld.val());
	if (errorText != "") {
		addError(fld.parent(), errorText);
		hasErrors = true;
	}

	fld = $("#Phone");
	errorText = validatePhone(fld.val());
	if (errorText != "") {
		addError(fld.parent(), errorText);
		hasErrors = true;
	}

	fld = $("#email");
	errorText = validateEmail(fld.val());
	if (errorText != "") {
		addError(fld.parent(), errorText);
		hasErrors = true;
	}



	if (!hasErrors) {
		$("#requestQuote").submit();
		
	}
}


jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object') ? arguments[0] : arguments;
	for(i=0; i < a.length; i++) {
		jQuery("<img>").attr("src", a[i]);
	}
};


var tnavOffArr = ["../img/metal_stamping.png","../img/tool_design.png","../img/product_assembly.png","../img/packaging.png"];
var tnavOnArr = ["../img/metal_stamping-on.png","../img/tool_design-on.png","../img/product_assembly-on.png","../img/packaging-on.png"];
$(document).ready( function() {
	$.preloadImages(tnavOnArr);
	$("a.tnav", "#firstTierNav").each(function() {
		var navId = $(this).attr("id");
		var key = navId.split("tnav")[1];
		$(this).mouseover(function() {
			$("img","#"+navId).attr("src",tnavOnArr[key]);
		});
		$(this).mouseout(function() {
			$(this).find("img").attr("src",tnavOffArr[key]);
		});
	});
	var bodyId = $("body").attr("id");
	if (bodyId.indexOf("page") > -1) {
		var key = bodyId.split("page")[1];
		$("img","#tnav"+key).attr("src",tnavOnArr[key]);	
	}
});



$(document).ready( function() {
	//$("#content-wrap").height($("#photos-wrap").height());

});




/*
The promo scroller method used on the homepage content area
*/
var promoScroller = function($) {
	var scrollerActiveIndex = 0; //the array position of the first element in full view
	var scrollerContentArr = new Array();
	var collLen = 0;
	var scrollerContentWidth = 0;
	var scrollerStartingX = 20;
	var enableAutoScroll = true;
	var isAnimating = false;
	var delay = 4000; // default delay in miliseconds
	var timerID = null;

	var timerInit = function() {
		timerID = window.setTimeout("promoScroller.autoScroll()", delay);
	};

	var lineup = function() {
		collLen = scrollerContentArr.length;
		var slide1 = scrollerContentArr[0];
		var slideLast = scrollerContentArr[collLen-1];
		slide1.css("top", scrollerStartingX + "px");
		slideLast.css("top", (scrollerStartingX - scrollerContentWidth) + "px");

		for (i=1; i<=scrollerContentArr.length-2 ;i++) {
			var slide = scrollerContentArr[i];
			var x = scrollerStartingX + (i * scrollerContentWidth);
			slide.css("top", x + "px");
		}
	};

	var done = function() { isAnimating = false; };

	var left = function() {
		if (isAnimating) return;
		if (enableAutoScroll) window.clearTimeout(timerID);

		isAnimating = true;
		//find the rightmost el and move it to the end on the left
		var currLeftIndex = scrollerActiveIndex == 0 ? collLen - 1 : scrollerActiveIndex - 1;
		var currRightIndex = currLeftIndex == 0 ? collLen - 1 : currLeftIndex - 1;
		var leftStartingX = parseInt(scrollerContentArr[currLeftIndex].css("top")) - scrollerContentWidth;

		for (var i=0; i<collLen; i++) {
			if (i == currRightIndex) {
				scrollerContentArr[i].css("top", leftStartingX + "px");
			}
			scrollerContentArr[i].animate({ "top":"+=" + scrollerContentWidth + "px" }, 350, null, done);
		}

		if (scrollerActiveIndex == 0)
			scrollerActiveIndex = (collLen - 1);
		else
			scrollerActiveIndex--;

		if (enableAutoScroll) timerInit();
	};

	var right = function() {
		if (isAnimating) return;
		if (enableAutoScroll) window.clearTimeout(timerID);

		isAnimating = true;
		//find the leftmost el and move it to the end on the right
		var currLeftIndex = scrollerActiveIndex == 0 ? collLen - 1 : scrollerActiveIndex - 1;
		var currRightIndex = currLeftIndex == 0 ? collLen - 1 : currLeftIndex - 1;
		var rightStartingX = parseInt(scrollerContentArr[currRightIndex].css("top")) + scrollerContentWidth;

		for (var i=0; i<collLen; i++) {
			if (i == currLeftIndex) {
				scrollerContentArr[i].css("top", rightStartingX + "px");
			}
			scrollerContentArr[i].animate({ "top":"-=" + scrollerContentWidth + "px" }, 350, null, done);
		}

		if (scrollerActiveIndex == (collLen - 1))
			scrollerActiveIndex = 0;
		else
			scrollerActiveIndex++;

		if (enableAutoScroll) timerInit();
	};

	var buildButtons = function() {
		$(".promoScroller-root").prepend(
				'<div class="promoScroller-left"><div class="positioner"><img src="img/up-arrow.png" class="ie6png" alt="scroll right" /></div></div>'
			);
		$(".promoScroller-root").append(
				'<div class="promoScroller-right"><div class="positioner"><img src="img/down-arrow.png" class="ie6png" alt="scroll left" /></div></div>'
			);
		$(".promoScroller-left").css("opacity",.75).click(right).hover(
				function() {
					$(this).css("opacity",0.4);
					//$("img",this).show();
				},
				function() {
					$(this).css("opacity",0.75);
					//$("img",this).hide();
				}
			);
		$(".promoScroller-right").css("opacity",.75).click(left).hover(
				function() {
					$(this).css("opacity",0.4);
					//$("img",this).show();
				},
				function() {
					$(this).css("opacity",0.75);
					//$("img",this).hide();
				}
			);
	};

	return {
		autoScroll : function() {
			right();
		},

		init : function(rootId,delayInSecs) {
			var parent = $("#"+rootId);

			if (parent.length > 0) {
				//if delay is not specified, used the default delay
				if (delayInSecs > 0) delay = delayInSecs * 1000;


				//find siblings and build array
				//scrollerContentWidth = testEl.width(); //set width for animations while we are here
				scrollerContentWidth = 135; //set width for animations while we are here
				//height is 135, padding is 20px
				$(".promoScroller-slide").each( function(i){
					scrollerContentArr.push($(this));
				} );

				//display and bind scrollers if necessary
				if (scrollerContentArr.length > 1) {
					buildButtons();
					lineup();
					if (enableAutoScroll) timerInit();
				}
			}
		}
	};
}($);


$(document).ready( function() {
	promoScroller.init("slides");
});