///////////////////////
//	CALENDAR.JS - REQUIRES JQUERY 3x
//	Please do not steal.
//	Every time you steal, An angry Scotsman punches a puppy in the face! A really cute puppy!

//WAIT FOR THE PAGE TO BE READY
$(document).ready(function(){

	Array.prototype.keyExists = function ( sTest ) {
		for (var i = 0; i < this.length; i++){
	             if ( this[i].key == sTest ){
	                  return this[i].text;
	             }
	       }
		return false;
	}

	isOpen = false;
	banClick = false;
	 
	var datesObj = { d : 0, m : 0, y : 0 };

	$('div#calendar div.datepicker').datepicker({
		dateFormat: 'DD, d MM yy',
		onSelect: function(dateText, inst) {
			if(isOpen == false && banClick == false){
				banClick=true;
				$('div#calendar').stop([]).animate({width:400}, 600, 'easeInOutExpo', function(){
					$('div#calendar div.content').show().css({opacity:0}).animate({opacity:1}, function(){
						banClick=false;
					});
				});
				$('div#calendar a.close').show().css({opacity:0}).animate({opacity:1});
				$('div#calendar p.closed').animate({opacity:0}, function(){
					$(this).hide();
				});
				isOpen = true;
			}
			//A day has been clicked, show new content
			var iMonth = inst.selectedMonth + 1;
			$('div#calendar div.content').html('<span class="footer">'+dateText+'</span>' + aExDates.keyExists( inst.selectedDay.toString() + iMonth.toString() + inst.selectedYear.toString() ) );
			
		},	
		beforeShowDay: function( thedate ){
				var iMonth = thedate.getMonth() + 1;
				if( aExDates.keyExists( thedate.getDate().toString() + iMonth.toString() + thedate.getFullYear().toString() ) ){ return [true,'ui-datepicker-event-day']; }
				return [false,''];
			}
	});

	$('div#calendar a.close').click(function(){
		if(isOpen == true && banClick == false){
			banClick=true;
			$('div#calendar div.content').animate({opacity:0}, function(){
				$(this).hide();
			});
			$(this).animate({opacity:0},function(){
				$(this).hide();
				$('div#calendar').stop([]).animate({width:290}, 600, 'easeInOutExpo', function(){
					$('div#calendar p.closed').show().animate({opacity:1}, function(){
						banClick=false;
					});
					isOpen = false;
				});
			});
		}
	});

// End jQuery goodness
});

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});