(function($) { 
   
	function calendarWidget(el, params) { 
		
		var now   = new Date();
		var thismonth = now.getMonth();
		var thisyear  = now.getFullYear();
		
		var opts = {
			month: thismonth,
			year: thisyear
		};
		
		$.extend(opts, params);
		
		var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
		var dayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
		month = i = parseInt(opts.month);
		year = parseInt(opts.year);
		var m = 0;
		var table = '';
			// next month
			if (month == 11) {
				var next_month = '<a href="/events/summary/' + (year + 1) + '/1/" title="' + monthNames[0] + ' ' + (year + 1) + '">&raquo;</a>';
			} else {
				var next_month = '<a href="/events/summary/' + (year) + '/' + (month + 2) + '/" title="' + monthNames[month + 1] + ' ' + (year) + '">&raquo;</a>';
			}
				
			// previous month
			if (month == 0) {
				var prev_month = '<a href="/events/summary/'+(year - 1)+'/12/" title="' + monthNames[11] + ' ' + (year - 1) + '">&laquo;</a>';
			} else {
				var prev_month = '<a href="/events/summary/'+ (year) + '/' + (month) + '/" title="' + monthNames[month - 1] + ' ' + (year) + '">&laquo;</a>';
			}

			table += ('<div class="calendar-title"><span class="nav-prev">'+ prev_month +'</span>&nbsp;&nbsp;');
			table += ('<span id="current-month">'+monthNames[month]+' '+year+'</span>');
			table += ('&nbsp;&nbsp;<span class="nav-next">'+ next_month +'</span></div>');
			table += ('<input id="data-year" value="'+year+'" style="display:none;" />');
			table += ('<input id="data-month" value="'+(month+1)+'" style="display:none;" />');
			table += ('<table class="calendar-month " ' +'id="calendar-month'+i+' " cellspacing="0">');	
		
			table += '<tr>';
			
			for (d=0; d<7; d++) {
				table += '<th class="weekday">' + dayNames[d] + '</th>';
			}
			
			table += '</tr>';
		
			var days = getDaysInMonth(month,year);
            var firstDayDate=new Date(year,month,1);
            var firstDay=firstDayDate.getDay();
			
			var prev_days = getDaysInMonth(month,year);
            var firstDayDate=new Date(year,month,1);
            var firstDay=firstDayDate.getDay();
			
			var prev_m = month == 0 ? 11 : month-1;
			var prev_y = prev_m == 11 ? year - 1 : year;
			var prev_days = getDaysInMonth(prev_m, prev_y);
			firstDay = (firstDay == 0 && firstDayDate) ? 6 : firstDay;
	
			var i = 0;
			var extraCounter = 0;
            for (j=1;j<=42;j++){
			  var extraClass = "";
			  if(j%7==6 || j%7==0) {
				var extraClass = " weekend";
			  }
              if ((j<firstDay)){
                table += ('<td class="other-month'+extraClass+'"><span class="day">'+ (prev_days-firstDay+j+1) +'</span></td>');
			  } else if ((j>=firstDay+getDaysInMonth(month,year))) {
				i = i+1;
				extraCounter = extraCounter+1;
				table += ('<td class="other-month'+extraClass+'"><span class="day">'+ i +'</span></td>');
              }else{
				if(thisyear == year && thismonth == month){
					if((j-firstDay+1) == now.getDate()){
						extraClass += " today";
					}
				}
				table += ('<td class="current-month day'+(j-firstDay+1)+extraClass+'"><span class="day"><a href="/events/summary/'+year+'/'+(month+1)+'/'+(j-firstDay+1)+'">'+(j-firstDay+1)+'</a></span></td>');
              }
              if (j%7==0) {
				table += ('</tr>');
				if(extraCounter > 0){
					break;
				}
			  }
			  
            }

            table += ('</table>');

		el.html(table);
		
		var theLink = $(el).parent().parent().find('.all-month-events');
		theLink.find('span').html('All '+monthNames[month]+' events');
		theLink.attr('href','/events/summary/'+year+'/'+(month+1));
	}
	
	function getDaysInMonth(month,year)  {
		var daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];
		if ((month==1)&&(year%4==0)&&((year%100!=0)||(year%400==0))){
		  return 29; //leap year
		}else{
		  return daysInMonth[month];
		}
	}
	
	
	function finishIt(calObj){
		$('.nav-prev a').click(function(){
			moveCal(calObj, $(this).attr("href"));
			return false;
		});
		$('.nav-next a').click(function(){
			moveCal(calObj, $(this).attr("href"));
			return false;
		});		
	}
	
	function updateLinks(calObj, month){
		var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	}
	
	function moveCal(calObj, hrefVal){
		var posStart = hrefVal.indexOf("/events/summary/");
		var hrefVal = hrefVal.substring("/events/summary/".length + posStart);
		var firstSlash = hrefVal.indexOf("/");
		var yearVal = hrefVal.substring(0,firstSlash);
		var monthVal = hrefVal.substring(firstSlash+1,hrefVal.indexOf("/",firstSlash+1)) - 1;
		$(calObj).fadeOut(function(){
			$(this).calendarWidget({
				month: monthVal,
				year: yearVal
			})
			$(this).fadeIn();
		});
		
	}
	
	
	// jQuery plugin initialisation
	$.fn.calendarWidget = function(params) {    
		calendarWidget(this, params);
		applyEvents(this, params);
		//applyMeetings(this, params);
		var calObj = $(this);
		$(this).find('.date_has_event').each(function () {
			applyShindigPopups(calObj,$(this));
		});
		/*$(this).find('.date_has_meeting').each(function () {
			applyShindigPopups(calObj,$(this));
		});
		$(this).find('.date_has_event_and_meeting').each(function () {
			applyShindigPopups(calObj,$(this));
		});*/		
		finishIt(this);
		return this; 
	}; 

	
	function applyEvents(calObj, params){
	
		var now   = new Date();
		var thismonth = now.getMonth();
		var thisyear  = now.getFullYear();
		
		var opts = {
			month: thismonth,
			year: thisyear
		};
		
		$.extend(opts, params);
		var requestUrl = '/events/ajaxGetEvents/'+opts.year+'/'+(opts.month+1)+'/';
		
		$.ajax({
			url: requestUrl,
			dataType: 'json',
			async: false, //blocks until finished
			success: function(data){
				$.each(data, function(i,item){
					var event = item['Event'];
					if(opts.year == event['date'].substring(0,4) && opts.month == parseInt(event['date'].substring(5,7))-1){
						var e_day = $(calObj).find('td.day' + parseInt(event['date'].substring(8,10)));
						var has_existing = false;
						
						if($(e_day).hasClass('date_has_event')){
							has_existing = true;
						/*
						} else if($(e_day).hasClass('date_has_meeting')){
							has_existing = true;
							$(e_day).removeClass('date_has_meeting');
							$(e_day).addClass('date_has_event_and_meeting');
						} else if($(e_day).hasClass('date_has_event_and_meeting')){
							has_existing = true;
						*/
						} else {
							$(e_day).addClass('date_has_event');
						}
						
						if(has_existing){
							var e_list = $(e_day).find('ul');
							var new_html = $(e_list).html();
							new_html += '<li class="event">';
							new_html += '<span class="title"><a href="/events/view/'+ event['id'] + '">'+ event['title'] + '</a></span>';
							new_html += '<span class="date">'+ event['dateNice'] + '</span>';
							new_html += '<span class="blurb">'+ event['blurb'] + '</span>';
							new_html += '</li>';						
							$(e_list).html(new_html);
						} else {
							var new_html = $(e_day).html();
							new_html += '<div class="shindigs">';
							new_html += '<ul style="opacity: 0;">';
							new_html += '<li class="event">';
							new_html += '<span class="title"><a href="/events/view/'+ event['id'] + '">'+ event['title'] + '</a></span>';
							new_html += '<span class="date">'+ event['dateNice'] + '</span>';
							var shortBlurb = event['blurb'];
							if(shortBlurb.length > 25){
								shortBlurb = shortBlurb.substring(0,shortBlurb.indexOf(' ',25)) + "...";
							}
							new_html += '<span class="blurb">'+ shortBlurb + '</span>';
							new_html += '</li>';
							new_html += '</ul>';
							new_html += '</div>';
							$(e_day).html(new_html);
						}
					}
				});
				
				$(calObj).find('td .shindigs ul').find('li').removeClass('last');
				$(calObj).find('td .shindigs').find('ul li:last').addClass('last');
			}
		});
		
	}
	/*
	function applyMeetings(calObj, params){
	
		var now   = new Date();
		var thismonth = now.getMonth();
		var thisyear  = now.getFullYear();
		
		var opts = {
			month: thismonth,
			year: thisyear
		};
		
		$.extend(opts, params);
		
		$.ajax({
			url: '/meetings/ajaxGetMeetings/'+opts.year+'/'+(opts.month+1)+'/',
			dataType: 'json',
			async: false, //blocks until finished
			success: function(data){
				$.each(data, function(i,item){
					var meeting = item['Meeting'];
					//alert(meeting['title'] + ' on ' + meeting['date']);
					if(opts.year == meeting['date'].substring(0,4) && opts.month == parseInt(meeting['date'].substring(5,7))-1){
						var e_day = $(calObj).find('td.day' + parseInt(meeting['date'].substring(8,10)));
						var has_existing = false;
						
						if($(e_day).hasClass('date_has_meeting')){
							has_existing = true;
						} else if($(e_day).hasClass('date_has_event')){
							has_existing = true;
							$(e_day).removeClass('date_has_event');
							$(e_day).addClass('date_has_event_and_meeting');
						} else if($(e_day).hasClass('date_has_event_and_meeting')){
							has_existing = true;
						} else {
							$(e_day).addClass('date_has_meeting');
						}
						
						if(has_existing){
							var e_list = $(e_day).find('ul');
							var new_html = $(e_list).html();
							new_html += '<li class="meeting">';
							new_html += '<span class="title"><a href="/meetings/view/'+ meeting['id'] + '">'+ meeting['title'] + '</a></span>';
							new_html += '<span class="date">'+ meeting['dateNice'] + '</span>';
							new_html += '<span class="blurb">'+ meeting['blurb'] + '</span>';
							new_html += '</li>';						
							$(e_list).html(new_html);
						} else {
							var new_html = $(e_day).html();
							new_html += '<div class="shindigs">';
							new_html += '<ul style="opacity: 0;">';
							new_html += '<li class="meeting">';
							new_html += '<span class="title"><a href="/meetings/view/'+ meeting['id'] + '">'+ meeting['title'] + '</a></span>';
							new_html += '<span class="date">'+ meeting['dateNice'] + '</span>';
							var shortBlurb = meeting['blurb'];
							if(shortBlurb.length > 25){
								shortBlurb = shortBlurb.substring(0,shortBlurb.indexOf(' ',25)) + "...";
							}
							new_html += '<span class="blurb">'+ shortBlurb + '</span>';
							new_html += '</li>';
							new_html += '</ul>';
							new_html += '</div>';
							$(e_day).html(new_html);
						}
					}
				});
				
				$(calObj).find('td .shindigs ul').find('li').removeClass('last');
				$(calObj).find('td .shindigs').find('ul li:last').addClass('last');
			}
		});
		
	}
	*/
	function applyShindigPopups(calObj, divObj){
		
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

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

		var trigger = $(divObj);
		var popup = $('.shindigs ul', $(divObj)).css('opacity', 0);

		// 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);

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

				// reset position of popup box
				popup.css({
					bottom: 20,
					left: -76,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					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;
				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 doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	}
	
	
})(jQuery);

jQuery.fn.extend({
  slideRightShow: function() {
    return this.each(function() {
        $(this).show('slide', {direction: 'right'}, 1000);
    });
  },
  slideLeftHide: function() {
    return this.each(function() {
      $(this).hide('slide', {direction: 'left'}, 1000);
    });
  },
  slideRightHide: function() {
    return this.each(function() {
      $(this).hide('slide', {direction: 'right'}, 1000);
    });
  },
  slideLeftShow: function() {
    return this.each(function() {
      $(this).show('slide', {direction: 'left'}, 1000);
    });
  }
});



