
svgui.themeManager = function(){
    
    var ThemeManager = function(){
        this.themesNodeId = "themes";
    }
    
    ThemeManager.prototype.findTheme = function(themeClass){
        var themes = document.getElementById(this.themesNodeId).getElementsByTagNameNS(svgui.xmlns, "theme");
        var theme;
        for(var i = 0; i<themes.length; i++){
            theme = themes.item(i);
            if(theme.hasAttributeNS(svgui.xmlns, "class")){
                if(theme.getAttributeNS(svgui.xmlns, "class") == themeClass){
                    return theme.cloneNode(true);
                }
            }
        }
    }
    
    ThemeManager.prototype.buildTheme = function(themeObj, themeClassName){
        function parseTheme(obj, node){
            if(node.nodeType == 1){
                if(node.hasAttributeNS(svgui.xmlns, "applyName")){
                    node.removeAttributeNS(svgui.xmlns, "applyName");
                    node.setAttributeNS(svgui.xmlns, "name", obj.component.name);
                }
                if(node.hasAttributeNS(svgui.xmlns, "applyXY")){
                    node.removeAttributeNS(svgui.xmlns, "applyXY");
                    node.setAttribute("transform", "translate(" + obj.component.x + ", " + obj.component.y + ")");
                }
                if(node.hasAttributeNS(svgui.xmlns, "applyWidth")){
                    node.removeAttributeNS(svgui.xmlns, "applyWidth");
                    node.setAttribute("width", obj.component.width);
                }
                if(node.hasAttributeNS(svgui.xmlns, "applyHeight")){
                    node.removeAttributeNS(svgui.xmlns, "applyHeight");
                    node.setAttribute("height", obj.component.height);
                }
                if(node.hasAttributeNS(svgui.xmlns, "obj")){
                    var name = node.getAttributeNS(svgui.xmlns, "obj");
                    node.removeAttributeNS(svgui.xmlns, "obj");
                    obj[name] = node;
                    
                }
                
                for(var i = 0; i<node.childNodes.length; i++){
                    parseTheme(obj, node.childNodes.item(i));
                }
                
                if(node.hasAttributeNS(svgui.xmlns, "appendTo")){
                    var name = node.getAttributeNS(svgui.xmlns, "appendTo");
                    node.removeAttributeNS(svgui.xmlns, "appendTo");
                    obj[name].appendChild(node)
                }
            }
        }
        var theme = this.findTheme(themeClassName);
        parseTheme(themeObj, theme);
    }
    
    ThemeManager.prototype.applyTheme = function(obj, className){
        obj["theme"] = new svgui.themes[className+"Theme"](obj);
    }
    
    return new ThemeManager();
}();
