function plog(info) {
    if(DEBUG) {
        desktop.elements.debug_info.html = info + "<br>" + desktop.elements.debug_info.html;
    }
}


// make interface
function createButtonDom(title, action, icon, id, img_id) {
    if(icon) {
        if(title) {
            var button = { tag: "a", href: "void", events: {onclick: action}, 
                           childs: [
                             { tag: "img", id: img_id, src: icon, 
                               style: {margin: "0px 4px 0px 0px", verticalAlign: "middle"}},
                             { tag: "span", html: title, style: {position: "relative"}}
                           ]};
            if(id) button.childs[1].id = id;
        } else {
            var button = { tag: "a", href: "void", events: {onclick: action}, 
                           childs: [
                             { tag: "img", id: img_id, src: icon}
                           ]};
            if(id) button.id = id;
        }
    } else {
        var button = { tag: "a", href: "void", events: {onclick: action}, html: title};
        if(id) button.id = id;
    }

    return button;
}





// tds: [{content:dom, width: ""}]
function createTableDom(tds, width) {
    var table = { tag: "table", width: (width ? width : "100%"), 
                  childs: {
                    tr: { tag: "tr",
                          childs: {} }
                  }
                };
    for(var i=0; i<tds.length; i++) {
        if(typeof(tds[i].content) == "string") {
            table.childs.tr.childs[i] = { tag: "td", width: tds[i].width, html: tds[i].content };
        } else {
            table.childs.tr.childs[i] = { tag: "td", width: tds[i].width, 
                                          childs: [tds[i].content] };
        }
    }
    return table;
}












function createLinkDividerDom(html) {
    return { tag: "span", cn: "link_divider", html: html};
}





// params: id, title, icon, icon_id, action, cn
function createLinkDom(p) {
    var m = [];
    if(p.icon) {
        if(p.title) {
            m = { tag: "a", href: "void", events: {onclick: p.action}, 
                  childs: [
                    { tag: "img", id: p.icon_id, src: p.icon, cn: "link_icon"},
                    { tag: "span", html: p.title, style: {position: "relative"}}
                  ]};
        } else {
            m = { tag: "a", href: "void", events: {onclick: p.action}, 
                  childs: [
                    { tag: "img", id: p.icon_id, src: p.icon}
                  ]};
        }
    } else {
        m = { tag: "a", href: "void", events: {onclick: p.action}, html: p.title};
    }
    m.style = { margin: "2px 0px 2px 0px"};

    if(p.cn) {
        m.className = p.cn;
    }

    if(p.id) {
        m.id = p.id;
    }

    return m;
}



