
$(function () {

	$('.date_has_event').each(function () {
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 500;
		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $(this);
		var popup = $('.events ul', this).css('opacity', 0);
   
    
		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
    
      var col = $(this).parent().children().index($(this));
      var row = $(this).parent().parent().children().index($(this).parent()); 
      
      //count the number of li  
      var count = $('li', this).size();     
      var h = $( "ul", this).height(); 

			// 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(row == 1 && count >= 4){
          var space_h = h - 360;
          $("#space").animate({"height": "+="+space_h}, 500);
        }else if(row == 2 && count >= 3){
          var space_h = h - 300;
          $("#space").animate({"height": "+="+space_h}, 500);
        }else if(row == 3 && count >= 3){
          var space_h = h - 260;
          $("#space").animate({"height": "+="+space_h}, 500);
        }
				// reset position of popup box
				popup.css({
					top: -8,
					left: -216,
					display: 'inline' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
      
     // $("#space").css("height","100px");
     // $("#space").css("clear","both");
           
		}).mouseout(function () {
      
      var col = $(this).parent().children().index($(this));
      var row = $(this).parent().parent().children().index($(this).parent());

      var count = $('li', this).size(); 
      var h = $( "ul", this).height(); 
      
			// 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;
        
        if(row == 1 && count >= 4){
           var space_h = h - 360;
           $("#space").animate({"height": 0}, 1000);
        }else if(row == 2 && count >= 3){
          var space_h = h - 300;
          $("#space").animate({"height": 0}, 1000);
        }else if(row == 3 && count >= 3){
          var space_h = h - 260;
          $("#space").animate({"height": 0}, 1000);
        }
        
				popup.animate({
					opacity: 0
				}, 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');
				});
			}, hideDelay);
		});
	});
});
