function ToDoList() {
    this.init();

    this.domSettings = [
           { tag: "div", className: "settings_section",
              childs: [
                  { tag: "span", innerHTML: "New To Do: ", className: "settings_label"},
                  {tag: "input", id: "todo_title", type: "text", size: "15", className: "settings_control"},
                  {tag: "input", type: "button", value: " Add ", events: {onclick: "addItem()"}, className: "settings_control"}
              ]} 
    ]

    this.domContent = { tag: "table", className: "sys_table", id: "items_table",
                        childs: [] }

    this.cfg = {
        hasRefreshBtn: false,
        title_prefix: "To Do List: ",
        title: "",
        module: "ToDoList"
    }

    this.defaultProfile["items"] = [];
    this.defaultProfile["title"] = "";


    this.onBuildInterface = function() {
        this.buildDomModel(this.elements.settings, this.domSettings);
        this.renderItems();
    }


    this.onOpen = function() {
        this.setTitle(this.profile.title);
    }

    this.setTitle = function(html) {
        this.elements.title.innerHTML = this.cfg.title_prefix + html;
    }


    this.renderItems = function() {
        this.elements.content.innerHTML = "";
        this.buildDomModel(this.elements.content, this.domContent);
        for(var i=0; i<this.profile.items.length; i++) {
            this.renderItem(i);
        }
    }

    this.renderItem = function(n) {
        this.buildDomModel(this.elements.items_table_tbody,
            { tag: "tr", id: "item"+n,
              childs: [
                { tag: "td", width: "1%",
                  childs: [
                    { tag: "input", id: "item_checkbox"+n, type: "checkbox", events: {onclick: "switchItem("+n+")"}, checked: this.profile.items[n].completed}
                  ]},
                { tag: "td", width: "100%",
                  style: {textDecoration: this.profile.items[n].completed ? "line-through" : ""},
                  id: "item_title"+n, innerHTML: this.profile.items[n].title},

                { tag: "td", width: "1%",
                  childs: [ createButtonDom(false, "moveItemUp("+n+")", theme.icons.move_up) ]},

                { tag: "td", width: "1%",
                  childs: [ createButtonDom(false, "moveItemDown("+n+")", theme.icons.move_down) ]},

                { tag: "td", width: "1%",
                  childs: [ createButtonDom(false, "editItem("+n+")", theme.icons.edit) ]},

                { tag: "td", width: "1%",
                  childs: [ createButtonDom(false, "deleteItem("+n+")", theme.icons.delete_link) ]}
              ]
            });
    }


    this.addItem = function() {
        var title = trim(this.elements.todo_title.value);
        if(!title) {
            return;
        }

        var newId = this.profile.items.length;
        var newItem = {title: title, completed: false};
        this.profile.items[newId] = newItem;
        this.renderItem(newId);
        this.save();

        this.elements.todo_title.value = '';
    }


    this.switchItem = function(id) {
        this.profile.items[id].completed = !this.profile.items[id].completed;
        this.elements['item_title'+id].style.textDecoration = this.profile.items[id].completed ? "line-through" : "" ;
        this.save();
    }


    this.moveItemUp = function(id) {
          if(id > 0) {
            this.swapItems(id, id-1);
            this.renderItems();
            this.save();
        }
    }

    this.moveItemDown = function(id) {
        if(id < this.profile.items.length - 1) {
            this.swapItems(id, id+1);
            this.renderItems();
            this.save();
        }
    }


    this.editItem = function(id) {
        var res = trim(prompt('To Do Edit:', this.profile.items[id].title));
        if(res) {
            this.profile.items[id].title = res;
            this.elements['item_title'+id].innerHTML = res;
        }
        this.save();
    }



    this.deleteItem = function(id) {
        if(confirm('Are you sure you want to delete the To Do: '+this.profile.items[id].title+' ?')) {
            var tmp = [];
            for(var i=0; i<this.profile.items.length; i++) {
                if(i != id) {
                    tmp.push(this.profile.items[i]);
                }
            }
            this.profile.items = tmp;
            tmp = null;
            this.renderItems();
            this.save();
        }
    }



    this.swapItems = function(a,b) {
        var tmp = this.profile.items[a];
        this.profile.items[a] = this.profile.items[b];
        this.profile.items[b] = tmp;
    }    
}
ToDoList.prototype = new Widget();