/*
  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
 
*/

/*
    module svg
    Experimental module for dynamically loading SVG files.
    Might be integrated into jsolait at some point.
    
    methods:
        
    exceptions:
    
*/
new Module("svg","0.0.1", function(thisMod){
    var ELEMENT_NODE = 1;
    
    var ScopeEventHandler = function(evalInScope,src){
        this.evalInScope=evalInScope;
        this.src = src;
    }
                    
    ScopeEventHandler.prototype.handleEvent=function(evt){
        this.evalInScope(this.src, evt);
    }
    var evtAttr2evtName ={"onmousedown":"mousedown",
                                     "onmouseup":"mouseup",
                                     "onmousemove":"mousemove",
                                     "onmouseover":"mouseover",
                                     "onmouseout" : "mouseout",
                                     "onclick":"click" ,
                                     "onkeypress":"keypress",
                                     "onkeydown":"keydown",
                                     "onkeyup":"keyup",
                                     "onfocusin":"focusin",
                                     "onfocusout":"focusout"};
    
    thisMod.replaceEventHandlers =  function(node, evalInScope){
        //parse attributes
        for(var i=0; i<node.attributes.length; i++){
            var currNode = node.attributes.item(i);
            var attrName = currNode.name;
            var attrValue = currNode.value;
            //replace the eventattribs with event listeners that evaluate the original attrib value inside the files script scope
            if (evtAttr2evtName[attrName]){
                if(attrValue == ""){
                }else{
                    var evtName =evtAttr2evtName[attrName];
                    node.addEventListener(evtName,new ScopeEventHandler(evalInScope, attrValue),false);
                    node.setAttribute(attrName,"");
                }
            }
        }
        
        //parse all children elements
        for(var i=0; i<node.childNodes.length; i++){
            var currNode = node.childNodes.item(i);
            if(currNode.nodeType == ELEMENT_NODE){
                thisMod.replaceEventHandlers(currNode, evalInScope);
            }
        }
    }
    
    thisMod.evalScripts=function(node){
        var src = thisMod.getScript(node);
        var evtEval = evalSrc(src);
        thisMod.replaceEventHandlers(node, evtEval);
        return evtEval;
    }
    
    thisMod.getScript=function(node){
        var parseNodes = function(node){
            var s="";
            var sn="";
            if (node.tagName=="script"){
                for(var i=0; i<node.childNodes.length; i++){
                    switch (node.childNodes.item(i).nodeType){
                        case 3:
                        case 4:
                            sn=node.childNodes.item(i).nodeValue;
                            s=s+"\n"+sn;
                    }
                }
            }
            for(var i=0; i<node.childNodes.length; i++){
                sn = parseNodes(node.childNodes.item(i));
                s=s+"\n"+sn;
            }
            
            return s;
        }
        var s=parseNodes(node);
        s=s.replace(/\s*$/,""); 
        s=s.replace(/^\s*/,"");
        return s;
    }    

    thisMod.importSVG = function(svgSrc, parent){
        var xml=importModule("xml");
        
        var d =  xml.parseXML(svgSrc);
        var de = xml.importNode(d.documentElement);
        parent.appendChild(de);
        var evtEval = thisMod.evalScripts(de);
        var src = de.getAttribute("onload");
        if(src != ""){
            evtEval(src, {"documentElement":de});
        }
        return de;
    }
    
    thisMod.importSVGFile = function(url, parentNode, callback){
        function CallbackHandler(){
        }
        CallbackHandler.prototype.operationComplete = function (status){
            var e = thisMod.importSVG(status.content, parentNode);
            callback(e);
        }
        getURL(url, new CallbackHandler());
    }
    
})



var evalSrc=function(_s_){
    eval(_s_)
    var f = function(_sr_, evt){
        eval(_sr_)
    };
    return f;
}


