// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.5/standard_upgrade_01/wwwroot/js/standard/cbmapdrawer.js $ 
// $Date: 20-03-09 14:19 $
// $Revision: 11 $ 
// $Author: Kpo $
// =============================================================== 

//************************************************
// Global vars
//************************************************
//Default options
_cbMapDefault_PolygonOptions = {
    "mode":"draw",                                                     //modes supported: draw, move
    "startwkt":"null",                                                 //Multigeometries not supported
    "cursor":"url('images/standard/cursors/polygon.cur'),crosshair",
    "color":"#FF0000",
    "silent":"false",
    "autook":"false",
    "title":"Polygon",
    "info":"Tegn en polygon ved at klikke med musen i kortet",
    "default_helptext":"",
    "next_helptext":"",
    "okbuttontext":"OK",
    "ok_helptext":"Tegn videre eller klik OK for at acceptere",
    "cancelbuttontext":"Fortryd"};

_cbMapDefault_LineOptions = {
    "mode":"draw",                                                     //modes supported: draw, move
    "startwkt":"null",                                                 //Multigeometries not supported
    "cursor":"url('images/standard/cursors/polygon.cur'),crosshair",
    "color":"#FF0000",
    "silent":"false",
    "autook":"false",
    "title":"Line",
    "info":"Tegn line ved at klikke med musen i kortet",
    "default_helptext":"",
    "next_helptext":"",
    "okbuttontext":"OK",
    "ok_helptext":"Tegn videre eller klik OK for at acceptere",
    "cancelbuttontext":"Fortryd"};


_cbMapDefault_RectangleOptions = {
    "mode":"draw",                                                      //modes supported: draw, move
    "endwkt":"null",                                                    //Only POINT geometry supported for rectangles
    "startwkt":"null",                                                  //Only POINT geometry supported for rectangles
    "width":"0",
    "height":"0",
    "show_angle_option":"false",                                         
    "angle":"0",                                                        //Clockwise angle in degrees
    "cursor":"url('images/standard/cursors/rectangle.cur'),crosshair",
    "color":"#FF0000",
    "silent":"false",
    "autook":"false",
    "title":"Rektangel",
    "info":"Tegn et rektangel ved at klikke med musen i kortet",
    "default_helptext":"Klik i første hjørne",
    "next_helptext":"Klik i modsatte hjørne",
    "okbuttontext":"OK",
    "ok_helptext":"Klik OK for at acceptere eller klik igen for at tegne et andet rektangel",
    "cancelbuttontext":"Fortryd"};

_cbMapDefault_CircleOptions = {
    "mode":"draw",                                                     //modes supported: draw, move
    "startwkt":"null",                                                 //Only POINT geometry supported for circles
    "radius":"0",
    "cursor":"url('images/standard/cursors/circle.cur'),crosshair",
    "color":"#FF0000",
    "silent":"false",
    "autook":"false",
    "title":"Cirkel",
    "info":"Tegn en cirkel ved at klikke med musen i kortet",
    "default_helptext":"Klik i centrum",
    "next_helptext":"Klik i omkredsen",
    "okbuttontext":"OK",
    "ok_helptext":"Klik OK for at acceptere eller klik igen for at tegne en ny cirkel",
    "cancelbuttontext":"Fortryd"};

_cbMapDefault_PointOptions = {
    "mode":"draw",                                                     //modes supported: draw, move
    "startwkt":"null",                                                 //Only POINT geometry supported for points
    "cursor":"url('images/standard/cursors/point.cur'),crosshair",
    "color":"#FF0000",
    "silent":"false",
    "autook":"false",
    "title":"Udpeg punkt",
    "info":"Udpeg ved at klikke med musen i kortet",
    "default_helptext":"",
    "next_helptext":"",
    "okbuttontext":"OK",
    "ok_helptext":"Klik OK for at acceptere eller klik igen",
    "cancelbuttontext":"Fortryd"};

