var y,mm,d,dt,h,m,s;
var nowDt,nowTm;
var sss,mmm,hhh;
var skin,showsec,secSpring,timeoffset;
var vhour,vhalfhour;
function timer0OnTimer()
{
  dt = new Date();
  h = dt.getHours() + timeoffset;
  m = dt.getMinutes();
  s = dt.getSeconds();
  y = dt.getYear();
  mm = dt.getMonth()+1;
  d = dt.getDate();
  nowDt=y+"-"+mm+"-"+d;
  nowTm=h+":"+m+":"+s;

  //showTrayHint("",dt.toLocaleString());
  if(showsec==0)
  {
    if((hhh==h)&&(mmm==m))
    {
      hhh = h;
      mmm = m;
      return;
    }
  }
  
  if((sss==s)&&(mmm==m))
    return;
  sss = s;
  mmm = m;
  
  LockRefresh;
  
  if(showsec==1)
  {
    if(secSpring==0)
      imgsec.angle = s*6
    else
    {
      imgsec.angle = s*6+4;
    }
  } 
  
 
  imgmin.angle =   parseInt(s/10) +m*6;
  imgh.angle =  parseInt(m/2) + parseInt(h % 12*30) ;
  txt.tooltip = y + "年" + mm + "月" + d + "日 " + h + ":" + m + ":" + s ;
  
  UnLockRefresh;

  if((showsec==1)&&(secSpring==1))
    timer1.enabled = true;
  
  if(vhalfhour==1)  
    if( (m==30)&&(s==0) )
    {
       playVoiceTime;
       Print("半点报时");
    }

  if(vhour==1)  
    if( (m==0)&&(s==0) )
    {
       playVoiceTime;
       Print("整点报时");
    }
//showTrayHint( dt.toLocaleString().split(" ").length,dt.toLocaleString());    
//runTaskList(dt.toLocaleString().split(" ")[0],dt.toLocaleString().split(" ")[parseInt(dt.toLocaleString().split(" ").length-1)]);    
runTaskList(y+"-"+mm+"-"+d,h+":"+m+":"+s);
}   

function timer1OnTimer()
{
  timer1.enabled = false;
  imgsec.angle = s*6;
}

function widgetOnInit()
{ 
  sss = -1;
  hhh = -1;
  mmm = -1;
  
  LockRefresh;
  
  timeoffset = parseInt(GetSetting("timeoffset"));
  showsec = GetSetting("sec");
  secSpring = GetSetting("spring");
  txt.caption = GetSetting( "cname" );
  txt.fontStr = 'clBlack/-19/黑体/8/0000';
   
  
  skin = GetSetting("skin");
  Print( skin);//cxy**************
  bg.Src = "images\\" + skin + ".png"; 
  imgmin.Src = "images\\" + skin + "_m.png"; 
  imgh.Src = "images\\" + skin + "_h.png"; 
  imgsec.Src = "images\\" + skin + "_s.png"; 
  imghl.Src = "images\\" + skin + "_highlights.png"; 
  imgdot.Src = "images\\" + skin + "_dot.png";
  
  if((skin=="diner")||(skin=="modern")   )
    txt.fontColor = "clWhite" 
  else if((skin=="flower") )
    txt.fontColor = "230,111,3" 
  else if((skin=="cronometer") )
    txt.fontColor = "clRed"
  else 
    txt.fontColor = "clBlack";
    
  imgsec.Visible = showsec;
  
  
  //语音报时设置
  vhour = GetSetting("valhour");  
  vhalfhour = GetSetting("valhalfhour");  
  
  UnLockRefresh;
  
  timer0OnTimer();
}

function OnAllSettingsSaved()
{
  widgetOnInit();
}

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

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

function nMenu1OnClick()
{
  playVoiceTime;
}

function nMenu5OnClick()
{
  if(GetSetting("valhalfhour")==1)
    SetSetting("valhalfhour",0)
   else    
    SetSetting("valhalfhour",1);
  vhalfhour = GetSetting("valhalfhour");
  SaveSetting("valhalfhour");
}

function nMenu4OnClick()
{
  if(GetSetting("valhour")==1)
    SetSetting("valhour",0)
   else    
    SetSetting("valhour",1);
  vhour = GetSetting("valhour");
  SaveSetting("valhour");
  
}

function widgetOnPopupMenu()
{
  if(GetSetting("valhour")==1)
    nMenu4.Checked = true
  else
    nMenu4.Checked = false;
    
  if(GetSetting("valhalfhour")==1)
    nMenu5.Checked = true
  else
    nMenu5.Checked = false;
    
}

