// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.5/standard_upgrade_01/wwwroot/js/standard/cbdraw.js $ 
// $Date: 5-08-09 12:54 $
// $Revision: 17 $ 
// $Author: Kpo $
// =============================================================== 


function CBdraw(type,mapbox,id, doTmpDraw, color, size)
{
    this.id = id || -1;
    this.points = new Array();
    this.nPoints = 0;
    this.wpoints = new Array();
    this.type = type.toLowerCase();
    this.simpletmp = false
    //this.size = '2';
    this.size = (size=="" || !size ? '2' : size);
    this.color = (color=="" || !color ? '#FF0000' : color);
    this.angle = 0;
    this.refPoint = {x:0,y:0};

    this.setMapBox(mapbox);
    this.graphics = new jsGraphics(mapbox);
    this.graphics.setColor(this.color);
    this.graphics.setStroke(this.size);

    this.doTmpDraw = doTmpDraw;
    if (this.doTmpDraw == true)
    {
        this.tmpColor = this.color;
        this.tmpName = mapbox + "__cbDrawTmpDiv";
        tmpbox = getElement(this.tmpName);
        
        if (tmpbox == null){
            tmpbox = getElement(mapbox).cloneNode(true);
            tmpbox.setAttribute("id", this.tmpName);
            getElement(mapbox).parentNode.appendChild(tmpbox);
        }
    
        this.setMapBox(this.tmpName);
        this.tmpgraphics = new jsGraphics(this.tmpName);
        this.tmpgraphics.setColor(this.tmpColor);
        this.graphics.setStroke(1);
        this.isTmpDrawing = false;
    }
}
CBdraw.prototype.setMapBox = function(mapbox)
{
    var e = getElement(mapbox);
    if(!e)
    {
        e = document.createElement('div');
        e.id = mapbox;
        e.style.position = 'absolute';
        e.style.zIndex = '2';
        document.body.appendChild(e);
    }
    e.style.left = cbKort.mapX+'px';
    e.style.top  = cbKort.mapY+'px';
    if(ie)
    {
        e.style.width = cbKort.mapWidth +'px';
        e.style.height = cbKort.mapHeight +'px';
        e.style.overflow = 'hidden';
    }
    else
    {   e.style.clip = 'rect(0px, '+ cbKort.mapWidth +'px, '+ cbKort.mapHeight +'px, 0px)';
    }
    return;
}
CBdraw.prototype.getID = function()
{
    return this.id;
}
CBdraw.prototype.setType = function(type)
{
    this.type = type;
}
CBdraw.prototype.setSize = function(size)
{
    this.size = parseInt(size);
    this.graphics.setStroke(this.size);
}
CBdraw.prototype.setColor = function(color,tmpColor)
{
    this.color = color;
    this.tmpColor = this.color;
    if(tmpColor)
        this.tmpColor = tmpColor;
    this.graphics.setColor(this.color);
    if (this.doTmpDraw == true)
        this.tmpgraphics.setColor(this.tmpColor);
}
CBdraw.prototype.setAngle = function(angleDegree)
{
    angleDegree = angleDegree % 360;
    this.angle = Math.PI * angleDegree / 180;
}
CBdraw.prototype.setRefPoint = function(p)
{
    this.refPoint = p;
}

CBdraw.prototype.closePolygon = function()
{
    if(this.type == 'polygon')
    {
        if(this.nPoints>2)
        {
            this.graphics.drawLine(this.points[this.nPoints-1].x,this.points[this.nPoints-1].y,this.points[0].x,this.points[0].y);
            this.graphics.paint();
        }
    }
}

