function setupMenu() {
  var timeout = null;
  function mouseOutItem() {
    var $item = $(this);
    $item.closest("li").removeClass("hover");
    $item.unbind("mouseout", mouseOutItem);
    $item.closest("li").find(".submenu").hide();
  }
  
  function mouseOutTimeout() {
    var e = this;
    clearTimeout(timeout);
    timeout = setTimeout(function() { mouseOutItem.call(e); }, 400);
  }
  
  function mouseOverItem() {
    clearTimeout(timeout);
    var $item = $(this);
    $item.closest("ul").find("li").children("a").each(mouseOutItem);
    $item.closest("li").addClass("hover");
    $item.closest("li").find(".submenu").show().hover(function() {
      clearTimeout(timeout);
    }, function() {
      mouseOutTimeout.call($item[0]);
    });
    $item.mouseout(mouseOutTimeout);
  }
  
  $("#header .hmenu>li>a").mouseover(mouseOverItem);
  $("#header .hmenu a[href=#]").click(function(){ return false; });
}

function setupRequestAppointmentForm() {
  $("#request_appointment_form .field_pair:even").addClass("even");
  $("#request_appointment_form").submit(function() {
    var missingRequired = false;
    if($.trim($("#request_appointment_form [name=subject]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=name]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=phone_number]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=email]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=subject]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=address1]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=city]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=state]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=zip]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=how_we_can_help]").val()) == "") {
      missingRequired = true;
    }
    if($.trim($("#request_appointment_form [name=referral]").val()) == "") {
      missingRequired = true;
    }
    if(missingRequired) {
      alert("Please fill out all fields with a red asterisk before submitting.");
      return false;
    }
  });
}

function setupPopups() {
  function showOverlay() {
    $("#overlay").show().fadeTo('medium', 0.5).width($(document).width()).height($(document).height());
  }
  function hideOverlay() {
    $("#overlay").fadeTo('medium', 0.0, function() { $(this).hide(); });
  }
  $(window).resize(function() {
    if($("#overlay").is(":visible")) {
      showOverlay();
    }
  });
  $("#overlay").fadeTo(0,0).hide();
	
	$('.popup .close').live("click", function(){
    var $popup = $(this).closest(".popup");
    var finalWidth = $popup.width();
    $popup.removeClass("open");
    hideOverlay();
    $popup.fadeOut();
    return false;
	});
  $(".popup").draggable({ handle: ".head" });
  $(".newsletter_signup").click(function() {
    showOverlay();
    $("#popup_mailing_list_signup").css({ left: $(document).width() / 2 - parseInt($("#popup_mailing_list_signup").css("width")) / 2, top: $(window).scrollTop() + 200 }).fadeIn();
    return false;
  });
  $("#newsletter_signup_form, #popup_newsletter_signup_form").submit(function() {
    $.post(window.location, {
      op: "newsletter",
      name: $(this).find("[name=name]").val(),
      email: $(this).find("[name=email]").val()
    }, function(data) {
      data = $.parseJSON(data);
      if(data.success) {
        alert("You're now signed up for our newsletter!");
        $("#popup_mailing_list_signup .close:first").click();
      }
      else {
        alert("There was an error signing you up: " + data.error);
      }
    });
    return false;
  });
  $("#book_exercises_form").submit(function() {
    var firstName = $(this).find("[name=first_name]").val(),
        lastName = $(this).find("[name=last_name]").val(),
        email = $(this).find("[name=email]").val();
    if(!firstName || !lastName || !email) {
      alert("Please enter all information before submitting.");
      return false;
    }
    $.post(window.location, {
      op: "book_exercises",
      'first_name': firstName,
      'last_name': lastName,
      'email': email
    }, function(data) {
      data = $.parseJSON(data);
      if(data.success) {
        alert("Thanks for signing up for book exercises! An email has been sent to you with a few links.");
      }
      else {
        alert("There was an error signing you up: " + data.error);
      }
    });
    return false;
  });
}

$(document).ready(function() {
  setupMenu();
  setupRequestAppointmentForm();
  setupPopups();
  $("#commentform input[type=submit]").addClass("button");
  $("select").addClass("round_sb").sb();
});

$(window).load(function() {
  $(".player").each(function() {
    flowplayer(this, {
      src: "/wp-content/themes/charschan/videos/flowplayer-3.2.5.swf",
      wmode: "transparent"
    }, {
      clip: {
        autoBuffering: true,
        autoPlay: false
      }
    });
  });
  $(".success_stories ul").marquee();
  $(".stretch").stretch();
  $(".submenu").css("visibility", "visible").hide();
  
  // fix sidebar padding based on the image
  
  $(".sidebar").css("padding-bottom", Math.max(parseInt($(".sidebar").css("padding-bottom")), $("#footer .body_accent").height()));
  if($("#body .c1").innerHeight() > $("#body .c2").innerHeight()) {
    $("#footer .body_accent").hide();
    $(".sidebar").css("padding-bottom", "");
  }
  
  // Fix issues for a growing footer
  $("#footer").css("margin-top", -$("#footer").height());
  $("#footer_pad").css("padding-bottom", $("#footer").height());
  $(".body_accent").css("top", -$(".body_accent").height() - parseInt($("#footer").css("padding-top")));
  
  $(".submit").click(function() {
    $(this).closest("form").submit();
    return false;
  });
});
