$(document).ready(function(){
	currentID="0";
	clickedInput = false;
	$(".date").click(function(){
		currentID = $(this).attr("id").split("-")[1];
		clickedInput = true;
		$('#ui-datepicker-div').removeClass().addClass('currentCalendar-'+currentID);
	});

	
	$('body').click(function(){
		if (clickedInput == false && $("#ui-datepicker-div").css('display') == 'none')
			currentID = "0";
		clickedInput = false;
		$('#ui-datepicker-div').removeClass().addClass('currentCalendar-'+currentID);
	});
});
jQuery(function($) {
	$.datepicker._load = function(inst) {
		var id = inst.input.parents("tr")[0].id;
		if (id == "RRR") {
			$.getJSON(
				$.datepicker._get(inst, 'url'),
				{
					id: -1,
					month: inst.selectedMonth + 1, // JS 0=jan instead of CF 1=jan...
					year: inst.selectedYear
				},
				$.datepicker._loadCallBack //$.datepicker._updateDatepicker(inst, true)
			);
		} else {
			$.getJSON(
				$.datepicker._get(inst, 'url'),
				{
					id: id,
					month: inst.selectedMonth + 1, // JS 0=jan instead of CF 1=jan...
					year: inst.selectedYear
				},
				$.datepicker._loadCallBack
			);
		}
	},

	$.datepicker._loadCallBack = function(data) {
		var input = $('#' + data.ID).find("input.date")[0];
		var inst = $.datepicker._getInst(input);

		mydata = data;
		id = inst.id.split("-")[1];
		if (currentID == id)
			$.datepicker._updateDatepicker(inst, true);
	},

	/* Generate the date picker content. */
	$.datepicker._updateDatepicker = function(inst, loaded) {
		var id =inst.id.split("-")[1];
		var loaded = (loaded == null) ? false : loaded;

		var dims = {
			width : inst.dpDiv.width() + 4,
			height : inst.dpDiv.height() + 4
		};
		
		inst.dpDiv.empty().append(this._generateHTML(inst)).find('iframe.ui-datepicker-cover').css({
			width : dims.width,
			height : dims.height
		});
		
		var numMonths = this._getNumberOfMonths(inst);
		inst.dpDiv[(numMonths[0] != 1 || numMonths[1] != 1 ? 'add' : 'remove') + 'Class']('ui-datepicker-multi');
		inst.dpDiv[(this._get(inst, 'isRTL') ? 'add' : 'remove') + 'Class']('ui-datepicker-rtl');
		if (inst.input && inst.input[0].type != 'hidden')
			$(inst.input[0]).focus();

		if (!loaded) {
			$('.ui-datepicker-one-month', inst.dpDiv).append('<div class="ui-datepicker-loading"><div>recherche des disponibilités...</div></div>');
			if ($.browser.msie && $.browser.version < 7) {
				$('.ui-datepicker-new-month').hide();
				$('.ui-datepicker-new-year').hide();
				$('.ui-datepicker-loading').height(inst.dpDiv.height() + 20);
			}
			$.datepicker._load(inst);
		}
	},

	/* jQuery extend now ignores nulls! */
	function extendRemove(target, props) {
		$.extend(target, props);
		for (var name in props)
			if (props[name] == null || props[name] == undefined)
				target[name] = props[name];
		return target;
	};

});