﻿	//获取当前时间
    Date.prototype.getTheDate=function(){
		return {Y:this.getFullYear(),M:this.getMonth()+1,D:this.getDate()};
	}

	Date.prototype.beforeTheDate=function(Y,M,D){
		if(Y<this.getFullYear()||(Y==this.getFullYear()&&M<this.getMonth()+1)||(Y==this.getFullYear()&&M==this.getMonth()+1&&D<this.getDate()))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	Date.prototype.afterTheDate=function(Y,M,D){
		if(Y>this.getFullYear()||(Y==this.getFullYear()&&M>this.getMonth()+1)||(Y==this.getFullYear()&&M==this.getMonth()+1&&D>this.getDate()))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	Date.prototype.getThePreDate=function(dateObj,minYear){
		var M=dateObj.M;
		var Y=dateObj.Y;
		var D=dateObj.D;
		M--;
		if(M<1)
		{
			M=12;
			Y--;
		}
		if(Y<minYear)
		{
			Y=minYear;
		}
		return "{Y:"+Y+",M:"+M+",D:"+D+"}";
	}

	Date.prototype.getTheNextDate=function(dateObj,maxYear){
		var M=dateObj.M;
		var Y=dateObj.Y;
		var D=dateObj.D;
		M++;
		if(M>12)
		{
			M=1;
			Y++;
		}
		if(Y>maxYear)
		{
			Y=maxYear;
		}
		return "{Y:"+Y+",M:"+M+",D:"+D+"}";
	}

	/*****************
	 *日历的构造函数
	 ******************/
	function MarcianCalendar(calendarObjName,display,minYear,maxYear,afterNotUsed,beforeNotUsed)
	{
		/*属性*/
		this.calendarName=calendarObjName;
		this.display=display?"block":"none";
		this.minYear=minYear?minYear:1900;
		this.maxYear=maxYear?maxYear:2900;
		this.afterNotUsed=afterNotUsed?afterNotUsed:false;
		this.beforeNotUsed=beforeNotUsed?beforeNotUsed:false;
		this.calendarContainer=null;
		this.calendarMenuContainer=null;
		this.calendarDateContainer=null;
		this.calendarWeekContainer=null;
		this.weekAry=["日","一","二","三","四","五","六"];
		this.ltTod=ltDate;
		this.date=new Date();//日期对象
		this.id=0;//当前ID号

		//获取页面元素的快捷方式
		this.$=function(id)
		{
			return document.getElementById(id);
		}

		/*设置日历时间*/
		this.setDate=function(dateObj){
			if(this.calendarDateContainer.childNodes)
			{
				for(var j=this.calendarDateContainer.childNodes.length-1;j>=0;j--)
				{
					this.calendarDateContainer.removeChild(this.calendarDateContainer.childNodes[j]);
				}
			}
		    var W=1;
			var Y=dateObj.Y;
			var M=dateObj.M;
			var D=dateObj.D;
			var dayAry=[31,28,31,30,31,30,31,31,30,31,30,31];
			var r=[0,3,3,6,1,4,6,2,5,0,3,5];
			var c=6;
			if(Y%400==0||(Y%4==0&&Y%100!=0))
			{
				dayAry[1]=29;
			}
			if((Y%400==0||(Y%4==0&&Y%100!=0))&&M<3)
			{
				c=5;
			}
			var y=Y%400;
			w=(y+Math.floor(y/4)-Math.floor(y/100)+r[M-1]+1+c)%7;			
			var R=1;
		    R=(dayAry[M-1]-7+w)%7==0?R+(dayAry[M-1]-7+w)/7:R+Math.floor((dayAry[M-1]-7+w)/7)+1;
			var start=0;
			var d=1;
			var ltcss="";
			for(var i=0;i<R;i++)
			{
				var div=document.createElement("div");				
				for(var k=0;k<7;k++)
				{
					var p=document.createElement("p");
					p.style.position = "relative";
					var lturl=d;
					if(start>=w&&d<=dayAry[M-1])
					{
					ts2=""+Y+""+ (M<10?"0"+M:M) +""+ (d<10?"0"+d:d);
						for(var s=0; s<this.ltTod.length; s++){
							ts=this.ltTod[s].split(",");
							ts1=ts[0];
                            
							if(ts1==ts2){
								lturl="<a href="+ts[1]+" target='_blank'>"+d+"</a>";
								ltcss="ct2";
								p.t1 = ts[2];
								p.t2 = ts[3];
								if(p.t1 || p.t2){
								    p.onmouseover = this.showpopup;
								    p.onmouseout = this.hidepopup;
								}
								//lturl +='<span style="position:absolute;z-index:100;left:35px;top:30px;display:block;border:solid 1px blue;color:#888;background:red;width:200px;">'+ts[2]+'<br/>'+ts[3]+'</span>';								
								break;
							}
							else{
								lturl=d;
								ltcss="";
							}
							}
						p.className=ltcss;
						p.innerHTML=lturl;						
						d++;
					}
					start>w?start=w:start++;
					div.appendChild(p);
				}
			  this.calendarDateContainer.appendChild(div);
			}
		}

		/*设置日历控制菜单*/
		this.setMenu=function(dateObj){
			this.calendarMenuContainer.innerHTML="<table width='232' border='0' align='center' cellpadding='0' cellspacing='0'><tr class='ct1'><td width='50' align='right'><a  href='javascript:"+this.calendarName+".setMenu("+this.date.getThePreDate(dateObj,this.minYear)+");void(0)'><img src='/images/sblt/clf.gif' width='13' height='9' /></a></td><td width='70' height='22'>"+dateObj.Y+"-"+(dateObj.M>9?dateObj.M:"0"+dateObj.M)+"</td><td width='50' align='left'><a href='javascript:"+this.calendarName+".setMenu("+this.date.getTheNextDate(dateObj,this.maxYear)+");void(0)'><img src='/images/sblt/crt.gif' width='13' height='9' /></a></td></tr></table>";
			this.setDate(dateObj);
		}

		/*初始化*/
		this.initial=function(){
			this.calendarContainer=document.createElement("div");
			this.calendarContainer.style.cssText="display:"+this.display+";";
			this.calendarContainer.setAttribute("css",this.calendarContainer.style.cssText);
			this.calendarContainer.setAttribute("id","exCalen");
			this.calendarMenuContainer=document.createElement("div");
			
			this.calendarWeekContainer=document.createElement("div");
			this.calendarWeekContainer.setAttribute("id","exWeek");
			for(i=0;i<7;i++)
			{
				var p=document.createElement("p");
				p.innerHTML=this.weekAry[i];
				this.calendarWeekContainer.appendChild(p);
			}
			this.calendarDateContainer=document.createElement("div");
			this.calendarDateContainer.setAttribute("id","exDate");

			this.calendarContainer.appendChild(this.calendarMenuContainer);
			this.calendarContainer.appendChild(this.calendarWeekContainer); 
			this.calendarContainer.appendChild(this.calendarDateContainer); 

			document.getElementById("apCalen").appendChild(this.calendarContainer);
			this.setMenu(this.date.getTheDate());
		}
		
		
		this.showpopup = function(e){
		    var sf = this;
		    getpopup = function(t1, t2){
		        if(!sf.popup){
		            var el = document.createElement('div');
		            el.style.cssText = 'position:absolute;z-index:100;left:35px;top:30px;display:block;border:solid 1px #b3d581;color:#888;font-size:14px;background:white;border-bottom-width:2px;border-right-width:2px;padding:3px;';
    		        
		            document.body.appendChild(el);
		            sf.popup = el;
		        }
		        sf.popup.innerHTML = t1+"<br/>"+t2;
		        sf.popup.style.display = "block";
		        return sf.popup;
		    }
		
		    e = e || window.event;
		    function gett(el){
		        if(el.tagName.toUpperCase() == "P"){
		            return el;
		        }
		        return gett(el.parentNode);
		    }
		    var p = gett(e.target || e.srcElement);
		    
		    var popup = getpopup(p.t1, p.t2);
		    
		    var xy = [e.pageX || e.clientX, e.pageY || e.clientY];
		    if (document.all) {
		        xy[0] += getScroll()[1];
		        
		        xy[1] += getScroll()[0];
		        
		        
            }            
		    popup.style.left = (xy[0] + 18) + 'px';
		    popup.style.top = (xy[1] + 15)+ 'px';
		}
		this.hidepopup = function(e){
		    e = e || window.event;
		    
		    if(this.popup){
		        this.popup.style.display = "none";
		    }
		    
		}

			  this.initial();


	}
	//创建日历
	calendar=new MarcianCalendar("calendar",true,"2008","2011",false,false);

getScroll = function() {
    var dd = document.documentElement, db = document.body;
    if (dd && (dd.scrollTop || dd.scrollLeft)) {
        return [parseInt(dd.scrollTop), parseInt(dd.scrollLeft)];
    }else if (db) {
        return [parseInt(db.scrollTop), parseInt(db.scrollLeft)];
    } else {
        return [0, 0];
    }
}/*  |xGv00|51dcc07e73d5189e75eef80c0f1a02b1 */
