var api = null;
var FontColor = null;
var ResetClicked = false;
function settingsClosed(p_event) {
    if (ResetClicked) {
        return;
    }
    if (p_event.closeAction == p_event.Action.commit) {

        Global.SetTheme(Global.Themes[api.getIndex()]);
        if (FontColor != null) {
            Global.SetFontColor(FontColor);
        }
        Global.SetSliderSpeed(parseInt($('#SliderSpeedCombo').val()));
        Global.SetSliderAnimatedType(parseInt($('#SliderAnimateStyle').val()));
        Global.SetFontStyle($('#ThemeSelect').val());
        Global.SetFontSize($('#ThemeFontSize').val());
        Global.SetShowScroll($('#ScrollCheck').is(':checked'));
        Global.SetSyncMinutes($('#SetSyncTime').val());
        Global.SetPromptDelete($('#PromptDelete').is(':checked'));
        Global.SetTextAlign($('#SetAlign').val());
        Global.SetLoginType($('#SetSyncOpt').val());
        Global.SetExitPrompt($('#PromptExit').is(':checked'));
        Global.SetLogoutPrompt($('#PromptLogout').is(':checked'));
        Global.SetNoteSize($('#LargeThemeSelect').val());
    }
}

function Settings() { }
Settings.SettingsLoad = function() {
    Global.SettingsInit();
    System.Gadget.onSettingsClosing = settingsClosed;
    document.body.style.height = "400px";
    document.body.style.width = "300px";
    $("#fragment-1h").text(Strings.GetthemeLabel());
    $("#fragment-2h").text(Strings.GetgeneralLabel());
    $("#fragment-3h").text(Strings.Getabout());
    $("#fragment-4h").text(Strings.GetresetLabel());
    $("#SetText").text(Strings.GetwelcomeText());
    $("#AlignTextLabel").text(Strings.GetalignLabel());
    $("#ScrollbarLabel").text(Strings.GetshowScroll());
    $("#FontLabel").text(Strings.GetfontType());
    $("#SizeLabel").text(Strings.GetfontSize());
    $("#ColorLabel").text(Strings.GetfontColor());
    $("#NoteChangeLabel").text(Strings.Getonnotechange());
    $("#SliderSpeedLabel").text(Strings.Getsliderspeed());
    $("#LargerClientLabel").text(Strings.Getlargerclient());
    $("#ConfDeleteLabel").text(Strings.GetdelPrompt());
    $("#ConfLogout").text(Strings.GetpromptLogout());
    $("#onnotesshowlabel").text(Strings.GetstartUp());
    $("#SyncTimeLabel").text(Strings.Getsynctime());
    $("#ConfirmExitLabel").text(Strings.GetpromptExit());
    $("#CheckSyncOpt").text(Strings.Getsyncopts()[0]);
    $("#GetServerOpt").text(Strings.Getsyncopts()[1]);
    $("#DoNothingOpt").text(Strings.Getsyncopts()[2]);
    $("#tenminopt").text(Strings.Getsynctimeopts()[0]);
    $("#thirtyminopt").text(Strings.Getsynctimeopts()[1]);
    $("#Houropt").text(Strings.Getsynctimeopts()[2]);
    $("#Dayopt").text(Strings.Getsynctimeopts()[3]);
    $("#Weekopt").text(Strings.Getsynctimeopts()[4]);
    $("#AboutText").text(Strings.GetAboutText());
    $("#donatelabel").text(Strings.GetPlDonate1());
    $("#DonateButton").attr("title", Strings.GetPlDonate2());
    $("#ResetDesc").text(Strings.GetresetDesc());
    $("#DoyouResetlabel").text(Strings.Getproceed());
    $("#AreYouSurelabel").text(Strings.GetresetSure());
    $("#ResetOk1").attr("value", Strings.Getok());
    $("#ResetOk2").attr("value", Strings.Getok());
    $("#ResetCancel").attr("value", Strings.Getcancel());
    $("#OnlineHelpLabel").text(Strings.GetOnlineHelp());
    $("#SendAnEmailLabel").text(Strings.GetSendAnEmail());
    $("#UpdatesNewClientsLabel").text(Strings.GetUpdatesNewClients());
    $("#leftopt").text(Strings.Getaligns()[0]);
    $("#centeropt").text(Strings.Getaligns()[1]);
    $("#rightopt").text(Strings.Getaligns()[2]);
    $("#sizeopt1").text(Strings.Getsizes()[0]);
    $("#sizeopt2").text(Strings.Getsizes()[1]);
    $("#sizeopt3").text(Strings.Getsizes()[2]);
    $("#sizeopt4").text(Strings.Getsizes()[3]);
    $("#sizeopt5").text(Strings.Getsizes()[4]);
    $("#sizeopt6").text(Strings.Getsizes()[5]);
    $("#sizeopt7").text(Strings.Getsizes()[6]);
    $("#sizeopt8").text(Strings.Getsizes()[7]);
    $("#sizeopt9").text(Strings.Getsizes()[8]);
    $("#animate1").text(Strings.GetAnimeTypes()[0]);
    $("#animate2").text(Strings.GetAnimeTypes()[1]);
    $("#animate3").text(Strings.GetAnimeTypes()[2]);
    $("#speed1").text(Strings.GetAnimeSpeed()[0]);
    $("#speed2").text(Strings.GetAnimeSpeed()[1]);
    $("#speed3").text(Strings.GetAnimeSpeed()[2]);
    $("#speed4").text(Strings.GetAnimeSpeed()[3]);
    $("#speed5").text(Strings.GetAnimeSpeed()[4]);
    $("#VistaLabel").text(Strings.GetVistaLabel());
    
    var selObj = document.getElementById('LargeThemeSelect');
    for (var i = 1; i < Global.Sizes.length; i++) {

            var opt = new Option(Global.Sizes[i][0].toString() + " X " + Global.Sizes[i][1].toString(), i);
            selObj.options[i-1] = opt;
    }
    selObj = document.getElementById('ThemeSelect');
    for (var i = 0; i < Strings.GetFonts().length; i++) {
        var opt = new Option(Strings.GetFonts()[i][0], Strings.GetFonts()[i][1]);
        selObj.options[i] = opt;
    }
    while (document.getElementById("SetColorBox").childNodes.length > 0) {
        document.getElementById("SetColorBox").removeChild(document.getElementById("SetColorBox").childNodes[0]);
    }
    var ipRef = document.createElement("input");
    ipRef.setAttribute("type", "text");
    ipRef.setAttribute("id", "selectedExample");
    ipRef.style.display = "none";
    document.getElementById("SetColorBox").appendChild(ipRef);
    api = $("ul.tabs").tabs("div.panes > div", {
        onClick: function(index) {
            try {
            }
            catch (err) { };
        }
    });
    $("#infinite").scrollable({ size: 1 }).circular();
    api = $("#infinite").scrollable();
    for (var i = 0; i < Global.Themes.length; i++) {
        api.getItemWrap().append("<img src='Themes/" + Global.Themes[i] + "/bg.png'/>");
    }
    api.reload().end();
    $.modal.close();
    $('#back').modal({ close: (false), containerCss: { height: (parseInt(document.body.style.height) - 5) + '', width: (parseInt(document.body.style.width) - 5) + '' }, position: ['0px', '0px'] });
    $(function() {
        $('#selectedExample').crayonbox({
            colors: new Array('#000000', '#777', '#888', '#999', '#AAA', '#BBB', '#CCC', '#DDD', '#FFF', '#102061', '#1f3fbf', '#0000ff', '#21bfdb', '#0b9c0b', '#00ff00', '#76f04d', '#ffff00', '#737308', '#8f7118', '#a60d0d', '#ff0000', '#ff0073', '#ff00ff'),
            selected: Global.FontColor,
            clearButton: false,
            onSelection: function() {
                var color = $(this).find('.coloring').attr('title');
                FontColor = color;
                document.getElementById("SetText").style.color = color;
            }
        });
    });
    $('#SetText').css('font-size', Global.FontSize);
    $("#ThemeFontSize").val(Global.FontSize);
    $('#SetText').css('color', Global.FontColor);
    $('#SetText').css('text-align', Global.TextAlign);
    $("#SetAlign").val(Global.TextAlign);
    $('#ScrollCheck').attr('checked', Global.ShowScroll);
    $("#ThemeSelect").val(Global.FontStyle);
    $('#SetText').css('font-family', Global.FontStyle);
    $("#LargeThemeSelect").val(Global.NoteSize);
    $("#SliderAnimateStyle").val(Global.SliderAnimatedType);
    $("#SliderSpeedCombo").val(Global.SliderSpeed);
    for (var i = 0; i < Global.Themes.length; i++) {
        if (Global.Themes[i] == Global.Theme) {
            api.setPage(i);
            break;
        }
    }
    $('#PromptDelete').attr('checked', Global.PromptDelete);
    $('#PromptExit').attr('checked', Global.ExitPrompt);
    $('#PromptLogout').attr('checked', Global.LogoutPrompt);
    $("#SetSyncOpt").val(Global.LoginType);
    $("#SetSyncTime").val(Global.SyncMinutes);
    Settings.SetSyncTime($('#SetSyncOpt').val());
    ShowSlideSpeed($('#SetSyncOpt').val());
}

