(function($){
	var defaults = {
		nextSelector: null,
		prevSelector: null,
		hideDefaultNextAndPrevIfNewSelectorsSet: true,
		datesInfo: {}
	};
	var $this;
	var methods = {
		init: function(options){
			return this.each(function(){
				$this = $(this);
				$this.opts = $.extend(defaults, options);
				methods.render();
				methods._repaintCalendars();//repaint calendars
			});
		},
		destroy: function( ) {
			$(window).unbind('.staticdatepicker');
		},
		_onChangeMonthYear:function(year,month,inst){
			methods._repaintCalendars();
		},
		_repaintCalendars:function(e){
			setTimeout(methods._updateDatesInfo,0);
		},
		render:function(){
			//"repair" css styles
			cssStyles = 		'div.ui-datepicker{'
								+	'	font-size: 11px !important;'
								+	'	line-height: 1;'
								+	'}'
								+	'#calholder a.ui-state-default{'
								+	'	background: url(images/ui-bg_glass_100_fafaf4_1x400.png) repeat-x scroll 50% 50% #FAFAF4;'
								+	'	border: 1px solid #D4CCB0;'
								+	'	color: #459E00;'
								+	'	font-weight: bold;'
								+	'}'
								+	'#calholder a.ui-state-default.ui-state-hover{'
								+	'	background: url(images/ui-bg_glass_25_67b021_1x400.png) repeat-x scroll 50% 50% #67B021;'
								+	'	border: 1px solid #327E04;'
								+	'	color: #FFFFFF;'
								+	'	font-weight: bold;'
								+	'}'
								+	'#calholder a.ui-state-default.ui-state-highlight{'
								+	'	background: #847E74;'
								+	'	border: 1px solid #847E74;'
								+	'	color: white;'
								+	'}'
								+	'#calholder a.ui-state-default.ui-state-exposed{'
								+	'	background: url("images/ui-bg_glass_15_459e00_1x400.png") repeat-x scroll 50% 50% #459E00;'
								+	'	border: 1px solid #327E04;'
								+	'	color: #FFFFFF;'
								+	'	font-weight: bold;'
								+	'}'
								+	'#sdp_sel_dayinfo_popup{'
								+	'	position: absolute;'
								+	'	border: 1px solid #327E04;'
								+	'	background: #FFFFFF;'
								+	'}';
			//hide next/prev buttons if set so
			if($this.opts.hideDefaultNextAndPrevIfNewSelectorsSet && $this.opts.nextSelector)
				cssStyles += '#calholder a.ui-datepicker-next{'
								+	'	display: none;'
								+	'}';
			if($this.opts.hideDefaultNextAndPrevIfNewSelectorsSet && $this.opts.prevSelector)
				cssStyles += '#calholder a.ui-datepicker-prev{'
								+	'	display: none;'
								+	'}';
			$('head').append('<style type="text/css">' + cssStyles + '</style>');
			if(!$this.opts.onChangeMonthYear || typeof $this.opts.onChangeMonthYear !== 'function'){
				$this.opts.onChangeMonthYear = methods._onChangeMonthYear;
			}
			$($this).datepicker($this.opts);
			if($this.opts.nextSelector){
				$($this.opts.nextSelector).click(function(){
					$($this).find('a.ui-datepicker-next').click();
					return false;
				});
			}
			if($this.opts.prevSelector){
				$($this.opts.prevSelector).click(function(){
					$($this).find('a.ui-datepicker-prev').click();
					return false;
				});	
			}
		},
		_updateDatesInfo:function(){
			//for each date holder, find out the year and the month displayed
			$this.find('table.ui-datepicker-calendar').each(function(){
				$(this).find('td:not(.ui-state-disabled)').each(function(){
					var matches = $(this)[0].getAttributeNode('onclick').value.replace(/\s+/,'').match(/_selectDay\(.*,(\d{1,2}),(\d{4}),this\)/);
					if(matches && matches[1] && matches[2]){//found month and year
						m=((matches[1]|0)+1);//months start with 0
						y=matches[2]|0;
						d=$(this).find('a').html()|0;
						key = y+'-'+m+'-'+d;
						//check if this day has any info
						if($this.opts.datesInfo[key]){
							$(this).find('a').addClass('ui-state-exposed').attr('key',key);
						}
					}
				});
				//disable clicks
				$(this).find('td').each(function(){
					$(this)[0].setAttribute('onclick', '');
				}).click(function(e){
					//if this was not a click on popup, dont respond to this click
					return $(e.target).closest('#sdp_sel_dayinfo_popup').length > 0;
				});
				//enable hover on exposed days
				$(this).find('td a.ui-state-exposed').parent().hover(
					function(){
						methods._showPopup($(this));
					},
					function(){
						methods._hidePopup($(this));
					}
				);
			})
			.css({
				//position: 'relative'//set tables to relative position, so its children will have properly set postion.top and left values
			});
		},
		_showPopup:function($el){
			if(!$this._sdp_popupInfo){
				$this._sdp_popupInfo = $('<div id="sdp_sel_dayinfo_popup" />');
				$this._sdp_popupInfo.hide();
				//$('body').append($this._sdp_popupInfo);
			}
			css={
				zIndex: 30,
				top:$el.position().top+$el.outerHeight(true)
			};
			dpGroup=$el.closest('div.ui-datepicker-group');
			css['left'] = dpGroup.position().left;
			if($this.width()/2 < dpGroup.position().left)
				css['left'] = $this.width()-$this.datepicker('widget').width();
			$this._sdp_popupInfo
				.html($this.opts.datesInfo[$el.find('a').attr('key')])
				.css(css)
				.show();
			$el.append($this._sdp_popupInfo);
		},
		_hidePopup:function($el){
			if($el)
				$el.find('#sdp_sel_dayinfo_popup').remove();
			else
				$('#sdp_sel_dayinfo_popup').remove();
		}
	};	
	$.fn.extend({
		staticdatepicker: function(method){
			if (methods[method]) {
    			return methods[method].apply(this, Array.prototype.slice.call( arguments, 1 ));
    		}
			else if(typeof method === 'object' || ! method) {
    			return methods.init.apply(this, arguments);
			}
			//pass it to ui datepicker
			else{
				var args = arguments;
				$(this).each(function(){
					$this.datepicker.apply($this,args);
				});
			}
		}
	})
})(jQuery);
