////////////////////////////////////////////////////////////////////////////////
//
//  THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT.
//  Copyright (c) 2007 Microsoft Corporation.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//
// GLOBAL VARIABLES
//
////////////////////////////////////////////////////////////////////////////////
var g_defaultShowBroadbandContent  = true;
var g_defaultBroadbandContentCount = 6;
var g_defaultShowRecordedTVItem    = true;
var g_defaultRecordedTVItemCount   = 6;
var g_defaultTransitionTime        = 10;

var g_keyShowBroadbandContent      = "mceShowBroadbandContent";
var g_keyBroadbandContentCount     = "mceBroadbandContentCount";
var g_keyShowRecordedTVItem        = "mceShowRecordedTVItem";
var g_keyRecordedTVItemCount       = "mceRecordedTVItemCount";
var g_keyTransitionTime            = "mceTransitionTime";

var g_maxBBSourceCount = 20;
var g_regKeyBBSourceTitle = "BBSourceTitle";
var g_regKeyBBSourceShow = "BBSourceShow";

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
function load()
{
    System.Gadget.onSettingsClosing = OnSettingsClosing;
    GetMCEGadgetSettings();
    
    LoadString();
}

function LoadString()
{
    L_SETTINGS_TEXT1.innerText = System.Gadget.Settings.read("mceSettingText1");
    L_SETTINGS_TEXT2.innerText = System.Gadget.Settings.read("mceSettingText2");
    L_SETTINGS_TEXT3.innerText = System.Gadget.Settings.read("mceSettingText3");
    L_SETTINGS_TRANSITIONTIME.innerText = System.Gadget.Settings.read("mceSettingText5");
    L_SETTINGS_INTERNETTV_TITLE.innerText = System.Gadget.Settings.read("mceSettingInternetTVTitle");
    L_SETTINGS_INTERNETTV_COUNT.innerText = System.Gadget.Settings.read("mceSettingInternetTVCount");
}

function SetDefaultSettingsValue()
{
    var mceShowBroadbandContent  = System.Gadget.Settings.read(g_keyShowBroadbandContent);
    var mceBroadbandContentCount = System.Gadget.Settings.read(g_keyBroadbandContentCount);
    var mceShowRecordedTVItem  = System.Gadget.Settings.read(g_keyShowRecordedTVItem);
    var mceRecordedTVItemCount = System.Gadget.Settings.read(g_keyRecordedTVItemCount);
    var mceTransitionTime      = System.Gadget.Settings.read(g_keyTransitionTime);
    
    //////////////////////////////////////////////////////////////////////////////////////
    // Set default value if it is not ever set
    if(mceBroadbandContentCount == "")
    {
        // Set default value for RecordedTVItemCount when there is no saved data
        mceBroadbandContentCount = g_defaultBroadbandContentCount;
        System.Gadget.Settings.write(g_keyBroadbandContentCount, mceBroadbandContentCount);
        
        // Set default value for ShowRecordedTVItem when there is no saved data
        mceShowBroadbandContent = g_defaultShowBroadbandContent;
        System.Gadget.Settings.write(g_keyShowBroadbandContent,  mceShowBroadbandContent);
    }
    
    if(mceRecordedTVItemCount == "")
    {
        // Set default value for RecordedTVItemCount when there is no saved data
        mceRecordedTVItemCount = g_defaultRecordedTVItemCount;
        System.Gadget.Settings.write(g_keyRecordedTVItemCount, mceRecordedTVItemCount);
        
        // Set default value for ShowRecordedTVItem when there is no saved data
        mceShowRecordedTVItem = g_defaultShowRecordedTVItem;
        System.Gadget.Settings.write(g_keyShowRecordedTVItem,  mceShowRecordedTVItem);
    }
    
    // Set default value for TransitionTime when there is no saved data
    if(mceTransitionTime == "")
    {
        mceTransitionTime = g_defaultTransitionTime;
        System.Gadget.Settings.write(g_keyTransitionTime, mceTransitionTime);
    }
}