CBdraw.prototype.setTempPoint = function(p, lastPoint)
{
    var dx = p.x;
    var dy = p.y;
    if (dx == -1 && dy == -1)
    {
        this.tmpgraphics.clear();
        if(this.type == 'polygon' && this.nPoints > 1)
        {
            var lastX = this.points[this.nPoints-1].x;
            var lastY = this.points[this.nPoints-1].y;
            var firstX = this.points[0].x;
            var firstY = this.points[0].y;
            
                    
            this.tmpgraphics.drawLine(lastX,lastY, firstX, firstY);
            this.tmpgraphics.paint();
        }
    }
    else if (this.doTmpDraw == true && this.isTmpDrawing == false && this.nPoints > 0)
    {   
        this.isTmpDrawing = true;
        this.tmpgraphics.clear();
        var tmpX = dx;
        var tmpY = dy;
        var lastX = this.points[this.nPoints-1].x;
        var lastY = this.points[this.nPoints-1].y;
        if(lastPoint)
        {
            var lastX = lastPoint.x;
            var lastY = lastPoint.y;
        }
        
        var firstX = this.points[0].x;
        var firstY = this.points[0].y;
        //Adjust tmpPoint so that you don't draw directly under cursor.
        if (tmpX > lastX)
            tmpX = tmpX - 1;
        else
            tmpX = tmpX + 1;
        
        if (tmpY > lastY)
            tmpY = tmpY - 1;
        else
            tmpY = tmpY + 1;
        
        
      
        if(this.type == 'polygon')
        {
            if (this.nPoints == 1)
            {
                this.tmpgraphics.drawLine(lastX,lastY, tmpX, tmpY);
                this.tmpgraphics.paint();
            }
            else if (this.nPoints > 1)
            {
                this.tmpgraphics.drawLine(lastX,lastY, tmpX, tmpY);
                this.tmpgraphics.drawLine(firstX,firstY, tmpX, tmpY);
                this.tmpgraphics.paint();
            }
        }
        else if(this.type == 'line')
        {
            if (this.nPoints > 0)
            {
                this.tmpgraphics.drawLine(lastX,lastY, tmpX, tmpY);
                this.tmpgraphics.paint();
            }
        }
        else if (this.type == 'rectangle')
        {
            var pointarray = new Array();
            pointarray[0] = {x:lastX, y:lastY};
            pointarray[1] = {x:tmpX, y:tmpY};
            if (this.nPoints == 1)
                this.drawRectangle(pointarray,this.tmpgraphics)
            else if (this.nPoints == 2)
            {
                if(!this.simpletmp)
                    this.drawRectangleCorners(pointarray,this.tmpgraphics)
                else
                    this.drawRectangle(pointarray,this.tmpgraphics)
            }
        }
        else if(this.type == 'circle')
        {
            var pointarray = new Array();
            pointarray[0] = {x:lastX, y:lastY};
            pointarray[1] = {x:tmpX, y:tmpY};
            if(this.nPoints == 1)
                this.drawCircle(pointarray,this.tmpgraphics)
        }
        this.isTmpDrawing = false;
    }
}

