////////////////////////////////////////////////////////////////////////////////
//
//  THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT.
//  Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
//
//  Sept 2009:
//  File changed by Pauliewog for FUSION clock.
//
////////////////////////////////////////////////////////////////////////////////

var imagePath = "images/";
var imageArray = new Array("f1_settings.png", "f2_settings.png", "f3_settings.png", "f4_settings.png", "f5_settings.png");
var imageIndex = 0; 

var mySetting = new clockSettings();


// -----------------------------------------------------------------------------

function loadTextLabels() {

  labelClockName.innerHTML       = L_localizedStrings_Settings['ClockName'];
  labelTimeZone.innerHTML        = L_localizedStrings_Settings['TimeZone'];

  labelTimeZone.innerHTML        = L_localizedStrings_Settings['TimeZone'];
  
  labelShowDay.innerHTML         = L_localizedStrings_Settings['ShowDay'];
  labelShowDate.innerHTML        = L_localizedStrings_Settings['ShowDate'];
  labelShowDigital.innerHTML     = L_localizedStrings_Settings['ShowDigital'];
  label24Hours.innerHTML         = L_localizedStrings_Settings['24HourDisplay'];
  
  labelSecondsAnalog.innerHTML   = L_localizedStrings_Settings['SecondsAnalog'];
  labelSecondsDigital.innerHTML  = L_localizedStrings_Settings['SecondsDigital'];
  
}

////////////////////////////////////////////////////////////////////////////////
// load clock settings
////////////////////////////////////////////////////////////////////////////////

function loadSettings()
{

  loadTextLabels();

  var pageDir = document.getElementsByTagName("html")[0].dir;
    
  if (pageDir == "rtl")
  {
      settingsButtonTable.dir = "rtl";
      
      var temp = settingsButtonLeftCell.innerHTML;
      settingsButtonLeftCell.innerHTML = settingsButtonRightCell.innerHTML;
      settingsButtonRightCell.innerHTML = temp;
      
      temp = settingsImageLeft.onmousedown;
      settingsImageLeft.onmousedown = settingsImageRight.onmousedown;
      settingsImageRight.onmousedown = temp;
      
      temp = settingsLeftAnchor.onkeypress;
      settingsLeftAnchor.onkeypress = settingsRightAnchor.onkeypress;
      settingsRightAnchor.onkeypress = temp;

      settingsImageLeft.alt  = L_localizedStrings_Settings['Next'];
      settingsImageRight.alt = L_localizedStrings_Settings['Previous'];
  }
  else
  {
      settingsImageLeft.alt = L_localizedStrings_Settings['Previous'];
      settingsImageRight.alt = L_localizedStrings_Settings['Next'];
  }
  
  mySetting.load();
  loadTimeZones();

  imageIndex = mySetting.themeID;
  
  settingsUpdateIndex();

  clockProperties.innerHTML = L_localizedStrings_Settings['ChangeComputerDateAndTime'];
  
  clockName.value = mySetting.clockName;
  
  with (timeZoneIndex)
  {
      value = getValidTimeZone(mySetting.timeZoneIndex);
      title = options[selectedIndex].text;
  }
  
  showDigital.checked           = mySetting.showDigital;
  showDate.checked              = mySetting.showDate;
  showDay.checked               = mySetting.showDay;
  showWeek.checked              = mySetting.showWeek;
  secondsEnabled.checked        = mySetting.secondsEnabled;
  smoothSeconds.checked         = mySetting.smoothSeconds;
  smoothSeconds.checked         = mySetting.smoothSeconds;
  secondsEnabledDigital.checked = mySetting.secondsEnabledDigital;
  show24.checked                = mySetting.show24;

  settingsUpdateTitleVisibility();
  
  System.Gadget.onSettingsClosing = settingsClosing;
}

//-----------------------------------------------------------------------------------

function settingsUpdateImage(img, state)
{
    img.src = imagePath + "settings_" + img.src.split("_")[1] + "_" + state + ".png";
}

//-----------------------------------------------------------------------------------

function settingsUpdateTitleVisibility()
{
  var lvisible;

  //settingsUpdateTitleVisibility
  if (showWeek.checked) 
     {var lVisible='none';}
  else 
    {lVisible='inline';}
  
  document.getElementById('optionTitle').style.display = lVisible; 
}

//-----------------------------------------------------------------------------------

function settingsButtonBack()
{
    if (event.keyCode == 32 || event.button == 1)
    {
        imageIndex--;
        
        if (imageIndex < 0)
        {
            imageIndex = imageArray.length - 1;
        }
        
        settingsUpdateIndex();
    }
}

//-----------------------------------------------------------------------------------

function settingsButtonForward()
{
    if (event.keyCode == 32 || event.button == 1)
    {
        imageIndex++;
        
        if (imageIndex == imageArray.length)
        {
            imageIndex = 0;
        }
        
        settingsUpdateIndex();
    }
}

//-----------------------------------------------------------------------------------

function settingsUpdateIndex()
{
    mySetting.themeID = imageIndex;
    
    settingsImagePreview.src = imagePath + imageArray[imageIndex];
    currentIndex.innerHTML = imageIndex + 1;
    maxIndex.innerHTML = imageArray.length;
}

////////////////////////////////////////////////////////////////////////////////
//
// settings event closing
//
////////////////////////////////////////////////////////////////////////////////

