function date_selected(date) {
	alert(date.id);
	
} 
 
 $.widget("ui.calendar", {
   _init: function() {
     // init code for mywidget
     // can use this.options
 //    alert('hello');
 		
	    var self = this;
	    var url = this.options.url;
	    this.options.onChangeMonthYear = function(year, month, inst) {
	   		$.getJSON( url+'?month='+month+'&year='+year+'&count='+self.options.numberOfMonths, function(json) {
	   			self.options.dates = json;
	   			self.resetDates();
	   		});
	    };
	    
	    var datepicker = this.element.datepicker(this.options);
	   	this._remove_default_onclicks();
   },
   resetDates:function() {
   	var self = this;
   		
   		$("td a").each(function(){
	   		$(this).attr('id','ui_dp_date__'+$(this).html());
   			var date_object = '';
   			
   	//		$(this).attr('href','');
		});
		this._setDates(this.options.dates);
		$("td a").each(function(){   			
			var $this = $(this);
			var event = self.options.events[$this.html()];
			if (event != undefined) {
				$this.attr('title',event.spaces_left+' Spaces left');
				$this.addClass('tooltip');
			}
			else {
				$this.css('cursor','default');
			}
			$(this).click(function() {
   				var event = self.options.events[$(this).html()];
				if (event == undefined) {
				}
				else {
					
					var data = {text: event.date_string,id: event.id,spaces:event.spaces_left}
					self.options.callback(data);
				}
	//			return false;
			});
		});
	   	
   },
   _setDates: function(dates) {

   		for (var i=0;i<dates.date.length;i++)
   			this._setDate(dates.date[i],dates.text[i]);
   		
   },
   _remove_default_onclicks: function() {
   		$("td a").each(function(){
	   		$(this).attr('id','ui_dp_date__'+$(this).html());
   			var date_object = '';
   			
   		//	$(this).attr('href','');
		});
   		this.element.find("td[onclick]").attr('onclick','');
   },
   _setDate: function (date, text) {
   		date = date.split('-');
   		day = date[0];
   		month = date[1];
   		year = date[2];
   		self = this;
   		el = this.element.find("a#ui_dp_date__"+day).each(function() {
   			if ($(this).html() == day) {
	   			self.options.events[day] = text;
	   			$(this).addClass('day-event');
	   			$(this).addClass('tooltip');
				var parent = $(this).parent();
				parent.addClass('day-event');
		//		parent.attr('onclick','return false;');
			}
			else {
			}
			
			if ($(this).html() == day) {		   			
	   			$(this).addClass('day-event');
	   			$(this).addClass('tooltip');
			}
   		
   		});
		   		
   		
		
   },
   destroy: function() {
       $.widget.prototype.apply(this, arguments); // default destroy
        // now do other stuff particular to this widget
   }
 });
 $.extend($.ui.calendar, {
   defaults: {
     contents: true,
     dates: undefined,
     url: '/json/events',
     numberOfMonths: 1,
     changeMonth: false,
     changeYear: false,
     events: {},
     callback: function(data) {
     }
   }
 });