CBdraw.prototype.drawTmp = function(pointarray)
{
    var dx = pointarray[0].x;
    var dy = pointarray[0].y;
    if (dx == -1 && dy == -1)
    {
        this.tmpgraphics.clear();
        if(this.type == 'polygon' && this.nPoints > 1)
        {
            this.tmpgraphics.drawLine(this.points[this.nPoints-1].x,this.points[this.nPoints-1].y, this.points[0].x, this.points[0].y);
            this.tmpgraphics.paint();
        }
    }
    else if (this.doTmpDraw == true && this.isTmpDrawing == false && pointarray.length > 1)
    {
        this.isTmpDrawing = true;
        this.tmpgraphics.clear();
        //Adjust tmpPoint so that you don't draw directly under cursor.
/*
        if (tmpX > lastX) tmp--;
        else tmp++;
        if (tmpY > lastY) tmpY--;
        else tmpY++;
*/        
        if(this.type == 'polygon' || this.type == 'line')
        {
            if(pointarray.length > 1)
            {
                for(var i = 1;i<pointarray.length;i++)
                    this.tmpgraphics.drawLine(pointarray[i-1].x,pointarray[i-1].y, pointarray[i].x, pointarray[i].x);
                this.tmpgraphics.paint();
            }
        }
        else if (this.type == 'rectangle')
        {
            if(pointarray.length == 2)
                this.drawRectangle(pointarray,this.tmpgraphics)
        }
        else if(this.type == 'circle')
        {
            if(pointarray.length == 2)
                this.drawCircle(pointarray,this.tmpgraphics);
        }
        this.isTmpDrawing = false;
    }

}
CBdraw.prototype.drawCircle = function(pointarray,graphics)
{
    if(!graphics)
        graphics = this.graphics;
    var r = Math.round(Math.sqrt(Math.pow(pointarray[0].x-pointarray[1].x,2) + Math.pow(pointarray[0].y-pointarray[1].y,2)));
    var d = 2 * r;
    graphics.clear();
    graphics.drawEllipse(pointarray[0].x - r, pointarray[0].y - r, d, d);
    graphics.paint();
}
CBdraw.prototype.drawRectangle = function(pointarray,graphics)
{
    if(!graphics)
        graphics = this.graphics;
    var apointsarray = this.getAdditionalRectanglePoints({x:pointarray[0].x, y:pointarray[0].y}, {x:pointarray[1].x, y:pointarray[1].y});
    var pa = apointsarray[0];
    var pb = apointsarray[1];
    graphics.drawLine(parseInt(pointarray[0].x), parseInt(pointarray[0].y), parseInt(pa.x), parseInt(pa.y));
    graphics.drawLine(parseInt(pa.x), parseInt(pa.y), parseInt(pointarray[1].x), parseInt(pointarray[1].y));
    graphics.drawLine(parseInt(pointarray[1].x), parseInt(pointarray[1].y), parseInt(pb.x), parseInt(pb.y));
    graphics.drawLine(parseInt(pb.x), parseInt(pb.y), parseInt(pointarray[0].x), parseInt(pointarray[0].y));
    graphics.paint();
}
CBdraw.prototype.drawRectangleCorners = function(pointarray,graphics)
{
    if(!graphics)
        graphics = this.graphics;
    var apointsarray = this.getAdditionalRectanglePoints({x:pointarray[0].x, y:pointarray[0].y}, {x:pointarray[1].x, y:pointarray[1].y});
    var pa = apointsarray[0];
    var pb = apointsarray[1];
    var offset = 20; 
    var oldstroke = graphics.stroke;
    graphics.setStroke(2);
    
    var a;
    a = this.getAdditionalPoint(pointarray[0],pa,offset);
    graphics.drawLine(pointarray[0].x, pointarray[0].y, a.x, a.y);
    a = this.getAdditionalPoint(pointarray[0],pb,offset);
    graphics.drawLine(pointarray[0].x, pointarray[0].y, a.x, a.y);
    
    a = this.getAdditionalPoint(pointarray[1],pa,offset);
    graphics.drawLine(pointarray[1].x, pointarray[1].y, a.x, a.y);
    a = this.getAdditionalPoint(pointarray[1],pb,offset);
    graphics.drawLine(pointarray[1].x, pointarray[1].y, a.x, a.y);

    a = this.getAdditionalPoint(pa,pointarray[0],offset);
    graphics.drawLine(pa.x, pa.y, a.x, a.y);
    a = this.getAdditionalPoint(pa,pointarray[1],offset);
    graphics.drawLine(pa.x, pa.y, a.x, a.y);

    a = this.getAdditionalPoint(pb,pointarray[0],offset);
    graphics.drawLine(pb.x, pb.y, a.x, a.y);
    a = this.getAdditionalPoint(pb,pointarray[1],offset);
    graphics.drawLine(pb.x, pb.y, a.x, a.y);

    graphics.paint();
    graphics.setStroke(oldstroke);
}
CBdraw.prototype.getAdditionalPoint = function(startpoint,endpoint,offset)
{
    if(!offset)
        return;
    var rx = endpoint.x-startpoint.x;
    var ry = endpoint.y-startpoint.y;
    var fo = Math.sqrt((rx*rx)+(ry*ry))/offset;
    if(fo>1)
        return {x:startpoint.x+(rx/fo),y:startpoint.y+(ry/fo)};
    else
        return endpoint;
}


