
(function($) {
	
	$(document).ready(function() {
		
		$('div.events div.event div.title').livequery('click', function() {
			$(this).next().slideToggle().parents('div.events').find('div.details:visible').not( $(this).next() ).slideUp();
		});
		
		$.blockUI.defaults.fadeOut = true;
		$.blockUI.defaults.pageMessage = 'Loading...';

		$.extend( $.blockUI.defaults.pageMessageCSS, { 
			backgroundColor:	'#fff',
			color: 				'#000',
			border: 			'none',
			textAlign: 			'left',
			cursor:				'default'
		 });

		
		$('a.event').livequery('click', function() {
			$.ajax({
				url: 		$(this).attr('href'),
				type: 		'get',
				dataType: 	'html',
				success: 	function(response) {
					$.blockUI( response , { 
						top: 				'90px',
						width: 				'500px',
						height:				'auto',
						marginLeft: 		'-250px',
						border: 			'1px #999 solid'
					});

					$(document).keypress(function(event) {
						if (event.which == 27) {
							$.unblockUI();

							$(this).unbind('keypress');
						}
					}); 					
				}
			});
			
			return false;
		});


		$('form.event').livequery('submit', function() {
			$(this).ajaxSubmit({
				target: $('#calendar')
			});

			$.unblockUI();
			$(this).unbind('keypress');
	
			return false;
		});

		$('a.remove').click( function() {
			var clicker = this;

			$.ajax({
				url: 		$(this).attr('href'),
				success: 	function(response) {
					$('div#calendar').replaceWith( response );
				}
			});

			return false;
		});

		$('div.navigation').livequery('dblclick', function() {
			var url = $('div.navigation div.current a').attr('href') + '&calendar=true';

			$.ajax({
				url: 		url,
				type: 		'get',
				dataType: 	'html',
				success: 	function(response) {
					$.blockUI( response , { 
						top: 				'80px',
						width: 				'750px',
						height:				'auto',
						marginLeft: 		'-375px'
					});

					$(document).keypress(function(event) {
						if (event.which == 27) {
							$.unblockUI();

							$(this).unbind('keypress');
						}
					}); 					
				}
			});

			return false;
		});
	});
	
})(jQuery);

