/*----------------------------- calendar -----------------------------*/
(function($){
var
defaults = {
countCal:1,
sendSkipHour:0,
headline:'calendar',
button:['<', '>'],
weekDayList:[
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sun'
],
currentYear:-1,
currentMonth:-1,
holidays:{
//曜日の休暇日
regular:[
0,
6
],
//祝日
public:[],
//店舗の休暇日
shop:[],
}
};
$.fn.calendar = function(options){
var
el = this,
lenEl = el.length;
if(lenEl === 0)return this;
if(lenEl > 1){
el.each(function(){$(this).calendar(options)});
return this;
}
var
configs = {},
date = new Date(),
theYear = date.getFullYear(),
theMonth = date.getMonth() + 1,
theDay = date.getDate(),
theHour = date.getHours(),
theMinute = date.getMinutes(),
theSecond = date.getSeconds(),
sendDate = [],
funcInit = function(){
configs = $.extend({}, defaults, options);
if(configs.currentYear === -1)
configs.currentYear = theYear;
if(configs.currentMonth === -1)
configs.currentMonth = theMonth;
funcGetSendDay();
funcInitHtml(el, configs.countCal);
},
funcInitHtml = function(el, count){
el.append('
');
for(var i = 0;i < count;i++)
el.append('\
\
');
el.funcSetCalendar();
},
funcGetDate = function(timestamp){
var
date = new Date(timestamp * 1000);
return [
date,
date.getFullYear(),
date.getMonth() + 1,
date.getDate(),
date.getHours() < 10?'0' + date.getHours():date.getHours(),
date.getMinutes() < 10?'0' + date.getMinutes():date.getMinutes(),
date.getSeconds() < 10?'0' + date.getSeconds():date.getSeconds()
];
},
funcGetTimestamp = function(date){
return Date.parse(date.replace(/-/g, '/')) / 1000;
},
funcConfirmLeapYear = function(year){
return ((year % 4 === 0) && (year % 100 != 0)) || (year % 400 === 0)?true:false;
},
funcGetFirstDay = function(theYear, theMonth){
return new Date(theYear, theMonth - 1, 1).getDay();
},
funcGetMonthDays = function(theYear, theMonth){
var
monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if(funcConfirmLeapYear(theYear))
monthDays[1] = 29;
return monthDays[theMonth - 1];
},
funcSetPrevMonth = function(){
if(configs.currentMonth === 1){
configs.currentMonth = 12;
configs.currentYear--;
}else{
configs.currentMonth--;
}
},
funcSetNextMonth = function(){
if(configs.currentMonth === 12){
configs.currentMonth = 1;
configs.currentYear++;
}else{
configs.currentMonth++;
}
},
funcGetSendDay = function(){
var
oneDayTime = 86400,
sendTime = funcGetTimestamp(theYear + '-' + theMonth + '-' + theDay + ' ' + (theHour < 10?'0' + theHour:theHour) + ':' + (theMinute < 10?'0' + theMinute:theMinute) + ':' + (theSecond < 10?'0' + theSecond:theSecond)),
targetDate = {
1:theYear,
2:theMonth,
3:theDay
},
td = new Date(targetDate[1] + '/' + targetDate[2] + '/' + targetDate[3]);
switch(td.getDay()){
case 0:
sendTime += oneDayTime;
break;
case 1:
sendTime += theHour < configs.sendSkipHour?0:oneDayTime;
break;
case 2:
sendTime += theHour < configs.sendSkipHour?0:oneDayTime;
break;
case 3:
sendTime += theHour < configs.sendSkipHour?0:oneDayTime;
break;
case 4:
sendTime += theHour < configs.sendSkipHour?0:oneDayTime;
break;
case 5:
sendTime += theHour < configs.sendSkipHour?0:oneDayTime*3;
break;
case 6:
sendTime += oneDayTime * 2;
break;
}
targetDate = funcGetDate(sendTime);
while($.inArray(targetDate[1] + '-' + targetDate[2] + '-' + targetDate[3], configs.holidays.shop) !== -1){
sendTime += oneDayTime;
targetDate = funcGetDate(sendTime);
var
td = new Date(targetDate[1] + '/' + targetDate[2] + '/' + targetDate[3]);
switch(td.getDay()){
case 0:
sendTime += oneDayTime;
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
sendTime += oneDayTime * 2;
break;
}
targetDate = funcGetDate(sendTime);
}
while($.inArray(targetDate[2] + '-' + targetDate[3], configs.holidays.public) !== -1){
sendTime += oneDayTime;
targetDate = funcGetDate(sendTime);
var
td = new Date(targetDate[1] + '/' + targetDate[2] + '/' + targetDate[3]);
switch(td.getDay()){
case 0:
sendTime += oneDayTime;
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
sendTime += oneDayTime * 2;
break;
}
targetDate = funcGetDate(sendTime);
}
sendDate = funcGetDate(sendTime);
};
el.funcSetCalendar = function(){
var
currentYear = configs.currentYear,
currentMonth = configs.currentMonth;
$('.calendar_pager', el).eq(0).html('\
' + configs.button[0] + '\
' + configs.headline + '\
' + configs.button[1] + '\
');
for(var i = 0;i < configs.countCal;i++){
$('.calendar_header' + i, el).eq(0).html('\
' + currentYear + ' / ' + currentMonth + '\
');
$('.calendar_body_parent' + i, el).eq(0).html(el.funcGetCalendar(currentYear, currentMonth));
currentYear = currentMonth === 12?currentYear + 1:currentYear;
currentMonth = currentMonth === 12?1:currentMonth + 1;
}
$('.calendar_prev', el)
.eq(0)
.on({
'click':function(){
funcSetPrevMonth();
el.funcSetCalendar();
}
});
$('.calendar_next', el)
.eq(0)
.on({
'click':function(){
funcSetNextMonth();
el.funcSetCalendar();
}
});
},
el.funcGetCalendar = function(theYear, theMonth){
var
col = 1,
calendarStr,
numOfDays = funcGetMonthDays(theYear, theMonth),
dayOfWeek = funcGetFirstDay(theYear, theMonth),
dayStr;
calendarStr ='\
\
\
';
for(var i = 0, l = configs.weekDayList.length;i < l;i++)
calendarStr += '\
\
' + (i === 0?'' + configs.weekDayList[i] + '':i === 6?'' + configs.weekDayList[i] + '':configs.weekDayList[i]) + '\
| \
';
calendarStr += "\
\
\
";
for(var i = 1;i <= dayOfWeek;i++){
col++;
calendarStr += '\
\
\
| \
';
};
for(var day = 1;day <= numOfDays;day++){
if(
theYear === sendDate[1] &&
theMonth === sendDate[2] &&
day === sendDate[3]
){
dayStr = '\
\
' + (col === 1?'' + day + '':col === 7?'' + day + '':day) + '\
| \
';
}else
if(
$.inArray(col - 1, configs.holidays.regular) !== -1 ||
$.inArray(theMonth + '-' + day, configs.holidays.public) !== -1 ||
$.inArray(theYear + '-' + theMonth + '-' + day, configs.holidays.shop) !== -1
){
dayStr = '\
\
' + (col === 1?'' + day + '':col === 7?'' + day + '':day) + '\
| \
';
}else{
dayStr = '\
\
' + (col === 1?'' + day + '':col === 7?'' + day + '':day) + '\
| \
';
}
if(col === 7){
calendarStr += dayStr + '\
\
\
';
col = 1;
}else{
calendarStr += dayStr;
col++;
}
}
col--;
while(0 !== col % 7){
calendarStr += '\
\
\
| \
';
col++;
};
calendarStr += '\
\
\
';
return calendarStr;
};
funcInit();
};
})(jQuery);
/*----------------------------- /calendar -----------------------------*/