$(document).ready(function(){

	/* Products fade-in
	var products = $(".product").size();
	for (i = 0; i < products; i++) {
		value = i * 100 + 100;
		$(".product:eq("+i+")").animate(
			{ top: 0 },
			value,
			function(){ 
				$(this).animate(
					{ opacity: 1 },
					500
				)
			}
		);
	}
	$(".nitrile-group h2").animate({ top: 0 }, 400, function(){
		$(this).animate({ opacity: 1 }, 500);
	});
	$(".vinyl-group h2").animate({ top: 0 }, 600, function(){
		$(this).animate({ opacity: 1 }, 500);
	});
	$(".latex-group h2").animate({ top: 0 }, 700, function(){
		$(this).animate({ opacity: 1 }, 500);
	}); */
	
	/* Home */
	$(".main img").stop().animate({ bottom: '-20px', right: '-60px' }, 750, 'easeOutCubic');
	$(".main-copy").stop().animate({ top: '0px' }, 875, 'easeOutCubic');
	$(".ice-link").stop().animate({ left: '20px' }, 1000, 'easeOutBack');
	
	/* Adjustments */
	
	$(".product-group").each(function(){
		$(this).find(".product:last").css("margin-right","0");
	});
	$(".study:first, blockquote:first").addClass("first-border");
	
	/* Image nav button fade */
	$(".images").hover(function(){
		$("a.prevnext").fadeIn(250, 'easeInOutQuad');
	}, function(){
		$("a.prevnext").fadeOut(250, 'easeInOutQuad');
	});
	
	/* Image navigation logic */
	$("a.prevnext").click(function(e){
		e.preventDefault();
		var current = $(".image").index($(".img-on"));
		var total = $(".image").size();
		var last = total - 1;
		if ($(this).hasClass("prev")) {
			if (current == 0) {
				target_eq = last;
				target_moveto = '-4999px';
				current_moveto = '4999px';
			} else {
				target_eq = (current - 1);
				target_moveto = '-4999px';
				current_moveto = '4999px';
			}
		} else {
			if (current == last) {
				target_eq = 0;
				target_moveto = '4999px';
				current_moveto = '-4999px';
			} else {
				target_eq = (current + 1);
				target_moveto = '4999px';
				current_moveto = '-4999px';
			}
		}
		$(".image:eq("+target_eq+")").css("left",target_moveto).addClass("changing");
		$(".img-on").stop().animate({ left: current_moveto }, 500, 'easeInOutQuad', function(){ $(this).removeClass("img-on"); });
		$(".changing").stop().animate({ left: '0' }, 500, 'easeInOutQuad', function(){ $(this).addClass("img-on").removeClass("changing"); });
	});
	
	/* Compare table hover
	
	$("table.specs tbody td").hover(function(){
		var index = $(this).index();
		$(".hovered").removeClass("hovered").removeClass("super-hovered").removeClass("not-hovered");
		$("table.specs tbody td").addClass("not-hovered");
		$("table.specs thead th:eq("+index+"), table.specs tfoot th:eq("+index+")").addClass("hovered").removeClass("not-hovered");
		$("table.specs tbody tr").each(function(){
			$("td:eq("+(index - 1)+")", this).addClass("hovered").removeClass("not-hovered");
		});
		$(this).addClass("super-hovered").siblings().addClass("hovered").removeClass("not-hovered");
	});
	$("table.specs tbody th").hover(function(){
		$(".hovered").removeClass("hovered").removeClass("super-hovered").removeClass("not-hovered");
		$("table.specs tbody td").addClass("not-hovered");
		$(this).addClass("hovered").siblings().addClass("hovered").removeClass("not-hovered");
	});
	$("table.specs tbody th *").hover(function(){
		$(".hovered").removeClass("hovered").removeClass("super-hovered").removeClass("not-hovered");
		$("table.specs tbody td").addClass("not-hovered");
		$(this).parent().addClass("hovered").siblings().addClass("hovered").removeClass("not-hovered");
	});
	$("table.specs thead th, table.specs tfoot th").hover(function(){
		var index = $(this).index();
		$(".hovered").removeClass("hovered").removeClass("super-hovered").removeClass("not-hovered");
		$("table.specs tbody td").addClass("not-hovered");
		$("thead th:eq("+index+"), tfoot th:eq("+index+")").addClass("hovered").removeClass("not-hovered");
		$("table.specs tbody tr").each(function(){
			$("td:eq("+(index - 1)+")", this).addClass("hovered").removeClass("not-hovered");
		});
	});
	$("table.specs thead th *, table.specs tfoot th *").hover(function(){
		var index = $(this).parent().index();
		$(".hovered").removeClass("hovered").removeClass("super-hovered").removeClass("not-hovered");
		$("table.specs tbody td").addClass("not-hovered");
		$("thead th:eq("+index+"), tfoot th:eq("+index+")").addClass("hovered").removeClass("not-hovered");
		$("table.specs tbody tr").each(function(){
			$("td:eq("+(index - 1)+")", this).addClass("hovered").removeClass("not-hovered");
		});
	});
	$("table.specs").mouseout(function(){
		$(".hovered").removeClass("hovered").removeClass("super-hovered");
		$(".not-hovered").removeClass("not-hovered");
	}); */
	
	$("#specs tr").each(function(){
		var col = $("#specs").attr("class").substr(3);
		$(this).children("th, td").eq(col).addClass("hovered");
	});
	
	/* Attribute hover */
	$("ul.attributes .attr a").hover(function(){
		$(this).siblings("h4").stop().animate({
			opacity: 1,
			top: "64px"
		}, 200);
	}, function(){
		$(this).siblings("h4").stop().animate({
			opacity: 0,
			top: "88px"
		}, 200, function(){
			$(this).css("top","40px");
		});
	});
	
	/* Same thing, only department page hover */
	$(".recommended a").hover(function(){
		$("h5", this).stop().css("left","-125px").animate({
			opacity: 1,
			top: "36px"
		}, 200);
	}, function(){
		$("h5", this).stop().animate({
			opacity: 0,
			top: "20px"
		}, 200, function(){
			$(this).css("left","-9999px");
		});
	});
	
	/* Blockquote fix */
	$("blockquote:odd").addClass("odd");
	
	/* Sample request */	
	$(".glove-box").click(function(){
		$(":radio").attr("checked","");
		$(this).find(":radio").attr("checked","checked");
		if ($(this).hasClass("comment-box")) {
			$("fieldset.facility-info").hide();
			$("textarea").siblings("label").show().parent().addClass("required").siblings("legend").find("small").hide();
			$("#acctno-item, #title-item").hide();
			$("#name").width("624px").css("margin-right","0px");
			$(":submit").val('Send Your Question or Comment');
		} else {
			$("fieldset.facility-info").show();
			$("textarea").siblings("label").hide().parent().removeClass("required").siblings("legend").find("small").show();
			$("#acctno-item, #title-item").show();
			$("#name").width("440px").css("margin-right","10px");
			$(":submit").val('Send Your Free Sample Request');
		}
	});
	
	/* Glove subnav */
	$("#subnav a").click(function(e){
		if (!$(this).parent().hasClass("subactive")) {
			var href = $(this).attr("href");
			href = href.substr(1);
			switch(href) {
				case "description": left = '151px'; break;
				case "specifications": left = '461px'; break;
				case "recommendations": left = '771px'; break;
				/*case "description": left = '108px'; break;
				case "specifications": left = '343px'; break;
				case "recommendations": left = '578px'; break;
				case "gallery": left = '813px'; break;*/
			}
			$("html, body").animate({scrollTop: $(".new-content").offset().top - 30}, 250);
			/*$(".content-boxes").height($("#recommendations"));
			$(".subactive-box").fadeOut(250, function(){
				$(this).removeClass("subactive-box");
				$("#"+href).fadeIn(250).addClass("subactive-box");
			});*/
			$(".subactive-box").stop().animate({ opacity: 0 }, 250, function(){
				$(this).css("z-index",5).removeClass("subactive-box").addClass("pending-box");
				$("#"+href).addClass("subactive-box").fadeIn(250, function(){
					$(".pending-box").css("z-index",6).css("opacity",1).removeClass("pending-box");
				});
			});
				
			$(".subactive a").stop().animate({ paddingTop: '9px', paddingBottom: '9px' }, 250, function(){
				$(".subactive").removeClass("subactive");
			});
			$(this).stop().animate({ paddingTop: '0', paddingBottom: '18px' }, 250, function(){
				$(this).parent().addClass("subactive");
			});
			$("#subnav").stop().animate({ backgroundPosition: left+" bottom" }, 250);
		}
		e.preventDefault();
	});
		
	/* Question hover */
	$(".form-item label a").click(function(e){
		e.preventDefault();
		$("#acctno-item p").stop().css("z-index","60").animate({ top: '48px', opacity: 1 }, 250);
	});
	$("#acctno-item p").click(function(){
		$(this).stop().animate({ top: '15px', opacity: 0 }, 125, function(){
			$(this).css("z-index","1");
		});
	});
	$("#acctno-item p a").click(function(e){ e.preventDefault(); });
	
	/* Videos */
	$("a.video, .gallery-images a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'titlePosition' : 'inside',
		'padding' : 0,
		'cyclic' : true
	});
	
	/* Input focus */
	$(":text").focus(function(){
		var scroll = $(this).offset();
		$("html, body").animate({scrollTop: (scroll.top - 80)}, 250);
	});
	
	/* Form validation */
	var $email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[a-zA-Z]+\.[a-zA-Z]{2,4}$/;
	var $phone_regex = /^\d{3}-\d{3}-\d{4}$|^\d{10}$|^\d{3}.\d{3}.\d{4}$/;
	var $zip_regex = /^\d{5}$|^\d{5}-\d{4}$|^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} \d{1}[A-Z]{1}\d{1}$/;
	var $state_array = new Array("AL","AK","AS","AZ","AR","CA","CO","CT","DE","DC","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP","OH","OK","OR","PW","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY","AB","BC","MB","NB","NL","NS","NT","NU","ON","PE","QC","SK","YT");
	
	$("form").submit(function(e){
		var $errors = '';
		
		if ($("input[name='glove-radio']:checked").val() === undefined) {
			$errors += '<li>Please <strong>choose a glove to sample</strong>, or select the question/comment box if you do not want gloves.</li>';
		}
		
		$(".required").each(function(){
			var $this = $(this);
			if ($this.is(":visible")) {
				if ($("textarea", this).exists()) {
					var $input = $("textarea", this);
				} else {
					var $input = $("input", this);
				}
				var $label = $("label", this);
				$label.removeClass("mistake");
				if ($input.hasClass("text")) {
					if ($input.val() == '') {
						$label.addClass("mistake");
						$errors += '<li><strong>'+$label.html()+'</strong> needs a value.</li>';
					}
				} else if ($input.hasClass("email")) {
					if ($input.val() == '') {
						$label.addClass("mistake");
						$errors += '<li><strong>'+$label.html()+'</strong> needs a value.</li>';
					} else if (!$email_regex.test($input.val())) {
						$label.addClass("mistake");
						$errors += '<li>The value entered for <strong>'+$label.html()+'</strong> is invalid.</li>';
					}
				} else if ($input.hasClass("phone")) {
					if ($input.val() == '') {
						$label.addClass("mistake");
						$errors += '<li><strong>'+$label.html()+'</strong> needs a value.</li>';
					} else if (!$phone_regex.test($input.val())) {
						$label.addClass("mistake");
						$errors += '<li>The value entered for <strong>'+$label.html()+'</strong> is invalid. (Use 123-456-7890 format.)</li>';
					}
				} else if ($input.hasClass("state")) {
					if ($input.val() == '') {
						$label.addClass("mistake");
						$errors += '<li><strong>'+$label.html()+'</strong> needs a value.</li>';
					} else if ($.inArray($input.val().toUpperCase(), $state_array) == -1) {
						$label.addClass("mistake");
						$errors += '<li>The value entered for <strong>'+$label.html()+'</strong> is invalid.</li>';
					}
				} else if ($input.hasClass("zip")) {
					if ($input.val() == '') {
						$label.addClass("mistake");
						$errors += '<li><strong>'+$label.html()+'</strong> needs a value.</li>';
					} else if (!$zip_regex.test($input.val().toUpperCase())) {
						$label.addClass("mistake");
						$errors += '<li>The value entered for <strong>'+$label.html()+'</strong> is invalid.</li>';
					}
				} else if ($input.hasClass("math")) {
					if ($input.val() != 10) {
						$label.addClass("mistake");
						$errors += '<li>Please <strong>answer the math question</strong> correctly.</li>';
					}
				} else if ($("textarea", this).exists()) {
					if ($input.val() == '') {
						$label.addClass("mistake");
						$errors += '<li>Please enter a <strong>question or comment</strong> if you aren&rsquo;t requesting a glove sample.</li>';
					}
				}
			}
		});
		if ($errors != '') {
			$("#message-box").empty().show().prepend('<div id="errors"></div>').css("margin-bottom","2em");
			$("#errors").append('<h3>I&rsquo;m sorry, but your form couldn&rsquo;t be submitted. Please address the following values:</h3><ul>'+$errors+'</ul>');
			e.preventDefault();
			$("html, body").animate({scrollTop: 0}, 250);
		} else {
			$("#message-box").empty();
			$(":submit").attr("disabled","disabled").val("Submitting...");
			return true;
		}
	});
	
	/* Recommendations */
	$(".attributes-holder a").click(function(e){
		e.preventDefault();
		var href = $(this).attr("href");
		$("html, body").animate({scrollTop: $(href).offset().top}, 500, 'easeOutCubic');
	});

});