//Internal house keeping
var cbmapdrawer_currentDrawer = null;
var _CBMapDrawerToolMode = null;
var _CBMapDrawerLastToolMode = null;
var _CBMapDrawerDialog = null;

//************************************************
// Static functions given to clients. (The API)
//************************************************
function CBMapDrawer_drawPolygon(callback, options)
{
    CBMapDrawer_setToolMode();
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.cleanObjects();
    var newCBMapDrawer = new CBMapPolygonDrawer(options);
    newCBMapDrawer.setCallBack(callback);
    CBMapDrawer_activate(newCBMapDrawer);
}

function CBMapDrawer_drawLine(callback, options)
{
    CBMapDrawer_setToolMode();
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.cleanObjects();
    var newCBMapDrawer = new CBMapLineDrawer(options);
    newCBMapDrawer.setCallBack(callback);
    CBMapDrawer_activate(newCBMapDrawer);
}


function CBMapDrawer_drawPoint(callback, options)
{
    CBMapDrawer_setToolMode();
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.cleanObjects();
    var newCBMapDrawer = new CBMapPointDrawer(options);
    newCBMapDrawer.setCallBack(callback);
    CBMapDrawer_activate(newCBMapDrawer);
}

function CBMapDrawer_drawRectangle(callback, options)
{
    CBMapDrawer_setToolMode();
    if (cbmapdrawer_currentDrawer != null)
      cbmapdrawer_currentDrawer.cleanObjects();
    var newCBMapDrawer = new CBMapRectangleDrawer(options);
    newCBMapDrawer.setCallBack(callback);
    CBMapDrawer_activate(newCBMapDrawer);
}

function CBMapDrawer_drawCircle(callback, options)
{
    CBMapDrawer_setToolMode();
    if (cbmapdrawer_currentDrawer != null)
      cbmapdrawer_currentDrawer.cleanObjects();
    var newCBMapDrawer = new CBMapCircleDrawer(options);
    newCBMapDrawer.setCallBack(callback);
    CBMapDrawer_activate(newCBMapDrawer);
}

//************************************************
// Static functions used internally.
//************************************************
function CBMapDrawer_activate(newCBMapDrawer)
{
    cbmapdrawer_currentDrawer = newCBMapDrawer;
    var tm = cbKort.toolMode;
    if (tm == _CBMapDrawerToolMode)
        cbmapdrawer_currentDrawer.init();
    else
    {
        _CBMapDrawerLastToolMode = tm;
        cbKort.setToolMode(_CBMapDrawerToolMode);
    }
}
function CBMapDrawer_setToolMode()
{
    if(_CBMapDrawerToolMode == null)
    {
        _CBMapDrawerToolMode = cbKort.registerToolMode(CBMapDrawer_init, CBMapDrawer_downHandler, CBMapDrawer_moveHandler, CBMapDrawer_upHandler, null, CBMapDrawer_toolChangeHandler);
        cbKort.registerMapChangeHandler(CBMapDrawer_mapChangeHandler);
    }
}

//************************************************
// Static callback functions given to cbKort.
//************************************************
function CBMapDrawer_init()
{
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.init();
}
function CBMapDrawer_downHandler(x, y)
{
    var p = cursorLocationCorrection(x,y);
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.downHandler(p.x, p.y);
}
function CBMapDrawer_moveHandler(x, y)
{
    var p = cursorLocationCorrection(x,y);
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.moveHandler(p.x, p.y);
}
function CBMapDrawer_upHandler(x, y)
{
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.upHandler();
}
function CBMapDrawer_outHandler()
{
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.outHandler();
}
function CBMapDrawer_mapChangeHandler()
{
    if (cbmapdrawer_currentDrawer != null)
    {
        var clear_on_toolchange = cbmapdrawer_currentDrawer.customOptions.clear_on_toolchange;
        if (clear_on_toolchange == undefined || clear_on_toolchange==true)
            cbmapdrawer_currentDrawer.cleanObjects();
        else
            cbmapdrawer_currentDrawer.redrawObjects();
    }
}
function CBMapDrawer_toolChangeHandler()
{
    if (cbmapdrawer_currentDrawer != null)
    {
        var clear_on_toolchange = cbmapdrawer_currentDrawer.customOptions.clear_on_toolchange;
        if (clear_on_toolchange == undefined || clear_on_toolchange==true)
            cbmapdrawer_currentDrawer.cleanObjects();
    }
}