function runTask(){
        var taskFileName=openFileDialog;
        alert(taskFileName);
        OpenFile(taskFileName);
         //openFolderDialog;
        // openPicDialog;
}


function showDetailTask(sender){
  //widget.alert(sender.ItemIndex);
  try{
      edtTaskName.Text=taskListArray[sender.ItemIndex][0]; 
      edtAction.Text=actionArray[taskListArray[sender.ItemIndex][1]];
      //actionDate.date=taskListArray[sender.ItemIndex][2];
      var newDate=taskListArray[sender.ItemIndex][2].replace("年","-");
      newDate=newDate.replace("月","-");
      newDate=newDate.replace("日","");
      actionDate.date=strtodate(newDate);
      actionTime.time=strtotime(taskListArray[sender.ItemIndex][3]);
      }catch(e){
      Print("数组为空");      
      }
}

function closePC(delay){
    //alert("关闭计算机代码");
    //delay=delay ? delay : 30;
    run("Shutdown -s -f -t "+delay);
}
function restartPC(delay){
    //alert("重启计算机代码");
    //delay=delay ? delay : 30;
    run("shutdown -r -t "+delay);
}
function openExe(source,option){
   // alert("打开应用程序");
    runEx(source,option);
}

var srcExe,url,cbxMode;
function viewUrlFile(){
    url.Text=openFileDialog;    
}
function viewSrcFile(){
    srcExe.Text=openFileDialog;
}
function viewFolder(){
    url.Text= openFolderDialog;
}
function btOkOnClick(){
  actionOption2=(url)? url.Text :"";
  actionOption1=(srcExe)? srcExe.Text :(cbxMode)? cbxMode.Text : "";
  url=false;
  srcExe=false;
  cbxMode=false;
  
  //alert(actionOption1+"\r"+actionOption2);
  frm1.close();      
}

function taskAlert(mode,content){
   switch(parseInt(mode)){
     case 0 : alert(content);
              break;
     case 1 : showTrayHint("任务提醒",content);
              break;
     case 2 : showPopHint("任务提醒",content);
              break;
     default: alert("任务提醒模式出错");
   }
}

