var curId    = 0;
var lastId   = 0;
var totalId  = 0;
var noteArray  = new Array();
var curCount = 1;
var pageSeparator = "/";
var maxNumberNotes = 10;

function loadFontSet()
{
  noteText.fontName = GetSetting("fName");
  noteText.fontColor = GetSetting("fColor");
  if(widgetWidth==130) 
    noteText.fontSize = GetSetting("fDockedSize");
  else
    noteText.fontSize = GetSetting("fSize");

}



function widgetOnKeyDown()
{
  //当按下对应按键时，上翻或者下翻
  
  //向上或者向左 则执行翻到上一页 
  if((keyCode==37)||(keyCode==38)) 
  {
    leftArrowOnMouseDown();
  }
  else if ((keyCode==39)||(keyCode==40))
  {
    //按下 向下或者向右 键 时，则执行下翻函数 
    rightArrowOnMouseDown();
  } 
}


function widgetOnInit()
{
  loadFontSet();
  maxNumberNotes = GetSetting("MaxNoteCount"); 
    
  //载入curCount  便笺数量 
  if( isNaN(  parseInt(  ReadIni("NoteCount")  )  ) ) 
    curCount=1
  else
    curCount = parseInt(ReadIni("NoteCount"));

  if(  curCount<1  ) 
  {
    curCount=1;
    WriteIni("NoteCount",curCount);
  }
      
  for (var i = 0; i < curCount; i++)
  {
    noteArray.push(new NewNote());
    
    noteArray[i].oId = i;
    noteArray[i].oNoteText= ReadIni(i);
   
    curId++;
    lastId++;
    totalId++;
  }
      
  //载入curID  便笺编号  
  if(  isNaN(parseInt(ReadIni("NoteState"))))
  {
    curId = 0;
    WriteIni("NoteState",curId);
  }
  else 
    curId = parseInt(ReadIni("NoteState"));
  
  if (curId > (curCount - 1))
    curId = curCount - 1;

  if (curId < 0)
  {
    curId = 0;
    WriteIni("NoteState",curId);
  }
           
  //显示便笺内容  
  noteText.text= noteArray[curId].oNoteText;
  //显示当前note数目 
  var displayCount = curId + 1;
  pageNumb.caption = displayCount + " " + pageSeparator + " " + noteArray.length;
  
  saveAllText();  
}

function widgetOnClose()
{
  //保存当前便笺  
  saveText();   

}

function widgetOnDeActive()
{
  saveText();

}

function widgetOnUnDock()
{
   var bgImgDockState = "";
   //先锁定刷新，增快执行效率 
   LockRefresh;
   
   //设置便笺 未停靠状态下的外观
   var color = GetSetting("curColor");
   bg.Src = "images/sticky_" + color + bgImgDockState + ".png";
   if(color=='yellow')
     noteText.editorBgColor = '255,255,188'
   else if(color=='blue')
     noteText.editorBgColor = '201,244,253'
   else if(color=='green')
     noteText.editorBgColor = '195,245,190'
   else if(color=='pink')
     noteText.editorBgColor = '253,194,254'
   else if(color=='purple')
     noteText.editorBgColor = '204,211,254'
   else if(color=='white')
     noteText.editorBgColor = '249,249,248';     
     
   widgetWidth = 197;
   widgetHeight = 177;
   
    with (noteText)
    {
        width = 160;
        height = 125;
        top = 10;
        left = 16;
        //fontSize = curSizeUndocked;
    }

    pageWell.left = 59; 
    pageWell.top = 138; 
   
    pageNumb.left = 59; 
    pageNumb.top = 139; 
   
    leftArrow.left = 63; 
    leftArrow.top = 140; 
   
    rightArrow.left = 117; 
    rightArrow.top = 140; 
   
    deleteIcon.left = 10; 
    deleteIcon.top = 141; 
   
    plusIcon.left = 164; 
    plusIcon.top = 140; 
   
   //解开刷新所动 
   UnLockRefresh;
   loadFontSet();   
   //刷新 
   Refresh;
}

function widgetOnDock()
{
   var bgImgDockState = "_docked";
   //先锁定刷新，增快执行效率 
   LockRefresh;
   
   //设置便笺 未停靠状态下的外观
   var color = GetSetting("curColor");
   bg.Src = "images/sticky_" + color + bgImgDockState + ".png";
   if(color=='yellow')
     noteText.editorBgColor = '255,255,188'
   else if(color=='blue')
     noteText.editorBgColor = '201,244,253'
   else if(color=='green')
     noteText.editorBgColor = '195,245,190'
   else if(color=='pink')
     noteText.editorBgColor = '253,194,254'
   else if(color=='purple')
     noteText.editorBgColor = '204,211,254'
   else if(color=='white')
     noteText.editorBgColor = '249,249,248';     
   
   
   widgetWidth = 130;
   widgetHeight = 121;
   
    with (noteText)
    {
        width = 102;
        height = 65;
        top = 12;
        left = 11;
        //fontSize = curSizedocked;
    }
    
    pageWell.left = 25; 
    pageWell.top = 81; 
   
    pageNumb.left = 25; 
    pageNumb.top = 82; 
   
    leftArrow.left = 29; 
    leftArrow.top = 83; 
   
    rightArrow.left = 83; 
    rightArrow.top = 83; 
   
    deleteIcon.left = 8; 
    deleteIcon.top = 84; 
   
    plusIcon.left = 104; 
    plusIcon.top = 83; 
   
   //解开刷新所动 
   UnLockRefresh;
   loadFontSet();   
   Refresh;
  
}