//************************************************
// Static callback function given to Timer.
//************************************************
function CBMapDrawer_doTmpDraw()
{
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.doTmpDraw();
}
function CBMapDrawer_doTmpDrawMove()
{
    if (cbmapdrawer_currentDrawer != null)
        cbmapdrawer_currentDrawer.doTmpDrawMove();
}

//************************************************
// Implementation - Private
// Class CBMapDrawerBase is defined
//   CBMapDrawerBase is never used directly, but inherited from
//************************************************


function CBMapDrawerBase(drawMode, defaultOptions, customOptions)
{
    if( typeof(drawMode) != "undefined" )
    {
        this.drawmode = drawMode;
        this.mode = null;
        this.callback = null;
        
        this.pointsAsString = "";
        this.wkt = "";
        this.radius = 0;
        this.cursor = 'crosshair';
        this.dialogInput = '';
        this.tmpPoint = null;
        this.isTmpWaiting = false;
        this.stop = false;
        this.silent = 'false';
        this.mapOutHandlerNum;
        this.defaultoptions = defaultOptions;
        if (customOptions == null)
            this.customOptions = defaultOptions;
        else
            this.customOptions = customOptions;

        this.color = '#FF0000';
        if(this.customOptions.color)
            this.color = this.customOptions.color;

        this.size = 2;
        
        this.cbdraw = new CBdraw(drawMode,'CBMapDrawer_mapbox', -1, true, this.color, this.size);
        this.cbdraw.setSize(this.size);

    }
}

CBMapDrawerBase.prototype.init = function ()
{
    this.silent = this.customOptions.silent;
    if (this.silent == undefined) this.silent = this.defaultoptions.silent;

    if (this.silent != "true")
    {
        if(_CBMapDrawerDialog == null)
            _CBMapDrawerDialog = new Dialog('',cbmapdrawer_currentDrawer.dialogCancel,'CBMapDrawer_dialog');

        _CBMapDrawerDialog.addContentHTML(this.dialogContent());
        
        var title = this.customOptions.title;
        if (title == undefined) title = this.defaultoptions.title;
        this.setTitle(title);

        var info = this.customOptions.info;
        if (info == undefined) info = this.defaultoptions.info;
        this.setInfo(info);

        var autook = this.customOptions.autook;
        if (autook == undefined) autook = this.defaultoptions.autook;

        this.setOKButton(false);

        if (autook == 'true')
            hideElement(getElement("CBMapDrawer_dialogOK_button"));
        else
        {
            showElement(getElement("CBMapDrawer_dialogOK_button"));
            var okbuttontext = this.customOptions.okbuttontext;
            if (okbuttontext == undefined) okbuttontext = this.defaultoptions.okbuttontext;
            getElement("CBMapDrawer_dialogOK_button").innerHTML = okbuttontext;
        }
        var cancelbuttontext = this.customOptions.cancelbuttontext;
        if (cancelbuttontext == undefined) cancelbuttontext = this.defaultoptions.cancelbuttontext;
        getElement("CBMapDrawer_dialogCancel_button").innerHTML = cancelbuttontext;
        
        _CBMapDrawerDialog.showDialog()
    }

    var cursor = this.customOptions.cursor;
    if (cursor == undefined) cursor = this.defaultoptions.cursor;
    this.cursor = cursor;

    var mode = this.customOptions.mode;
    if (mode == undefined) mode = this.defaultoptions.mode;
    this.mode = mode;

    var startwkt = this.customOptions.startwkt;
    if(startwkt != undefined)
    {
        if (startwkt != "null")
            this.cbdraw.addPointsFromWKT(startwkt);
    }
    
    this.setInput();

    this.mapOutHandlerNum = addMapcontainerOnMouseOutHandler(null);
    setMapcontainerOnMouseOutHandler(this.mapOutHandlerNum, CBMapDrawer_outHandler);
    this.setCursor();
    
    this.initEnd();
}