CBdraw.prototype.addPoint = function(dx,dy)
{
    if (this.type == 'point' || this.type == 'label')
        this.reset();
    else if (this.type == 'rectangle' && this.nPoints == 2)
        this.reset();
    else if (this.type == 'circle' && this.nPoints == 2)
        this.reset();

    this.points[this.nPoints] = {x:dx, y:dy};
    
    this.wpoints[this.nPoints] = this.getWPoint(this.points[this.nPoints]);
    this.nPoints++;
}

// Multigeometry not supported!
CBdraw.prototype.addPointsFromWKT = function(wkt)
{
    var wkts = wkt.split('(');
    wkts[0] = wkts[0].toUpperCase();
    
    if(wkts[0] == 'POINT')
    {
        var p = wkts[1].split(')')[0];
        var pd = p.split(' ');
        pd = this.getDPoint({x:pd[0],y:pd[1]});
        this.addPoint(pd.x,pd.y);
        this.draw();
    }
    else if(wkts[0] == 'LINESTRING')
    {
        var p = wkts[1].split(')')[0];
        p = p.split(',');
        for(var i=0;i<p.length;i++)
        {
            var pd = p[i].split(' ');
            if(pd.length < 2)
                pd = p[i].split('+');
            pd = this.getDPoint({x:pd[0],y:pd[1]});
            this.addPoint(pd.x,pd.y);
            this.draw();
        }        
    }
    else if(wkts[0] == 'POLYGON')
    {
        var p = wkts[2].split(')')[0];
        p = p.split(',');
        for(var i=0;i<p.length;i++)
        {
            var pd = p[i].split(' ');
            if(pd.length < 2)
                pd = p[i].split('+');
            pd = this.getDPoint({x:pd[0],y:pd[1]});
            this.addPoint(pd.x,pd.y);
            this.draw();
        }        
    }
    else
        alert(cbInfo.getString('standard.message.draw.no_points_added',wkt));
}

CBdraw.prototype.getDPoint = function(wpoint)
{
    var p = cbKortUdstyr.getPixelCoords(wpoint.x,wpoint.y);
    return p;
}

CBdraw.prototype.getWPoint = function(dpoint)
{
    return cbKortUdstyr.getMapCoords(dpoint.x,dpoint.y);
}

CBdraw.prototype.removeLastPoint = function()
{
    if(this.nPoints>0)
    {
        this.nPoints--;
        this.points.length = this.nPoints;
    }
}

CBdraw.prototype.getLastPoint = function()
{
    return this.points[this.nPoints-1];
}

CBdraw.prototype.draw = function()
{
    if (this.doTmpDraw == true)
        this.tmpgraphics.clear();
        
    if(this.type == 'polygon' || this.type == 'line')
    {
        if(this.nPoints>1)
        {
            if(this.nPoints==2)
                this.graphics.clear();
            this.graphics.drawLine(parseInt(this.points[this.nPoints-2].x),parseInt(this.points[this.nPoints-2].y),parseInt(this.points[this.nPoints-1].x),parseInt(this.points[this.nPoints-1].y));
            this.graphics.paint();
        }
        else if(this.nPoints==1)
        {
            var x = this.points[0].x - this.size/2 + 1;
            var y = this.points[0].y - this.size/2 + 1;
            this.graphics.fillEllipse(parseInt(x),parseInt(y),this.size,this.size);
            this.graphics.paint();
        }
    }
    else if(this.type == 'point' || this.type == 'label')
    {
        if(this.nPoints>0)
        {
            var x = this.points[0].x - this.size/2 + 1;
            var y = this.points[0].y - this.size/2 + 1;
            this.graphics.clear();
            this.graphics.fillEllipse(parseInt(x),parseInt(y),this.size,this.size);
            this.graphics.paint();
        }
    }
    else if(this.type == 'rectangle')
    {
        if(this.nPoints == 2)
            this.drawRectangle(this.points)
    }
    else if(this.type == 'circle')
    {
        if(this.nPoints == 2)
        {
            var x = this.points[0].x;
            var y = this.points[0].y;
            var x2 = this.points[1].x;
            var y2 = this.points[1].y;
            
            var r = Math.round(Math.sqrt(Math.pow(x-x2,2) + Math.pow(y-y2,2)));
            var d = 2* r;
 
            this.graphics.clear();
            this.graphics.drawEllipse(parseInt(x) - r , parseInt(y) - r, d, d);
            this.graphics.paint();
        }
    }
}

