Event.observe(window,"load", function() {
	$$("input.default").invoke("observe","focus", function(ev) {
		el = ev.element();
		if (el.value == el.defaultValue)
			el.value = "";
	});
	$$("input.default").invoke("observe","blur", function(ev) {
		el = ev.element();
		if (el.value == "")
			el.value = el.defaultValue;
	});
	
	$$(".tab").invoke("observe","click",function(ev) {
		ev.stop();
		$$(".tab_content").invoke("hide");
		$$(".tab").invoke("removeClassName","active");
		$$(this.getAttribute("href")).invoke("show");
		this.addClassName("active");
	});
	
	$$(".instant_buy").invoke("observe","click",function(ev) {
		ev.stop();
		new Ajax.Request("http://www.novaloops.com/modules/willow/_ajax/add-to-cart.php?product=" + this.name + "&shiptype=0");
	});
	
	$$(".ship_it").invoke("observe","click",function(ev) {
		ev.stop();
		new Ajax.Request("http://www.novaloops.com/modules/willow/_ajax/add-to-cart.php?product=" + this.name + "&shiptype=1");
	});
	
	$$(".update_cart").invoke("observe","click",function(ev) {
		ev.stop();
		this.up("form").submit();
	});
	
	$$("#whats_new .mini-page a").invoke("observe","click",function(ev) {
		ev.stop();
		num = parseInt(this.readAttribute("href").substr(1)) - 1;
		$$("#whats_new p").invoke("hide");
		$$("#whats_new p")[num].show();
		$$("#whats_new .mini-page a").invoke("removeClassName","active");
		this.addClassName("active");
	});
	
	$$("#nav > li").invoke("observe","mouseenter", function(ev) { this.addClassName("hover"); });
	$$("#nav > li").invoke("observe","mouseleave", function(ev) { this.removeClassName("hover"); });
	
	// Homepage Browse By Items.
	
	$$("#tab-pages li a").invoke("observe","click",function(ev) {
		ev.stop();
		$$("#tab-pages li a").invoke("removeClassName","active");
		this.addClassName("active");
		pos = (parseInt(this.readAttribute("href").substr(1)) - 1) * -532;
		$$("#tab-content div").each(function(el) {
			if (el.visible())
				new Effect.Morph(el.down("ul"), { duration: 0.5, style: "margin-left: " + pos + "px" });
		});
	});
	
	$$("#tabs li a").invoke("observe","click",function(ev) {
		ev.stop();
		$$("#tabs li a").invoke("removeClassName","active");
		this.addClassName("active");
		id = this.readAttribute("href").substr(1);
		$$("#tab-content div").invoke("hide");
		$$("#tab-pages div").invoke("hide");
		$(id).show();
		$(id).down("ul").setStyle({ marginLeft: "0px" });
		$(id.replace("category","pages")).show();
		$$("#tab-pages li a").invoke("removeClassName","active");
		$$("#" + id.replace("category","pages") + " ul li a")[0].addClassName("active");
	});
	
	// Send to Friend
	$("invite_a_friend").observe("click",function(ev) {
		ev.stop();
		new Effect.toggle("send_to_friend","appear", { duration: 0.5 });
	});
	$$("#send_to_friend a").invoke("observe","click", function(ev) {
		ev.stop();
		new Effect.Fade("send_to_friend", { duration: 0.5 });
	});
	$$("#send_to_friend form").invoke("observe","submit", function(ev) {
		ev.stop();
		new Ajax.Request("http://www.novaloops.com/modules/send-to-friend.php?url=" + escape(document.location.href) + "&" + $("stf_form").serialize());
	});
	
	// Password Request
	$$(".forgot_password").invoke("observe","click",function(ev) {
		ev.stop();
		email = $("login_email").value;
		if (!email)
			throwAlert("Please enter your email address and click the Forgot Password link again to have your password emailed to you.");
		else
			new Ajax.Request("http://www.novaloops.com/modules/password-request.php?email=" + escape(email));
	});
	
	// Free Sounds submissions
	$$("form.free_sounds").invoke("observe","submit",function(ev) {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test($("email").value)) {
			ev.stop();
			alert("Please enter a valid email address.");
		}
	});
});

function showAlert(msg) {
	alert(msg);
}

function throwAlert(msg) { showAlert(msg); }