//初始化日历
function initCalendar() {
   //设置选择框
	/* 以后使用新的日期选择界面 */
	/*
	var cldNav = document.forms['cldNav'];
	cldNav.SY.selectedIndex=g_tY-1900;
	cldNav.SM.selectedIndex=g_tM;   
	*/
	/* 新的日期选择 */
	document.getElementById('yearValue').innerHTML = g_cldYear+"年&nbsp;"+g_cldMonth+"月";
	//document.getElementById('monthValue').innerHTML = g_cldMonth+"月";
	dateSelection.init(g_cldYear, g_cldMonth);
   //画日历
   drawCld(g_cldYear,g_cldMonth);
}


//=======画日历在网页上
function drawCld(year,month) {  
   var i,sD,s,size;
   var gObj,sObj,lObj,schdObj;
   var thisMonth = false;

  var SY = parseFloat(year);
  var SM = parseFloat(month) -1;
   
  //判断是否是本月
  if(g_tY==SY&&g_tM==SM)
    thisMonth = true;
    
  g_cld = new calendar(SY,SM);
  
  document.getElementById("lunarYear").innerHTML = ' 农历 ' + cyclical(SY-1900+36) + '年 【'+Animals[(SY-4)%12]+'年】';

  //YMBG.innerHTML = "&nbsp;" + SY + "<BR>&nbsp;" + monthName[SM];

  for(i=0;i<42;i++) {

    gObj=document.getElementById("GD"+i);//单元格
    sObj=document.getElementById("SD"+i);//日期
    lObj=document.getElementById("LD"+i);//节日

    //清空单元格样式
    gObj.className = 'notDateTd';
    sObj.className = '';
    lObj.className = '';
    
    //清空内容
    sObj.innerHTML = '';
    lObj.innerHTML = '';

    //清空日程提醒中的内容
    schdObj=document.getElementById('SCHD'+ i);
    schdObj.innerHTML = '';
    sD = i - g_cld.firstWeek;
      
    if(sD>-1 && sD<g_cld.length) { //日期内
      gObj.className = "dateTd";

      //ie鼠标悬停实现
		if($.browser.msie){
			$(gObj).hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});
		}
      
      sObj.className = "dateNum";
      sObj.innerHTML = sD+1;
      lObj.className = "dateLunar";
      
      //今日颜色
      if(thisMonth&&(sD+1)==g_tD){
        gObj.className = 'todayTd'; 
        sObj.className = 'todayDateNum'; //今日颜色
      }

      sObj.style.color = g_cld[sD].color; //法定假日颜色


      if(g_cld[sD].lDay==1) //显示农历月
        lObj.innerHTML = (g_cld[sD].isLeap?'闰':'') + g_cld[sD].lMonth + '月' + (monthDays(g_cld[sD].lYear,g_cld[sD].lMonth)==29?'小':'大');
      else //显示农历日
        lObj.innerHTML = cDay(g_cld[sD].lDay);

      s=g_cld[sD].lunarFestival;
      if(s.length>0) { //农历节日
        if(s.length>6) s = s.substr(0, 4)+'...';
        lObj.className = "lunarFestival";
      }
      else { //廿四节气
        s=g_cld[sD].solarTerms;
        if(s.length>0){
          lObj.className = "solarTerm";             
          if((s =='清明')||(s =='芒种')||(s =='夏至')||(s =='冬至')){
            lObj.className = "lunarFestival";
            if(s =='清明') s = '清明节';
          }             
        }
        else { //公历节日
          s=g_cld[sD].solarFestival;
          if(s.length>0) {
            if(s.length>6) s = s.substr(0, 4)+'...';
            lObj.className = "solarFestival";
          }
        }
      }
      if(s.length>0) lObj.innerHTML = s;
    }
  }

}


