/*
  copyright (c) 2003 Jan-Klaas Kollhof
 
  This is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
 
  This software is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this software; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
*/

svgui.themes = function(){

    //----------------------------------------------GenericTheme-------------------------------------------
    var GenericTheme = function(){
        var Super = Object;
        var GenericTheme = newClass(Super);

        GenericTheme.prototype.init = function(component){
            this.component = component;
            this.buildTheme();
            this.component.addEventListener("positionChanged", this);
            this.component.addEventListener("childAdded", this);
            this.component.addEventListener("broughtToFront", this);
            
        }
        
        GenericTheme.prototype.buildTheme = function(){
            svgui.themeManager.buildTheme(this, "GenericTheme");
        }
        
        GenericTheme.prototype.handleEvent = function(evt){
            if(this[evt.type]){
                this[evt.type](evt);
            }
        }
        GenericTheme.prototype.broughtToFront = function(evt){
            if (this.rootNode.parentNode){
                var pn = this.rootNode.parentNode;
                pn.removeChild(this.rootNode);
                pn.appendChild(this.rootNode);
            }
        }
        
        GenericTheme.prototype.positionChanged = function(evt){
            this.rootNode.setAttribute("transform", "translate(" + this.component.x + ", " + this.component.y + ")");
        }
        
        GenericTheme.prototype.childAdded = function(evt){
            this.contentNode.appendChild(evt.child.theme.rootNode);
        }
    
        return GenericTheme;
    }();
    
    
    
    
    //-----------------------------------------------FrameTheme------------------------------------------------------------
    var FrameTheme = function(){
        var Super = GenericTheme;
        var FrameTheme = newClass(Super);
        
        FrameTheme.prototype.init = function(component){
            Super.prototype.init.call(this,component);
            this.component.addEventListener("focusChanged", this);
        }
        
        FrameTheme.prototype.buildTheme = function(){
            Super.prototype.buildTheme.call(this)
            svgui.themeManager.buildTheme(this, "FrameTheme");
        }
        
        FrameTheme.prototype.focusChanged = function(evt){
            switch (this.component.focusState){
                case 0:
                    this.frame.setAttribute("stroke", "black");
                    break;
                case 1:
                    this.frame.setAttribute("stroke", "darkblue");
                    break;
                case 2:
                    this.frame.setAttribute("stroke", "blue");
                    break;
            }
        }
       
        return FrameTheme;
    }();
    
    
    //----------------------------------------------------Button---------------------------------------
    var ButtonTheme = function(){
        var Super = FrameTheme;
        var ButtonTheme = newClass(Super);

        ButtonTheme.prototype.init = function(component){
            Super.prototype.init.call(this, component);
            this.component.addEventListener("activate", this);
        }
            
        ButtonTheme.prototype.activate = function(evt){
            if(evt.active){
                this.frame.setAttribute("fill", "lightsteelblue");
            }else{
                this.frame.setAttribute("fill", "white");
            }
        }
        
        return ButtonTheme;
    }();
    
    
    //----------------------------------------------------TextBoxTheme-------------------------------------------------------
    var TextBoxTheme = function(){
        var Super = FrameTheme;
        var TextBoxTheme = newClass(Super);
        
        TextBoxTheme.prototype.init = function(component){
            Super.prototype.init.call(this,component);
            this.component.addEventListener("textChanged", this);
            this.component.addEventListener("selectionChanged", this);
        }
        TextBoxTheme.prototype.buildTheme = function(){
            Super.prototype.buildTheme.call(this)
            svgui.themeManager.buildTheme(this, "TextBoxTheme");
        }
        
        TextBoxTheme.prototype.textChanged = function(){
            this.redraw();
        }
        
        TextBoxTheme.prototype.selectionChanged = function(){
            this.redraw();
        }
        
        TextBoxTheme.prototype.redraw =  function(){
            var txn = this.textNode;
            while(txn.hasChildNodes()){
                txn.removeChild(txn.firstChild);
            }
            var txPreSel = this.component.getPreSelection()
            var txSel = this.component.getSelection();
            var txPostSel = this.component.getPostSelection();
            var carretPos = 0;
            
            if(txPreSel != ""){
                var tsp = document.createElement("tspan");
                tsp.appendChild(document.createTextNode(txPreSel));
                this.textNode.appendChild(tsp);
                if (this.component.selLength<=0){
                    carretPos = tsp.getEndPositionOfChar(txPreSel.length-1).x;
                }
            }
            if(txSel != ""){
                var tsp = document.createElement("tspan");
                tsp.setAttribute("fill", "blue");
                tsp.appendChild(document.createTextNode(txSel));
                this.textNode.appendChild(tsp);
                if(this.component.selLength>0){
                    carretPos = tsp.getEndPositionOfChar(txSel.length-1).x;
                }
            }
            if(txPostSel != ""){
                var tsp = document.createElement("tspan");
                tsp.appendChild(document.createTextNode(txPostSel));
                this.textNode.appendChild(tsp);
            }
            this.carretNode.setAttribute("x", carretPos)
        }
        
        TextBoxTheme.prototype.focusChanged = function(){
            if(this.component.focusState == 2){
                this.carretNode.setAttribute("fill", "black");
            }else{
                this.carretNode.setAttribute("fill", "none");
            }
            Super.prototype.focusChanged.call(this);
        }
        
        return TextBoxTheme;
    }();
    
    
    return {FrameTheme:FrameTheme, ButtonTheme: ButtonTheme, TextBoxTheme:TextBoxTheme, GenericTheme:GenericTheme};
}();



