var xpress;
var ypress;
var zoomLevel=0;
var dragOn=0;
var mainview;
var prevLayer="none";
var svgdoc;
var action='info';
var pageLanguage="french";
var serviceURL="http://enhanced.aui.ma/axis/OTIDWebServices.jws?wsdl";

function mouseClickedEvent(evt){
    if(action=="in")
        zoomin(evt.clientX,evt.clientY);
    else if(action=="out")
        zoomout(evt.clientX,evt.clientY);
    else if(action=="info")
        itemSelected(evt);
}

function zoomin(ex,ey)
{
    if(parseInt(zoomLevel)<=10)
        {
            view = svgdoc.getElementById("map").getAttributeNS(null,"viewBox");
            
            var tokens = view.tokenize(" "," ", true);
            
            x = tokens[0];
            y = tokens[1];
            w = tokens[2];
            h = tokens[3];
            
            tw=w;
            th=h;
            
            w=w/1.5;
            h=h/1.5;
            
            dx=(ex-0)*(tw/400);
            dy=(ey-0)*(th/390);
            
            tp1x=(dx-0)-(dx*(w/tw));
            tp1y=(dy-0)-(dy*(h/th));
            
            x=(x-0)+(tp1x-0);
            y=(y-0)+(tp1y-0);
            
            view = x+" "+y+" "+w+" "+h;
            
            svgdoc.getElementById("map").setAttributeNS(null,"viewBox",view);
            zoomLevel=parseInt(zoomLevel)+1;
            if(zoomLevel<=2)
                {
                    svgdoc.getElementById("com1").setAttributeNS(null,"visibility","visible");
                }
                else
                    {
                        svgdoc.getElementById("com1").setAttributeNS(null,"visibility","hidden");
                    }
                    if(zoomLevel>=5)
                        {
                            svgdoc.getElementById("text").setAttributeNS(null,"visibility","visible");
                        }
                        else
                            {
                                svgdoc.getElementById("text").setAttributeNS(null,"visibility","hidden");
                            }
                        }
                    }
                    
                    function zoomout(ex,ey)
                    {
                        if(parseInt(zoomLevel)>0)
                            {
                                
                                view = svgdoc.getElementById("map").getAttributeNS(null,"viewBox");
                                
                                var tokens = view.tokenize(" "," ", true);
                                
                                x = tokens[0];
                                y = tokens[1];
                                w = tokens[2];
                                h = tokens[3];
                                
                                tw=w;
                                th=h;
                                
                                w=w*1.5;
                                h=h*1.5;
                                
                                dx=(ex-0)*(tw/400);
                                dy=(ey-0)*(th/390);
                                
                                tp1x=(dx-0)-(dx*(w/tw));
                                tp1y=(dy-0)-(dy*(h/th));
                                
                                x=(x-0)+(tp1x-0);
                                y=(y-0)+(tp1y-0);
                                
                                view = x+" "+y+" "+w+" "+h;
                                
                                svgdoc.getElementById("map").setAttributeNS(null,"viewBox",view);
                                zoomLevel=parseInt(zoomLevel)-1;
                                if(zoomLevel>2)
                                    {
                                        svgdoc.getElementById("com1").setAttributeNS(null,"visibility","hidden");
                                    }
                                    
                                    else
                                        {
                                            svgdoc.getElementById("com1").setAttributeNS(null,"visibility","visibile");
                                        }
                                        if(zoomLevel<5)
                                            {
                                                svgdoc.getElementById("text").setAttributeNS(null,"visibility","hidden");
                                            }
                                            
                                            else
                                                {
                                                    svgdoc.getElementById("text").setAttributeNS(null,"visibility","visibile");
                                                }
                                            }
                                        }
                                        
                                        function pan(k1,k2)
                                        {
                                            view = svgdoc.getElementById("map").getAttributeNS(null,"viewBox");
                                            
                                            var tokens = view.tokenize(" "," ", true);
                                            
                                            x = tokens[0];
                                            y = tokens[1];
                                            w = tokens[2];
                                            h = tokens[3];
                                            
                                            tk1=k1*(w/400);	
                                            tk2=k2*(h/390);
                                            
                                            tpx=(x-0)+tk1;
                                            tpy=(y-0)+tk2;
                                            
                                            x=tpx;
                                            y=tpy;
                                            
                                            view = x+" "+y+" "+w+" "+h;
                                            
                                            svgdoc.getElementById("map").setAttributeNS(null,"viewBox",view);
                                        }
                                        
                                        function reset()
                                        {
                                            svgdoc.getElementById("map").setAttributeNS(null,"viewBox",mainview);
                                            zoomLevel=1;
                                            svgdoc.getElementById("com1").setAttributeNS(null,"visibility","visibile");
                                            svgdoc.getElementById("text").setAttributeNS(null,"visibility","visibile");
                                        }
                                        
                                        function init()
                                        {
                                            
                                            
                                            try {
                                                var embed = document.getElementById('embed');
                                                pageLanguage=embed.name;
                                                svgdoc = embed.getSVGDocument();
                                            }
                                            catch(exception) {
                                                alert(exception.message);
                                            }
                                            
                                            mainview=svgdoc.getElementById("map").getAttributeNS(null,"viewBox");
                                            mapRoot = svgdoc.getElementById("map");
                                            mapRoot.addEventListener('click',mouseClickedEvent,false);
                                            mapRoot.addEventListener('mousedown',pressed,false);
                                            mapRoot.addEventListener('mouseup',released,false);
                                            mapRoot.addEventListener('mousemove',dragged,false);
                                            
                                        }
                                        
                                        function ajoutLayer(item)
                                        {
                                            try{
                                                
                                                if(prevLayer=="none")
                                                    {
                                                        svgdoc.getElementById(item).setAttributeNS(null,"visibility","visible");
                                                        prevLayer=item;
                                                    }
                                                    
                                                    else if(prevLayer==item)
                                                        {
                                                            if(svgdoc.getElementById(item).getAttributeNS(null,"visibility")=="visible")
                                                                svgdoc.getElementById(item).setAttributeNS(null,"visibility","hidden");
                                                            else
                                                                svgdoc.getElementById(item).setAttributeNS(null,"visibility","visible");	
                                                        }
                                                        
                                                        else
                                                            {
                                                                svgdoc.getElementById(prevLayer).setAttributeNS(null,"visibility","hidden");
                                                                svgdoc.getElementById(item).setAttributeNS(null,"visibility","visible");	
                                                                prevLayer=item;
                                                            }
                                                        }catch(oException){alert(oException.message);}
                                                        
                                                    }
                                                    
                                                    function itemSelected(evt)
                                                    {
                                                        if(action=="info")
                                                            {	
                                                                try{
                                                                    webserv.useService(serviceURL,"getNameServ");
                                                                    var iCallID;
                                                                    if (webserv.getNameServ)
                                                                        {
                                                                            iCallID= webserv.getNameServ.callService(resMethod,"getSiteInfo",evt.getTarget().getAttributeNS(null,"id"),pageLanguage,"fes");
                                                                        }
                                                                    }catch(e){alert(e.message)}
                                                                }
                                                            }       
                                                            function resMethod(result)
                                                            {
                                                                //if an error then get error details
                                                                if(result.error)
                                                                    {
                                                                        //Pull error info from event.result.errorDetail properties
                                                                        var xfaultcode   = result.errorDetail.code;
                                                                        var xfaultstring = result.errorDetail.string;
                                                                        var xfaultsoap   = result.errorDetail.raw;
                                                                        
                                                                        alert("code "+xfaultcode+"\nstring "+xfaultstring+"\nsoap fault "+xfaultsoap);
                                                                    }
                                                                    else
                                                                        {
                                                                            document.getElementById("infoArea").value=result.value;
                                                                        }
                                                                    }
                                                                    
                                                                    function selectedButChanged(type)
                                                                    {
                                                                        action=type;
                                                                    }
                                                                    
                                                                    function dragged(evt)
                                                                    {
                                                                        if(action=="pan" && dragOn!=0)
                                                                            {
                                                                                k1 = xpress-evt.clientX;
                                                                                k2 = ypress-evt.clientY;
                                                                                pan(k1,k2);
                                                                                xpress=evt.clientX;
                                                                                ypress=evt.clientY;	
                                                                            }
                                                                        }
                                                                        
                                                                        function pressed(evt)
                                                                        {
                                                                            if(action=="pan")
                                                                                {
                                                                                    
                                                                                    xpress = evt.clientX;
                                                                                    ypress = evt.clientY;
                                                                                    dragOn=1;
                                                                                    
                                                                                }
                                                                            }
                                                                            
                                                                            function released(evt)
                                                                            {
                                                                                dragOn=0;
                                                                            }
                                                                            
                                                                            String.prototype.tokenize = tokenize;
                                                                            
                                                                            function tokenize()
                                                                            {
                                                                                var input             = "";
                                                                                var separator         = " ";
                                                                                var trim              = "";
                                                                                var ignoreEmptyTokens = true;
                                                                                
                                                                                try {
                                                                                    String(this.toLowerCase());
                                                                                }
                                                                                catch(e) {
                                                                                    window.alert("Tokenizer Usage: string myTokens[] = myString.tokenize(string separator, string trim, boolean ignoreEmptyTokens);");
                                                                                    return;
                                                                                }
                                                                                
                                                                                if(typeof(this) != "undefined")
                                                                                    {
                                                                                        input = String(this);
                                                                                    }
                                                                                    
                                                                                    if(typeof(tokenize.arguments[0]) != "undefined")
                                                                                        {
                                                                                            separator = String(tokenize.arguments[0]);
                                                                                        }
                                                                                        
                                                                                        if(typeof(tokenize.arguments[1]) != "undefined")
                                                                                            {
                                                                                                trim = String(tokenize.arguments[1]);
                                                                                            }
                                                                                            
                                                                                            if(typeof(tokenize.arguments[2]) != "undefined")
                                                                                                {
                                                                                                    if(!tokenize.arguments[2])
                                                                                                        ignoreEmptyTokens = false;
                                                                                                }
                                                                                                
                                                                                                var array = input.split(separator);
                                                                                                
                                                                                                if(trim)
                                                                                                    for(var i=0; i<array.length; i++)
                                                                                                        {
                                                                                                            while(array[i].slice(0, trim.length) == trim)
                                                                                                                array[i] = array[i].slice(trim.length);
                                                                                                            while(array[i].slice(array[i].length-trim.length) == trim)
                                                                                                                array[i] = array[i].slice(0, array[i].length-trim.length);
                                                                                                        }
                                                                                                        
                                                                                                        var token = new Array();
                                                                                                        if(ignoreEmptyTokens)
                                                                                                            {
                                                                                                                for(var i=0; i<array.length; i++)
                                                                                                                    if(array[i] != "")
                                                                                                                        token.push(array[i]);
                                                                                                                }
                                                                                                                else
                                                                                                                    {
                                                                                                                        token = array;
                                                                                                                    }
                                                                                                                    
                                                                                                                    return token;
                                                                                                                }
                                                                                                                
                                                                                                                
