function isValid(type, str) {
	if (type.toLowerCase() == "email") {
		if ((str == null) || (str == "")) return false;
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1) return false;
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
		if (str.indexOf(at,(lat+1))!=-1) return false;
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
		if (str.indexOf(dot,(lat+2))==-1) return false;
		if (str.indexOf(" ")!=-1) return false;
		return true;
	}
};

function initFeature($, f, ext, imageSelector) {
	$(imageSelector).each(function() {
		setupFeature($, f, ext, imageSelector, ".map .dura", "-dura" + ext);
		setupFeature($, f, ext, imageSelector, ".map .digital", "-digital" + ext);
		setupFeature($, f, ext, imageSelector, ".map .empress", "-empress" + ext);
		setupFeature($, f, ext, imageSelector, ".map .lava", "-lava" + ext);
		setupFeature($, f, ext, imageSelector, ".map .implants", "-implants" + ext);
		setupFeature($, f, ext, imageSelector, ".map .copings", "-copings" + ext);
		setupFeature($, f, ext, imageSelector, ".map .scanner", "-scanner" + ext);
	});
}

function setupFeature($, f, defaultExt, imageSelector, selector, ext) {
	var img = new Image();
	img.src = f + ext;
	$(selector).hoverIntent(function() { $(imageSelector).attr("src", f + ext); }, function() { $(imageSelector).attr("src", f + defaultExt); });
}

(function($) {
	$(document).ready(function() {
		$("input[type=text][title]").each(function() { $(this).val($(this).attr("title")); if($.trim($(this).val()) == "") $(this).val($(this).attr("title")); $(this).focus(function() { if($(this).val() == $(this).attr("title")) $(this).val(""); }).blur(function() { if($.trim($(this).val()) == "") $(this).val($(this).attr("title")); }); });
		$("a[href][rel*=external]").attr("target", "_blank");
		$(".fade-hover").hoverIntent(function() { $(this).fadeTo("fast", 0.5); }, function() { $(this).fadeTo("fast", 1.0); });
		$(".lo").hoverIntent(function() { $(this).removeClass("lo").addClass("hi"); }, function() { $(this).removeClass("hi").addClass("lo"); });
		$("#nav .lo").hoverIntent(function() { $(this).removeClass("lo").addClass("hi").find(".subcontent").show("slide", {direction: "up"}); }, function() { $(this).removeClass("hi").addClass("lo").find(".subcontent").hide("slide", {direction: "up"}); });
		$("#nav li").each(function() {
			var spaceLeft = $(this).width();
			$(this).nextAll().each(function() { spaceLeft += $(this).width(); });
			$(this).find(".subcontent").each(function() {
				if(spaceLeft < $(this).width()) $(this).css("left", spaceLeft - $(this).width());
			});
		});
		$("form#contact-form").submit(function() {
			if(!isValid("email", this.email.value)) { alert("Please enter a valid email"); return false; }
			if(this.recaptcha_response_field.value == "") { alert("Invalid reCaptcha"); return false; }
			return true;
		});

		$("form#firstcase-form").submit(function()
		{
			var returnVal = true;
			var message = "";

			if( this.firstname.value == "" ){				message = "Please enter your first name"; $('#firstname').focus(); }
			else if( this.lastname.value == "" ){			message = "Please enter your last name"; $('#lastname').focus(); }
			//else if( this.accountnumber.value == "" ){		message = "Please enter your account number"; $('#accountnumber').focus(); }
			else if( this.address1.value == "" ){			message = "Please enter your address"; $('#address1').focus(); }
			else if( this.city.value == "" ){				message = "Please enter your city"; $('#city').focus(); }
			else if( this.state.value == "" ){				message = "Please enter your state"; $('#state').focus(); }
			else if( this.zip.value == "" ){				message = "Please enter your zip code";	$('#zip').focus(); }
			//else if( this.specialty.value == "" ){			message = "Please enter your specialty";	$('#specialty').focus(); }
			else if( this.phone.value == "" ){				message = "Please enter your company telephone number";	$('#phone').focus(); }
			else if( !isValid("email", this.email.value) ){ message = "Please enter a valid email"; $('#email').focus(); }
			//else if( this.recaptcha_response_field.value == "" ){ message = "Invalid reCaptcha"; $('#recaptcha_response_field').focus(); }

			if( message != "" )
			{
				returnVal = false;
				alert(message);
			}

			return returnVal;
		});

		//$("#home-feature .feature").bind("click", function(e) { window.location.href = $(this).find("a").attr("href"); });
		initFeature($, "images/feature/featured", ".jpg", "#featured");
		initFeature($, "images/feature/products/featured", ".png", "#products-featured");
	});
})(jQuery);
