cfgWidget = {
    hasIcon: true,
    hasSizeBtn: true,
    hasCloseBtn: true,
    hasRefreshBtn: true,
    hasSettingsBtn: true,
    hasDrag: true,
    hasOnCloseConfirm: false,
    hasProfile: true,
    isOpenHidden: false,
    isSystem: false,
    title: "Jaws widget",
    module: "base",
    saveMethod: "GET"
}

SYSWIDGET_WKEY = 5000;

domTitleSettings = [
    { tag: "div", className: "settings_section",
      childs: [
          {tag: "span", innerHTML: "Title:", className: "settings_label"},
          {tag: "input", id: "input_title", type: "text", size: "15", className: "settings_control"}, 
          {tag: "input", type: "button", value: " Set ", events: {onclick: "settingsSetTitle()"}, className: "settings_control"}
      ]},

    { tag: "div", className: "settings_section", 
      childs: [
        { tag: "span", innerHTML: "Caption color:", className: "settings_label", style: {marginTop: "0px"}},
        { tag: "span", innerHTML: "&nbsp;", className: "color_box", style: {background: "#EEEEFF"}, events: {onclick: "actionSetCaptionColor('#EEEEFF')"}},
        { tag: "span", innerHTML: "&nbsp;", className: "color_box", style: {background: "#FFFFCC"}, events: {onclick: "actionSetCaptionColor('#FFFFCC')"}},
        { tag: "span", innerHTML: "&nbsp;", className: "color_box", style: {background: "#FFCCFF"}, events: {onclick: "actionSetCaptionColor('#FFCCFF')"}},
        { tag: "span", innerHTML: "&nbsp;", className: "color_box", style: {background: "#CCFFFF"}, events: {onclick: "actionSetCaptionColor('#CCFFFF')"}},
        { tag: "span", innerHTML: "&nbsp;", className: "color_box", style: {background: "#FFCCCC"}, events: {onclick: "actionSetCaptionColor('#FFCCCC')"}},
        { tag: "span", innerHTML: "&nbsp;", className: "color_box", style: {background: "#CCCCFF"}, events: {onclick: "actionSetCaptionColor('#CCCCFF')"}}
      ]}
]
 