CBMapDrawerBase.prototype.dialogContent = function ()
{
    var h = '        <table id="CBMapDrawer_table" class="divtable">'+
            '            <tr align="left">'+
            '                <td colspan="2"><span id="CBMapDrawer_info"></span></td>'+
            '            </tr>'+
            '            <tr align="left">'+
            '            </tr>'+
            '            <tr align="left">'+
            '                <td id="CBMapDrawerDialog_input" colspan="2">'+
            '                </td>'+
            '            </tr>'+
            '            <tr align="left">'+
            '                <td valign="top">'+
            '                    <img id ="CBMapDrawer_help_icon" src="/images/standard/buttons/i_info2.gif"/>'+
            '                </td>'+
            '                <td valign="top">'+
            '                    <span id="CBMapDrawer_help"></span>'+
            '                </td>'+
            '            </tr>'+
            '            <tr align="right">'+
            '                <td colspan="2">'+cbInfo.getString('standard.message.draw.drawmode_unavailable',mode)
            '                    <button class="menubuttonDisable" id="CBMapDrawer_dialogOK_button" onclick="cbmapdrawer_currentDrawer.drawOK();" disabled="true"></button>'+
            '                    <button class="menubutton" id="CBMapDrawer_dialogCancel_button" onclick="cbmapdrawer_currentDrawer.dialogCancel();"></button>'+
            '                </td>'+
            '            </tr>'+
            '        </table>';
    return h;
}

CBMapDrawerBase.prototype.setCallBack = function(callback){
    this.callback = callback;
}

CBMapDrawerBase.prototype.onDrawSuccess = function(CBMapDrawerBase)
{
    //this.pointsAsString = this.cbdraw.getPointsAsString();
    this.wkt = this.cbdraw.getWKT();
    this.radius = this.cbdraw.getRadius();
    if(this.wkt)
    {
        cbKort.setToolMode(_CBMapDrawerLastToolMode);
        this.cleanObjects();
    }
    this.callback.onDrawSuccess(CBMapDrawerBase);
}


CBMapDrawerBase.prototype.redrawObjects = function()
{
    this.setColor();
    this.setSize();
    
    this.cbdraw.drawAll(this.cbdraw.wpoints)
    cbKort.setToolMode(_CBMapDrawerToolMode);
}

CBMapDrawerBase.prototype.dialogCancel = function ()
{
    cbKort.setToolMode(_CBMapDrawerLastToolMode);
    this.cleanObjects(true);
}


CBMapDrawerBase.prototype.cleanObjects = function (dialogClose)
{
    this.cbdraw.reset();
    setMapcontainerOnMouseOutHandler(this.mapOutHandlerNum, null);
    if(!dialogClose)
    {
        if(_CBMapDrawerDialog != null)
            _CBMapDrawerDialog.hideDialog();
    }
    if (this.silent != "true")
    {
        hideElement(getElement("CBMapDrawer_help_icon"));
        hideElement(getElement("CBMapDrawer_dialogOK_button"));
    }
    this.setTitle("");
    this.setInfo("");
    this.setHelpText("");
    cbmapdrawer_currentDrawer = null;
}

CBMapDrawerBase.prototype.getPointsAsString = function()
{
    return this.cbdraw.getPointsAsString();
}

CBMapDrawerBase.prototype.getWKT = function()
{
    return this.wkt;
}

CBMapDrawerBase.prototype.getRadius = function()
{
    return this.radius;
}

