﻿// JScript 文件
    var currDate = new Date();
    var flag =false;
    
    
        var x = 0;
    var y = 0;
    function ShowHint(id,title,time,server)
    {
        var divS = document.getElementById("divHint");
        
        divS.style.display = "";
        
        divS.style.left=(x-95)+"px";
        divS.style.top=(y-100)+"px";
        
        document.getElementById("pServer").innerHTML = "["+server+"]";
        document.getElementById("pTitle").innerHTML = title;
        document.getElementById("pTime").innerHTML = time+" 震撼开启";

        return false;
    }
    function HideHint()
    {
        $("#divHint").hide();
    }
    function mousePosition(ev){
        if(ev.pageX || ev.pageY){
            return {x:ev.pageX, y:ev.pageY};
        }
        return {
            x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            y:ev.clientY + document.body.scrollTop  - document.body.clientTop
        };
    }
    function mouseMove(ev){
        ev = ev || window.event;
        var mousePos = mousePosition(ev);
        x = mousePos.x;
        y = mousePos.y;
    }
    document.onmousemove = mouseMove;  
    
    
function CalendarGetMaxDay(nowYear,nowMonth)			/// Get MaxDay of current month
{
	var nextMonth,nextYear,currDate2,nextDate,theMaxDay
	nextMonth=nowMonth+1;
	if(nextMonth>11)
	{
		nextYear=nowYear+1;
		nextMonth=0;
	}
	else	
	{
		nextYear=nowYear;	
	}
	currDate2=new Date(nowYear,nowMonth,1);
	nextDate=new Date(nextYear,nextMonth,1);
	theMaxDay=(nextDate-currDate2)/(24*60*60*1000);
	return theMaxDay;
}
function CalendarCellSetCss(sMode,oCell)			/// Set Cell Css
{
	// sMode
	// 0: OnMouserOut 1: OnMouseOver 
//	if(sMode)
//	{
//		oCell.style.border="1px solid #5589AA";
//		oCell.style.backgroundColor="#BCD0DE";
//	}
//	else
//	{
//		oCell.style.border="1px solid #FFFFFF";
//		oCell.style.backgroundColor="#FFFFFF";
//	}	
}
function setFlag()
{
    flag =true;
}
function CalendarRestore()					/// Clear Data
{
    var oTable=document.getElementById("cTable");
	for(i=1;i<oTable.rows.length;i++)
	{
		for(j=0;j<oTable.rows[i].cells.length;j++)
		{
			CalendarCellSetCss(0,oTable.rows[i].cells[j]);
			oTable.rows[i].cells[j].innerHTML="&nbsp;";
		}
	}	
}
function CalendarRefresh(newDate)					///
{
	currDate=newDate;
	CalendarRestore();	
	CalendarFill();	
}
function prevM(oInstance)  //往前翻月份
{
    setFlag();
    var sDay,sMonth,sYear,newDate
	sYear=currDate.getFullYear();
	sMonth=currDate.getMonth();
	sDay = currDate.getDate();
	if(sMonth==2&&sDay>28)
	{
	    sDay = 1;
	}
	if(sMonth<=0)
	{
	    sYear--;
	    sMonth = 11;
	}else
	{
	    sMonth--;
	}    
    CalendarRefresh(new Date(sYear,sMonth,sDay));
}
function nextM(oInstance)  //往后翻月份
{
    setFlag();
    var sDay,sMonth,sYear,newDate
	sYear=currDate.getFullYear();
	sMonth=currDate.getMonth();
	sDay = currDate.getDate();
	if(sMonth==0&&sDay>28)
	{
	    sDay = 1;
	}
	
	if(sMonth>=12)
	{
	    sYear--;
	}else
	{
	    sMonth++;
	}
	
    CalendarRefresh(new Date(sYear,sMonth,sDay));
}

