$(document).ready(function() {
	
	$('.visibility-hide').addClass('hide').removeClass('visibility-hide');
	
	// Tabs
	$(".tabs li:eq(0)").each(function(){
		$(this).addClass("tab-current");
	});
	$(".tab-content").each(function(){
		$(this).children(":not(:first)").hide();	
	});
	$(".tabs a").click(function(){
	if ($(this).parent("li").hasClass("tab-current") == false)
		{
			var thisTarget = $(this).attr("href");
			$(this).parent("li").addClass('tab-current').siblings('.tab-current').removeClass('tab-current');
			$(this).parents(".tabs").nextAll(".tab-content").children(":visible").fadeOut(1, function() {
				$(thisTarget).fadeIn("fast");
			});
		}
	return false; 
	});
	
	
	// New Windows in an accessible manner
	$("a[href^='http']").not("[href*='kidsahead.com']").addClass("new-window").attr("title","This link will open in a new window/tab").click(function(){
			window.open(this.href);
			return false;
		});
	
	// Bio Bubbles (to help with info overload)
	$('.bubbleInfo').each(function () {
      var distance = 10;
      var time = 250;
      var hideDelay = 500;

      var hideDelayTimer = null;

      var beingShown = false;
      var shown = false;
      var trigger = $('.trigger', this).addClass("linked");
			var triggerText = "<h4>" + trigger.text() + "</h4>";
      var info = $('.popup', this).css('opacity', 0);

			info.prepend(triggerText);

      $([trigger.get(0), info.get(0)]).mouseover(function () {
          if (hideDelayTimer) clearTimeout(hideDelayTimer);
          if (beingShown || shown) {
              // don't trigger the animation again
              return;
          } else {
              // reset position of info box
              beingShown = true;

              info.css({
                  top: -90,
                  left: -33,
                  display: 'block'
              }).animate({
                  left: '-=' + distance + 'px',
                  opacity: 1
              }, time, 'swing', function() {
                  beingShown = false;
                  shown = true;
              });
          }

          return false;
      }).mouseout(function () {
          if (hideDelayTimer) clearTimeout(hideDelayTimer);
          hideDelayTimer = setTimeout(function () {
              hideDelayTimer = null;
              info.animate({
                  top: '-=' + distance + 'px',
                  opacity: 0
              }, time, 'swing', function () {
                  shown = false;
                  info.css('display', 'none');
              });

          }, hideDelay);

          return false;
      });
  });	
	
	// Email Form Validation
	$('#fsForm965864').submit(function() {
			$('#requiredFieldsError').hide();
			var isError = false;
			$('.fsRequired').each(function() {
				var value = $(this).val();
				var emailpat = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
				if(value.replace(" ","") === "") {
					isError = true;
				} else if($(this).hasClass('fsFormatEmail')) {
					if(value.match(emailpat)) {
						isError = false;
					} else {
						isError = true;
					}
				}
			});
			
			if (isError){
				$("#requiredFieldsError").show();
				return false;
			}
		});
});