////////////////////////////////////////////////////////////////////////////////
//  Retrieve values from registry
////////////////////////////////////////////////////////////////////////////////
function GetMCEGadgetSettings()
{
    var mceShowBroadbandContent  = System.Gadget.Settings.read(g_keyShowBroadbandContent);
    var mceBroadbandContentCount = System.Gadget.Settings.read(g_keyBroadbandContentCount);
    var mceShowRecordedTVItem  = System.Gadget.Settings.read(g_keyShowRecordedTVItem);
    var mceRecordedTVItemCount = System.Gadget.Settings.read(g_keyRecordedTVItemCount);
    var mceTransitionTime      = System.Gadget.Settings.read(g_keyTransitionTime);
    
    SetDefaultSettingsValue();    
    
    //////////////////////////////////////////////////////////////////////////////////////
    // Apply the previous value to UI
    // set "ShowRecoredTVItem" check box with previous value
    mceShowRecordedTVItemCheck.checked = mceShowRecordedTVItem;
    
    // Set "RecordedTVItemCount" combobox box with previous value
    for(var i = 0; i < mceRecordedTVItemCountSelection.options.length; i++)
    {
        if(mceRecordedTVItemCount == mceRecordedTVItemCountSelection.options(i).text)
        {
            mceRecordedTVItemCountSelection.options(i).selected = true;
            break;
        }
    }
    
    // Set "ShowBroadband" check box with previous value
    mceShowBroadbandContentCheck.checked = mceShowBroadbandContent;
    
    // Set "BroadbandContentCount" combobox box with previous value
    for(var i = 0; i < mceBroadbandContentCountSelection.options.length; i++)
    {
        if(mceBroadbandContentCount == mceBroadbandContentCountSelection.options(i).text)
        {
            mceBroadbandContentCountSelection.options(i).selected = true;
            break;
        }
    }
    
    // Set checkbox title with broadband source name.
    var count = 0;
    for(count = 0; count < g_maxBBSourceCount; count++)
    {
        var checkboxname = "checkboxTitleBBSource" + count;
        var key = g_regKeyBBSourceTitle + count;
        var sourceTitle = System.Gadget.Settings.read(key);
        if (sourceTitle == "")
        {
            break;
        }
        
        document.getElementById(checkboxname).innerText = sourceTitle;
        
        // Set checkbox to be checked/unchecked with the last users setting.
        var checkbox = "checkBoxBBSource" + count;
        key = g_regKeyBBSourceShow + sourceTitle;
        if (System.Gadget.Settings.read(key) == "Hide")
        {
            document.getElementById(checkbox).checked = false;
        }
        else
        {
            document.getElementById(checkbox).checked = true;
        }    
    }
    
    if (count == 0)
    {
        document.getElementById("SourceListDiv").style.display = "none";
    }
    
    // hide checkbox controls if sources are not enough.
    for(; count<g_maxBBSourceCount;count++)
    {
        var checkboxname = "checkboxTitleBBSource" + count;
        document.getElementById(checkboxname).style.display = "none";
        var checkbox = "checkBoxBBSource" + count;
        document.getElementById(checkbox).style.display = "none";
        var row = "Row" + count;
        document.getElementById(row).style.display = "none";
    }
    
    // set "TransitionTime" combobox box with previous value
    for(var i = 0; i < mceTransitionTimeSelection.options.length; i++)
    {
        if(mceTransitionTime == mceTransitionTimeSelection.options(i).text)
        {
            mceTransitionTimeSelection.options(i).selected = true;
            break;
        }
    }
    
    // Enable/Disable RecordedTV setting.
    var isTVSetupDone = System.Gadget.Settings.read("IsTVSetupDone");   
    if (isTVSetupDone == true)
    {
        mceShowRecordedTVItemCheck.disabled = false;
        mceRecordedTVItemCountSelection.disabled = false;
    }
    else
    {
        mceShowRecordedTVItemCheck.disabled = true;
        mceRecordedTVItemCountSelection.disabled = true;
    }
    
    // Enable/Disable Internet TV setting.
    var isBBContentAvailable = System.Gadget.Settings.read("IsBBContentAvailable");   
    if (isBBContentAvailable == true)
    {
        mceShowBroadbandContentCheck.disabled = false;
        mceBroadbandContentCountSelection.disabled = false;
    }
    else
    {
        mceShowBroadbandContentCheck.disabled = true;
        mceBroadbandContentCountSelection.disabled = true;
    }
    
    
}

////////////////////////////////////////////////////////////////////////////////
// Get value of ShowBroadbandContent
////////////////////////////////////////////////////////////////////////////////
function GetShowBroadbandContentSetting()
{
    var mceShowBroadbandContent = System.Gadget.Settings.read(g_keyShowBroadbandContent);
    var mceBroadbandContentCount = System.Gadget.Settings.read(g_keyBroadbandContentCount);
      
    // Set default value for TransitionTime when there is no saved data
    if (mceBroadbandContentCount == "")
    {
        mceShowBroadbandContent = g_defaultShowBroadbandContent;
        System.Gadget.Settings.write(g_keyShowBroadbandContent, mceShowBroadbandContent);
    }
      
    return mceShowBroadbandContent;
}

