var alertBusy = false;

Event.observe(window,"load", function() {
	
	// Mailing List Signup
	$$("#mailing_list_wrapper form").invoke("observe","submit",function(ev) {
		e = this.down("input").value;
		if (e.indexOf(".") == -1 || e.indexOf("@") == -1) {
			throwAlert("Please enter a valid email address!");
			ev.stop();
		}
	});
	
	$$("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;
	});
	
	$("show_cart").observe("click",function(ev) {
		ev.stop();
		if ($("cart_drop").style.display == "none")
			new Effect.Appear("cart_drop", { duration: 0.5 });
		else
			new Effect.Fade("cart_drop", { duration: 0.5 });
	});
	
	$("close_cart").observe("click",function(ev) {
		ev.stop();
		new Effect.Fade("cart_drop", { duration: 0.5 });
	});
	
	$("stf_link").observe("click",function(ev) {
		ev.stop();
		$("send_to_friend").show();
	});
	
	$$("#send_to_friend .text").invoke("observe","focus", function(ev) {
		el = ev.element();
		if (el.value == el.defaultValue)
			el.value = "";
	});
	
	$$("#send_to_friend .text").invoke("observe","blur", function(ev) {
		el = ev.element();
		if (el.value == "")
			el.value = el.defaultValue;
	});
	
	if ($("stf_form")) {
		$("stf_form").observe("submit",function(ev) {
			ev.stop();
			new Ajax.Request("http://www.russellmackenna.com/modules/send-to-friend.php?url=" + escape(document.location.href) + "&" + $("stf_form").serialize());
		});
	}
	
	if ($("stf_form_secure")) {
		$("stf_form_secure").observe("submit",function(ev) {
			ev.stop();
			new Ajax.Request("secure_root/modules/send-to-friend.php?url=" + escape(document.location.href) + "&" + $("stf_form_secure").serialize());
		});
	}
	
	
	$$("#ete_link").click(function(ev) {
		ev.stop();
		$("easy_to_enter").show();
	});

	
	$$(".popup").invoke("observe","click",function(ev) {
		ev.stop();
		el = ev.element();
		if (!el.href)
			el = el.up("a");
		window.open(el.href.gsub("theurl",escape(document.location.href)),'','toolbar=0, resizable=yes, scrollbars=yes, status=0, width=800, height=600' );
	});
});

function stfClose() {
	$("send_to_friend").hide();
}

function eteClose() {
	$("easy_to_enter").hide();
}


function throwAlert(message) {
	if (alertBusy) {
		$("alert_message").innerHTML = message;
		clearTimeout(cleartimer);
		cleartimer = setTimeout('new Effect.Fade("alert_message", { afterFinish: function() { alertBusy = false; }});',1500);
	} else {
		alertBusy = true;
		container = new Element("div", { style: "background: url(http://www.russellmackenna.com/images/alert_bg.png); padding: 13px 30px; font-size: 16px; position: absolute; z-index: 1002; color: #FFF; ", id: "alert_message" });
		$$("body")[0].appendChild(container);
		$("alert_message").innerHTML = message;
		dims = windowDimensions();
		cdims = container.getDimensions();
		offsets = document.viewport.getScrollOffsets();
		topas = Math.round((dims.height - cdims.height) / 2) + offsets.top;
		leftas = Math.round((dims.width - cdims.width) / 2) + offsets.left;
		$("alert_message").style.top = topas + "px";
		$("alert_message").style.left = leftas + "px";
		$("alert_message").hide();
		new Effect.Appear("alert_message");
		cleartimer = setTimeout('new Effect.Fade("alert_message", { afterFinish: function() { alertBusy = false; }});',1500);
	}
}

function windowDimensions() {
	if (window.innerWidth) {
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	return {"width": windowWidth, "height": windowHeight};
}