CBMapDrawerBase.prototype.setOKButton = function(enabled)
{
    var oke = getElement("CBMapDrawer_dialogOK_button");
    if (enabled)
    {
        var silent = this.customOptions.silent;
        if (silent == undefined) silent = this.defaultoptions.silent;
        if (silent == "true")
            this.drawOK();
        else
        {
            if(oke)
            {
                oke.className = "menubutton";
                oke.disabled = false;
            }
            this.setHelpText('ok');
            var autook = this.customOptions.autook;
            if (autook == undefined) autook = this.defaultoptions.autook;
            if (autook == "true") this.drawOK();
        }
    }
    else
    {
        if(oke)
        {
            oke.className = "menubuttonDisable";
            oke.disabled = true;
        }
        this.setHelpText('default');
    }
}

CBMapDrawerBase.prototype.setTitle = function(title)
{
    var e = getElement("CBMapDrawer_dialog_titleBar");
    if(e)
        e.innerHTML = title;
}

CBMapDrawerBase.prototype.setInfo = function(info)
{
    var e = getElement("CBMapDrawer_info");
    if(e)
        e.innerHTML = info;
}

CBMapDrawerBase.prototype.setHelpText = function(text)
{
    var help = "";
    var silent = this.customOptions.silent;
    if (silent == undefined) silent = this.defaultoptions.silent;

    if (silent != "true")
    {
        if (text == 'default')
        {
            help = this.customOptions.default_helptext;
            if (help == undefined) help = this.defaultoptions.default_helptext;
        }
        else if (text == 'next')
        {
            help = this.customOptions.next_helptext;
            if (help == undefined) help = this.defaultoptions.next_helptext;
        }
        else if (text == 'ok')
        {
            help = this.customOptions.ok_helptext;
            if (help == undefined) help = this.defaultoptions.ok_helptext;
        }

        if (help == "")
          hideElement(getElement("CBMapDrawer_help_icon"));
        else
            showElement(getElement("CBMapDrawer_help_icon"));
        getElement("CBMapDrawer_help").innerHTML = help;
    }
}

CBMapDrawerBase.prototype.setInput = function()
{
    var e = getElement("CBMapDrawerDialog_input");
    if(e)
        e.innerHTML = this.dialogInput;
}

CBMapDrawerBase.prototype.setCursor = function ()
{
    if (this.cbdraw)
        cbKort.map.style.cursor = this.cursor;
}

CBMapDrawerBase.prototype.setColor = function(color)
{
    if(color)
        this.color = color;
    this.cbdraw.setColor(this.color,this.color);
}

CBMapDrawerBase.prototype.setSize = function(size)
{
    if(size)
        this.size = size;
    this.cbdraw.setSize(this.size);
}

CBMapDrawerBase.prototype.doTmpDraw = function ()
{
    this.cbdraw.setTempPoint(this.tmpPoint);
    this.isTmpWaiting = false;
}

CBMapDrawerBase.prototype.doTmpDrawMove = function ()
{
    this.cbdraw.moveTmpObject(this.tmpPoint);
    this.cbdraw.setRefPoint(this.tmpPoint);
    this.isTmpWaiting = false;
}

CBMapDrawerBase.prototype.moveHandler = function (mx, my)
{
    this.tmpPoint = {x:mx, y:my};
    if(this.mode == 'draw')
    {
        if (this.isTmpWaiting == false && this.cbdraw.nPoints > 0)
        {
            this.isTmpWaiting = true;
            setTimeout(CBMapDrawer_doTmpDraw, 100);
        }
    }
    else if(this.mode == 'move' || this.mode == 'movesimple')
    {
        if(this.mode == 'movesimple') this.cbdraw.simpletmp = true;
        else this.cbdraw.simpletmp = false;
        if (this.isTmpWaiting == false && this.cbdraw.nPoints > 0 && !this.stop)
        {
            this.isTmpWaiting = true;
            setTimeout(CBMapDrawer_doTmpDrawMove, 100);
        }
    }
}

CBMapDrawerBase.prototype.outHandler = function ()
{
    this.moveHandler(-1, -1);
}

//Abstract member functions
CBMapDrawerBase.prototype.drawOK = function(){}
CBMapDrawerBase.prototype.downHandler = function(x1,y1){}
//CBMapDrawerBase.prototype.moveHandler = function(x1,y1){}
CBMapDrawerBase.prototype.upHandler = function(x1,y1){}
CBMapDrawerBase.prototype.initEnd = function(){}

