$(document).ready(function () {
	
	/* Setup hovers */
	if($(".clickableElement").length  > 0)
		$(".clickableElement").ahover({opacity: 1, className: 'clickableElementHover'});

	var cssObj = {'padding': '15px', 'padding-top': '25px'};
	if($.browser.msie && $.browser.version < 7)
		cssObj['background-image'] = "url(/images/background-popup-IE.gif)";
	else
		cssObj['background-image'] = "url(/images/background-popup.png)";
	

	/* Add Scale9 to popups */
	$('.mainMenuPopup').scale9Grid({top:13,bottom:12,left:12,right:12},cssObj);


	/* Do the main menu stuff */
  $('.bubbleInfo').each(function () {
  
    // options
    var distance = -20;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var beingHidden = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.mainMenuPopup', this);
    
    if(!$.browser.msie)
    	$(popup).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function (event) {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);


      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;
		 
		  if(beingHidden) {
		  	popup.stop();
		  	beingHidden = false;
		  }

		/* Find the position of trigger */
		var targetPosition = findElementPosition(this);


	
		cssObj = { top: '+=' + distance + 'px'};
        
        if(!$.browser.msie)
        	cssObj['opacity'] = 1;

        // reset position of popup box
        popup.css({
          top: targetPosition[1] + 37,
          left: targetPosition[0] - 15,
          display: 'block' // brings the popup back in to view
        })
        // (we're using chaining on the popup) now animate it's opacity and position
        .animate(cssObj, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          beingHidden = false;
          popup.css({display: 'block'});
          shown = true;
        });
      }
    }).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;
        beingHidden = true;
        
        cssObj = { top: '-=' + distance + 'px'};
        
        if(!$.browser.msie)
        	cssObj['opacity'] = 0;
        
        
        popup.animate(cssObj, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
          beingHidden = false;
        });
      }, hideDelay);
    });
  });
});