//=============================显示详细日期及日程资料
function showDetail(event, v){  
	var s, festival;
	var sObj = document.getElementById('SD'+ v);
	var detail = document.getElementById("detail");
	var detailFestival = document.getElementById("detailFestival");
	var detailDate = document.getElementById("detailDate");
	var detailLunar = document.getElementById("detailLunar");
	var detailJiuLi = document.getElementById("detailJiuLi");
	var detailHL_y = document.getElementById("detailHL_y");
	var detailHL_j = document.getElementById("detailHL_j");

	if (sObj!= null && sObj.innerHTML != '') 
	{
		var d = parseInt(sObj.innerHTML) - 1;
		
		if (g_cld[d].solarTerms == '' && g_cld[d].solarFestival == '' && g_cld[d].lunarFestival == '')
		{
		   detailFestival.innerHTML = '';
	  }
		else
		{		   
		   detailFestival.innerHTML = '<br>'+g_cld[d].solarTerms + ' ' + g_cld[d].solarFestival + ' ' + g_cld[d].lunarFestival;
		}
		
		detailDate.innerHTML = g_cld[d].sYear+'年'+g_cld[d].sMonth+'月'+g_cld[d].sDay+'日          星期'+g_cld[d].week;
		detailLunar.innerHTML = '农历'+(g_cld[d].isLeap?'闰 ':' ')+g_cld[d].lMonth+' 月 '+g_cld[d].lDay+' 日';
		detailJiuLi.innerHTML = g_cld[d].cYear+'年 '+g_cld[d].cMonth+'月 '+g_cld[d].cDay + '日';
		
		//存储一下选择的日，供程序其它部分使用
		g_selectedDay = g_cld[d].sDay;

		if(g_cld[d].sYear == 2008){
		  var hl = eval("HuangLi.d"+(g_cld[d].sMonth<10?('0'+g_cld[d].sMonth):g_cld[d].sMonth)+(g_cld[d].sDay<10?('0'+g_cld[d].sDay):g_cld[d].sDay));
		  if(hl){
  	    detailHL_y.style.display = "";
  	    detailHL_j.style.display = "";
  		  detailHL_y.innerHTML = hl.y;
  		  detailHL_j.innerHTML = hl.j;
  		}
  		else{
  	    detailHL_y.style.display = "none";
  	    detailHL_j.style.display = "none";
  		}
		}
		else{
	    detailHL_y.style.display = "none";
	    detailHL_j.style.display = "none";
		  detailHL_y.innerHTML = '';
		  detailHL_j.innerHTML = '';
		}

    //显示详细信息中的各种列表数据
    CalendarData.showDetailData(g_cld[d].sDay,v%7==0?7:v%7);//参数：几号，周几（从1到7）
    
	detail.style.display = 'block';
	
    //计算detail层的高度和宽度
    layoutDetail();

	}
}

//调整detail层的大小
function layoutDetail(){
  var el = document.getElementById("detailOptPane");
  var st= el.style;
  st.height = '0px';
    
//  window.alert("offsetHeight="+el.offsetHeight+" clientHeight="+el.clientHeight+" scrollHeight"+el.scrollHeight);
  if(el.scrollHeight > g_detail_maxHeight){
    st.height = g_detail_maxHeight+"px";    
    st.overflowY = "auto";
    //如果是IE7
    if(MzBrowser.version==7)
      st.paddingRight = "20px";
  }
  else{
    st.height = el.scrollHeight+"px";
    //st.overflowY = "hidden";
    //如果是IE7
    if(MzBrowser.version==7)
      st.paddingRight = "";
  }
}

//========================隐藏详细信息窗口
function closeDetail(){
	$('#detail').hide();
}

//显示日历详细信息中的数据
function showDetailPane(html){
  document.getElementById("detailOptPane").innerHTML = html;
  layoutDetail();
}



//================================点击变更年月的按钮后触发
function navCld(K){
	$('#dateSelectionDiv').hide();
	switch (K) {
		case 'YU':
			dateSelection.prevYearOne();
			break;
		case 'YD':
			dateSelection.nextYearOne();
			break;
		case 'MU':
			dateSelection.prevMonth();
			break;
		case 'MD':
			dateSelection.nextMonth();
			break;
	}
	dateSelection.commit();
}


//==============================变更日期后处理
function changeCld() {
  var cldNav = document.forms['cldNav'];
	/*
	g_cldYear = cldNav.SY.selectedIndex+1900;
	g_cldMonth = cldNav.SM.selectedIndex+1;
	*/
	g_cldYear = cldNav.SY.value;
	g_cldMonth = cldNav.SM.value;
	//g_cldMonth = g_cldMonth<10?'0'+g_cldMonth:g_cldMonth;
	document.getElementById('yearValue').innerHTML = g_cldYear+"年&nbsp;"+g_cldMonth+"月";
	//document.getElementById('monthValue').innerHTML = g_cldMonth+"月";
	drawCld(g_cldYear, g_cldMonth);
  CalendarData.drawCalendarData();
}