Settings.SaveSettings = function() {
    UI.SetSettings1();
}

Settings.AlertChange = function(bool) {
    document.getElementById("ProceedReset").style.display = (bool) ? "block" : "none";
    document.getElementById("ConfirmReset").style.display = (bool) ? "none" : "block";
}

Settings.Reset = function() {
    //Notes.ClearSyncTime();
    Global.SetFirstLoad("");
    Global.SetGUID("");
    ResetClicked = true;
    var iswindows = true; //try { if (System.Gadget.docked) { iswindows = true; } } catch (err) { }
    if (iswindows) {
        document.getElementById("VistaLabel").style.display = "block";
        System.Gadget.document.parentWindow.fload = true;
    }
    else {
    }
    //CloseModal();
    //Global.Init();
    //UI.CommonLoad();
}


Settings.SetFontStyle = function(val) {
    document.getElementById("SetText").style.fontFamily = val;
}

Settings.SetFontSize = function(val) {
    document.getElementById("SetText").style.fontSize = val + "px";
}

Settings.SetSyncTime = function(val) {
    switch (val) {
        case "0":
            $("#notesshowexplain").text(Strings.Getsyncdesc()[2]);
            break;
        case "1":
            $("#notesshowexplain").text(Strings.Getsyncdesc()[0]);
            break;
        case "2":
            $("#notesshowexplain").text(Strings.Getsyncdesc()[1]);
            break;
    }
    Settings.SetSyncTimeDesc();
}

Settings.SetSyncTimeDesc = function() {
    $("#SyncTimeDesc").text(Strings.Getsynctimedesc1() + $('#SetSyncTime :selected').text() + Strings.Getsynctimedesc2() + Strings.Getsyncopts()[0]);
}

Settings.SetAlign = function(val) {
    Settings.Align = val;
    if (val != "System Default") {
        document.getElementById("SetText").style.textAlign = val;
    }
    else {
        document.getElementById("SetText").style.textAlign = "";
    }

}

function ShowSlideSpeed(val) {
    if (val == "2") {
        document.getElementById("SetSpeed").style.display = "none";
    }
    else {
        document.getElementById("SetSpeed").style.display = "block";
    }
}
