var DatePicker=new Class({initialize:function(dp,options,dp_hidden){this.element=dp;this.dp_hidden=dp_hidden;this.dayChars=1;this.dayNames=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];this.daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];this.format='Y-m-d';this.monthNames=['January','February','March','April','May','June','July','August','September','October','November','December'];this.startDay=7;this.yearOrder='asc';this.yearRange=10;this.yearStart=(new Date().getFullYear());this.dp=dp;dp.today=new Date();dp.then=new Date();if(options.value!=''){dp.then.setTime(parseInt(options.value)*1000);}
dp.oldYear=dp.year=dp.then.getFullYear();dp.oldMonth=dp.month=dp.then.getMonth();dp.oldDay=dp.then.getDate();dp.nowYear=dp.today.getFullYear();dp.nowMonth=dp.today.getMonth();dp.nowDay=dp.today.getDate();dp.options={monthNames:(options.monthNames&&options.monthNames.length==12?options.monthNames:this.monthNames)||this.monthNames,daysInMonth:(options.daysInMonth&&options.daysInMonth.length==12?options.daysInMonth:this.daysInMonth)||this.daysInMonth,dayNames:(options.dayNames&&options.dayNames.length==7?options.dayNames:this.dayNames)||this.dayNames,startDay:options.startDay||this.startDay,dayChars:options.dayChars||this.dayChars,format:options.format||this.format,yearStart:options.yearStart||this.yearStart,yearRange:options.yearRange||this.yearRange,yearOrder:options.yearOrder||this.yearOrder};dp.setProperties({'id':dp.getProperty('name'),'readonly':true});dp.container=false;dp.calendar=false;dp.interval=null;dp.active=false;dp.onclick=dp.onfocus=this.create.pass(dp,this);},create:function(dp){if(dp.calendar)return false;if(window.ie6){$$('select').addClass('dp_hide');}
dp.container=new Element('div',{'class':'dp_container'}).injectBefore(dp);dp.container.onmouseover=dp.onmouseover=function(){$clear(dp.interval);};dp.container.onmouseout=dp.onmouseout=function(){dp.interval=setInterval(function(){if(!dp.active){this.remove(dp);}}.bind(this),500);}.bind(this);dp.calendar=new Element('div',{'class':'dp_calendar'}).injectInside(dp.container);var date=new Date();if(dp.month&&dp.year){date.setFullYear(dp.year,dp.month,1);}else{dp.month=date.getMonth();dp.year=date.getFullYear();date.setDate(1);}
dp.year%4==0?dp.options.daysInMonth[1]=29:dp.options.daysInMonth[1]=28;var firstDay=(1-(7+date.getDay()-dp.options.startDay)%7);monthSel=new Element('select',{'id':dp.id+'_monthSelect'});for(var m=0;m<dp.options.monthNames.length;m++){monthSel.options[m]=new Option(dp.options.monthNames[m],m);if(dp.month==m)monthSel.options[m].selected=true;}
yearSel=new Element('select',{'id':dp.id+'_yearSelect'});i=0;dp.options.yearStart?dp.options.yearStart:dp.options.yearStart=date.getFullYear();if(dp.options.yearOrder=='desc'){for(var y=dp.options.yearStart;y>(dp.options.yearStart-dp.options.yearRange-1);y--){yearSel.options[i]=new Option(y,y);if(dp.year==y)yearSel.options[i].selected=true;i++;}}else{for(var y=dp.options.yearStart;y<(dp.options.yearStart+dp.options.yearRange+1);y++){yearSel.options[i]=new Option(y,y);if(dp.year==y)yearSel.options[i].selected=true;i++;}}
calTable=new Element('table');calTableThead=new Element('thead');calSelRow=new Element('tr');calSelCell=new Element('th',{'colspan':'7'});monthSel.injectInside(calSelCell);yearSel.injectInside(calSelCell);calSelCell.injectInside(calSelRow);calSelRow.injectInside(calTableThead);calTableTbody=new Element('tbody');calDayNameRow=new Element('tr');for(var i=0;i<dp.options.dayNames.length;i++){calDayNameCell=new Element('th');calDayNameCell.appendText(dp.options.dayNames[(dp.options.startDay+i)%7].substr(0,dp.options.dayChars));calDayNameCell.injectInside(calDayNameRow);}
calDayNameRow.injectInside(calTableTbody);while(firstDay<=dp.options.daysInMonth[dp.month]){calDayRow=new Element('tr');for(i=0;i<7;i++){if((firstDay<=dp.options.daysInMonth[dp.month])&&(firstDay>0)){calDayCell=new Element('td',{'class':dp.id+'_calDay','axis':dp.year+'|'+(parseInt(dp.month)+1)+'|'+firstDay}).appendText(firstDay).injectInside(calDayRow);}else{calDayCell=new Element('td',{'class':'dp_empty'}).appendText(' ').injectInside(calDayRow);}
if((firstDay==dp.oldDay)&&(dp.month==dp.oldMonth)&&(dp.year==dp.oldYear)){calDayCell.addClass('dp_selected');}
if((firstDay==dp.nowDay)&&(dp.month==dp.nowMonth)&&(dp.year==dp.nowYear)){calDayCell.addClass('dp_today');}
firstDay++;}
calDayRow.injectInside(calTableTbody);}
calTableThead.injectInside(calTable);calTableTbody.injectInside(calTable);calTable.injectInside(dp.calendar);$$('td.'+dp.id+'_calDay').each(function(el){el.onmouseover=function(){el.addClass('dp_roll');}.bind(this);}.bind(this));$$('td.'+dp.id+'_calDay').each(function(el){el.onmouseout=function(){el.removeClass('dp_roll');}.bind(this);}.bind(this));$$('td.'+dp.id+'_calDay').each(function(el){el.onclick=function(){ds=el.axis.split('|');dp.value=this.formatValue(dp,ds[0],ds[1],ds[2]);this.remove(dp);}.bind(this);}.bind(this));monthSel.onfocus=function(){dp.active=true;};monthSel.onchange=function(){dp.month=monthSel.value;dp.year=yearSel.value;this.remove(dp);this.create(dp);}.bind(this);yearSel.onfocus=function(){dp.active=true;};yearSel.onchange=function(){dp.month=monthSel.value;dp.year=yearSel.value;this.remove(dp);this.create(dp);}.bind(this);},formatValue:function(dp,year,month,day){var dateStr='';var m=month-1;if(day<10)day='0'+day;if(month<10)month='0'+month;dateStr=dp.options.format.replace(/d/i,day).replace(/m/i,month).replace(/Y/i,year);dateStr=dateStr.replace(/F/i,dp.options.monthNames[m]);dp.month=dp.oldMonth=''+(month-1)+'';dp.year=dp.oldYear=year;dp.oldDay=day;var hidden_format=new String('Y-m-d');var dateHiddenStr=hidden_format.replace(/d/i,day).replace(/m/i,month).replace(/Y/i,year);this.dp_hidden.value=dateHiddenStr;return dateStr;},remove:function(dp){$clear(dp.interval);dp.active=false;if(window.opera)dp.container.empty();else if(dp.container)dp.container.empty();dp.calendar=false;dp.container=false;$$('select.dp_hide').removeClass('dp_hide');}});