var dateSelection = new function() {
	this.currYear = 0;
	this.currMonth = 0;

	this.beginYear = 0;
	this.endYear = 0;

	this.minYear = 1901;
	this.maxYear = 2100;

	this.tmpYear = 0;
	this.tmpMonth = 0;

	this.init = function(year, month) {
		this.setYear(year);
		this.setMonth(month);
		this.showYearContent();
		this.showMonthContent();
	};
	this.today = function() {
		var today = new Date();
		var year = today.getFullYear();
		var month = today.getMonth() + 1;
		month = month < 10 ? '0' + month : month;
		
		if (this.currYear != year || this.currMonth != month) {
			if (this.tmpYear == year && this.tmpMonth == month) {
				this.rollback();
			}
			else {
				this.init(year, month);
				this.commit();
			}
		}
	};
	this.commit = function() {
		if (this.tmpYear != 0 || this.tmpMonth != 0) {
			this.tmpYear = 0;
			this.tmpMonth = 0;
			changeCld();
		}
	};
	this.rollback = function() {
		if (this.tmpYear != 0) {
			this.setYear(this.tmpYear);
		}
		if (this.tmpMonth != 0) {
			this.setMonth(this.tmpMonth);
		}
		this.showYearContent();
		this.showMonthContent();
	};
	this.prevMonth = function() {
		var month = this.currMonth - 1;
		if (month == 0) {
			var year = this.currYear - 1;
			if (year >= this.minYear) {
				month = 12;
				this.setYear(year);
			}
			else {
				month = 1;
			}
		}
		month = month < 10 ? '0' + month : month;
		this.setMonth(month);
	};
	this.nextMonth = function() {
		var month = this.currMonth - 0 + 1;
		if (month == 13) {
			var year = this.currYear + 1;
			if (year <= this.maxYear) {
				month = 1;
				this.setYear(year);
			}
			else {
				month = 12;
			}
		}
		month = month < 10 ? '0' + month : month;
		this.setMonth(month);
	};
	this.prevYearOne = function() {
		var year = this.currYear - 1;
		if (year >= this.minYear) {
			if (year < this.beginYear) {	//如果需要翻页
				this.prevYear();
			}
			this.setYear(year);
		}
	};
	this.nextYearOne = function() {
		var year = this.currYear - 0 + 1;
		if (year <= this.maxYear) {
			if (year > this.endYear) {	//如果需要翻页
				this.nextYear();
			}
			this.setYear(year);
		}
	};
	this.prevYear = function() {
		this.endYear = this.beginYear - 1;
		this.showYearContent(null, this.endYear);
	};
	this.nextYear = function() {
		this.beginYear = this.endYear + 1;
		this.showYearContent(this.beginYear, null);
	};
	this.setYear = function(value) {
		if (this.tmpYear == 0) {
			this.tmpYear = this.currYear;
		}
		$('#SY' + this.currYear).removeClass('curr');
		this.currYear = value;
		document.forms['cldNav'].SY.value = value;
		$('#SY' + this.currYear).addClass('curr');
	};
	this.setMonth = function(value) {
		if (this.tmpMonth == 0) {
			this.tmpMonth = this.currMonth;
		}
		$('#SM' + this.currMonth).removeClass('curr');
		this.currMonth = value;
		document.forms['cldNav'].SM.value = value;
		$('#SM' + this.currMonth).addClass('curr');
	}
	this.showYearContent = function(beginYear, endYear) {
		if (!beginYear) {
			if (!endYear) {
				endYear = this.currYear + 1;
			}
			this.endYear = endYear;
			if (this.endYear > this.maxYear) {
				this.endYear = this.maxYear;
			}
			this.beginYear = this.endYear - 3;
			if (this.beginYear < this.minYear) {
				this.beginYear = this.minYear;
				this.endYear = this.beginYear + 3;
			}
		}
		if (!endYear) {
			if (!beginYear) {
				beginYear = this.currYear - 2;
			}
			this.beginYear = beginYear;
			if (this.beginYear < this.minYear) {
				this.beginYear = this.minYear;
			}
			this.endYear = this.beginYear + 3;
			if (this.endYear > this.maxYear) {
				this.endYear = this.maxYear;
				this.beginYear = this.endYear - 3;
			}
		}

		var s = '';
		for (var i = this.beginYear; i <= this.endYear; i ++) {
			s += '<span id="SY' + i + '" class="year" onclick="dateSelection.setYear(' + i + ')">' + i + '</span>';
		}
		document.getElementById('yearListContent').innerHTML = s;
		$('#SY' + this.currYear).addClass('curr');
	};
	this.showMonthContent = function() {
		var s = '';
		for (var i = 1; i <= 12; i ++) {
			var monthValue = i < 10 ? '0' + i : i;
			s += '<span id="SM' + monthValue + '" class="month" onclick="dateSelection.setMonth(\'' + monthValue + '\')">' + i + '</span>';
		}
		document.getElementById('monthListContent').innerHTML = s;
		$('#SM' + this.currMonth).addClass('curr');
	};
};

function dateSelectionOk() {
	$('#dateSelectionDiv').hide();
	dateSelection.commit();
}
function dateSelectionToday() {
	$('#dateSelectionDiv').hide();
	dateSelection.today();
}
function dateSelectionCancel() {
	$('#dateSelectionDiv').hide();
	dateSelection.rollback();
}