function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
$(document).ready(function(){
    MM_preloadImages('graphics/m-home_f2.gif','graphics/m-about_f2.gif','graphics/m-port_f2.gif','graphics/m-serv_f2.gif','graphics/m-contact_f2.gif');
    $("#menu a").each(function(i) {
      if (window.pageName && $(this).attr("href")==pageName) {
	  // This is the link to the current page, which we don't want to show up; we want it to show the _f2 image instead
	  $(this).addClass($(this).attr("id")+"-classHover")
	  // set the "ref" attribute to what was in the "href" attribute for future recall
	  .attr("ref",$(this).attr("href"))
	  .removeAttr("href");
      } else $(this).addClass($(this).attr("id")+"-class");
    })
    .bind("click",function(e){
	e.preventDefault();
	var thisID = $(this).attr('id');
	//alert('thisID='+thisID);
	$.get($(this).attr('href'),function(loadedHtml) {
	    // isolate the loaded data in the #content div
	    var $newContent = $("#content", loadedHtml);
	    var newBG = $("#bg", loadedHtml).attr("data");
	    //alert('content='+$newContent.html()+', bg='+newBG);
	    // put the new data into the current page's content div
	    if ($newContent.html() == null) return;
	    $("#content").html($newContent.html())
	    .css("background-image","url("+newBG+")");
	    setMenuItem(thisID);
	  });
    });
    bindMenuHover();
    /* $("#main").css("position","relative")
    .prepend("<div id='leftSpacer'>&nbsp;</div>") */
});

function bindMenuHover() {
    $("#menu a").unbind("mouseenter").unbind("mouseleave");
    $("#menu a").filter("a[href]")
     //.each(function(j){ alert('this='+$(this).attr("id")); }) 
    .hover(
	 function () { 
		$(this).addClass($(this).attr("id")+"-classHover");
		$(this).removeClass($(this).attr("id")+"-class");
	 }, 
	 function () { 
		$(this).addClass($(this).attr("id")+"-class");
		$(this).removeClass($(this).attr("id")+"-classHover");
	 }
    )
}

function setMenuItem(whichID) {
  //alert('setting menu '+whichID);
  // first reset all menu items
  $("#menu a").each(function(i) {
    $(this).removeClass($(this).attr("id")+"-classHover")
    .addClass($(this).attr("id")+"-class");
    if ($(this).attr("ref") != null) $(this).attr("href",$(this).attr("ref"));
    //alert("clearing "+$(this).attr("id")+', class='+$(this).attr("class")+', href='+$(this).attr("href"));
  });
  $("#"+whichID).removeClass(whichID+"-class")
  .addClass(whichID+"-classHover")
  .attr("ref",$("#"+whichID).attr("href"))
  .removeAttr("href");
  // reset the hover functions so they are based on the new link layout
  bindMenuHover();
  //$("#"+whichID).unbind("mouseover").unbind("mouseout");
  //alert("now this link's href="+$("#"+whichID).attr("href")+', ref='+$("#"+whichID).attr("ref")+', class='+$("#"+whichID).attr("class"));
}