// params: title, name, content, width
function createPopupDom(p) {
    var m = { tag: "div", cn: "popup_panel", id: p.name+"_window",
              is_alone: p.is_alone,
              style: {width: (p.width ? p.width + "px" : "300px")},
              childs: [
                { tag: "div", cn: "popup_caption",
                  childs: [
                    { tag: "span", cn: "popup_title", html: p.title, id: p.name + "_title"},
                    { tag: "img", src: "img/close.gif", cn: "popup_close_btn",
                      events: {onclick: "hidePopup('"+p.name+"')"} }
                  ]}
              ]};

    if(p.menu) {
        m.childs.push({ tag: "div", cn: "popup_menu", id: p.name+"_menu"});
    }

    var cc = [];
    if(p.content) {
        switch(p.content) {
            case "text_input":
                cc = [ { tag: "input", type: "text", id: p.name + "_input", style: {width: "190px"}},
                       { tag: "input", type: "button", value: " Ok ", cn: "button",
                         events: {onclick: p.action}} ];
                var style = { textAlign: "center" };
                break;

            case "link_input":
                cc = [ { tag: "div", cn: "popup_section",
                         childs: [
                           { tag: "span", html: "Name: ", cn: "popup_input_title"},
                           { tag: "input", type: "text", id: p.name + "_input_name", cn: "popup_input"}
                         ]},
                       { tag: "div", cn: "popup_section",
                         childs: [
                           { tag: "span", html: "Url: ", cn: "popup_input_title"},
                           { tag: "input", type: "text", id: p.name + "_input_url", cn: "popup_input"}
                         ]},
                       { tag: "input", type: "button", value: " Ok ", cn: "button",
                         events: {onclick: p.action}} ];
                var style = { textAlign: "center" };
                break;

            case "confirm":
                cc = [ { tag: "input", type: "button", value: " Ok ", cn: "button",
                         events: {onclick: p.action}},
                       { tag: "input", type: "button", value: " Cancel ", cn: "button",
                         events: {onclick: "hidePopup('"+p.name+"')"}} ];
                var style = { textAlign: "center" };
                break;

            case "edit_file":
                cc = [{ tag: "div", cn: "popup_section",
                        childs: [
                          { tag: "span", html: "Title: ", cn: "popup_input_title" },
                          { tag: "input", type: "text", cn: "popup_input", name: "name", id: p.name + "_name"},
                        ]},
                      { tag: "div", cn: "popup_section", 
                        childs: [
                          { tag: "span", html: "Description: ", cn: "popup_input_title" },
                          { tag: "textarea", cn: "popup_input", name:"description", id: p.name + "_description"},
                        ]},
                      { tag: "input", type: "button", value: "Save", id: p.name+"_action_btn", cn: "button", events: {onclick: p.action}}]
                var style = { textAlign: "center" };
                break;

            case "upload":
                cc = { tag: "div", 
                       innerHTML: "<iframe name='"+p.name+"_upload_iframe' style='width: 0px; height: 0px; border: 0px'></iframe>",
                       childs: [
                         { tag: "form", method: "post", encoding: "multipart/form-data", enctype: "multipart/form-data", action:"upload.php", 
                           target: p.name+"_upload_iframe",
                           id: p.name+"_form",
                           style: {padding: "0px", margin: "0px"},
                           childs: [
                             { tag: "input", type: "hidden", name: "cmd", value:"add_file"},
                             { tag: "input", type: "hidden", name: "MAX_FILE_SIZE", value:"500000"},
                             { tag: "input", type: "hidden", name: "parent_folder_id", id: p.name + "_parent_folder_id"},
                             { tag: "input", type: "hidden", name: "mode", id: p.name + "_mode"},
                             { tag: "div", cn: "popup_section",
                               childs: [
                                 { tag: "span", html: "Title: ", cn: "popup_input_title" },
                                 { tag: "input", type: "text", cn: "popup_input", name: "name", id: p.name + "_name"},
                               ]},
                             { tag: "div", cn: "popup_section", 
                               childs: [
                                 { tag: "span", html: "File: ", cn: "popup_input_title" },
                                 { tag: "input", type: "file", cn: "popup_input", name:"user_file", id: p.name + "_user_file"},
                               ]},

                             { tag: "div", cn: "popup_section", 
                               childs: [
                                 { tag: "span", html: "Description: ", cn: "popup_input_title" },
                                 { tag: "textarea", cn: "popup_input", name:"description", id: p.name + "_description"},
                               ]},
                             { tag: "input", type: "button", cn: "button", value: "Upload", id: p.name+"_action_btn", events: {onclick: p.action}}
                           ]}
                       ]
                     }
                var style = { textAlign: "center" };
                break;

            default:
                cc = (p.content.length ? p.content : [p.content]);
                break;

        }
        m.childs.push({ tag: "div", id: p.name + "_content", cn: "popup_content", childs: cc, style: style });
    } else {
        m.childs.push({ tag: "div", id: p.name + "_content", cn: "popup_content"});
    }
    return m;
}


// params: title, name, content, is_alone
//         is_alone - если да, то не закрывается при открытии др. попапа
//         content - (dom hash)|text_input|confirm|upload
function createPopupLink(p) {
varp(p);
    var lp = clone(p);
    lp.id = lp.name + "_link";
    lp.action = "showPopupAtLink('"+p.name+"')";
    return [
        createLinkDom(lp),
        createPopupDom(p)
    ]
}




function createMenuDom(name, items) {
    var itemsDom = [];
    var idx;
    for(var i=0; i<items.length; i++) {
        if(items[i].title == "-") {
            itemsDom[i] = { tag: "hr", cn: "menu_hr"}
        } else {
            idx = name+"_menu_item_"+(items[i].name ? items[i].name : i);
            itemsDom[i] = { tag: "div", cn: "menu_item", id: idx, html: items[i].title,
                            display: !(items[i].hidden),
                            events: {
                              onmouseover: "selectMenuItem('"+idx+"')",
                              onmouseout: "unselectMenuItem('"+idx+"')",
                              onclick: items[i].action}
                          }
        }
    }

    var m = 
        { tag: "div", cn: "menu_box", id: name + "_menu", 
          childs: [
//            { tag: "div", cn: "menu_shadow" },
            { tag: "div", cn: "menu_content", childs: itemsDom }
          ]}
    return m;
}

