/* div to select code for interest form dropdown
*/
function divToSelect(id) {
	var d  = document.getElementById(id);
	var as = d.getElementsByTagName('a');
	var o  = "<select name='"+id+"Options' ";
	    o += "onChange='location.href = this[this.selectedIndex].value'>";
	    o += "<option selected>What services interest you?</option>";
	for(var i=0;i<as.length;i++) {
		var a   = as[i];
		url = a.href; 
		txt = a.innerHTML;
		o  += "<option value='"+url+"'>"+txt+"</option>";
	} 
	o += "</select>";
	d.innerHTML = o;
}
/*
  Highlight the appropriate topnav item based on the 
  URL of the current page.
*/
$("#topnav").ready(function() {
  var activeElement = null;
  if (location.pathname == "/") {
    activeElement = $("#topnav a[href='/'] img");
  }
  else {
    activeElement = eval("$(\"#topnav a[href^='/" + location.pathname.split("/")[1] + "/'] img\")");
  }
  if (!activeElement.attr("src")) return;
  activeElement.attr("onmouseover", "");
  activeElement.attr("onmouseout", "");
  activeElement.attr("src", activeElement.attr("src").replace(/_off.jpg$/, "_on.jpg"));
});

/*
  Higlight the appropriate leftnav item based on the
  URL of the current page and collapse/expand the
  appropriate sub menus.
*/
$("#leftnav").ready(function() {
  $("#leftnav ul").hide();
  var activeElement = eval("$(\"#leftnav a[href='" + location.pathname.replace(/index\.asp$/, "") + "']\")");
  if (!activeElement.attr("href")) return;
  activeElement.addClass("active");
  activeElement.next("ul").show();
  activeElement.parents("ul").show();
});

$("document").ready(function() {
  // Fix background image caching problem
  if (jQuery.browser.msie) {
    try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
  }

  /*
    Set up the onclick event for any link marked with the
    apply-now class to show reservation number popup.
  */
  $("a.apply-now").click(function(e) {
    $("#reservation-num").css("left", $(this).offset().left + "px");
    $("#reservation-num").css("top", $(this).offset().top + "px");
    $("#reservation-num").show();
    if ($(this).parents("div#gutter").text()) {
      $("#reservation-num").addClass("wide");
    }
    else {
      $("#reservation-num").removeClass("wide");
    }
    return false;
  });
  
  /*
    Set up the onclick event for the yes/no radio buttons on
    the reservation number form.
  */
  $("#reservation-form input[name='have-reservation-num']").click(function(e) {
    if (!$("#reservation-form input[name='have-reservation-num']:checked").val().match(/^http:\/\//)) {
      options = "width=620,scrollbars=1,resizable=1,status=1,toolbar=1,location=1,menubar=1,directories=1";
    }
    window.open($("#reservation-form input[name='have-reservation-num']:checked").val(), "", options);
    $("#reservation-form input[name='have-reservation-num']:checked").attr("checked", false);
  });
  
  /*
    Set up the onmouseover event for any link marked with the
    whats-this class to show the reservation info popup.
  */
  $("a.whats-this").mouseover(function(e) {
    $("#reservation-point").css("top", parseInt($("#reservation-box").height() / 2) - 30 + "px");
    $("#reservation-box").css("left", "143px");
    $("#reservation-box").css("top", parseInt($("#reservation-num").height() / 2) - parseInt($("#reservation-box").height() / 2) + "px");
    $("#reservation-box").show();
  });
  
  /*
    Set up the onmouseout event for any link marked with the
    whats-this class to hide the reservation info popup.
  */
  $("a.whats-this").mouseout(function(e) {
    $("#reservation-box").hide();
  });
  
  /*
    Set up the onmouseout event for any link marked with the
    close-reservation class to hide the reservation info popup.
  */
  $("a.close-reservation").click(function(e) {
    $("#reservation-num").hide();
    return false;
  });
  
  /*
    Set up the onclick event for the continue application
    button on the consent form.
  */
  $("#agree-terms #continue-app").click(function(e) {
    if ($("#agree-terms #agree").attr("checked")) {
      document.location = $("#agree-terms #continue-app").attr("href");
    }
    else {
      alert("Please agree to the terms before continuing.");
    }
    return false;
  });
  
  /*
    Set up the onclick event for any tab links.
  */
  $("ul.tabs a").click(function(e) {
    $("#" + $("ul.tabs a.active").attr("id") + "-content").hide();
    $("ul.tabs a.active").removeClass("active");
    $("#" + $(this).attr("id") + "-content").show();
    $(this).addClass("active");
    return false;
  });
  
  /*
    Set up the onclick event for any tab links.
  */
  $("a.js-new-win").click(function(e) {
    window.open($(this).attr("href"), "", "scrollbars=1,resizable=1,status=1,toolbar=1,location=1,menubar=1,directories=1");
    return false;
  });
  
  /*
    Set up the onclick event for any link marked with the
    assumptions class to control the size of the new window.
  */
  $("a.assumptions").click(function(e) {
    window.open($(this).attr("href"), "", "width=620,scrollbars=1,resizable=1,status=1,toolbar=1,location=1,menubar=1,directories=1");
    return false;
  });
});