function DateManager(dateel, datedisplayel, startel, endel)
{
	if ( arguments.length > 0 )
		this.init(dateel, datedisplayel, startel, endel)
		this.ajaxurl = '/assets/plugins/bookings/ajax.php';
}
DateManager.prototype.init = function(dateel, datedisplayel, startel, endel)
{
	this.dateel = dateel;
	this.datedisplayel = datedisplayel;
	this.startel = startel;
	this.endel = endel;
}

DateManager.prototype.setVals = function(start, end, id, facility_id)
{
	this.startval = start;
	this.endval = end;
	this.id = id;
	this.facility_id = facility_id;
}

DateManager.prototype.toOptions = function(data)
	{
		out = '';
		for(i=0; i < data.length; i++) {
			out += '<option value="'+data[i]+'">'+data[i]+'</option>';
		}
		return out;
	}

DateManager.prototype.run = function() 
{
	var obj = this;

	this.dateel.datepicker({
		beforeShowDay: $.datepicker.noWeekends,
		dateFormat: $.datepicker.ISO_8601, 
		showOn: "both", 
		buttonImage: "/assets/plugins/bookings/images/calendar.gif", 
		buttonImageOnly: true,
		defaultDate: +0
	});

	this.dateel.change(
		function(){
			
			$.get(obj.ajaxurl, { date: $(this).val(), 'id': obj.id, 'facility_id': obj.facility_id }, function(data){ 
				obj.startel.html(obj.toOptions(data)).prepend('<option value="">-- Choose --</option>').removeAttr('disabled');
					
				obj.endel.empty().append('<option value="">Select time first</option>').attr('disabled', 'disabled'); 

				if (obj.startval != '')
				{
					obj.startel.find("option[value='"+obj.startval+"']").attr("selected", 'selected').end().change();
					obj.startval = '';
				} else {
					obj.startel.find("option:first").attr("selected", 'selected');
				}

				obj.datedisplayel.text($.datepicker.formatDate("d MM yy", $.datepicker.parseDate($.datepicker.ISO_8601, obj.dateel.val())));
			}, 'json');
				
		}
	).change();

	this.startel.change(function(){
		$.get(obj.ajaxurl, { date: obj.dateel.val(), startdate: $(this).val(), 'id': obj.id, 'facility_id': obj.facility_id }, function(data){
			obj.endel.empty().html(obj.toOptions(data)).removeAttr('disabled'); 
			if (obj.endval != '')
			{
				obj.endel.find("option[value='"+obj.endval+"']").attr("selected", 'selected');
				obj.endval = '';
			}
		}, 'json');
			
	});
}
