(function($) {

	$.fn.calendar = function(options) {
		var opts = $.extend({}, $.fn.calendar.defaults, options);
		$this = $(this);
		$options = opts;

		return this.each(function() {
			$.fn.calendar.load();
		});

	};

	$.fn.calendar.load = function(options) {
		var ajax_options = $.extend({}, $.fn.calendar.ajax_defaults, options)

		$.ajax({
			url: $options.ajax_url,
			type: "get",
			data: {method: '_generate_calendar', year: ajax_options.year, month: ajax_options.month},
			success: function(data) {
				if(data) {
					$this.html(data);
					$.fn.calendar.observe();
				}
			}
		});
	};

	$.fn.calendar.observe = function() {
		if($options) {
			$.each($options, function(k, e) {
				if(k != 'ajax_url') {
					$(e).bind('click', function(ev) {
						ev.stopPropagation();

						var this_id = $(this).attr('id');
						var current_year = parseInt($($options.calendar_container + ' ' + $options.previous_year).parent('th').next('th').attr('title'));
						var current_month = parseInt($($options.calendar_container + ' ' + $options.previous_month).parent('th').next('th').attr('title'));

						// what was clicked
						if(this_id.match(/^prev/) && this_id.match(/year$/)) {
							current_year = current_year-1;
						}
						else if(this_id.match(/^next/) && this_id.match(/year$/)) {
							current_year = current_year+1;
						}
						else if(this_id.match(/^prev/) && this_id.match(/month$/)) {
							if(current_month == 1) {
								current_year = current_year-1;
							}
							current_month = (current_month == 1) ? 12 : (current_month - 1);
						}
						else if(this_id.match(/^next/) && this_id.match(/month$/)) {
							if(current_month == 12) {
								current_year = current_year+1;
							}
							current_month = (current_month == 12) ? 1 : (current_month + 1);
						}

						$.fn.calendar.load({year: current_year, month: current_month});
					});
				}
			});

			/* events tooltip */
			$($options.calendar_container + ' td a').hover(function(ev) {
				var $tooltip_link = $(this),
					$tooltip = $('<span id="calendar_tooltip">&nbsp;</span>').appendTo($tooltip_link),
					tooltip_value = $tooltip_link.attr('title'),
					link_pos_left = $tooltip_link.offset().left + $tooltip_link.width(),
					link_pos_top = $tooltip_link.offset().top,
					mouse_pos_left = ev.pageX,
					mouse_pos_top = ev.pageY,
					arrow_pos_right = 25,
					arrow_pos_top = 40;

				$tooltip_link.attr('title', '');
				$tooltip.text(tooltip_value).append('<span>&nbsp;</span>').end().css({
					'right' : (link_pos_left - mouse_pos_left) - arrow_pos_right,
					'top' : (mouse_pos_top - link_pos_top) - arrow_pos_top
				}).show();

				$tooltip_link.bind('mousemove', function(ev) {
					mouse_pos_left = ev.pageX;
					mouse_pos_top = ev.pageY;
					$tooltip.css({
						'right' : (link_pos_left - mouse_pos_left) - arrow_pos_right,
						'top' : (mouse_pos_top - link_pos_top) - arrow_pos_top
					});
				});

			}, function() {
				var $tooltip_link = $(this),
					$tooltip = $tooltip_link.find('#calendar_tooltip'),
					tooltip_value = jQuery.trim($tooltip.text());

				$tooltip_link.unbind('mousemove');
				$tooltip_link.attr('title', tooltip_value);
				$tooltip.remove();
			});
		}

	}

	$.fn.calendar.stop_observe = function() {

	}

	$.fn.calendar.defaults = {
		ajax_url			: '',
		calendar_container	: '#calendar',
		previous_year		: '#prev_year',
		next_year			: '#next_year',
		previous_month		: '#prev_month',
		next_month			: '#next_month'
	};

	$.fn.calendar.ajax_defaults = {
		year	:	'',
		month	:	''
	};

})(jQuery);
