(function($) {
	// init
	$.fn.cmCal = function (options) {
		if ($.fn.cmCal.defaults.showSwitch) {
			$(this).after($('<p id="calendarSwitcher" style="margin: 3em 0; text-align: center;"><a href="big">Big</a> | <a href="small">Small</a> | <a href="list">List</a></p>'));
			$('p#calendarSwitcher > a')
				.bind('click', function (e) {
					$.fn.cmCal.changeState($(this).attr('href'));
					eraseCookie('cm_cal_state');
					createCookie('cm_cal_state', $(this).attr('href'), 365);
					e.preventDefault();
				});
			var cm_cal_state = readCookie('cm_cal_state');
			if(cm_cal_state != null) {$.fn.cmCal.changeState(cm_cal_state);}
		}
		return this.each(function (i) {
			// grab/extend the options
			cmCalOptions = $.extend({}, $.fn.cmCal.defaults, options);
			$('li.event, li.event ol.events li', this).each(function (i) {
				$(this).data('flyoutID', cmCalOptions.uniqueID + i);
				var flyoutHeight	= cmCalOptions.height;
				var flyoutWidth		= cmCalOptions.width;
				var flyout			= $(this)
										.clone()
										.dialog({
											 autoOpen	: false
											,buttons	: {"Close" : function() {$(this).dialog("close");}}
											,minWidth	: cmCalOptions.width
											,width		: cmCalOptions.width
											/*
											,position	: [
															$(this).offset().left - (flyoutWidth / 3)
														   ,$(this).offset().top - (flyoutHeight / 2)
														  ]
											*/
										})
										.attr({'id'		: cmCalOptions.uniqueID + i})
										.addClass('flyout');
				var eventContainer = this;
				if (!$('.date', eventContainer).length) {
					eventContainer = $($(this).parents('li.event').get(0));
				}
				var date = $('.date', eventContainer).text();
				var flyoutTitleBar	= $(flyout.siblings('.ui-dialog-titlebar').get(0));
				var monthTitleText	= "Events on " + date;
				var monthTitle		= $("<h1 class='monthTitle'></h1>").prepend(monthTitleText);
				flyoutTitleBar.prepend(monthTitle);
				
				if (!$('> ol.events', flyout).length) {
					flyout.addClass('single');
				}
				$('a.date, a.moreLink', flyout).remove();
			});			
			$('li.event a, li.event ol.events li h3', this)
				.bind(cmCalOptions.trigger, function (e) {
					$.fn.cmCal.showFlyout($(this));
					e.preventDefault();
			});
			/* *** */

			//var re = /.*eid=(\d+).*/;
			//var m = re.exec(location.hash);
			//if (m.length > 1) {
			//	$('.eid' + m[1] + ' a').trigger(cmCalOptions.trigger);
			//}

			/* *** */
		});
	};
	// open the flyout
	$.fn.cmCal.showFlyout = function (trigger) {
		var parent			= $(trigger.parents('li').get(0));
		var thisFlyout		= $('#'+parent.data('flyoutID'));
		var otherFlyouts	= $('div[id^="'+cmCalOptions.uniqueID+'"]').not(thisFlyout);
		var flyoutDisplay	= function () {
			otherFlyouts.dialog('close');
			thisFlyout.dialog('open');
		};
		setTimeout(flyoutDisplay, cmCalOptions.delay);
	};
	// close the flyout
	$.fn.cmCal.hideFlyout = function (trigger) {
		var parent		= $(trigger.parents('li').get(0));
		var flyout		= $('#'+parent.data('flyoutID'));
		var flyoutHide	= function () {flyout.dialog("close");}
		setTimeout(flyoutHide, cmCalOptions.delay);
	}
	// change the calendar state
	$.fn.cmCal.changeState = function (state){
		$('div.calendar').attr({'class':'calendar'});
		$('div.calendar').addClass(state);
	}
	// defaults
	$.fn.cmCal.defaults = {
		 delay		: 300
		,height		: 400
		,showSwitch : false
		,trigger	: 'click'
		,uniqueID	: 'cmCalEvent'
		,width		: 500
	};
})(jQuery);