//控制书图片大小
function suitImage(img,w,h)
{
		var image=new Image();
        image.src=img.src;

		var iw=image.width;
		var ih=image.height;
		var iratio=iw/ih;
        
        if(iw>w)
		{
                iw=w;
				ih=w/iratio;
        }
        if(ih>h)
		{
                ih=h;
                iw=h*iratio;
        }  
		img.width=iw;
		img.height=ih;
}

function atCalendarControl(){
  var calendar=this;
  this.calendarPad=null;
  this.prevMonth=null;
  this.nextMonth=null;
  this.prevYear=null;
  this.nextYear=null;
  this.goToday=null;
  this.calendarClose=null;
  this.calendarAbout=null;
  this.head=null;
  this.body=null;
  this.today=[];
  this.currentDate=[];
  this.sltDate;
  this.target;
  this.source;

  /************** 加入日历底板及阴影 *********************/
  this.addCalendarPad=function(){
   document.write("<div id='divCalendarpad' style='position:absolute; width:150;height:100;background-color:#ffffff;'>");
   document.write("<iframe  frameborder=0 height=0 width=150></iframe>");
   document.write("</div>");
   calendar.calendarPad=document.all.divCalendarpad;
  }
  /************** 加入日历面板 *********************/
  this.addCalendarBoard=function(){
   var BOARD=this;
   var divBoard=document.createElement("div");
   calendar.calendarPad.insertAdjacentElement("beforeEnd",divBoard);
   divBoard.style.cssText="position:relative;top:0;left:0;width:150;height:100;background-color:#F7F7F7;";
   var tbBoard=document.createElement("table");
   divBoard.insertAdjacentElement("beforeEnd",tbBoard);
   tbBoard.style.cssText="width:100%;height:10;font-size:9pt;";
   tbBoard.cellPadding=0;
   tbBoard.cellSpacing=1;
   tbBoard.bgColor="#F7F7F7";

  /************** 设置第一行的日期显示 *********************/
   /*********** Calendar Head ***************/
   trRow = tbBoard.insertRow(0);
   tbCell=trRow.insertCell(0);
   tbCell.colSpan=7;
   tbCell.height=18;
   //tbCell.background="images/right1.gif";
   tbCell.align="center";
   tbCell.style.cssText = "cursor:default;color: #000000;font-size:10pt;font-weight: bold;";
   calendar.head=tbCell;
  /*********** 设置第二行的Calendar PrevYear Button ***************/
   trRow = tbBoard.insertRow(1);
   calendar.prevYear = calendar.insertTbCell(trRow,0,"&lt;&lt;","center");
   calendar.prevYear.title="上一年";
   calendar.prevYear.onmousedown=function(){
    calendar.currentDate[0]--;
    calendar.show(calendar.target,calendar.currentDate[0]+"-"+calendar.currentDate[1]+"-"+calendar.currentDate[2],calendar.source);
   }
   /*********** 设置第二行的Calendar PrevMonth Button ***************/
   calendar.prevMonth = calendar.insertTbCell(trRow,1,"&lt;","center");
   calendar.prevMonth.title="上一月";
   calendar.prevMonth.onmousedown=function(){
	calendar.currentDate[1]--;
    if(calendar.currentDate[1]==0){
     calendar.currentDate[1]=12;
     calendar.currentDate[0]--;
    }
	if(calendar.currentDate[1]<10){
		calendar.currentDate[1]="0"+calendar.currentDate[1];    //小于两位数的月份前加0	
	}
    calendar.show(calendar.target,calendar.currentDate[0]+"-"+calendar.currentDate[1]+"-"+calendar.currentDate[2],calendar.source);
   }
   /*********** 设置第二行的Calendar Today Button ***************/
   calendar.goToday = calendar.insertTbCell(trRow,2,"今天","center",3);
   calendar.goToday.title="选择今天";
   calendar.goToday.onclick=function(){
    calendar.sltDate=calendar.today[0]+"-"+calendar.today[1]+"-"+calendar.today[2];
	//alert(calendar.sltDate);
    calendar.target.value=calendar.sltDate;
    calendar.show(calendar.target,calendar.today[0]+"-"+calendar.today[1]+"-"+calendar.today[2],calendar.source);
    window.location="more.php?date="+calendar.sltDate;  //根据选择日期查询数据库
   }
   /*********** 设置第二行的Calendar NextMonth Button ***************/
   calendar.nextMonth = calendar.insertTbCell(trRow,3,"&gt;","center");
   calendar.nextMonth.title="下一月";
   calendar.nextMonth.onmousedown=function(){
    calendar.currentDate[1]++;
    if(calendar.currentDate[1]==13){
     calendar.currentDate[1]=1;
     calendar.currentDate[0]++;
    }
	if(calendar.currentDate[1]<10){
		calendar.currentDate[1]="0"+calendar.currentDate[1];   ////小于两位数的月份前加0	
	}
    calendar.show(calendar.target,calendar.currentDate[0]+"-"+calendar.currentDate[1]+"-"+calendar.currentDate[2],calendar.source);
   }
   /*********** 设置第二行的Calendar NextYear Button ***************/
   calendar.nextYear = calendar.insertTbCell(trRow,4,"&gt;&gt;","center");
   calendar.nextYear.title="下一年";
   calendar.nextYear.onmousedown=function(){
    calendar.currentDate[0]++;
    calendar.show(calendar.target,calendar.currentDate[0]+"-"+calendar.currentDate[1]+"-"+calendar.currentDate[2],calendar.source);
}

   /*********** 设置第三行的星期几的显示 ***************/
   trRow = tbBoard.insertRow(2);
   var cnDateName = new Array("日","一","二","三","四","五","六");
   for (var i = 0; i < 7; i++) {
    tbCell=trRow.insertCell(i)
    tbCell.innerText=cnDateName[i];
    tbCell.align="center";
    tbCell.width=24;
	if(i==0||i==6)
    tbCell.style.cssText="cursor:default;border:1 solid #EEEEEE;background-color:#DFDFDF;COLOR: #FF00FF;";	
	else
	tbCell.style.cssText="cursor:default;border:1 solid #EEEEEE;background-color:#DFDFDF;";
   }

   /*********** Calendar Body ***************/
   trRow = tbBoard.insertRow(3);
   tbCell=trRow.insertCell(0);
   tbCell.colSpan=7;
   tbCell.height=65;
   tbCell.vAlign="top";
   tbCell.bgColor="#F7F7F7";
   var tbBody=document.createElement("table");
   tbCell.insertAdjacentElement("beforeEnd",tbBody);
   tbBody.style.cssText="width:150;height:80;font-size:9pt;"
   tbBody.cellPadding=0;
   tbBody.cellSpacing=0;
   calendar.body=tbBody;
  }
  /************** 加入功能按钮公共样式 *********************/
  this.insertTbCell=function(trRow,cellIndex,TXT,trAlign,tbColSpan){
   var tbCell=trRow.insertCell(cellIndex);
   if(tbColSpan!=undefined) tbCell.colSpan=tbColSpan;

   var btnCell=document.createElement("button");
   tbCell.insertAdjacentElement("beforeEnd",btnCell);
   btnCell.value=TXT;
   btnCell.style.cssText="width:100%;border:1 outset;background-color:buttonface;";
   btnCell.onmouseover=function(){
    btnCell.style.cssText="width:100%;border:1 outset;background-color:#F7F7F7;";
	}
    btnCell.onmouseout=function(){
    btnCell.style.cssText="width:100%;border:1 outset;background-color:buttonface;";
   }
  // btnCell.onmousedown=function(){
  //  btnCell.style.cssText="width:100%;border:1 inset;background-color:#F0F0F0;";
  // }
   btnCell.onmouseup=function(){
    btnCell.style.cssText="width:100%;border:1 outset;background-color:#F7F7F7;";
   }
   btnCell.onclick=function(){
    btnCell.blur();
   }
   return btnCell;
  }
  this.setDefaultDate=function(){
	   var dftDate=new Date();
	   calendar.today[0]=dftDate.getYear();
	   calendar.today[1]=dftDate.getMonth()+1;	  
	   if(calendar.today[1]<10){	   
		  calendar.today[1]="0"+calendar.today[1];    //小于两位数的月份前加0		  
		}	
	   calendar.today[2]=dftDate.getDate();
	   if(calendar.today[2]<10){
		   calendar.today[2]="0"+calendar.today[2];   //小于两位数的日期前加0
	   }
  }

  /****************** Show Calendar *********************/
  this.show=function(targetObject,defaultDate,sourceObject){
   if(targetObject==undefined) {
    alert("未设置目标对像. \n方法: ATCALENDAR.show(obj 目标对像,string 默认日期,obj 点击对像);\n\n目标对像:接受日期返回值的对像.\n默认日期:格式为\"yyyy-mm-dd\",缺省为当日日期.\n点击对像:点击这个对像弹出calendar,默认为目标对像.\n");
    return false;
   }
   else    calendar.target=targetObject;
   if(sourceObject==undefined) calendar.source=calendar.target;
   else calendar.source=sourceObject;

   var firstDay;
   var Cells=new Array();
//alert(defaultDate);
   if(defaultDate==undefined || defaultDate=="" || defaultDate==null ){
    var theDate=new Array();
    calendar.head.innerText = calendar.today[0]+"-"+calendar.today[1]+"-"+calendar.today[2];
    theDate[0]=calendar.today[0]; theDate[1]=calendar.today[1]; theDate[2]=calendar.today[2];
   }
   else{
    var reg=/^\d{4}-\d{1,2}-\d{2}$/
    if(!defaultDate.match(reg)){
     alert("默认日期的格式不正确\n\n默认日期可接受格式为:'yyyy-mm-dd'");
     return;
    }
    var theDate=defaultDate.split("-");
    calendar.head.innerText = defaultDate;
   }
   calendar.currentDate[0]=theDate[0];
   calendar.currentDate[1]=theDate[1];   
   calendar.currentDate[2]=theDate[2];
   theFirstDay=calendar.getFirstDay(theDate[0],theDate[1]);
   theMonthLen=theFirstDay+calendar.getMonthLen(theDate[0],theDate[1]);
   //calendar.setEventKey();

   calendar.calendarPad.style.display="";
   var theRows = Math.ceil((theMonthLen)/7);
   //清除旧的日历;
   while (calendar.body.rows.length > 0) {
    calendar.body.deleteRow(0)
   }
   //建立新的日历;
   var n=0;day=0;
   for(i=0;i<theRows;i++){
    theRow=calendar.body.insertRow(i);
    for(j=0;j<7;j++){
     n++;
     if(n>theFirstDay && n<=theMonthLen){
      day=n-theFirstDay;
      calendar.insertBodyCell(theRow,j,day);
     }
     else{
      var theCell=theRow.insertCell(j);
      theCell.style.cssText="background-color:#F7F7F7;cursor:default;";
     }
    }
   }

  
  }
  

   //************* 插入日期单元格 **************/
  this.insertBodyCell=function(theRow,j,day,targetObject){
   var theCell=theRow.insertCell(j);
   if(j==0||j==6)
	   theCell.style.cssText="border:1 solid #CCCCCC;cursor:hand;COLOR:#FF00FF;";
   else 
	   theCell.style.cssText="border:1 solid #CCCCCC;cursor:hand;";
   if(day==calendar.currentDate[2]) 
	   theCell.style.cssText="border:1 solid #CCCCCC;cursor:hand;";
   if(day==calendar.today[2]) 
	   theCell.style.cssText="border:1 solid #CCCCCC;cursor:hand;COLOR:#FF0000;font-size:16px;font-weight:bold"; //日期字体
   var theBgColor="#ffffff";
   theCell.bgColor=theBgColor;  
   theCell.innerText=day;
   theCell.align="center";
   theCell.width=24;   
   theCell.onmouseover=function(){ 
    theCell.bgColor="#F7F7F7";     
   }
   theCell.onmouseout=function(){ 
    theCell.bgColor=theBgColor;     
   }
   theCell.onmousedown=function(){ 
    theCell.bgColor="#F7F7F7"; 
   }
   /****************NEW点击日历日期所出发的事件******************************/
   theCell.onclick=function(){
    //alert(calendar.currentDate[1]);
    if(calendar.currentDate[1].length<2) calendar.currentDate[1]="0"+calendar.currentDate[1];
	//alert(calendar.currentDate[1]);
	calendar.currentDate[1];
    if(day.toString().length<2) day="0"+day;
    calendar.sltDate=calendar.currentDate[0]+"-"+calendar.currentDate[1]+"-"+day;
    calendar.target.value=calendar.sltDate;
	calendar.head.innerText = calendar.sltDate;	
	window.location="more.php?date="+calendar.sltDate;  //根据选择日期查询数据库
   }
  }
  /************** 取得月份的第一天为星期几 *********************/
  this.getFirstDay=function(theYear, theMonth){
   var firstDate = new Date(theYear,theMonth-1,1);
   return firstDate.getDay();
  }
  /************** 取得月份共有几天 *********************/

 
  this.getMonthLen=function(theYear, theMonth) {
   theMonth--;
   var oneDay = 1000 * 60 * 60 * 24;
   var thisMonth = new Date(theYear, theMonth, 1);
   var nextMonth = new Date(theYear, theMonth + 1, 1);
   var len = Math.ceil((nextMonth.getTime() - thisMonth.getTime())/oneDay);
   return len;
  }
  /************** 隐藏日历 *********************/
  /*this.hide=function(){
   //calendar.clearEventKey();
   calendar.calendarPad.style.display="none";
  }*/
  /************** 从这里开始 *********************/
  this.setup=function(defaultDate){
   calendar.addCalendarPad();
   calendar.addCalendarBoard();
   calendar.setDefaultDate();
  }
 
  calendar.setup();
 }