jQuery.fn.exists = function(){return jQuery(this).length>0;}


/* http://keith-wood.name/backgroundPos.html
   Background position animation for jQuery v1.0.1.
   Written by Keith Wood (kbwood{at}iinet.com.au) November 2010.
   Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 
   MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 
   Please attribute the author if you use it. */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(7($){5 g=\'F\';$.r.s[\'9\']=$.r.s[\'h-j\']=7(a){k(!a.t){5 b=$(a.u);5 c=b.v(g);b.l(\'9\',c);a.8=m(c);a.3=m($.n.G>=\'1.6\'?a.3:a.w.x[\'9\']||a.w.x[\'h-j\']);H(5 i=0;i<a.3.y;i++){k(a.3[i][0]){a.3[i][1]=a.8[i][1]+(a.3[i][0]==\'-=\'?-1:+1)*a.3[i][1]}}a.t=I}$(a.u).l(\'h-j\',((a.z*(a.3[0][1]-a.8[0][1])+a.8[0][1])+a.3[0][2])+\' \'+((a.z*(a.3[1][1]-a.8[1][1])+a.8[1][1])+a.3[1][2]))};7 m(c){5 d={A:\'o%\',J:\'0%\',K:\'B%\',C:\'0%\',D:\'B%\'};5 e=c.L(/ /);5 f=7(a){5 b=(d[e[a]]||e[a]||\'o%\').M(/^([+-]=)?([+-]?\\d+(\\.\\d*)?)(.*)$/);e[a]=[b[1],N(b[2]),b[4]||\'O\']};k(e.y==1&&$.P(e[0],[\'C\',\'D\'])>-1){e[1]=e[0];e[0]=\'o%\'}f(0);f(1);p e}$.n.E=7(e){p 7(a,b,c,d){k(a[\'9\']||a[\'h-j\']){q.v(g,q.l(\'9\')||\'A\')}p e.Q(q,[a,b,c,d])}}($.n.E)})(R);',54,54,'|||end||var||function|start|backgroundPosition||||||||background||position|if|css|parseBackgroundPosition|fn|50|return|this|fx|step|set|elem|data|options|curAnim|length|pos|center|100|top|bottom|animate|bgPos|jquery|for|true|left|right|split|match|parseFloat|px|inArray|apply|jQuery'.split('|'),0,{}))
