$( function() {

	// convert email addresses
	$("span.mail").each(function() {
		var address = $(this).text();
		address = address.replace(/ at /, "@");
		address = address.replace(/ dot /g, ".");
		$(this).html('<a href="mailto:'+address+'">'+address+'</a>');
	});

	$("input[name='newsletter_email']").focus(function() {
		if( $(this).val() == "join our mailing list" ) $(this).val("");
	});
	$("input[name='newsletter_email']").blur(function() {
		if( $(this).val() == "" ) $(this).val("join our mailing list");
	});

	// error tool tips
	$("p.e").brTip({
		title: "",
		toShow: 0,
		toHide: 0,
		fadeIn: "slow",
		fadeOut: "fast",
		opacity: 1,
		top: 15,
		left: 15
	});

	$(".gallery-item").each( function() {
		$(this).height($(this).parent().height());
	});

	$(".show-banner").hover(
		function() {
			AddOverlay($(this), "show");
		},
		function() {
			RemoveOverlay();
		}
	);

	$(".gallery-item").not(".blank").hover(
		function() {
			$(this).addClass("hover");
			//AddOverlay($(this), "product");
		},
		function() {
			$(this).removeClass("hover");
			//RemoveOverlay();
		}
	);

});

function AddOverlay(obj, type) {
	var link = obj.children("a").attr("href");
	$(obj).append('<div class="overlay"><a href="'+link+'"><span>view this '+type+'</span></a></div>');
}

function RemoveOverlay() {
	$(".overlay").remove();
}

// shows / hides a div by animating the height
var currDiv;
function ShowHideDiv(id, hideOthers) {
	// needs to be a class of other elements to hide
	if( hideOthers ) {
		if( currDiv != id ) {
			$("."+hideOthers).hide();
		}
		currDiv = id;
	}
	$("#"+id).animate( { height: "toggle" }, { duration: "slow" } );
	return false;
}

function closePopup(id) {
	$("#"+id).fadeOut("fast");
	$("#popup-overlay").unbind("click");
	$("#popup-overlay").remove();
	$("#cart-popup").remove();
	document.onkeydown = "";
	return false;
}

function popup(url, w, h) {
	var windowWidth= (w) ? w : 500;
	var windowHeight= (h) ? h : 500;
	var windowX=0;
	var windowY=0;
	var autocenter = true;

	if(autocenter) {
		windowX = (window.screen.width-windowWidth)/2;
		windowY = (window.screen.height-windowHeight)/2;
	}
	popup_window = window.open(url,'','menubar=no,toolbar=no,titlebar=no,status=no,scrollbars=yes,resizable=yes,width='+windowWidth+',height='+windowHeight+'');
	popup_window.moveTo(windowX,windowY);
	return false;
}
