DateInput=(function(a){function b(c,d){if(typeof(d)!="object"){d={}}a.extend(this,b.DEFAULT_OPTS,d);this.input=a(c);this.bindMethodsToObj("show","hide","hideIfClickOutside","hideOnEsc","selectDate","prevMonth","nextMonth");this.build();this.selectDate();this.hide()}b.DEFAULT_OPTS={month_names:["January","February","March","April","May","June","July","August","September","October","November","December"],short_month_names:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],short_day_names:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],start_of_week:1};b.prototype={build:function(){var c,d;this.monthNameSpan=a('<span class="month_name"></span>');c=a('<p class="month_nav"></p>').append(a('<a href="#" class="prev">&laquo;</a> ').click(this.prevMonth),this.monthNameSpan,a(' <a href="#" class="next">&raquo;</a>').click(this.nextMonth));d="<table><thead><tr>";a(this.adjustDays(this.short_day_names)).each(function(){d+="<th>"+this+"</th>"});d+="</tr></thead><tbody></tbody></table>";this.dateSelector=this.rootLayers=a('<div class="date_selector"></div>').append(c,d).appendTo(document.body);if(a.browser.msie&&a.browser.version<7){this.ieframe=a('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.dateSelector);this.rootLayers=this.rootLayers.add(this.ieframe)}this.tbody=a("tbody",this.dateSelector);this.input.change(this.bindToObj(function(){this.selectDate()}))},selectMonth:function(d){this.currentMonth=new Date(d.getFullYear(),d.getMonth(),1);var j=this.rangeStart(d),h=this.rangeEnd(d),c=this.daysBetween(j,h),f="",e,g;for(e=0;e<=c;e++){g=new Date(j.getFullYear(),j.getMonth(),j.getDate()+e,12,0);if(this.isFirstDayOfWeek(g)){f+="<tr>"}if(g.getMonth()==d.getMonth()){f+='<td date="'+this.dateToString(g)+'"><a href="#">'+g.getDate()+"</a></td>"}else{f+='<td class="unselected_month" date="'+this.dateToString(g)+'">'+g.getDate()+"</td>"}if(this.isLastDayOfWeek(g)){f+="</tr>"}}this.monthNameSpan.empty().append(this.monthName(d)+" "+d.getFullYear());this.tbody.empty().append(f);a("a",this.tbody).click(this.bindToObj(function(i){this.selectDate(this.stringToDate(a(i.target).parent().attr("date")));this.hide();return false}));a("td[date="+this.dateToString(new Date())+"]",this.tbody).addClass("today")},selectDate:function(c){if(typeof(c)=="undefined"){c=this.stringToDate(this.input.val())}if(c){this.selectedDate=c;this.selectMonth(c);var d=this.dateToString(c);a("td[date="+d+"]",this.tbody).addClass("selected");if(this.input.val()!=d){this.input.val(d).change()}}else{this.selectMonth(new Date())}},show:function(){this.rootLayers.css("display","block");this.setPosition();this.input.unbind("focus",this.show);a("a:last",this.dateSelector).blur(this.hide);a([window,document.body]).click(this.hideIfClickOutside).keyup(this.hideOnEsc)},hide:function(){this.rootLayers.css("display","none");a([window,document.body]).unbind("click",this.hideIfClickOutside).unbind("keyup",this.hideOnEsc);a("a:last",this.dateSelector).unbind("blur",this.hide);this.input.focus(this.show)},hideIfClickOutside:function(c){if(c.target!=this.input[0]&&!this.insideSelector(c)){this.hide()}},hideOnEsc:function(c){if(c.keyCode==27){this.hide()}},stringToDate:function(c){var d;if(d=c.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)){return new Date(d[3],this.shortMonthNum(d[2]),d[1],12,0)}else{return null}},dateToString:function(c){return c.getDate()+" "+this.short_month_names[c.getMonth()]+" "+c.getFullYear()},setPosition:function(){var c=this.input.offset();this.rootLayers.css({top:c.top+this.input.outerHeight(),left:c.left});if(this.ieframe){this.ieframe.css({width:this.dateSelector.outerWidth(),height:this.dateSelector.outerHeight()})}},moveMonthBy:function(c){this.selectMonth(new Date(this.currentMonth.setMonth(this.currentMonth.getMonth()+c)))},prevMonth:function(){this.moveMonthBy(-1);return false},nextMonth:function(){this.moveMonthBy(1);return false},monthName:function(c){return this.month_names[c.getMonth()]},insideSelector:function(c){var d=this.dateSelector.offset();d.right=d.left+this.dateSelector.outerWidth();d.bottom=d.top+this.dateSelector.outerHeight();return c.pageY<d.bottom&&c.pageY>d.top&&c.pageX<d.right&&c.pageX>d.left},bindToObj:function(d){var c=this;return function(){return d.apply(c,arguments)}},bindMethodsToObj:function(){for(var c=0;c<arguments.length;c++){this[arguments[c]]=this.bindToObj(this[arguments[c]])}},indexFor:function(e,d){for(var c=0;c<e.length;c++){if(d==e[c]){return c}}},monthNum:function(c){return this.indexFor(this.month_names,c)},shortMonthNum:function(c){return this.indexFor(this.short_month_names,c)},shortDayNum:function(c){return this.indexFor(this.short_day_names,c)},daysBetween:function(d,c){d=Date.UTC(d.getFullYear(),d.getMonth(),d.getDate());c=Date.UTC(c.getFullYear(),c.getMonth(),c.getDate());return(c-d)/86400000},changeDayTo:function(f,c,d){var e=d*(Math.abs(c.getDay()-f-(d*7))%7);return new Date(c.getFullYear(),c.getMonth(),c.getDate()+e)},rangeStart:function(c){return this.changeDayTo(this.start_of_week,new Date(c.getFullYear(),c.getMonth()),-1)},rangeEnd:function(c){return this.changeDayTo((this.start_of_week-1)%7,new Date(c.getFullYear(),c.getMonth()+1,0),1)},isFirstDayOfWeek:function(c){return c.getDay()==this.start_of_week},isLastDayOfWeek:function(c){return c.getDay()==(this.start_of_week-1)%7},adjustDays:function(e){var d=[],c;for(c=0;c<e.length;c++){d[c]=e[(c+this.start_of_week)%7]}return d}};a.fn.date_input=function(c){return this.each(function(){new b(this,c)})};a.date_input={initialize:function(c){a("input.date_input").date_input(c)}};return b})(jQuery);