//************************************************
// Implementation - Private
// Classes
//  CBMapPolygonDrawer
//  CBMapPointDrawer
//  CBMapRectangleDrawer
//************************************************
//Facilitate correct inheritance (http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html)
//Denne kode skal eksekveres FØR "Inherits" kaldes.
Object.prototype.Inherits = function( parent )
{
    if( arguments.length > 1 )
        parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
    else
        parent.call( this );
}

Function.prototype.Inherits = function( parent )
{
    this.prototype = new parent();
    this.prototype.constructor = this;
}

//************************************************
//CBMapPolygonDrawer
//************************************************
CBMapPolygonDrawer.Inherits(CBMapDrawerBase);

function CBMapPolygonDrawer(customOptions){
    this.Inherits( CBMapDrawerBase, "polygon", _cbMapDefault_PolygonOptions, customOptions);
}

CBMapPolygonDrawer.prototype.drawOK = function (){
    this.cbdraw.closePolygon();
    this.onDrawSuccess(this);
}

CBMapPolygonDrawer.prototype.downHandler = function (dx, dy)
{
    this.cbdraw.addPoint(dx,dy);
    this.cbdraw.draw();

    var silent = this.customOptions.silent;
    if (silent == undefined) silent = this.defaultoptions.silent;
    if (silent != 'true') {
        if(this.cbdraw.nPoints<3)
            this.setOKButton(false);
        else
            this.setOKButton(true);
    }
}

//************************************************
//CBMapLineDrawer
//************************************************

CBMapLineDrawer.Inherits(CBMapDrawerBase);

function CBMapLineDrawer(customOptions){
  this.Inherits( CBMapDrawerBase, "line", _cbMapDefault_LineOptions, customOptions);
}

CBMapLineDrawer.prototype.drawOK = function (){
  this.onDrawSuccess(this);
}

CBMapLineDrawer.prototype.downHandler = function (dx, dy)
{
  this.cbdraw.addPoint(dx,dy);
  this.cbdraw.draw();
}


//************************************************
//CBMapPointDrawer
//************************************************
CBMapPointDrawer.Inherits(CBMapDrawerBase);

function CBMapPointDrawer(customOptions){
    this.Inherits( CBMapDrawerBase,"point", _cbMapDefault_PointOptions, customOptions);
}

CBMapPointDrawer.prototype.downHandler = function (dx, dy)
{
    this.cbdraw.addPoint(dx,dy);
    this.cbdraw.draw();
    //this.onDrawSuccess(this);
    this.setOKButton(true);
}

CBMapPointDrawer.prototype.drawOK = function ()
{
    this.onDrawSuccess(this);
}

//************************************************
//CBMapRectangleDrawer
//************************************************
CBMapRectangleDrawer.Inherits(CBMapDrawerBase);

function CBMapRectangleDrawer(customOptions)
{
    this.Inherits( CBMapDrawerBase, "rectangle", _cbMapDefault_RectangleOptions, customOptions);
    this.angle = this.customOptions.angle;                                  //angle in degrees
    this.refPoint = null;
    if (this.angle == undefined) this.angle = this.defaultoptions.angle;
    
    this.cbdraw.setAngle(this.angle);

    var show_angle_option = this.customOptions.show_angle_option;
    if (show_angle_option == 'true')
        this.setDialogInput();
}

CBMapRectangleDrawer.prototype.downHandler = function (dx, dy)
{
    if(this.mode == 'draw')
        this.cbdraw.addPoint(dx,dy);

    if(this.cbdraw.nPoints==0)
    {
        this.setOKButton(false);
    }
    else if(this.cbdraw.nPoints==1)
    {
        this.setOKButton(false);
        this.setHelpText("next");
    }
    else if(this.cbdraw.nPoints==2)
    {
        this.cbdraw.setRefPoint({x:this.cbdraw.points[0].x+(this.cbdraw.points[1].x-this.cbdraw.points[0].x)/2,y:this.cbdraw.points[0].y+(this.cbdraw.points[1].y-this.cbdraw.points[0].y)/2})
        this.setOKButton(true);
        if(this.mode == 'move' || this.mode == 'movesimple')
            this.stop = true;
    }
    this.cbdraw.draw();
}

