function extractId(longId)
{
  idInfo = longId.split("_");
  return idInfo[idInfo.length - 1];
}

function activateBar(color, defaultColor)
{
  $("#services_" + defaultColor).addClass("hidden");
  $("#services_" + color).removeClass("hidden");
  return false;
}

function deactivateBar(color, defaultColor)
{
  if (color != defaultColor)
    $("#services_" + color).addClass("hidden");
  $("#services_" + defaultColor).removeClass("hidden");
  return false;
}

function generateEmails()
{
  $(".mailer").each( function()
  {
    shortId = extractId( $(this).attr("id") );
    $(this).html( '<a href="mailto:' + shortId + '@postnet.com">' + shortId + '@postnet.com</a>' );
  });
}

$(document).ready( function()
{

  generateEmails();  

  $("#container").corner();
  $(".store_listing").corner();
  $("#map_frame").corner();
  $(".admin_form").corner();
  $(".framed").corner();

  defaultColor = $("#active_color").html();

  $(".nav_button").mouseout( function()
  {
    id = extractId($(this).attr("id"));
    deactivateBar(id, defaultColor);
    return false;
  });

  $(".service_bar").mouseover( function()
  {
    id = extractId($(this).attr("id"));
    activateBar(id, defaultColor);
    return false;
  });

  $(".service_bar").mouseout( function()
  {
    id = extractId($(this).attr("id"));
    deactivateBar(id, defaultColor);
    $(".sub-nav_menu-item").removeClass("sub-nav_active");
    return false;
  });

  $(".sub-nav_menu-item").mouseover( function()
  {
    id = extractId($(this).attr("id"));
    activateBar(id, defaultColor);
    $(".sub-nav_menu-item").removeClass("sub-nav_active");
    $(this).addClass("sub-nav_active");
    return false;
  });

  $(".sub-nav_menu-item").mouseout( function()
  {
    id = extractId($(this).attr("id"));
    deactivateBar(id, defaultColor);
    return false;
  });

  $("#map_switch").click( function()
  {
    if ( $("#map").is(":visible") )
    {
      $("#map_switch").html("open map"); 
      $("#map").slideUp();
    }
    else
    {
      $("#map_switch").html("close map"); 
      $("#map").slideDown();
    }
  });

  $(".qanda_toggle").click( function()
  {
    if ( $(".qanda_question").is(":visible") )
    {
      $(".qanda_question").hide();
      $(".qanda_answer").show();
      $(".qanda_toggle").html("Show the question.");
    }
    else
    {
      $(".qanda_question").show();
      $(".qanda_answer").hide();
      $(".qanda_toggle").html("See the answer.");
    }
  });

  $(".header").click( function()
  {
    shortId = extractId( $(this).attr("id") );
    if ( $("#content_" + shortId).is(":visible") )
    {
      if ($("#content_" + shortId).hasClass("floaters"))
        $("#content_" + shortId).hide();
      else
        $("#content_" + shortId).slideUp("slow");
      $("#toggle_" + shortId).html('<img src="/img/icons/add.png" />');
    }
    else
    {
      if ($("#content_" + shortId).hasClass("floaters"))
        $("#content_" + shortId).show();
      else
        $("#content_" + shortId).slideDown("slow");
      $("#toggle_" + shortId).html('<img src="/img/icons/delete.png" />');
    }
  });

});
