function openBannerWindow(url,width,height) {
	var bannerWindow = window.open(url,'','scrollbars=yes,menubar=yes,height='+height+',width='+width+',resizable=yes,toolbar=no,location=yes,status=yes');
}

function isValid(type, str) {
	if (type.toLowerCase() === "email") {
		var reEmail = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		if (!reEmail.test(str)) return false;
		return true;
	} else if (type.toLowerCase() === "telephone") {
		var rePhoneNumber = new RegExp(/^(\+\d)*\s*(\(?\d{3}\)?\s*)*-?\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/);
		if (!rePhoneNumber.test(str)) return false;
		return true;
	}
};

function setEmailMarketingAjax($) {
	$("form#email-marketing-form").submit(function() {
		if(!isValid("email", this.email_address.value)) { alert("Please enter a valid email"); return false; }
		this.event.value = "emailmarketing.contact.add.vr";
		$.post("index.cfm", $(this).serializeArray(), function(data){
			$(data).find("success").each(function() {
				alert($(this).text());
				$("#email-marketing-form input[type=text]").val($("#email-marketing-form input[type=text]").attr("title"));
			});
		});
		return false;
	});
}

function setContactFormSubmit($) {
	$("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;
	});
}

function setCommentFormSubmit($) {
	$("form#comment-form").submit(function() {
		if(this.commentName.value == "") { alert("Please enter your name"); return false; }
		if(!isValid("email", this.commentEmail.value)) { alert("Please enter a valid email"); return false; }
		if(this.recaptcha_response_field.value == "") { alert("Invalid reCaptcha"); return false; }
		return true;
	});
}

function setMessageFormSubmit($) {
	$("form#message-form").submit(function() {
		if(this.anonymousAuthorName.value == "") { alert("Please enter your name"); return false; }
		if(this.title.value == "") { alert("Please enter a subject"); return false; }
		if(this.body.value == "") { alert("Please enter a message"); return false; }
		if(this.recaptcha_response_field.value == "") { alert("Invalid reCaptcha"); return false; }
		return true;
	});
}

jQuery.fn.simpleAccordion = function() {
	return this.each(function() {
		var height = 0;
		$(this).find("span").each(function() {
			if($(this).height() > height) height = $(this).height();
		});
		$(this).find("span").height(height);
		$(this).find(".drawer").hide();
		if($(this).find("li.active").size() > 0) $(this).find("li.active div.drawer").show()
		else $(this).find("li:first div.drawer").show();
		var $$ = $(this);
		$(this).find("li a").click(
			function() {
				var checkElement = $(this).next(".drawer");
				if((checkElement.is("div")) && (checkElement.is(":visible"))) {
					return false;
				}
				if((checkElement.is("div")) && (!checkElement.is(":visible"))) {
					$$.find("li.active").removeClass("active");
					$$.find("div.drawer:visible").slideUp("normal");
					checkElement.parent("li").addClass("active");
					checkElement.slideDown("normal");
					return false;
				}
			}
		);
	});
};

(function($) {
	$(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, .cartCheckoutTable img, .itemListingViewButton img, .itemListingElementRight img, input[type=image]").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("ul").show("slide", {direction: "up"}); }, function() { $(this).removeClass("hi").addClass("lo").find("ul").hide("slide", {direction: "up"}); });
		$("#nav ul").fadeTo("fast", 0.9);
		
		setEmailMarketingAjax($);
		setContactFormSubmit($);
		setCommentFormSubmit($);
		setMessageFormSubmit($);
		$("#faq-nav").simpleAccordion();
		
		//flash
		$("#home-flash-rotation .replace").flash({ swf: "flash/flash_main.swf", width: 552, height: 239, params: {wmode: "opaque"} });
	});
})(jQuery);