(function($) {
  var subMenuHeights = [];
  var addrDetail='ma'+"il";

  addrDetail += 'to:';
  
  function toggleMenu(m, now) {
    // Animate contained sub-menu
    var sub = $('.submenu',m);
    if (sub) {
      // Expand or collapse?
      var targetH = subMenuHeights[parseInt($(sub).attr('id'),10)];
      if ($(sub).css('position')=='relative') {
        // Hide
        $(m).removeClass('expanded');
        $(sub).animate({height:0},{duration:1000, complete:function(){$(this).css({height:''+targetH+'px',position:'absolute',top:'-10000px'});}})
        //$(sub).css({});
      }
      else {
        $(m).addClass('expanded');
        if (now==true) {
          $(sub).css('height',targetH+'px').css('position','relative').css('top','0') ;
        }
        else {
          $(sub).css('height','0').css('position','relative').css('top','0') ;
          $(sub).animate({height:targetH+'px'},1000);
        }
      }
    }
  }
  
  addrDetail += 'martin';
  function expandMenu(topItems,menu,now) {
    // Collapse all other menus
    var selHref = $('>a',menu).attr('href');
    $(topItems).each(function(i,m) {
      if ($('>a',m).attr('href')==selHref) {
        toggleMenu(m,now);
      }
      else {
        $(m).removeClass('expanded');
      }
    });
  }
  addrDetail += '\x40';

  function initialiseMenus() {
    // Work out the initial height of all sub-menus and give each a unique ID
    var submens = $('#menu .submenu').each(function(i,e) {
      $(e).attr('id',''+subMenuHeights.length+'sid');
      subMenuHeights.push($(e).height());
    });
    // Where we are now
    var thisHref = (new String(document.location)).replace(/^https?\:\/\/[^\/]*/i,'');

    // Find all top-level menu items and decide whether any should be expanded
    var allItems = $('#menu li');
    var topItems = allItems.filter(function(i,e) {
      var t = $(e).parent().attr('id')=='menu';
      if (t) {
        // Replace click with 'expand' if we have a submenu
        if ($('.submenu',e).length>0)
          $('>a',e).addClass('doexpand');
      }
      return t;
    })
    
    // Decide which top-level item currently applies
    var matched, matchedLen=0;
    //alert(thisHref);
    for (var i=0;i<allItems.length;i++) {
      var href = $('a',allItems[i]).attr('href');
      if (href && href.indexOf(thisHref)==0 && (matched==null || href.length<matchedLen)) {
        matched = allItems[i];
        matchedLen = href.length;
      }
    }
    //alert("MATCHED : "+$('a',matched).attr('href'));
    
    // If this is *not* a top-level element then expand that element
    if (matched) {
      $(matched).addClass('selected');
      var menu;
      if ($(matched).parent().attr('id')=='menu')
        menu = matched;
      else {
        menu = $(matched).parents('li');
      }
      if (menu)
        expandMenu(topItems,menu,true);
    }
  }

  addrDetail += 'bontoft';
  addrDetail += '.com';



  $(function() {
    initialiseMenus();
    $('#menu .doexpand').click(function(ev) {
      toggleMenu($(ev.target).parent());
      ev.stopPropagation();
      ev.preventDefault();
    });
    // Fad ein the body
    $('.contentbox').fadeIn(500);
    
    $('.addr').html('<a href="'+addrDetail+'">'+addrDetail.replace(/.*\:/,'')+'</a>');
  })
})(jQuery);
