var dd,mm,yy;
var curdate,oneday; 

function timer0OnTimer()
{
  var dt = new Date();
  var d = dt.getDate();
  var m = dt.getMonth()+1;
  var y = dt.getYear();
  var w = dt.getDay();
  
 
  if((dd==d)&&(mm==m)&&(yy==y))
    return;         
    
  dd = d;
  mm = m;
  yy = y;
  
 
  curdate = dt.getTime();
  GotoDate( curdate );
}


function widgetOnInit()
{
  loadSetting();
  
  curdate = (new Date()).getTime();
  oneday =  1000 * 60 * 60 * 24;
  timer0OnTimer();
}

function widgetOnDock()
{
  scale = 95;
}

function widgetOnUnDock()
{
  scale = 100;  
}


//获取中文的星期 
function getWeek(lan,week){
   var x;
   if(lan=="en")
   { 
     var x = new Array("Sunday", "Monday", "Tuesday");
     var x = x.concat("Wednesday","Thursday", "Friday");
     var x = x.concat("Saturday");

   }
   else
   {
     var x = new Array("星期日", "星期一", "星期二");
     var x = x.concat("星期三","星期四", "星期五");
     var x = x.concat("星期六");
   }
   return(x[week]);
}

function Run(objPath) 
{
    try 
    {
        var objShell = new ActiveXObject('wscript.shell');
        objShell.Run(objPath);
        objShell = null;
    }
    catch (e)
    {
    alert('找不到指定文件'+objPath+'(或它的组件之一)，请确定文件或路径是否正确，并确定更改了你的IE安全级别：工具--Internet 选项 -- 安全 -- 自定义级别 -- 没有标记为安全的 ActiveX 控件进行初始化和脚本运行\n设置为“启用”或 “提示”');
    }
}

function widgetOnDBClick()
{
  Run("RunDLL32.exe Shell32.dll,Control_RunDLL TimeDate.cpl");
}

function widgetOnMouseWheelUp()
{
    btfrtOnClick();
}


function GotoDate(mytime)
{
  var dt = new Date( mytime );
  var d = dt.getDate();
  var m = dt.getMonth()+1;
  var y = dt.getYear();
  var w = dt.getDay();

 
  LockRefresh;
  curdate = dt.getTime();
  
  txtDay.caption = d; 
  txtcn.caption = cnYearSky(y,m,d)+cnYearAnimal(y,m,d) +"年 "+ cnMonth(y,m,d) + cnDay(y,m,d);
  txtWeek.caption = getWeek("cn",w);
  txtYM.caption = y + "年 " + m + "月";
  if((w==0)||(w==6))
  {
    txtWeek.fontColor = "clWhite";
    //txtDay.fontColor = "clWhite";
    txtWeek.alpha = 100;
    //txtDay.alpha = 100;
  }
  else
  {
    txtWeek.fontColor = "clWhite";
    //txtDay.fontColor = "clWhite";
    txtWeek.alpha = 100;
    //txtDay.alpha = 100;
  }
  
  UnLockRefresh; 
  Print( dt );  

}

function widgetOnMouseWheelDown()
{
  btnextOnMouseDown();
}

function widgetOnKeyDown()
{
  Print(   keyCode );
  if((keyCode==39)||(keyCode==40)||(keyCode==34))
    btnextOnMouseDown()
  else if((keyCode==37)||(keyCode==38)||(keyCode==33))
    btfrtOnClick()
  else if(keyCode==36)
    btTodayOnMouseDown();
    
  
}

function nMenu1OnClick()
{
  GotoDate( curdate - oneday*30 );
}

function nMenu2OnClick()
{
  GotoDate( curdate - oneday );
}

function nMenu4OnClick()
{
  GotoDate( curdate + oneday );
}

function nMenu5OnClick()
{
  GotoDate( curdate + oneday*30 );
}

function btTodayOnMouseDown()
{
  curdate = (new Date()).getTime();
  GotoDate( curdate );
}

function btfrtOnClick()
{
  GotoDate( curdate - oneday );
}


function btnextOnMouseDown()
{
  GotoDate( curdate + oneday );
}

function loadSetting()
{
  LockRefresh;
  bg.Src = GetSetting("bg");

  //widgetWidth = bg.Width;
  //widgetHeight = bg.Height;

  UnLockRefresh;

}

function OnAllSettingsSaved()
{
  loadSetting();
}