/* jquery - jcdate v1.0 */
;(function($){
$.fn.jcdate = function(options) {
var defaults = {
icoclass : "jcdateico",
event : "click",
speed : 100,
left : 0,
top : 22,
format : "/",
timeout : 100
};
var options = $.extend(defaults,options);
return this.each(function() {
if($("#jcdate").length == 0){
$("body").prepend("
");
};
var $dateinput = $(this),
$window = $(window),
dateobj = new date(),
$jcdate = $("#jcdate"),
inputoffleft = $(this).offset().left,
inputofftop = $(this).offset().top,
$year = $("#jcdatett").find("span"),
$month = $("#jcdatett").find("samp"),
$b = $("#jcdatett").find("b"),
$jcdatemax = $("#jcdatemax"),
$weekframe = $("#jcdaycon"),
$datebtn = $("#jcdatebtn"),
$jcyeas = $("#jcyeas"),
$jcmonth = $("#jcmonth"),
$now = $datebtn.find("samp"),
$clear = $datebtn.find("span"),
$jcdaywrap = $("#jcdaywrap"),
$dayframe = $jcdaywrap.find("ul"),
$submit = $datebtn.find("a#d_sub"),
$hiddentext = $("input#datehidetext"),
$jctimewrap = $("#jctimewrap"),
$hh = $jctimewrap.find("input:eq(0)"),
$mm = $jctimewrap.find("input:eq(1)"),
$d_prev = $("#d_prev"),
$d_next = $("#d_next"),
// 获取当前时间
_year = parseint(dateobj.getfullyear()),
_month = dateobj.getmonth() + 1,
_date = dateobj.getdate(),
week = dateobj.getday(),
days = [ "日","一 ","二 ","三 ","四 ","五 ","六 "],
_day = days[week],
_hours = dateobj.gethours(),
_minutes = dateobj.getminutes(),
weekdl = "",
yeardl = ""+_year+"年"+"",
monthdl = "",
$ul = "",
_idx = 0,
definfo = "",
getdatecount = function(y, m){
return [31, y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ? 29 : 28 ,31,30,31,30,31,31,30,31,30,31][ m+1 ];
};
// 赋值数据
$hh.val("00");
$mm.val("00");
for(var w = 0 in days) {
weekdl = weekdl + "" + days[w] + "";
};
$weekframe.html(weekdl);
var nowy = _year,
yarr = new array(),
marr = new array();
for(var lys = 0; lys < 9 ; lys++ ){
lastyear = nowy - (lys+1);
yarr.push(""+lastyear+"年"+"")
};
yarr.reverse();
var dtf = ysarr(yarr);
yarr.length = 0;
for(var fys = 0; fys < 8 ; fys++ ){
firstyear = nowy + (fys+1);
yarr.push(""+firstyear+"年"+"")
};
var dtl = ysarr(yarr);
$jcyeas.html(dtf+yeardl+dtl);
function ysarr(arr){
var $dts = "";
for(var index = 0 in arr){
$dts = $dts + arr[index];
};
return $dts
};
for(var ms = 1; ms <= 12; ms++ ){
if(ms == _month){
marr.push("" + ms + "月" +"");
} else {
marr.push("" + ms + "月" +"");
};
};
for(var ma = 0 in marr){
monthdl += marr[ma];
};
$jcmonth.html(monthdl)
function shfade(show,hide,hide2,bool,index){
var fadespeed = options.speed;
hide.fadeout(fadespeed);
hide2.fadeout(fadespeed);
show.delay(fadespeed*2).fadein(fadespeed);
if(bool){
$("#d_prev,#d_next").fadeout(fadespeed);
$b.eq(index).hide();
} else {
$("#d_prev,#d_next").fadein(fadespeed);
$b.show();
$year.show();
$month.show();
};
};
$year.die().live("click",function(){
$(this).addclass("visited").next("samp").removeclass("visited");
shfade($jcyeas,$jcdaywrap,$jcmonth,true,1);
$year.show();
$month.hide();
});
$("#jcyeas dd,#jcyeas dt").die().live("click",function(){
var y = parseint($(this).text());
traversal(y);
$year.text(y);
shfade($jcdaywrap,$jcyeas,$jcmonth,false);
});
$month.die().live("click",function(){
$(this).addclass("visited").prev().removeclass("visited");
shfade($jcmonth,$jcdaywrap,$jcyeas,true,0);
$month.show();
$year.hide();
});
$jcmonth.find("dt").die().live("click",function(){
var m = parseint($(this).text());
manimate(m-1);
$month.text(m);
shfade($jcdaywrap,$jcyeas,$jcmonth,false);
});
function traversal(ty){
$year.text(ty);
for(var m = 0; m < $dayframe.length; m++){
var dayli = "",
$fli = "",
$lli = "",
firstli = "",
lastday = 0,
yearweek = new date(ty,m,1).getday();
getdatecount(ty,m-2) == undefined ? fristday = 31 : fristday = getdatecount(ty,m-2);
for(var d = 1; d <= getdatecount(ty,m-1); d++){
dayli = dayli + ""+ d +"";
};
for(var f = 0 ; f < yearweek; f++){
firstli += ""+ fristday +",";
fristday--;
};
for(var l = 0 ; l <= 42-(d+yearweek); l++ ){
lastday = l + 1;
$lli += ""+lastday+"";
};
fliarr = firstli.split(",").reverse();
for(var arr = 0 in fliarr){
$fli += fliarr[arr];
};
$addli = $fli + dayli + $lli;
$dayframe.eq(m).html($addli);
};
};
manimate(_month-1);
//功能方法
$d_prev.die().live("click",function(){
_idx = $dayframe.filter(".datev").index();
if( _idx > 0){
_idx --;
} else {
var _tyear = parseint($year.text());
_tyear--;
traversal(_tyear);
_idx = $dayframe.length-1;
};
manimate(_idx);
});
$d_next.die().live("click",function(){
_idx = $dayframe.filter(".datev").index();
if( _idx < $dayframe.length-1){
_idx ++;
} else {
var _tyear = parseint($year.text());
_tyear++;
traversal(_tyear);
_idx = 0;
};
manimate(_idx);
});
function manimate(index){
$dayframe.eq(index).addclass("datev").siblings().removeclass("datev");
$month.text(index+1);
$jcdatemax.animate({ "left":-index*256 },options.speed);
};
function today(y,m,d){
$year.text(y);
$month.text(m);
traversal(y);
$.each($dayframe.eq(m-1).find("li"),function(){
if($(this).hasclass("jcdatecolor")){
$dayframe.eq(m-1).find("li.jcdatecolor").eq(d-1).addclass("visited");
};
});
$hiddentext.val(_date);
$dayframe.eq(m-1).find("li").text();
manimate(m-1);
};
function clearvisited(){
$.each($("li.jcdatecolor",$dayframe),function(){
if($(this).hasclass("visited")){
$(this).removeclass("visited");
};
});
};
today(_year,_month,_date);
$dayframe.undelegate("li.jcdatecolor","click").delegate("li.jcdatecolor","click",function(){
clearvisited();
$(this).addclass("visited");
$hiddentext.val(parseint($(this).text()))
});
$dayframe.undelegate("li.jcdatecolor","dblclick").delegate("li.jcdatecolor","click",function(){
submitdate();
});
$now.die().live("click",function(){
today(_year,_month,_date);
});
$clear.die().live("click",function(){
$("input.datevisited").val("");
});
$submit.die().live("click",function(){
submitdate();
});
function submitdate(){
var sy = $year.text(),
sm = $month.text(),
sd = $hiddentext.val();
nowdatearr = new array(sy,sm,sd);
dateinfo =nowdatearr.join(options.format);
if($hh.val() != "00" || $mm.val() != "00" ){
var sh = $hh.val(),
sm = $mm.val();
nowdatearr.push(sh+":"+sm);
printdate = nowdatearr.join(options.format).substring(),
format = printdate.split("/");
dateinfo = format[0]+options.format+format[1]+options.format+format[2]+" "+format[3];
};
$("input.datevisited").val(dateinfo);
closedate();
};
$dateinput.addclass(options.icoclass)
//.val(definfo)
.die()
.live(options.event,function(e){
$(this).addclass("datevisited");
//$jcdate.css({ "left" : e.pagex+(options.left),"top" : e.pagey+(options.top) });
var iof = $(this).offset();
$jcdate.css({ "left" : iof.left+options.left,"top" : iof.top+options.top });
$jcdate.show(options.speed);
$jcdaywrap.show();
$jcyeas.hide();
$jcmonth.hide();
$("#d_prev,#d_next").show();
$year.show();
$month.show();
$b.show();
});
$jcdate.die().live("mouseleave",function(){
settimeout(closedate,options.timeout);
});
function closedate(){
$("input.datevisited").removeclass("datevisited");
$jcdate.hide(options.speed);
};
});
};
})(jquery)