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;
	}
};

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();
		$(this).find("a.topic").fadeTo("fast", 0.5);
		if($(this).find("li.active").size() > 0) $(this).find("li.active div.drawer").show().parent().find("a.topic").fadeTo("fast", 1.0);
		else $(this).find("li:first div.drawer").show().parent().find("a.topic").fadeTo("fast", 1.0);
		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("a.topic").fadeTo("fast", 0.5);
					$$.find("div.drawer:visible").slideUp("normal");
					checkElement.parent("li").addClass("active").find("a.topic").fadeTo("fast", 1.0);
					checkElement.slideDown("normal");
					return false;
				}
			}
		);
	});
};

(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");
		$(".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"}); });
		$("#sidebar-nav .lo").hoverIntent(function() {$(this).removeClass("lo").addClass("hi").find("ul").show("slide", {direction: "left"}); }, function() { $(this).removeClass("hi").addClass("lo").find("ul").hide("slide", {direction: "left"}); });
		
		$("#faq-nav").simpleAccordion();
		
		$("#footer-email-marketing > form").submit(function() {
			try {
				if(!isValid("email", this.email.value)) { alert("Please enter a valid email"); return false; }
				var params = {};
				$(this).find("input[@name='event'], input[name='email']").each(function() { params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; });
				params['event'] = params['event'] + "AJAX";
				$.post($(this).attr("action"), params, function(data) {
					$(data).find("success").each(function() {
						alert($(this).text());
						$("#footer-email-marketing input[name='email']").val("");
					});
				});
			}
			catch(e) { alert("javascript error"); return false; }
			return false;
		});
		
		//$("#home #flash-rotation").flash({ swf: "flash/flash_rotation.swf", width: 700, height: 358, params: {wmode: "opaque"} });
		
		$("#sidebar-gallery-5").each(function() {
			var pieces = Math.floor($("#sidebar-gallery-5").height() / $("#sidebar-gallery-4").height());
			if(pieces > 0) {
				$("#gallery-links .current").each(function() {
					var pagesDown = Math.floor($(this).position().top / $("#sidebar-gallery-4").height());
					var newY = $("#sidebar-gallery-4").height() * pagesDown;
					var maxY = $("#sidebar-gallery-5").height() - $("#sidebar-gallery-4").height();
					if(newY >= maxY) newY = maxY;
					$("#sidebar-gallery-4").scrollTop(newY);
				});
				$("#gallery-scroll-down").click(function() {
					if(!$("#sidebar-gallery-4").is(":animated")) {
						var maxY = $("#sidebar-gallery-5").height() - $("#sidebar-gallery-4").height();
						if($("#sidebar-gallery-4").scrollTop() >= maxY) return;
						var newY = $("#sidebar-gallery-4").scrollTop() + $("#sidebar-gallery-4").height();
						if(newY >= maxY) newY = maxY;
						$("#sidebar-gallery-4").animate({scrollTop: newY}, 2000);
					}
				});
				$("#gallery-scroll-up").click(function() {
					if(!$("#sidebar-gallery-4").is(":animated")) {
						if($("#sidebar-gallery-4").scrollTop() <= 0) return;
						var newY = $("#sidebar-gallery-4").scrollTop() - $("#sidebar-gallery-4").height();
						if(newY < 0) newY = 0;
						$("#sidebar-gallery-4").animate({scrollTop: newY}, 2000);
					}
				});
			}
			else {
				$("#gallery-scroll-controls").hide();
			}
		});
	});
})(jQuery);