function Widget() {
    this.defaultProfile = {
        title: "Jaws widget",
        titleColor: ""
    }


    this.init = function() {
        this.elements = {};
        this.id = null;
        this.isReduced = false;
        this.isSettingsReduced = true;
        this.profile = {};

        this.domModel = { tag: "div", className: "panel", id: "window", 
                          childs: {
                          caption: { tag: "div", className: "caption", id: "caption", 
                            childs: {
                            caption_table: { tag: "table", className: "sys_table", width: "100%",
                              childs: {
                              caption_tr: { tag: "tr", 
                                childs: {}
                              }} 
                            }}},
                          settings: { tag: "div", className: "settings", id: "settings" },
                          content: { tag: "div", className: "content", id: "content" }
                          }
                        }
    }
    this.init();





    this.open = function(parent, id, profile) {
        this.id = id ? id : kernel.getUniqueId();
        for(c in cfgWidget) {
            if(typeof(this.cfg[c]) == "undefined") {
                this.cfg[c] = cfgWidget[c];
            }
        }

        if(this.cfg.hasProfile) {
            profiler.addWidget(this);
            if(profile) {
                for(p in profile) {
                    this.profile[p] = profile[p];
                }
            }
        }

        this.buildWindow(parent);
        this.buildInterface();

        if(this.elements.window) {
            this.elements.window.wid = this.id;
        }
        
        kernel.addWidget(this);
        this.onOpen();
    }


    this.setParent = function(parentEl) {
        this.elements.window.parentNode.removeChild(this.elements.window);
        parentEl.appendChild(this.elements.window);
    }



    this.buildWindow = function(parent) {
        this.domModel.display = !this.cfg.isOpenHidden;
        with(this.domModel.childs.caption.childs.caption_table.childs.caption_tr) {
            if(this.cfg.hasSizeBtn) {
                childs.size_td = { tag: "td", width: 18,
                                   childs: [
                                   { tag: "a", href: "void" ,events: {onclick: "switchSize()"},
                                     childs: [
                                     {tag: "img", id: "btn_hide", src: kernel.imgCache.hide.src},
                                     {tag: "img", id: "btn_show", src: kernel.imgCache.show.src, display: false}]
                                   }] };
            }

            if(this.cfg.hasIcon) {
                childs.icon_td = { tag: "td", width: 18,
                                   childs: [{tag: "img", id: "icon", style: {width: "16px", height: "16px"}, src: "widgets/"+this.cfg.module.toLowerCase()+"/ico.gif"}] };
            }

            childs.title = { tag: "td", width: "100%", className: "caption_title", align: "left", id: "title"};

            if(this.cfg.hasRefreshBtn) {
                childs.refresh_td = { tag: "td", width: 18,
                                      childs: [ createButtonDom(false, "refresh()", /*kernel.imgCache.refresh*/kernel.imgCache.refresh.src) ] 
                                    }
                
            }

            if(this.cfg.hasSettingsBtn) {
                childs.settings_td = { tag: "td", width: 18,
                                       childs: [ createButtonDom("<b>edit</b>", "switchSettings()") ] 
                                     };
            }

            if(this.cfg.hasCloseBtn) {
                childs.close_td = { tag: "td", width: 18, align: "right",
                                      childs: [ createButtonDom(false, "close()", kernel.imgCache.close.src) ] 
                                  };
            }

        }
        this.buildDomModel(parent, this.domModel);
    }


    this.buildInterface = function() {
        if(this.cfg.hasDrag) {
            this.elements.caption.style.cursor = 'move';
	    drag.createSimpleGroup(this.elements.window, this.elements.caption/*, this.profile.trasp*/);
        }
        this.buildDomModel(this.elements.settings, domTitleSettings);

        this.setCaptionColor(this.profile.titleColor);
	this.elements.window.style.borderColor=kernel.wborder_color;
        this.onBuildInterface();
    }


    this.close = function() {
        var flag = this.cfg.hasOnCloseConfirm ? confirm('You realy want to close '+this.cfg.title+' ?') : true; 

        if(flag) {
            kernel.freeWidget(this.id);
            this.onClose();
            if(this.elements.window.parentNode) {
                this.elements.window.parentNode.removeChild(this.elements.window); 
            }
            this.elements.window.innerHTML = '';
            this.elements.window = null;
        }

        this.profile = false;
        this.save();
        desktop.savePanels();
    }




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


    this.switchSize = function() {
        if(this.isReduced) {
            this.show();
        } else {
            this.hide();
        }
    }


    this.show = function() {
        hideEl(this.elements.btn_show);
        showEl(this.elements.btn_hide);

        if(this.elements.content) {
            showEl(this.elements.content);
        }
        this.onShow();
        this.isReduced = false;
    }


    this.hide = function() {
        hideEl(this.elements.btn_hide);
        showEl(this.elements.btn_show);

        if(this.elements.content) {
            hideEl(this.elements.content);
        }

        this.hideSettings();
        this.onHide();
        this.isReduced = true;
    }



    this.switchSettings = function() {
        if(this.isSettingsReduced) {
            this.showSettings();
        } else {
            this.hideSettings();
        }
        this.isSettingsReduced = !this.isSettingsReduced;
    }


    this.showSettings = function() {
        this.onShowSettings();
        showEl(this.elements.settings);
    }


    this.hideSettings = function() {
        if(this.elements.settings) {
            hideEl(this.elements.settings);
        }
    }

    this.getSignature = function() {
        return this.id;
    }



    this.buildDomModel = function(parentEl, data) {
        if(data['tag']) {
            var el = document.createElement(data.tag);
            for (p in data) {
                switch(p) {
                    case null, "tag", "childs": break;

                    case "id":
                        this.elements[data.id] = el;
			//el.id=data.id;
                        break;                    

                    case "style":
                        for(var s in data.style) {
                            el.style[s] = data.style[s];
                        }
                        break;


                    case "options":
                        for(var i in data.options) {
                            el.options.add(new Option(data.options[i].text, data.options[i].value));
                        }
                        break;

                    case "src":
                        var src = data.src;
                        if(src.indexOf("http") == -1) {
                            src = "http://"+baseUrl+src;
                            
                        }

                        if(ie_nav) {
                            var setSrc = function() { el.src = src }
                            setTimeout(setSrc, Math.floor(Math.random()*400)+100);		
                        } else {
                            el.src = src;
                        }
			
                        break;

                    case "display":
                        if(!data[p]) {
                            el.style.display = 'none'; 
                        }
                        break;

                    case "href":
                        if(data.events && data.events.onclick) {
                            data.href = "void";
                        }
                        el.href = "javascript:"+ ((data.href == "void") ? "void(0);" : "kernel.getWidget("+this.id+")."+data.href+";");
                        break;

                    case "sysHref":
                        el.href = data.sysHref;
                        break;

                    case "events":
                        var wid = this.id;
                        if(typeof(data.events) == "object") {
                            for(var e in data.events) {
                                if(mozilla_nav) {
                                    el.setAttribute(e, "kernel.getWidget("+wid+")."+data.events[e]+";");
                                } else {
                                    el[e] = new Function("kernel.getWidget("+wid+")."+data.events[e]+";");
                                }
                            }
                        }
                        break;

                    case "sysEvents":
                        if(typeof(data.sysEvents) == "object") {
                            for(var e in data.sysEvents) {
                                el.setAttribute(e, data.sysEvents[e]+";");
                            }
                        }
                        break;

                    default:
                        el[p] = data[p];
                        break;
                }
            }

            parentEl.appendChild(el);

            if(data.tag == "table") {
                parentEl = el;
                var el = document.createElement("tbody");
                if(data.id) {
                    this.elements[data.id+"_tbody"] = el;
                } 
                parentEl.appendChild(el);
            }
            
            if(data.childs) {
                this.buildDomModel(el, data.childs);
            }

        } else {
            for(var e in data) {
                this.buildDomModel(parentEl, data[e]);
            }
        }

    }





    this.settingsSetTitle = function() {
        this.profile.title = trim(this.elements.input_title.value);
        this.setTitle(this.profile.title);
        this.save();
    }


    this.setCaptionColor = function(color) {
        this.elements.caption.style.backgroundColor = color;
    }


    this.actionSetCaptionColor = function(color) {
        this.profile.titleColor = color;
        this.setCaptionColor(color);
        this.save();
    }


    this.save = function() {
        profiler.saveProfile(this);
    }



    this.currentPopup = false;

    this.showPopup = function(btnEl, popupEl) {
        if(this.currentPopup) {
            if(this.currentPopup == popupEl) return;
            this.closePopup(this.currentPopup);
        }
        this.currentPopup = popupEl;
        var pos = {left: getLeft(this.elements[btnEl]) + 16, 
                   top:  getTop(this.elements[btnEl]) + 16};
        showEl(this.elements[popupEl]);
        setElPos(this.elements[popupEl], pos);       
    }


    this.closePopup = function(popupEl) {
        hideEl(this.elements[popupEl]);
        this.currentPopup = false;
    }


    this.selectColor = function(popupEl, target, color) {
        this.closePopup(popupEl);
        this.onSelectColor(target, color);
    }

    this.selectFont = function(popupEl, target, font) {
        this.closePopup(popupEl);
        this.onSelectFont(target, font);
    }

    this.selectFontSize = function(popupEl, target, fontSize) {
        this.closePopup(popupEl);
        this.onSelectFontSize(target, fontSize);
    }



    this.onOpen = function() {}
    this.onBuildInterface = function() {}
    this.onClose = function() {}
    this.onShow = function() {}
    this.onHide = function() {}
    this.onShowSettings = function() {}
    this.onDrag = function() {}
    this.onSelectColor = function(target, color) {}
    this.onSelectFont = function(target, font) {}
    this.onSelectFontSize = function(target, fontSize) {}

    this.onMousedown = function() { alert(kernel.dragging); if(kernel.dragging) return false;}


    this.timerHandler = function() {}
    this.refresh = function() {}
    this.dispatchMsg = function() {}
}