CBdraw.prototype.getRadius = function(){
    var r = 0;
    if(this.type == 'circle')
    {
        if(this.nPoints == 2)
        {
            var x = this.wpoints[0].x;
            var y = this.wpoints[0].y;
            var x2 = this.wpoints[1].x;
            var y2 = this.wpoints[1].y;
            
            r = Math.round(Math.sqrt(Math.pow(x-x2,2) + Math.pow(y-y2,2)));
        }
    }
    return r;
}


CBdraw.prototype.drawAll = function(fromWC)
{
    if (this.nPoints>0)
    {
        this.graphics.clear();
        if(fromWC)
        {
            for(var i=0;i<this.nPoints;i++)
            {
                var p = cbKortUdstyr.getPixelCoords(this.wpoints[i].x,this.wpoints[i].y);
                if(ie)
                {
                    p.x-=2;
                    p.y-=2;
                }
                this.points[i].x = p.x;
                this.points[i].y = p.y;
            }
        }
    }

    if (this.type == 'polygon' || this.type == 'line')
    {
        if (this.nPoints>1)
        {
            for (var i=0;i<this.nPoints-1;i++)
            {
                this.graphics.drawLine(this.points[i].x,this.points[i].y,this.points[i+1].x,this.points[i+1].y);
            }
            this.graphics.paint();
        }
    }
    else if (this.type == 'point' || this.type == 'label')
    {
        if( this.nPoints>0)
        {
            this.graphics.fillEllipse(this.points[0].x,this.points[0].y,2,2);
            this.graphics.paint();
        }
    }
}

CBdraw.prototype.reset = function()
{
    this.clear();
    this.nPoints = 0;
}

CBdraw.prototype.clear = function()
{
    if (this.doTmpDraw == true)
    {
        this.tmpgraphics.clear();
        this.isTmpDrawing = false;
    }
    this.graphics.clear();
}

CBdraw.prototype.getPointsAsString = function()
{
    var pointstring = "";
    if (this.type == 'polygon')
    {
        if(this.nPoints>2)
        {
            for (var i=0;i<this.nPoints;i++) 
                pointstring+=this.wpoints[i].x+" "+this.wpoints[i].y+",";

            pointstring+=this.wpoints[0].x+" "+this.wpoints[0].y;
        }
        else
        {
            alert(cbInfo.getString('standard.message.draw.three_points_minimum'));
            return false;
        }
    }
    else if(this.type == 'line')
    {
        if(this.nPoints>1)
        {
            for (var i=0;i<this.nPoints-1;i++)
                pointstring+=this.wpoints[i].x+" "+this.wpoints[i].y+",";

            pointstring+=this.wpoints[this.nPoints-1].x+" "+this.wpoints[this.nPoints-1].y;
        }
        else
        {
            alert(cbInfo.getString('standard.message.draw.two_points_minimum'));
            return false;
        }
    }
    else if (this.type == 'point' || this.type == 'label' ||  this.type == 'circle')
    {
        if (this.nPoints>0)
            pointstring+=this.wpoints[0].x+" "+this.wpoints[0].y;
        else
        {
            alert(cbInfo.getString('standard.message.draw.one_point_minimum'));
            return false;
        }
    }
    else if (this.type == 'rectangle')
    {
        if (this.nPoints == 2)
        {  
            var apointsarray = this.getAdditionalRectanglePoints(this.points[0],this.points[1]);
            var paw = this.getWPoint(apointsarray[0]);
            var pbw = this.getWPoint(apointsarray[1]);

            pointstring+= this.wpoints[0].x+" "+this.wpoints[0].y +",";
            pointstring+= paw.x+" "+paw.y +",";
            pointstring+= this.wpoints[1].x+" "+this.wpoints[1].y+",";
            pointstring+= pbw.x+" "+pbw.y +",";
            pointstring+= this.wpoints[0].x+" "+this.wpoints[0].y;
        }
        else
        {
            alert(cbInfo.getString('standard.message.draw.two_points'));
            return false;
        }
    }
    return pointstring;
}