////////////////////////////////////////////////////////////////////////////////
// Get value of BroadbandContentCount
////////////////////////////////////////////////////////////////////////////////
function GetBroadbandContentCountSetting()
{
    var mceBroadbandContentCount = System.Gadget.Settings.read(g_keyBroadbandContentCount);
      
    // Set default value for TransitionTime when there is no saved data
    if (mceBroadbandContentCount == "")
    {
        mceBroadbandContentCount = g_defaultBroadbandContentCount;
        System.Gadget.Settings.write(g_keyBroadbandContentCount, mceBroadbandContentCount);
    }
    
    return parseInt(mceBroadbandContentCount);
}

////////////////////////////////////////////////////////////////////////////////
// Get value of ShowRecordedTVITem
////////////////////////////////////////////////////////////////////////////////
function GetShowRecodedTVItemSetting()
{
    var mceShowRecordedTVItem = System.Gadget.Settings.read(g_keyShowRecordedTVItem);
    var mceRecordedTVItemCount = System.Gadget.Settings.read(g_keyRecordedTVItemCount);
      
    // Set default value for TransitionTime when there is no saved data
    if (mceRecordedTVItemCount == "")
    {
        mceShowRecordedTVItem = g_defaultShowRecordedTVItem;
        System.Gadget.Settings.write(g_keyShowRecordedTVItem, mceShowRecordedTVItem);
    }
      
    return mceShowRecordedTVItem;
}

////////////////////////////////////////////////////////////////////////////////
// Get value of RecordedTVItemCount
////////////////////////////////////////////////////////////////////////////////
function GetRecordedTVItemCountSetting()
{
    var mceRecordedTVItemCount = System.Gadget.Settings.read(g_keyRecordedTVItemCount);
      
    // Set default value for TransitionTime when there is no saved data
    if (mceRecordedTVItemCount == "")
    {
        mceRecordedTVItemCount = g_defaultRecordedTVItemCount;
        System.Gadget.Settings.write(g_keyRecordedTVItemCount, mceRecordedTVItemCount);
    }
    
    return parseInt(mceRecordedTVItemCount);
}

////////////////////////////////////////////////////////////////////////////////
// Get value of TransitionTime
////////////////////////////////////////////////////////////////////////////////
function GetTransitionTimeSetting()
{
    var mceTransitionTime = System.Gadget.Settings.read(g_keyTransitionTime);
      
    // Set default value for TransitionTime when there is no saved data
    if(mceTransitionTime == "")
    {
        mceTransitionTime = g_defaultTransitionTime;
        System.Gadget.Settings.write(g_keyTransitionTime, mceTransitionTime);
    }
    
    return parseInt(mceTransitionTime);
}

////////////////////////////////////////////////////////////////////////////////
//  Set settings value
////////////////////////////////////////////////////////////////////////////////
function SetMCEGadgetSettings(showBroadbandContent, broadbandContentCount, showRecordedTVItem, recordedTVItemCount, transitionTime)
{
    System.Gadget.Settings.write(g_keyShowBroadbandContent,  showBroadbandContent);
    System.Gadget.Settings.write(g_keyBroadbandContentCount, broadbandContentCount);
    System.Gadget.Settings.write(g_keyShowRecordedTVItem,  showRecordedTVItem);
    System.Gadget.Settings.write(g_keyRecordedTVItemCount, recordedTVItemCount);
    System.Gadget.Settings.write(g_keyTransitionTime,      transitionTime);
}

////////////////////////////////////////////////////////////////////////////////
// This eventhandler is called when Setting page is closed
////////////////////////////////////////////////////////////////////////////////
function OnSettingsClosing(event)
{
    // process settings page closing event
    if(event.closeAction == event.Action.commit) // "OK" button
    {
        SaveSettings();
    }
    else if (event.closeAction == event.Action.cancel)  // "Cancel" button
    {
    }
    
    event.cancel = false;
}

function SaveSettings()
{
    SetMCEGadgetSettings( mceShowBroadbandContentCheck.checked, 
                          mceBroadbandContentCountSelection.options(mceBroadbandContentCountSelection.selectedIndex).text, 
                          mceShowRecordedTVItemCheck.checked, 
                          mceRecordedTVItemCountSelection.options(mceRecordedTVItemCountSelection.selectedIndex).text, 
                          mceTransitionTimeSelection.options(mceTransitionTimeSelection.selectedIndex).text);        
                          
    for(var i = 0; i < g_maxBBSourceCount; i++)
    {
        var key = g_regKeyBBSourceTitle + i;
        var sourceTitle = System.Gadget.Settings.read(key);
        
        var checkboxname = "checkBoxBBSource" + i;
        key = g_regKeyBBSourceShow + sourceTitle;
        
        if (document.getElementById(checkboxname).checked == true)
        {
            System.Gadget.Settings.write(key, "Show");
        }
        else
        {
            System.Gadget.Settings.write(key, "Hide");
        }    
    }                          
}