$(function() {
	// options 
    var time = 300;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;	
	
	var trigger = $('a[rel=dropmenu1]');
    var popup = $('#dropmenu1').show();

	$(trigger.get(0)).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // if we're being shown, or already visible, we want to fold it back up
      if (beingShown || shown) {
      	// store the timer so that it can be cleared in the mouseover if required
		hideDelayTimer = setTimeout(function () {
		  hideDelayTimer = null;
		  shown = false;
		  popup.css({ visibility: "hidden" });
		}, hideDelay);
      } else {
        beingShown = true;
        // reset position of popup box
        popup.css({
		  visibility: "visible",
		  top: trigger.offset().top+15,
          left: trigger.offset().left
        })
		beingShown = false;
         shown = true;
      }
    });
	
    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        shown = false;
		popup.css({ visibility: "hidden" });
      }, hideDelay);
    });
});