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

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

		var trigger = $(this);
		var popup = $('.events', this).css('display', 'none');

		$(popup.get(0)).click(function (event) { // dont re-display the box when you click on the popup
			event.stopImmediatePropagation();
		})

		/*
		$(popup.get(0)).mouseleave(function (event) { // if you mouse out of a popup, after timeOut seconds, hide it.
			//event.stopImmediatePropagation();
			popup.animate({
				bottom: '-=' + distance + 'px',
				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 doesnt do the job)
				popup.css('display', 'none');
			});
		})
		*/

		// set the mouseover and mouseout on both element -- CHANGED TO CLICK
		//$([trigger.get(0), popup.get(0)]).click(function () {
		$(trigger.get(0)).click(function (event) { // dont attach listeners to the popup, just the trigger! -- but it doesnt work
			$('.events').css('display', 'none'); // hide them all
			$('.active').removeClass('active');
			$(this).addClass('active');
			
			

			// now show the one you just clicked on
			if (popup.css('display')!='block' || popup.css('opacity')==0) {
				//alert("SHOW IT: display is: "+popup.css('display')+"  opacity: "+popup.css('opacity'));
				// show it
				popup.css({
					//top: -10,
					//left: -10,
					opacity: 1 // brings the popup back in to view
				})

				 
				.fadeIn(time);//using fade instead of animate
				// end show it
			}
			else {
				alert("SKIP IT: display is: "+popup.css('display')+"  opacity: "+popup.css('opacity'));
			}
		})
	});
});