CBdraw.prototype.getWKT = function()
{
    var points = this.getPointsAsString();
    if(!points)
        return false;
    if (this.type == 'polygon' || this.type == 'rectangle')
        return 'POLYGON((' + points + '))';  
    else if (this.type == 'line')
        return 'LINESTRING(' + points + ')';  
    else if (this.type == 'point' || this.type == 'label' ||  this.type == 'circle')
        return 'POINT(' + points + ')';
}

CBdraw.prototype.getAdditionalRectanglePoints = function(p1, p2)
{
    var additionalpoints = new Array();
    var xa = null;
    var ya = null;
    var xb = null;
    var yb = null;

    if(this.angle == 0)
    {
        xa = p1.x;
        ya = p2.y;
        xb = p2.x;
        yb = p1.y;
    }
    else
    {
        var tanv = Math.tan(this.angle);
        var tan90v = Math.tan(Math.PI / 2 + this.angle);

        xa = ((p2.y - p2.x * tan90v) - (p1.y - p1.x * tanv)) / (tanv - tan90v);
        ya = xa * tanv + p1.y - p1.x * tanv;
        xb = ((p1.y - p1.x * tan90v) - (p2.y - p2.x * tanv)) / (tanv - tan90v);
        yb = xb * tanv + p2.y - p2.x * tanv;
        
        if(tanv > 10000)
        {
            ya = p2.y;
            yb = p1.y;
        }
    }
    
    additionalpoints[0] = {x:xa, y:ya};
    additionalpoints[1] = {x:xb, y:yb};
    
    return additionalpoints;
}

CBdraw.prototype.rotateObject = function(angleDegree, rotation_dpoint)
{
    var anglediff = (Math.PI * angleDegree / 180) - this.angle;
    this.setAngle(angleDegree);

    if(this.points.length<2)                //rotation is not a option when there is less than two points!
        return;
   
    if(!rotation_dpoint)                    //if no rotation_dpoint is 
        rotation_dpoint = this.points[0];
    
    var points = this.points;
    this.reset();
    
    // move each point in pointarray
    for(var i=0;i<points.length;i++)
    {
        var x ,y;
        if(rotation_dpoint != points[i])
        {
            var dist = Math.sqrt((rotation_dpoint.x-points[i].x)*(rotation_dpoint.x-points[i].x)+(rotation_dpoint.y-points[i].y)*(rotation_dpoint.y-points[i].y))
            var a = (points[i].x - rotation_dpoint.x)/dist;
            var rv;
            rv = Math.acos((points[i].x - rotation_dpoint.x)/dist);
            
            if(points[i].y < rotation_dpoint.y)
                rv = 2*Math.PI - rv;
            var v = rv + anglediff;
            x = rotation_dpoint.x + dist*Math.cos(v);
            y = rotation_dpoint.y + dist*Math.sin(v);
        }
        else
        {
            x = points[i].x;
            y = points[i].y;
        }
        this.addPoint(x,y);
        if(this.nPoints > 1)
        {
            this.draw();
        }
    }
}