function OnAllSettingsSaved()
{
  //检测是否停靠 
  if(dockedOnSidebar) 
    widgetOnDock()
  else   
    widgetOnUnDock();
  loadFontSet();
  maxNumberNotes = GetSetting("MaxNoteCount");
   
}

function widgetOnLeave()
{
  //先锁定刷新，增快执行效率 
  LockRefresh;

  //隐藏按钮 
  pageWell.Visible = false;
  pageNumb.Visible = false;
  leftArrow.Visible = false;
  rightArrow.Visible = false;
  deleteIcon.Visible = false;
  plusIcon.Visible = false;
  
  //解开刷新所动 
  UnLockRefresh;
  Refresh;
  
}

function widgetOnEnter()
{
  //先锁定刷新，增快执行效率 
  LockRefresh;

  //隐藏按钮 
  pageWell.Visible = true;
  pageNumb.Visible = true;
  leftArrow.Visible = true;
  rightArrow.Visible = true;
  deleteIcon.Visible = true;
  plusIcon.Visible = true;
  
  //解开刷新所动 
  UnLockRefresh;
  Refresh;

}

/*
function widgetOnKeyUp()
{
  //保存当前便笺  
  saveText();
}         */

////////////////////////////////////////////////////////////////////////////////
// create new note
////////////////////////////////////////////////////////////////////////////////
function NewNote()
{
  this.oId = "";
  this.oNoteText = "";
}


function leftArrowOnMouseDown()
{
  if (noteArray[curId - 1])
  {
    curId--;
     
    noteText.text= noteArray[curId].oNoteText;
    pageNumb.caption = (curId + 1) + " " + pageSeparator + " " + noteArray.length;
      
    WriteIni("NoteState", curId);
  }

}

function rightArrowOnMouseDown()
{
  if (noteArray[curId + 1])
  {
    curId++;
     
    noteText.text= noteArray[curId].oNoteText;
    pageNumb.caption = (curId + 1) + " " + pageSeparator + " " + noteArray.length;
      
    WriteIni("NoteState", curId);
  }
  
}

function saveText()
{
  //保存当前便笺  
  WriteIni(curId,noteText.text);
  WriteIni("NoteCount", noteArray.length);
  WriteIni("NoteState", curId);      
}

//保存所有便笺 
function saveAllText()
{
  WriteIni("NoteCount", noteArray.length);
  WriteIni("NoteState", curId);
        
  for (var i = 0; i < noteArray.length-1; i++)
  {
    WriteIni(i,noteArray[i].oNoteText);
  }
} 

function plusIconOnMouseDown()
{
  if (noteArray.length < maxNumberNotes)
  {
      if (noteArray.length > 0)
      {
          saveText();
      }

      lastId++;
      totalId++
      
      noteArray.push(new NewNote());
      noteText.text = "";
      
      var displayCount;
      
      displayCount = curId + 1;

      curId = noteArray.length - 1;

      pageNumb.caption = noteArray.length + " " + pageSeparator + " " + noteArray.length;
      
      saveText();
      //if (noteArray.length == maxNumberNotes)
      //{
      //  plusIcon.disable;    
      //}
  }

}

function deleteIconOnMouseDown()
{
  if(!widget.confirm('确认删除当前便签？'))
    return;
  //清空Ini中最后一个的内容 
  WriteIni(noteArray.length - 1, "");
  //从数组中删除curId的内容 
  noteArray.splice(curId, 1);
  //将新数组所有内容逐一保存 
  for (var i = 0; i < noteArray.length; i++)
  {
    WriteIni(i, noteArray[i].oNoteText);
  }
  //确保curId在范围内
  curCount = noteArray.length; 
  if (curId > (curCount - 1))
    curId = curCount - 1;
  
  //保存当前便笺的一些状态信息 
  if(curId == 0)
  {
    WriteIni("NoteState", curId);
  }
  else
  {
    WriteIni("NoteState", curId - 1);
  }
  WriteIni("NoteCount", noteArray.length);
  
  //设置当前note的内容 
  noteText.text = noteArray[curId].oNoteText;
   
  //显示当前note数目 
  var displayCount = curId + 1;
  pageNumb.caption = displayCount + " " + pageSeparator + " " + noteArray.length;
 
}

////////////////////////////////////////////////////////////////////////////////
// set note state
////////////////////////////////////////////////////////////////////////////////
function setNoteState()
{
  //载入curID  便笺编号  
  if(  isNaN(parseInt(ReadIni("NoteState"))))
  {
    curId = 0;
    WriteIni("NoteState",curId);
  }
  else if (curId > (curCount - 1))
  {
    curId = curCount - 1;
  } 

  //设置当前note的内容 
  noteText.text = noteArray[curId].oNoteText;
  
  //设置当前note数目 
  var displayCount = curId + 1;
  pageNumb.caption = displayCount + " " + pageSeparator + " " + noteArray.length;
  WriteIni("NoteState", curId);
}

function noteTextOnChange()
{
    //保存当前便笺  
    noteArray[curId].oNoteText =  noteText.text; 
    saveText();   	
}

function widgetOnMouseWheelDown()
{
  rightArrowOnMouseDown();

}

function widgetOnMouseWheelUp()
{
  leftArrowOnMouseDown();
}