// preload images

var home = new Image(59, 58)
home.src = "jpg/home_but_roll.jpg";

var test = new Image(59, 58)
test.src = "jpg/testimonial_but_roll.jpg";

var blog = new Image(59, 58)
blog.src = "jpg/blog_but_roll.jpg";

var news = new Image(59, 58)
news.src = "jpg/news_but_roll.jpg";

var ic = new Image(59, 58)
ic.src = "jpg/forum_but_roll.jpg";

var links = new Image(59, 58)
links.src = "jpg/links_but_roll.jpg";

var contact = new Image(59, 58)
contact.src = "jpg/contact_roll.jpg";

// global timer variable
var close_timer;

function open_submenu(button, left)
{
  // cancel the close timer
  clearTimeout(close_timer);
  
  // create submenu div
  $("body").append("<div id='drop-down-menu'></div>");

  // position div
  $("#drop-down-menu").css("left", left);
  
  $("#drop-down-menu").get(0).from_button = button;
  
  // which button is it?
  switch(button)
  {
    case "accommodation":
      $("#drop-down-menu").html("<a href='accommodation.php'>Where you sleep</a><a href='where-you-eat.php'>Where you eat</a><a href='where-you-relax.php'>Where you relax</a><a href='gardens.php'>Gardens</a>");
      menu_height = 92;
      break;
    case "what-we-offer":
      $("#drop-down-menu").html("<a href='weight-loss.php'>Weight loss</a><a href='graduate-weekends.php'>Graduate Weekends</a><a href='exercise.php'>Exercise</a><a href='nutrition.php'>Nutrition</a><a href='therapies.php'>Therapies</a>");
      menu_height = 115;
      break;
    case "typical-day":
      $("#drop-down-menu").html("<a href='typical-day.php'>Sample programme</a><a href='sample-menu.php'>Sample menu</a><a href='what-is-included.php'>What is included?</a>");
      menu_height = 69;
      break;
    case "team":
      $("#drop-down-menu").html("<a href='team.php'>Hypnotherapists</a><a href='team.php?dep=2'>Therapists</a><a href='team.php?dep=3'>Fitness</a><a href='team.php?dep=4'>Nutrition</a><a href='team.php?dep=5'>Expert Speakers</a><a href='team.php?dep=6'>Support</a>");
      menu_height = 138;
      break;
    case "faq":
      $("#drop-down-menu").html("<a href='faq.php'>Exercise &amp; fitness</a><a href='diet.php'>Diet</a><a href='booking.php'>Booking procedure</a>");
      menu_height = 69;
      break;
    default:
      $("#drop-down-menu").remove();
      return false;
      break;
  }
  
  $("#drop-down-menu").bind("mouseenter", function()
  {
    // cancel the close timer
    clearTimeout(close_timer);
    
    $("#drop-down-menu").bind("mouseleave", function()
    {
      close_timer = setTimeout("close_submenu()", 500);
    });
  });

  // animate menu
  $("#drop-down-menu").animate({height: menu_height}, 200, function()
  {

  });
}

function close_submenu()
{
  if($("#drop-down-menu").length)
  {
    $("#drop-down-menu").animate({height: 0}, 200, function()
    {
      $(this).remove();
    });
  }
}

// main menu rollovers
$("document").ready(function()
{
  // add rollover functinoality to menu buttons
  $("#main-menu a").bind("mouseenter", function()
  {
    // get position of button
    var top = 150;
    var left = $(this).offset().left;
    var button = $(this).attr("id");
    
    // is a submenu alrady open
    if($("#drop-down-menu").length)
    {
      // is this button the one that opened the menu
      if($("#drop-down-menu").get(0).from_button != button)
      {
        $("#drop-down-menu").animate({height: 0}, 200, function()
        {
          $(this).remove();
          open_submenu(button, left);
        });
      }
      else open_submenu(button, left);
    }
    else open_submenu(button, left);
  });
  
  $("#main-menu a").bind("mouseleave", function()
  {
    close_timer = setTimeout("close_submenu()", 500);
  });
  
  // remove the microsoft ads noscript image
  $("img#ms-ads-noscript").remove();
});