CBdraw.prototype.moveObject = function(toPoint, refPoint)
{
    if(refPoint)
        this.refPoint = refPoint;
    this.moveObjectDelta(toPoint.x - this.refPoint.x,toPoint.y - this.refPoint.y)
}
CBdraw.prototype.moveObjectDelta = function(deltax,deltay)
{
    var points = this.points;
    this.reset();
    // move each point in pointarray
    for(var i=0;i<points.length;i++)
    {
        this.addPoint(points[i].x+deltax,points[i].y+deltay);
        this.draw();
    }
}
CBdraw.prototype.moveTmpObject = function(toPoint, refPoint)
{
    if(refPoint)
        this.refPoint = refPoint;
    this.moveTmpObjectDelta(toPoint.x - this.refPoint.x,toPoint.y - this.refPoint.y, toPoint);
}
CBdraw.prototype.moveTmpObjectDelta = function(deltax,deltay,toPoint)
{
    var points = this.points;
    this.reset();
    // move each point in pointarray
    for(var i=0;i<points.length;i++)
        this.addPoint(points[i].x+deltax,points[i].y+deltay);

    this.setTempPoint(this.points[this.nPoints-1],this.points[0]);
}

// *****************************************

function getExtentFromWKT(wkt,buffer)
{
    if(!buffer)
        buffer = 0;
    var xmin = ymin = 9999999999;
    var xmax = ymax = -9999999999;
    var wkts = wkt.split('(');
    wkts[0] = wkts[0].toUpperCase();
    
    if(wkts[0] == 'POINT')
    {
        var p = wkts[1].split(')')[0];
        var pd = p.split(' ');
        xmin = pd[0] - buffer;
        ymin = pd[0] + buffer;
        xmax = pd[1] - buffer;
        ymax = pd[1] + buffer;
    }
    else if(wkts[0] == 'LINESTRING')
    {
        var p = wkts[1].split(')')[0];
        p = p.split(',');
        for(var i=0;i<p.length;i++)
        {
            var pd = p[i].split(' ');
            if(pd.length < 2)
                pd = p[i].split('+');
            var x = pd[0]-0;
            var y = pd[1]-0;
            if (i==0) {
                xmin = x;
                xmax = x;
                ymin = y;
                ymax = y;
            } else {
                if(x < xmin) xmin = x;
                if(x > xmax) xmax = x;
                if(y < ymin) ymin = y;
                if(y > ymax) ymax = y;
            }
        }        
        xmin -= buffer;
        ymin += buffer;
        xmax -= buffer;
        ymax += buffer;
    }
    else if(wkts[0] == 'POLYGON')
    {
        var p = wkts[2].split(')')[0];
        p = p.split(',');
        for(var i=0;i<p.length;i++)
        {
            var pd = p[i].split(' ');
            if(pd.length < 2)
                pd = p[i].split('+');
            var x = pd[0]-0;
            var y = pd[1]-0;
            if (i==0) {
                xmin = x;
                xmax = x;
                ymin = y;
                ymax = y;
            } else {
                if(x < xmin) xmin = x;
                if(x > xmax) xmax = x;
                if(y < ymin) ymin = y;
                if(y > ymax) ymax = y;
            }
        }        
        xmin -= buffer;
        ymin += buffer;
        xmax -= buffer;
        ymax += buffer;
    }
    return xmin+' '+ymin+' '+xmax+' '+ymax;
}


// *****************************************

function CBdrawMode(type)
{
    this.type = type;
}

function CBdrawModes()
{
    this.modes = new Array();
    this.nmodes = 0;
    this.currentMode = -1;
}

CBdrawModes.prototype.regMode = function(type)
{
    this.modes[this.nmodes] = new CBdrawMode(type);
    this.nmodes++;
    
    return this.nmodes-1;
}

CBdrawModes.prototype.setActiveMode = function(mode)
{
    if(mode<this.nmodes+1)
        this.currentMode = mode;
    else
        alert(cbInfo.getString('standard.message.draw.drawmode_unavailable',mode));
}

CBdrawModes.prototype.setActiveModeByType = function(type)
{
    for(var i=0;i<this.nmodes;i++)
    {
        if(this.modes[i].type == type)
        {
            this.currentMode = i;
            return;
        }
    }
    alert(cbInfo.getString('standard.message.draw.drawmode_unavailable',type));
    return 
}

CBdrawModes.prototype.getActiveMode = function()
{
    return this.currentMode;
}