CBMapRectangleDrawer.prototype.drawOK = function ()
{
    this.onDrawSuccess(this);
}

CBMapRectangleDrawer.prototype.setAngle = function (angle)
{
    this.angle = angle % 360;
    if(this.angle < 0) this.angle += 360;
    this.cbdraw.setAngle(this.angle);
}
CBMapRectangleDrawer.prototype.rotate = function (angle)
{
    this.angle = angle % 360;
    if(this.angle < 0) this.angle += 360;
    this.cbdraw.rotateObject(this.angle);
    var e = getElement('CBMapDrawerDialog_angleinput');
    if(e)
        e.value = this.angle;
    this.cbdraw.setRefPoint({x:this.cbdraw.points[0].x+(this.cbdraw.points[1].x-this.cbdraw.points[0].x)/2,y:this.cbdraw.points[0].y+(this.cbdraw.points[1].y-this.cbdraw.points[0].y)/2})
}
CBMapRectangleDrawer.prototype.setDialogInput = function ()
{
    var html = '                    <input onkeyup="if (event.keyCode>=32){cbmapdrawer_currentDrawer.rotate(this.value)}" style="width:50px;" type="text" id="CBMapDrawerDialog_angleinput" value="'+this.angle+'"></input>'+
               '                    <button class="menubutton" style="width:20px;font-size: 10pt;margin-left : 0px;" id="CBMapDrawerDialog_angleplus" onclick="cbmapdrawer_currentDrawer.rotate(parseInt(cbmapdrawer_currentDrawer.angle)-5);" >-</button>'+
               '                    <button class="menubutton" style="width:20px;font-size: 10pt;margin-left : 0px;" id="CBMapDrawerDialog_angleminus" onclick="cbmapdrawer_currentDrawer.rotate(parseInt(cbmapdrawer_currentDrawer.angle)+5);" >+</button>'+
//               '                    <br/><button class="menubutton" id="CBMapDrawerDialog_test" onclick="cbmapdrawer_currentDrawer.move();" >Move</button>'+
               '';
    this.dialogInput = html;
}
CBMapRectangleDrawer.prototype.initEnd = function ()
{
    var endwkt = this.customOptions.endwkt;
    if(endwkt != undefined)
    {
        if (endwkt != "null")
        {
            this.cbdraw.addPointsFromWKT(endwkt);
            this.cbdraw.setRefPoint({x:this.cbdraw.points[0].x+(this.cbdraw.points[1].x-this.cbdraw.points[0].x)/2,y:this.cbdraw.points[0].y+(this.cbdraw.points[1].y-this.cbdraw.points[0].y)/2})
        }
    }
}

//************************************************
//CBMapCircleDrawer
//************************************************
CBMapCircleDrawer.Inherits(CBMapDrawerBase);

function CBMapCircleDrawer(customOptions)
{
    this.Inherits( CBMapDrawerBase, "circle", _cbMapDefault_CircleOptions, customOptions);
}

CBMapCircleDrawer.prototype.downHandler = function (dx, dy)
{
    this.cbdraw.addPoint(dx,dy);
    
    if(this.cbdraw.nPoints==0)
    {
        this.setOKButton(false);
    }
    else if(this.cbdraw.nPoints==1)
    {
        if(this.customOptions.autook == 'true')
        {
            this.setOKButton(true);
        }
        else
        {
            this.setOKButton(false);
            this.setHelpText("next");
        }
    }
    else if(this.cbdraw.nPoints==2)
    {
        this.setOKButton(true);
    }
    this.cbdraw.draw();
}

CBMapCircleDrawer.prototype.drawOK = function ()
{
    this.onDrawSuccess(this);
}