function settingsClosing(event)
{
    if (event.closeAction == event.Action.commit)
    {
        saveSettings();
    }
}

////////////////////////////////////////////////////////////////////////////////
//
// create clock object
//
////////////////////////////////////////////////////////////////////////////////

function clockSettings()
{   
    this.save = saveSettingToDisk;
    this.load = loadSettingFromDisk;
    
    this.themeID               = 0;
    this.clockName             = "";
    this.timeZoneIndex         = -1;
    this.showWeek              = false;
    this.showDay               = true;
    this.showDate              = true;
    this.showDigital           = true;
    this.secondsEnabled        = true;
    this.smoothSeconds         = false;
    this.secondsEnabledDigital = true;
    this.show24                = true;
}

//-----------------------------------------------------------------------------------

function loadSettingFromDisk()
{
    if (System.Gadget.Settings.read("SettingsExist"))
    {
        this.clockName             = unescape(System.Gadget.Settings.readString("clockName"));
        this.themeID               = getValidThemeID(System.Gadget.Settings.read("themeID"));
        this.timeZoneIndex         = parseInt(System.Gadget.Settings.read("timeZoneIndex"));
        this.showDay               = System.Gadget.Settings.read("showDay");
        this.showWeek              = System.Gadget.Settings.read("showWeek");
        this.showDate              = System.Gadget.Settings.read("showDate");
        this.showDigital           = System.Gadget.Settings.read("showDigital");
        this.secondsEnabled        = System.Gadget.Settings.read("secondsEnabled");
        this.smoothSeconds        = System.Gadget.Settings.read("smoothSeconds");
        this.secondsEnabledDigital = System.Gadget.Settings.read("secondsEnabledDigital");
        this.show24                = System.Gadget.Settings.read("show24");
    }
}

//-----------------------------------------------------------------------------------

function saveSettingToDisk()
{
    System.Gadget.Settings.write("SettingsExist", true);
    System.Gadget.Settings.writeString("clockName", escape(this.clockName));
    System.Gadget.Settings.write("themeID", this.themeID);
    System.Gadget.Settings.write("timeZoneIndex", this.timeZoneIndex);
    System.Gadget.Settings.write("showDay", this.showDay);
    System.Gadget.Settings.write("showWeek", this.showWeek);
    System.Gadget.Settings.write("showDate", this.showDate);
    System.Gadget.Settings.write("showDigital", this.showDigital);
    System.Gadget.Settings.write("secondsEnabled", this.secondsEnabled);
    System.Gadget.Settings.write("smoothSeconds", this.smoothSeconds);	
    System.Gadget.Settings.write("secondsEnabledDigital", this.secondsEnabledDigital);
    System.Gadget.Settings.write("show24", this.show24);
}

////////////////////////////////////////////////////////////////////////////////
//
// save clock settings
//
////////////////////////////////////////////////////////////////////////////////

function saveSettings()
{
    mySetting.clockName             = trim(clockName.value, "both");
    mySetting.timeZoneIndex         = getValidTimeZone(timeZoneIndex.value);
    mySetting.secondsEnabled        = secondsEnabled.checked;
    mySetting.smoothSeconds         = smoothSeconds.checked;	
    mySetting.secondsEnabledDigital = secondsEnabledDigital.checked;
    mySetting.showDay               = showDay.checked;
    mySetting.showWeek              = showWeek.checked;
    mySetting.showDate              = showDate.checked;
    mySetting.showDigital           = showDigital.checked;
    mySetting.show24                = show24.checked;

    mySetting.save();
}

//-----------------------------------------------------------------------------------

function loadTimeZones()
{
    updateTimeZones(true);   

    timeZoneIndex.options[0] = new Option(L_localizedStrings_Settings['CurrentComputerTime'], "-1");

    for (var i = 0; i < e.length; i++)
    {
        timeZoneIndex.options[i + 1] = new Option(e[i][eZone], e[i][eItem], null, (e[i][eItem] == mySetting.timeZoneIndex));
        timeZoneIndex.options[i + 1].title = e[i][eZone];
    }
}

////////////////////////////////////////////////////////////////////////////////
//
// open system time date control panel
//
////////////////////////////////////////////////////////////////////////////////

function openSettings()
{
    System.Shell.execute("timedate.cpl");
}

////////////////////////////////////////////////////////////////////////////////
//
// trim white space
//
////////////////////////////////////////////////////////////////////////////////

function trim(stringIn, removeFrom) 
{ 
    var stringOut = "";
    stringIn = stringIn.toString();
    
    if (stringIn.length > 0)
    {
        switch (removeFrom) 
        { 
            case "left": 
                stringOut = stringIn.replace(/^\s+/g, ""); 
                break; 
            case "right": 
                stringOut = stringIn.replace(/\s+$/g, ""); 
                break; 
            case "both":

            default:
                stringOut = stringIn.replace(/^\s+|\s+$/g, ""); 
        }
    }

    return stringOut;
}

//-----------------------------------------------------------------------------------

function getValidThemeID(index)
{
    if (parseInt(index) > -1 && parseInt(index) < clockThemes.length)
    {
        return parseInt(index);
    }
    else
    {
        return 0;
    }
}

//-----------------------------------------------------------------------------------