function actionSelected(sender){
    sender.Visible=false;
    curActionValue=parseInt(sender.ItemIndex);
    actionOption1="";
    actionOption2="";
////////////创建form窗体///////////////////////
  frm1 = TForm.create(widget);
  frm1.width = 330;
  frm1.height = 260;
  frm1.Position = poScreenCenter;
  frm1.caption = '设置参数';
  frm1.font.name='宋体';
  frm1.font.size=9;
  frm1.ShowHint=true;
  frm1.BorderStyle=bsDialog;
  
  lbHelpInfo = TLabel.Create(widget);
  lbHelpInfo.parent = frm1;
  lbHelpInfo.rc = rect(5,5,50,100);
  lbHelpInfo.caption = "这里是提示信息，温馨提示!这里是提示信息，温馨提示!\r这里是提示信息，温馨提示!\r这里是提示信息，温馨提示!";
  //lbHelpInfo.transparent=false;
  //lbHelpInfo.autoSize=false;
  //lbHelpInfo.wordWrap=true;
  //lbHelpInfo.color="$000000FF";
  lbUrlName = TLabel.Create(widget);
  lbUrlName.parent = frm1;
  lbUrlName.rc = rect(5,125,50,14);
  lbUrlName.caption = "目标文件路径：";
 
  url = TEdit.Create(widget);
  url.parent = frm1;
  url.rc = Rect(85,120,170,14);
  
  viewUrl = TButton.Create(widget);
  viewUrl.parent = frm1;
  viewUrl.caption = "浏览";
  viewUrl.OnClickEx ="viewUrlFile";
  viewUrl.rc = Rect(260,120,40,21);

  btOk = TButton.Create(widget);
  btOk.parent = frm1;
  btOk.caption = "确定";
  btOk.OnClickEx = "btOkOnClick";
  btOk.rc = Rect(120,150,40,21);

  btCancel = TButton.Create(widget);
  btCancel.parent = frm1;
  btCancel.caption = "取消";
  btCancel.ModalResult = 2;
  btCancel.rc = Rect(170,150,40,21);
  btCancel.cancel = true;

      switch(curActionValue){
        case 0: lbUrlName.caption="延迟秒数：";
                lbHelpInfo.caption="\r温馨提示：\r\r\t关机等待时间，单位是秒，值是整数，默认值是30";
                viewUrl.Visible=false;
                break;
        case 1: lbUrlName.caption="网页文件地址：";
                lbHelpInfo.caption="\r温馨提示：\r\r\t1、直接输入url，如：http:\/\/www.baidu.com，\r打开Internet上的网页\r\r\t2、点击\"浏览\"按钮来选择打开本地网页";
                break;
        case 2: lbUrlName.caption="目标文件路径：";
                lbSrcName = TLabel.Create(widget);
                lbSrcName.parent = frm1;
                lbSrcName.rc = rect(5,95,50,14);
                lbSrcName.caption = "源程序地址：";
  
                srcExe = TEdit.Create(widget);
                srcExe.parent = frm1;
                srcExe.rc = Rect(85,90,170,14);

                viewSrc = TButton.Create(widget);
                viewSrc.parent = frm1;
                viewSrc.caption = "浏览";
                viewSrc.OnClickEx = "viewSrcFile";
                viewSrc.rc = Rect(260,90,40,21);

                lbHelpInfo.caption="\r温馨提示：\r\r\t1、源程序地址--将要运行的程序的路径\r\t2、目标文件路径--源程序即将打开的文件的路径，\r如果为空，表示直接运行源程序";
                break;
        case 3: lbUrlName.caption="文件地址：";
                lbHelpInfo.caption="\r温馨提示：\r\r\t直接以默认方式打开此文件";
                break;
        case 4: lbUrlName.caption="文件夹地址：";
                lbHelpInfo.caption="\r温馨提示：\r\r\t直接打开此文件夹";
                viewUrl.OnClickEx ="viewFolder";
                break;
        case 5: lbUrlName.caption="延迟秒数：";
                lbHelpInfo.caption="\r温馨提示：\r\r\t重启计算机的等待时间，单位为秒，\r值是整数，默认值是30";
                viewUrl.Visible=false;
                break;
        case 6: lbUrlName.caption="提示内容：";
                viewUrl.Visible=false;
                btOk.rc = Rect(120,175,40,21);
                btCancel.rc = Rect(170,175,40,21);
                lbHelpInfo.caption="\r温馨提示：\r\r\t1、显示方式--显示提示信息的方式，0代表弹\r出对话框，1代表任务栏气泡，2代表任务栏弹出信息框\r\r\t2、显示内容--将要定时显示的提示信息";
                lbSrcName = TLabel.Create(widget);
                lbSrcName.parent = frm1;
                lbSrcName.rc = rect(5,150,50,14);
                lbSrcName.caption = "显示方式：";
                cbxMode = TComboBox.Create(widget);
                cbxMode.parent = frm1;
                cbxMode.rc = Rect(85,145,150,14);
                cbxMode.text="0";
                cbxMode.items.add("0");
                cbxMode.items.add("1");
                cbxMode.items.add("2");
                //alert(cbxAction.text);
                //cbxAction.OnChange="tool";
                break;
        default: alert("default");break;
    }; 
    
  frm1.ShowModal==1
  frm1.free;
////////////////////创建窗体结束//////////////////   

    edtAction.Text=actionArray[sender.ItemIndex];
}


var pp;
var edtAction;
var edtTaskName;
var actionDate;
var actionTime;
var curActionValue;
var taskListBox;
var actionOption1,actionOption2;
var actionArray=new Array("关闭计算机","打开浏览器","打开应用程序","打开文件","打开文件夹","重启计算机","任务定时提醒");
var taskListArray=new Array();
//var taskListArray=new Array(["关机",0,"2008年8月12日","18:06:00","",30],["从起",5,"2008年8月12日","9:53:11","",10],["该做事情了",6,"2008年8月12日","12:00:00",0,"吃饭吃饭吃饭吃饭"],["游戏游戏",2,"2008年8月12日","12:38:30","D:\\Program Files\\QQGame\\QQGame.exe",""],["打开百度",1,"2008年8月11日","16:37:30","","http://www.baidu.com"],["放歌",3,"2008年8月12日","13:43:02","","E:\\音乐\\me and you.wma"],["打开应用程序",2,"2008年8月11日","17:10:02","C:\\Program Files\\Windows Media Player\\wmplayer.exe","D:\\QQDownload\\muzimei2005-9-12.mp3"]);
var isAdd;

function btAddOnClick(){
  alert("添加");
  edtTaskName.Text="新任务";
  edtAction.Text="请选择操作↓";
  
}

function btDelOnClick(){
  if(taskListBox.ItemIndex==-1){
      alert("未选中任何任务");
      return;
      }
 if(taskListArray.length==0){
     alert("当前没有任务");
     return;
     }
  edtTaskName.Text="";
  edtAction.Text="";
  taskListBox.items.Delete(taskListBox.ItemIndex);
  taskListArray.splice(taskListBox.ItemIndex,1);
  //alert(taskListBox.length);
  if((taskListArray.length==0)&&(taskListBox.items.count==0))taskListBox.items.add("当前没有任务需要执行");
}

