function UI() { }
tempSettingsId = 0;
UI.updateTimer = 0;
UI.insynctimer = 0;
UI.setsyncTimer = 0;
UI.Changed = new Array(11);
UI.Status = null;
UI.Export = null;
UI.NextButton = null;
UI.PrevButton = null;
UI.AddButton = null;
UI.DelButton = null;
UI.Position = null;
UI.Height = "0px";
UI.Width = "0px";

UI.EndShowSyncString = function() {
    clearTimeout(UI.insynctimer);
    UI.HideLoader();
    UI.ClearUpdateKey(true);
    Notes.GetInfo();
}

UI.RemoveTheme = function() {
    try {
        document.getElementsByTagName("head")[0].removeChild(document.getElementById("ThemeJS"));
        document.getElementsByTagName("head")[0].removeChild(document.getElementById("ThemeCSS"));
    }
    catch (err) { }
}

UI.LoadTheme = function() {
    var filejs = "Themes/" + Global.GetTheme() + "/default.js";
    var filecss = "Themes/" + Global.GetTheme() + "/default.css";

    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filejs);
    fileref.setAttribute("id", "ThemeJS");
    if (typeof fileref != "undefined") {
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
    fileref = document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", filecss);
    fileref.setAttribute("id", "ThemeCSS");
    if (typeof fileref != "undefined") {
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
    
}

UI.SetSize = function() {
    //debugger;
    UI.Width = Global.Sizes[Global.NoteSize][0] + "px";
    UI.Height = Global.Sizes[Global.NoteSize][1] + "px";
    if (!Global.Autosize) {
        try {//CHECK ONLY FOR VISTA
            if (Global.IsDock == "Y") {
                UI.Width = Global.Sizes[0][0] + "px";
                UI.Height = Global.Sizes[0][1] + "px";
            }

        }
        catch (err) {
        }
        document.body.style.width = UI.Width;
        document.body.style.height = UI.Height;
        $("#front").css("width", UI.Width);
        $("#front").css("height", UI.Height);
        $("#BackImg").css("width", UI.Width);
        $("#BackImg").css("height", UI.Height);
        $("#LoaderContent").css("width", UI.Width);
        $("#LoaderContent").css("height", UI.Height);
        $("#LoaderBg").css("width", UI.Width);
        $("#LoaderBg").css("height", UI.Height);
        var temp = parseInt(UI.Height) / 2;
        var t2 = temp - 16;
        t2 = t2 + "px";
        $("#LoaderRing").css("top", t2);
        t2 = ((parseInt(UI.Width) * 0.8) / 2) - 8;
        t2 = t2 + "px";
        $("#LoaderRing").css("left", t2);
        temp = temp + "px";
        $("#LoaderMessage").css("top", temp);

        /*var lmsg = parseInt(UI.Width) - 10;
        $("#LoaderMessage").css("width", lmsg + "px");
        lmsg = parseInt(UI.Height) - 10;
        $("#LoaderMessage").css("height", lmsg + "px");*/

        document.body.style.width = UI.Width;
        document.body.style.height = UI.Height;

    }
}

UI.SetMessage = function(element, msg) {
    $("#" + element).text(msg);
}

UI.ShowLoader = function(disp, func) {
    $("#LoaderMessage").text(disp);
    $("#LoaderContent").css("display", "block");
    if (func != null) {
        $("#LoaderContent").css("cursor", "pointer");
        $("#LoaderContent").click(func);
        $("#LoaderMessage").click(func);
    }
    else {
        $("#LoaderContent").css("cursor", "default");
        $("#LoaderContent").click(function() { func });
        $("#LoaderMessage").click(function() { func });
    }
}
UI.HideLoader = function() {
    $("#LoaderContent").css("display", "none");
}

UI.Init = function() {
    Global.Init();
    UI.SetSize();
    UI.ShowLoader(Strings.GetloadControl(), null);
    UI.RemoveTheme();
    try {//CHECK ONLY FOR WINDOWS
        if (System.Gadget != null) {
            UI.LoadTheme();
            tempSettingsId = setTimeout('UI.Init2()', 300);
            return;
        }

    } catch (err) { }

    jQuery.getScript("Themes/" + Global.GetTheme() + "/default.js", UI.Init2);
}

UI.TextChanged = function() {
    UI.Status.SetState(2);
    clearTimeout(UI.updateTimer);
    UI.ClearUpdateKey(false);
    UI.updateTimer = setTimeout("UI.Update()", 5000);

}
UI.Update = function() {

    var selecteNotes = new Array(11);
    selecteNotes[0] = '';
    for (var i = 1; i <= 10; i++) {
        if (UI.Changed[i]) {
            UI.Changed[i] = false;
            //selecteNotes[i] = $("#Layer" + i).val()
            selecteNotes[i] = document.getElementById("Layer" + i).value
            selecteNotes[0] = selecteNotes[0] + ((i == 10) ? "0" : i);
        }
        else {
            selecteNotes[i] = "";
        }
    }
    if (selecteNotes[0] != '') {
        Notes.UpdateSelectedNotes(selecteNotes[1], selecteNotes[2], selecteNotes[3], selecteNotes[4], selecteNotes[5], selecteNotes[6], selecteNotes[7], selecteNotes[8], selecteNotes[9], selecteNotes[10], selecteNotes[0]);
    }
    else {
        UI.Status.SetState(Notes.LoggedIn() ? 1 : 0);
    }
}

UI.ValidateUpdate = function(type, args) {

    switch (parseInt(args[0].Code)) {
        case 1:
            UI.Status.SetState(Notes.LoggedIn() ? 1 : 0);
            if (Notes.LoggedIn()) {
                UI.ClearUpdateKey(true);
            }
            break;
        default:
            if (Notes.LoggedIn()) {
                UI.Status.SetState(Notes.LoggedIn() ? 1 : 0);
                UI.ClearUpdateKey(false);
                UI.ShowLoader(Strings.Getnoteupdateerror(), Platform.ShowTry);
            }
            break;
    }
}

UI.SetUpdateKey = function() {
    UI.ClearUpdateKey(false);
    if (Notes.LoggedIn()) {
        UI.InitBySync(1);
    }
}

UI.LoadSettings = function() {

    wdth = parseInt(UI.Width) - 2 * Theme.Side;
    hght = parseInt(UI.Height) - Theme.Top - Theme.Bottom;
    for (var i = 1; i <= 10; i++) {
        $("#Layer" + i).css("top", Theme.Top + "px");
        $("#Layer" + i).css("left", Theme.Side + "px");
        $("#Layer" + i).css("width", wdth + "px");
        $("#Layer" + i).css("height", hght + "px");
    }
    UI.SetFonts();

    UI.NextButton.SetTheme(Global.Theme);
    UI.PrevButton.SetTheme(Global.Theme);
    UI.AddButton.SetTheme(Global.Theme);
    UI.DelButton.SetTheme(Global.Theme);
    UI.HideLoader();
}

UI.Init2 = function() {
    UI.NextButton = new NextButton(Global.GetTheme());
    UI.PrevButton = new PrevButton(Global.GetTheme());
    UI.AddButton = new AddButton(Global.GetTheme());
    UI.DelButton = new DelButton(Global.GetTheme());
    UI.Position = new Position();
    $("#front").bind("mouseout", function(e) {
        UI.SetOnClient(false);
    });
    $("#front").bind("mouseover", function(e) {
        UI.SetOnClient(true);
    });
    wdth = parseInt(UI.Width) - 2 * Theme.Side;
    hght = parseInt(UI.Height) - Theme.Top - Theme.Bottom;
    for (var i = 1; i <= 10; i++) {
        UI.Changed[i] = false;
        $("#Layer" + i).css("top", Theme.Top + "px");
        $("#Layer" + i).css("left", Theme.Side + "px");
        $("#Layer" + i).css("width", wdth + "px");
        $("#Layer" + i).css("height", hght + "px");
        $("#Layer" + i).bind("blur change keyup mouseup", function(e) {
            var i = parseInt(this.id.replace("Layer", ""));
            var mlength = 0;
            if (Global.ClientHasDiffLength && !Notes.LoggedIn()) {
                mlength = Global.ClientMaxNoteLength;
                if (UI.GetAllNotesCount(true) > mlength) {
                    var tempMaxLength = mlength - UI.GetAllNotesCount(false);
                    document.getElementById("Layer" + i).value = document.getElementById("Layer" + i).value.substring(0, tempMaxLength);
                    UI.ShowAlert(Strings.GetNotesWithout1() + Global.ClientMaxNoteLength + Strings.GetNotesWithout2() + Global.ServerMaxNoteLength + Strings.GetNotesWithout3(), null, null);
                }
            }
            else {
                mlength = Global.ServerMaxNoteLength;
                if (document.getElementById("Layer" + i).value.length > mlength) {
                    document.getElementById("Layer" + i).value = document.getElementById("Layer" + i).value.substring(0, mlength)
                    UI.ShowAlert(Strings.GetmaxNotes(), null, null);
                }
            }
            if (document.getElementById("Layer" + i).value != Global.GetNote(i)) {
                UI.Changed[i] = true;
                UI.TextChanged();
            }
        });
    }
    UI.Status = new Status(0);
    UI.Export = new ExportMenu();
    UI.SetFonts();
    //Notes.Init(0);
    if (Notes.LoggedIn()) {
        UI.ShowLoader(Strings.GetaccessServer(), null);
        UI.Status.SetState(1);
        Notes.Init(Global.GetLoginType());
    }
    else {

        UI.ShowLoader(Strings.GetLoading(), null);
        UI.Status.SetState(0);
        Notes.Init(0);
    }



}

UI.GetAllNotesCount = function(withCurrent) {
    var retval = 0;
    for (var i = 1; i <= 10; i++) {
        if (i == parseInt(Global.GetCurrentPosition())) {
            if (withCurrent) {
                retval += document.getElementById("Layer" + i).value.length;
            }
        }
        else {
            retval += document.getElementById("Layer" + i).value.length;
        }
    }
    return retval;
}

UI.InitByGUID = function(guid, initval) {
    Global.SetGUID(guid);
    UI.ShowLoader(Strings.GetaccessServer(), null);
    UI.Status.SetState(1);
    
    UI.InitByOption(initval);
}

UI.InitBySync = function(initval) {
    UI.ShowLoader(Strings.GetaccessServer(), null);
    UI.InitByOption(initval);
}

UI.InitClose = function() {
    Global.SetGUID("");
    UI.Status.SetState(0);
    UI.HideLoader();
}

UI.InitByOption = function(initval) {

    UI.ShowLoader(Strings.GetaccessServer(), null);
    Notes.Init(initval);
}

UI.ShowPosition = function(butt) {
    UI.Position.ShowPosition();
    UI.DelButton.SetState();
    UI.AddButton.SetState();
    UI.PrevButton.SetState();
    UI.NextButton.SetState();
    


}

UI.ClearUpdateKey = function(callfunc) {
    clearTimeout(UI.setsyncTimer);
    if (callfunc && Notes.LoggedIn()) {
        UI.setsyncTimer = setTimeout("UI.SetUpdateKey()", parseInt(parseInt(Global.GetSyncMinutes()) * 60000));
    }
}

UI.Move = function(currentIndex, newIndex, forward) {
    //debugger;
    if (Global.GetSliderAnimatedType() == "0") {
        UI.Swap(currentIndex, newIndex);
    }
    else {
        Animate.Choose(currentIndex, newIndex, forward);
    }
}
        
UI.Swap = function(cIndex, nIndex) {
            $("#Layer" + cIndex).css("display", "none");
            $("#Layer" + nIndex).css("display", "block");
        }
        
        

UI.DeleteNote = function()
        {
            var curr = Notes.CurrentNoteIndex();
            $("#Layer" + curr).text("");
            UI.Changed[curr] = true;
            UI.TextChanged();
               
            
            if (Notes.IsLast())
            {
                UI.Move(curr, Notes.PreviousNoteIndex(), false);
            }
            else
            {
                UI.Move(curr, Notes.NextNoteIndex(), true);
            }
            Notes.DeleteNote(curr);
        }

        UI.Init3 = function(type, args) {
            
            //PLAN TO USE DIFF ERROR CODES FOR REGISTER/LOGIN/SYNC ETC
            switch (parseInt(args[0].Code)) {
                case 1: //Everything Normal
                    UI.ShowPosition();
                    for (var i = 1; i <= 10; i++) {
                        $("#Layer" + i).css("display", "none");
                        
                        document.getElementById("Layer" + i).value = Global.GetNote(i);
                        Global.SetNote(i, document.getElementById("Layer" + i).value);

                    }
                    UI.HideLoader();
                    $("#Layer" + Notes.CurrentNoteIndex()).css("display", "block");
                    
                    if (Global.GetFirstLoad() == "") {
                        Global.SetFirstLoad("Set");
                        Platform.ShowWelcome();
                    }
                    else {
                        
                        Notes.GetInfo();
                    }
                    if (Notes.LoggedIn()) {
                        UI.ClearUpdateKey(true);
                    }
                    break

                case 2:
                    if (!Global.IsOpen) {
                        UI.ShowLoader(Strings.GetsyncNowDesc(), Platform.ShowSync);
                    }
                    Platform.ShowSync();
                    break;
                case 3: //In Sync
                    UI.ShowPosition();
                    for (var i = 1; i <= 10; i++) {
                        $("#Layer" + i).css("display", "none");
                        document.getElementById("Layer" + i).value = Global.GetNote(i);
                        Global.SetNote(i, document.getElementById("Layer" + i).value);

                    }
                    $("#Layer" + Notes.CurrentNoteIndex()).css("display", "block");
                    UI.ShowLoader(Strings.GetinSync(), null);
                    clearTimeout(UI.insynctimer);
                    UI.insynctimer = setTimeout("UI.EndShowSyncString()", 1000);

                    break
                case -1:
                    UI.ShowLoader(Strings.GetsyncServerErrorVista(), Platform.ShowTry);
                    break;
                case -2:
                    //Error updating//Client Update Error
                    UI.ShowLoader(Strings.GetsyncServerErrorVista(), Platform.ShowTry);
                    break;
                default:
                    UI.HideLoader();
                    break;
            }
        }
        UI.SetOnClient = function(val) {
            UI.NextButton.Show(val);
            UI.PrevButton.Show(val);
            UI.AddButton.Show(val);
            UI.DelButton.Show(val);
            if (UI.Status.State != 2) {
                UI.Status.Show(val);
            }
            else {
                UI.Status.Show(true);
            }
            UI.Export.Show(val);
            UI.Position.Show(val);
            
        }
        UI.DelCheckBox = false;
        UI.ShowDelete = function() {
            $("#DeleteHeader").text(Strings.GetdelPrompt());
            $("#DeleteDoYou").text(Strings.Getdoyou());
            $("#DeleteDoNot").text(Strings.GetDeleteDoNot());
            $("#DeleteCanBe").text(Strings.GetDeleteCanBe());
            $("#DoDelete").attr({ value: Strings.Getdel() });
            $("#CancelDelete").attr({ value: Strings.Getcancel() });
            UI.DelCheckBox = false;
            UI.ShowLoader("", null);
            $.modal.close();
            $('#DeleteContent').modal({ close: (false), containerCss: { height: '150', width: (parseInt(document.body.style.width) - 10) + ''} });
        }

        UI.ShowAlert = function(msg, url, urltxt) {
            //debugger;
            if(url != null){
                $("#Urltxt").text(urltxt);
                $("#Urltxt").attr({ href: url});
            }    
            $("#AlertHeader").text(Strings.GetsnoAlert());
            $("#AlertMsg").text(msg);
            $("#alertButton").attr({ value: Strings.Getok() });
            UI.ShowLoader("", null);
            $.modal.close();
            $('#AlertContent').modal({ close: (false), containerCss: { height: '150', width: (parseInt(document.body.style.width) - 10) + ''} });
        }
        UI.Msg = function(type, args) {
           //debugger;
            UI.ShowAlert(args[0].Text, args[0].Url, args[0].UrlText);
        }
        
        UI.CloseDelete = function() {
            UI.DelCheckBox = false;
            $.modal.close();
            UI.HideLoader();
            UI.DelCheckBox = false;
        }
        UI.CloseAlert = function() {
            $.modal.close();
            UI.HideLoader();
        }
        UI.PerformDelete = function() {
            if (UI.DelCheckBox) {
                Global.SetPromptDelete(false);
            }
            UI.DeleteNote();
            UI.ShowPosition();
            UI.CloseDelete();
        }
        UI.SetConfirmDeleteCheck = function(obj) {
            UI.DelCheckBox = obj.checked;
        }

        UI.SetFonts = function() {
        //$("#BackImg").attr("src", "Themes/" + Global.Theme + "/bg.png");
        BackImg.src = "url(themes/" + Global.Theme + "/bg.png)";
            for (var i = 1; i <= 10; i++) {
                tlayer = document.getElementById('Layer' + i);
                tlayer.style.fontSize = Global.FontSize + "px";
                tlayer.style.fontFamily = Global.FontStyle;
                tlayer.style.color = Global.FontColor;
                tlayer.style.textAlign = Global.TextAlign;
                try { tlayer.style.overflowX = 'hidden'; } catch (Error) { }
                if (Global.ShowScroll) {
                    tlayer.style.overflow = 'auto';
                }
                else {
                    tlayer.style.overflow = 'hidden';
                }
            }
        }

$(document).ready(function() {
    UI.Init();
});
onNotesInit.subscribe(UI.Init3);
onNotesUpdate.subscribe(UI.ValidateUpdate);
onNotesInfo.subscribe(UI.Msg);
