/*
 * @package WP Simple Booking Calendar
 *
 * Copyright (c) 2010 Lennart Visscher
 */

jQuery(function($) {
	
	$('div.sbc-calendar-wrapper').each(function() {
		var $calendar = $(this);
		
		function getCurrentMonth() {
			return $('.sbc-navigation select[name="sbcMonth"]', $calendar).val();
		}
		
		function getCurrentYear() {
			return $('.sbc-navigation select[name="sbcYear"]', $calendar).val();
		}
		
		function ajaxCalendarUpdate(operation) {
			var ajaxUrl = $('form', $calendar).attr('action');
			var data = {
				action: 'calendarNavigation',
				operation: operation,
				month: getCurrentMonth(),
				year: getCurrentYear(),
				id: $('form input[name="sbcId"]', $calendar).val(),
                view: $('form input[name="view"]', $calendar).val()
			};
			$('div.sbc-loader', $calendar).addClass('sbc-loader-visible');
			$.post(ajaxUrl, data, function(response) {
				$calendar.find('#sbc-calendar').replaceWith(response);
                maxHeight = 0;
                $('#sbc-calendar').find('.sbc-calendar-month table').each(function(i, e) {
                    if (e.offsetHeight > maxHeight) {
                        maxHeight = e.offsetHeight;
                    }
                });
               $('#sbc-calendar').find('.sbc-calendar-month table').each(function(i, e) {
                    maxHeight += 2;
                    e.style.height = maxHeight + 'px';
                });
				$('.sbc-navigation select', $calendar).bind('change', changeMonthOrYear);
			});
		}
		
		// Prev/next month
		$('a.sbc-prev-month, a.sbc-next-month', $calendar).live('click', function(event) {
			event.preventDefault();
			ajaxCalendarUpdate($(this).is('.sbc-prev-month') ? 'prevMonth' : 'nextMonth');
		});
		
		// Custom month/year
		function changeMonthOrYear () {
			ajaxCalendarUpdate('date');
		}
		$('.sbc-navigation select', $calendar).bind('change', changeMonthOrYear);
	});
	
});