function btSaveOnClick(){
  if(edtTaskName.Text==""){
    alert("任务名不能为空");
    return;
    }else{
      //alert(formatDateTime('yyyy"年"m"月"dd"日"',actionDate.date));
      //alert(formatDateTime('hh":"nn":"ss',actionTime.time));
      // alert(actionOption2+"\r"+curActionValue);
      //Print("ss:"+curActionValue);
      if((edtAction.Text=="")||(actionOption2==""&&actionOption1=="")||(actionOption2==undefined&&actionOption1==undefined)||(curActionValue==undefined)){
          alert("请选择将要执行的操作");
          return;
          }
      if((formatDateTime('yyyy"-"m"-"d',actionDate.date)>nowDt)||(formatDateTime('yyyy"-"m"-"d',actionDate.date)==nowDt&&formatDateTime('h":"n":"s',actionTime.time)>nowTm)){
          if((taskListArray.length==0)&&(taskListBox.items.count==1))taskListBox.items.Delete(0);
          var newArray=new Array(edtTaskName.Text,curActionValue,formatDateTime('yyyy"-"m"-"d',actionDate.date),formatDateTime('h":"n":"s',actionTime.time),actionOption1,actionOption2);
          taskListArray.push(newArray);
          print(taskListArray);
          taskListBox.items.add(edtTaskName.Text);
          alert("成功添加任务："+edtTaskName.Text);                  
      }else{
          alert("执行时间已经小于了当前时间");
          return;
      }
   
    }
}

function btCloseOnClick(){
    frm.close();
}

function runTaskList(dt,tm){
  var taskLength=taskListArray.length;
  var tag=true;
  var tempdt;
  var temptm;
  var temptaskname="无任务";
  var tasknametip="当前无任务需要执行";
  if(taskListArray.length==0) {
    curTaskName.caption="无任务";
    curTaskName.tooltip="当前无任务需要执行";
    return;
    }

  for(i=0;i<taskLength;i++){
      if((taskListArray[i][2]>dt)||(taskListArray[i][2]==dt&&taskListArray[i][3]>tm)){
        //Print(taskListArray[i][2]);
        //Print(taskListArray[i][3]);
         if(tag){
           tempdt=taskListArray[i][2];
           temptm=taskListArray[i][3];
           
           temptaskname=taskListArray[i][0];
          // Print(temptaskname);
           tasknametip="执行时间："+taskListArray[i][2]+taskListArray[i][3];
           tag=false;
           }else{
             if((taskListArray[i][2]<tempdt)||(taskListArray[i][2]==dt&&taskListArray[i][3]<temptm)){
                tempdt=taskListArray[i][2];
                temptm=taskListArray[i][3];
                temptaskname=taskListArray[i][0];
                tasknametip="执行时间："+taskListArray[i][2]+taskListArray[i][3];
                }
           }
      }else if(taskListArray[i][2]==dt&&taskListArray[i][3]==tm){
          //showTrayHint("",taskListArray[i][0]);
          //Print("ok");
          temptaskname=taskListArray[i][0];
          var taskID=taskListArray[i][1];
          //showTrayHint("新任务id",taskID);
            switch(taskID){
              case 0 : closePC(taskListArray[i][5]);
                       break;
              case 1 : OpenLink(taskListArray[i][5]);
                       break;
              case 2 : openExe(taskListArray[i][4],taskListArray[i][5]); 
                       break;
              case 3 : OpenFile(taskListArray[i][5]);
                       break;
              case 4 : OpenFile(taskListArray[i][5]);
                       break;
              case 5 : restartPC(taskListArray[i][5]);
                       break;
              case 6 : taskAlert(taskListArray[i][4],taskListArray[i][5]);
                       break;
              default : alert("任务出错!");
  
           }
      }
  }
  curTaskName.caption=temptaskname;
  curTaskName.tooltip="任务名称："+temptaskname+"\r"+tasknametip;
  return;
}