var item = "";
var beginItem = "";
var endItem = "";
function CalendarFill()			///
{
    

    var weekDaySting = new Array("日", "一", "二", "三", "四", "五", "六");
    var monthSting = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");    
    var daysMonth  = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var sMonth,sYear,sWeekDay,sToday,oTable,currRow,MaxDay,sDaySn,sIndex,rowIndex,cellIndex,oSelectMonth,oSelectYear
	sMonth=currDate.getMonth();
	sYear = currDate.getFullYear();

	sWeekDay=(new Date(sYear,sMonth,1)).getDay();

	sToday = document.getElementById("hidDay").value;// currDate.getDate();

    

	oTable=document.getElementById("cTable");
	currRow=oTable.rows[1];
    $.ajax({
                type:"GET",
                url:"/GetNewServerInfo.aspx",
                data:"date="+sYear+"-"+(sMonth+1)+"-"+currDate.getDate()+"&dtime="+(new Date().getTime()),
                dateType:"html",
                async:false,
                error:function(){},
                success:function(msg){
                
                    var serverinfos = msg.split("$");
                    if(serverinfos.length>2)
                    {
                        beginItem = serverinfos[0];
                        item = serverinfos[1];
                        endItem = serverinfos[2];
                    
                    }
                }
          });
          
	MaxDay=CalendarGetMaxDay(sYear,sMonth);
	

    document.getElementById("spanYM").innerHTML = sYear+"年"+ (sMonth+1)+"月";

    var tempY2 = document.getElementById("hidYear").value;

    var tempM2 = document.getElementById("hidMonth").value;
    
    var tempCurD = new Date(tempY2,(tempM2-1),sToday);
    
    var items = item.split("*");
    var tempType = "y";
    var tempType2 = "y";
    var houZhui = "jpg";
	for(sDaySn=1,sIndex=sWeekDay;sIndex<=6;sDaySn++,sIndex++)
	{
		    houZhui = "jpg";
		    var tempDT = new Date(sYear,(sMonth),sDaySn);
	        if(sDaySn==sToday&&tempY2==sYear&&tempM2==(sMonth+1))
		    {
		        tempType="y"
		        tempType2 = "w";
		        houZhui = "gif";
		    }else if(tempDT>tempCurD)//(sDaySn>sToday&&sYear>=tempY2&&(sMonth+1)>=tempM2)
		    {
		        tempType = "w";
		        tempType2 = "y";
		    }
		    else// if(sToday>sDaySn&&tempY2>=sYear&&tempM2>=(sMonth+1))		    
		    {
		        tempType="b"
		        tempType2 = "y";
		    }
        if(!getKaiFuInfo(currRow.cells[sIndex],item,items,sYear,sMonth+1,sDaySn,tempType,houZhui))
        {
		    if(sDaySn==sToday)
		    {
			    currRow.cells[sIndex].innerHTML="<img src='/images/change_pic/"+tempType2+"/"+tempType2+"_"+sDaySn+".jpg'>";
		    }
		    else
		    {
			    currRow.cells[sIndex].innerHTML="<img src='/images/change_pic/y/y_"+sDaySn+".jpg'>";
		    }
		    
		}
		CalendarCellSetCss(0,currRow.cells[sIndex]);
	}
	
	
	
	var duoyuN = 1;
	
	var rown = 0;
	var coln = 0;
	
	var tempC = 0;
	
	for(rowIndex=2;rowIndex<=6;rowIndex++)
	{
		if(sDaySn>MaxDay)
		{
		    ///自增数大于当月的天数并且等于6说明是最后一个
            if(tempC==6)
            {
	            rown = rowIndex;
	            coln = 0;
            }

		    break;
		}
		currRow=oTable.rows[rowIndex];
		for(cellIndex=0;cellIndex<currRow.cells.length;cellIndex++)
		{
		    houZhui = "jpg";
		    var tempDT = new Date(sYear,(sMonth),sDaySn);
	        if(sDaySn==sToday&&tempY2==sYear&&tempM2==(sMonth+1))
		    {
		        tempType="y";
		        tempType2 = "w";
		        houZhui = "gif";
		    }else  if(tempDT>tempCurD)//if(sDaySn>sToday&&sYear>=tempY2&&(sMonth+1)>=tempM2)
		    {
		        tempType = "w";
		        tempType2 = "y";
		    }
		    else// if(sToday>sDaySn&&tempY2>=sYear&&tempM2>=(sMonth+1))		    
		    {
	            tempType="b"
	            tempType2 = "y";
		    }
        
            if(!getKaiFuInfo(currRow.cells[cellIndex],item,items,sYear,sMonth+1,sDaySn,tempType,houZhui))
            {
            
			    if(sDaySn==sToday)
			    {
			        currRow.cells[cellIndex].innerHTML="<img src='/images/change_pic/"+tempType2+"/"+tempType2+"_"+sDaySn+".jpg'>";
    				
			    }
			    else
			    {
			        currRow.cells[cellIndex].innerHTML="<img src='/images/change_pic/y/y_"+sDaySn+".jpg'>";
			    }
			    CalendarCellSetCss(0,currRow.cells[cellIndex]);
			    
    		
		    } 
		    if(sDaySn>MaxDay)
		    {
		        rown = rowIndex;
		        coln = cellIndex;
		        break;	
		    }
			sDaySn++;
			tempC = cellIndex;
			
        }
	}
	
	houZhui = "jpg";
	if(rown==6&&coln>0&&sToday>19)
	{
	    if(MaxDay>29)
	    {
            oTable.rows[1].style.display="none";
            oTable.rows[6].style.display="";
	    }else
        {
            oTable.rows[6].style.display="none";
            oTable.rows[1].style.display="";
        }
	}
	else
	{
	        oTable.rows[1].style.display="";
	        oTable.rows[6].style.display="none";
	}
	
	///显示上个月的多余日历
	
	if(sWeekDay>0)
	{
	
        var beginItems = beginItem.split("*");
        var tempBMonth = sMonth;
        var tempBYear  = sYear;
        if(sMonth<1)
        {
            tempBMonth = 12;
            tempBYear --;
        }
	    currRow=oTable.rows[1];
	     var tempMonth = sMonth;
        if(sMonth>0)
        {
            tempMonth = sMonth-1;
        }
	    var duoyuN = daysMonth[tempMonth];
    	var pType = "b";
	    for(col=sWeekDay-1;col>=0;col--)
	    {
	    
	        
		    var tempDT = new Date(tempBYear,tempMonth,duoyuN);
		    if(tempDT>tempCurD)
		    {
		        pType = "w";
		    }
	        if(!getKaiFuInfo(currRow.cells[col],beginItem,beginItems,tempBYear,tempBMonth,duoyuN,pType,houZhui))
            {
		        currRow.cells[col].innerHTML="<img src='/images/change_pic/b/b_"+duoyuN+".jpg'>";
		    }
	        duoyuN--;
	    }
	    duoyuN = 1;
	}
	
	///显示下个月的多余日历

//    coln++;
    
    
    var tempEMonth = sMonth+2;
    var tempEYear  = sYear;
    if(tempEMonth>12)
    {
        tempEMonth = 1;
        tempEYear ++;
    }
    var endItems = endItem.split("*");
    var ppType = "b";
    
    for(row=rown;row<=6;row++)
    {
        currRow=oTable.rows[row];
        for(cellIndex=coln;cellIndex<7;cellIndex++)
	    {
	    
		    var tempDT = new Date(tempEYear,(tempEMonth-1),duoyuN);
		    
		    if(tempDT>tempCurD)
		    {
		        ppType = "w";
		    }
	        if(!getKaiFuInfo(currRow.cells[cellIndex],endItem,endItems,tempEYear,tempEMonth,duoyuN,ppType,houZhui))
            {
		        currRow.cells[cellIndex].innerHTML="<img src='/images/change_pic/b/b_"+duoyuN+".jpg'>";
		    }
	        duoyuN++;
	    }
	    coln=0;
    }
}

function getKaiFuInfo(currRow,item,items,year,month,day,type,houZhui)
{
    if(currRow.innerHTML.indexOf("bmp")<0&&item.indexOf(year)>-1&&item.indexOf(day)>-1)
    {
        if(items.length>0)
        {
            for(i=0;i<items.length;i++)
            {
                var itemss = items[i].split("#");
                if(itemss.length>1)
                {
                    if(itemss[0]==year&&itemss[1]==month&&itemss[2]==day)
                    {
                        currRow.innerHTML= "<a href='"+itemss[5]+"' target='_blank'><img src='/images/change_pic/"+type+"/"+type+"_s_"+itemss[3]+"."+houZhui+"' onmouseover=\"javascript:ShowHint("+itemss[3]+",'"+itemss[4]+"','"+itemss[7]+"','"+itemss[8]+"');\" onmouseout='javascript:HideHint();'/></a>";
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