function showActionList(){
/*  
  frm = TForm.create(widget);
  frm.width = 330;
  frm.height = 360;
  frm.Position = poScreenCenter;
  frm.caption = '设置动作';
  frm.font.name='宋体';
  frm.font.size=9;
  frm.ShowHint=true;
  frm.BorderStyle=bsDialog;
  */
  acListBox = TListBox.Create(widget);
  // 一定要将parent设为 panel 才可显示出来
  //alert(pp.height);
  acListBox.parent = pp;    
  acListBox.rc = Rect(120,180,170,90);   /////left,top,width,height
  for(var i=0;i<actionArray.length;i++)
    acListBox.items.add(actionArray[i]);
  if(acListBox.items.count>0)  acListBox.itemIndex = 0;
  acListBox.OnClickEx = "actionSelected"; 
 
 ////////////日期时间控件////////////////////////
 /* 
  actionDate=TDateTimePicker.Create(widget);
  actionDate.parent=frm;
  actionDate.rc=Rect(20,150,90,20);

  actionTime=TDateTimePicker.Create(widget);
  actionTime.parent=frm;
  actionTime.rc=Rect(20,180,90,20);
  actionTime.kind=dtkTime;
*///////////////////////////////////////////////  
  
 /*  if(frm.ShowModal==1)
  {
    alert("1");
  }     
  frm.free;*/
}

/////////////////////////////////////////////////

function freshTaskList(){
  for(var i=0;i<taskListArray.length;i++)
 // taskListBox.items[i]="hehe";
  taskListBox.items.add(taskListArray[i][0]);
  if(taskListBox.items.count<=0){
    taskListBox.items.add("当前没有任务需要执行");
    taskListBox.itemIndex = 0;
    }
  //taskListBox.ItemIndex=(taskListBox.ItemIndex)? taskListBox.ItemIndex : 0;
  taskListBox.OnClickEx = "showDetailTask"; 
}

function showTaskDialog(){
  frm = TForm.create(widget);
  frm.width = 350;
  frm.height = 360;
  frm.Position = poScreenCenter;
  frm.caption = '任务管理';
  frm.font.name='宋体';
  frm.font.size=9;
  frm.ShowHint=true;
  frm.BorderStyle=bsDialog;
   lbTaskCount = TLabel.Create(widget);
  lbTaskCount.parent = frm;
  pp=frm;
  lbTaskCount.rc = rect(55,12,235,14);
  lbTaskCount.caption = "任务列表：";
 
  taskListBox = TListBox.Create(widget);
  taskListBox.parent = frm;    
  taskListBox.rc = Rect(55,30,235,90);   /////left,top,width,height
  freshTaskList();
 

////////////////任务名称//////////////////// 
  lbTaskName = TLabel.Create(widget);
  lbTaskName.parent = frm;
  lbTaskName.rc = rect(55,135,50,14);
  lbTaskName.caption = "任务名称：";
  
  edtTaskName = TEdit.Create(widget);
  edtTaskName.parent = frm;
  edtTaskName.rc = Rect(120,130,170,14);
  edtTaskName.Text="新任务";
  
///////////执行操作///////////////////////////

  lbAction = TLabel.Create(widget);
  lbAction.parent = frm;
  lbAction.rc = rect(55,170,50,14);
  lbAction.caption = "执行操作：";
  
  edtAction = TEdit.Create(widget);
  edtAction.parent = frm;
  edtAction.rc = Rect(120,165,170,14);
  edtAction.OnClickEx="showActionList";
  edtAction.Text="请选择操作↓";

  lbDate = TLabel.Create(widget);
  lbDate.parent = frm;
  lbDate.rc = rect(55,205,50,14);
  lbDate.caption = "执行日期：";
  
  actionDate=TDateTimePicker.Create(widget);
  actionDate.parent=frm;
  actionDate.rc=Rect(120,200,120,20);

  lbTime = TLabel.Create(widget);
  lbTime.parent = frm;
  lbTime.rc = rect(55,235,50,14);
  lbTime.caption = "执行时间：";
  
  actionTime=TDateTimePicker.Create(widget);
  actionTime.parent=frm;
  actionTime.rc=Rect(120,230,120,20);
  actionTime.kind=dtkTime;

  btDel = TButton.Create(widget);
  btDel.parent = frm;
  btDel.caption = "删除任务";
  btDel.OnClickEx = "btDelOnClick";
  btDel.rc = Rect(140,270,60,21);

  btSave = TButton.Create(widget);
  btSave.parent = frm;
  btSave.caption = "确认添加";
  btSave.OnClickEx = "btSaveOnClick";
  btSave.rc = Rect(50,270,60,21);   
  
  btClose = TButton.Create(widget);
  btClose.parent = frm;
  btClose.caption = "关闭";
  btClose.OnClickEx = "btCloseOnClick";
  btClose.rc = Rect(230,270,60,21);   
     
  frm.showmodal;
}

function curTaskNameOnMouseDown(sender)
{
  //ShowSettingEx(4);
  showTaskDialog();
}

function button0OnMouseDown(sender)
{
 // ShowSettingEx(4);
  showTaskDialog();
}