// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.6/standard/wwwroot/js/standard/cbmapdrawer.js $ 
// $Date: 5-07-10 16:34 $
// $Revision: 12 $ 
// $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;
        
        //TODO: setToolMode should be called to change the cursor
        cbKort.setToolMode(_CBMapDrawerToolMode);
    }
    cbmapdrawer_currentDrawer.setCursor();
}
function CBMapDrawer_setToolMode()
{
    if(_CBMapDrawerToolMode == null) {
        _CBMapDrawerToolMode = cbKort.registerToolMode(CBMapDrawer_init, null, null, null, null, CBMapDrawer_toolChangeHandler, this.cursor);
    }
}

//************************************************
// Static callback functions given to cbKort.
//************************************************
function CBMapDrawer_init()
{
    return;
    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_toolChangeHandler()
{
    cbKort.mapObj.panzoom ();
    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
//************************************************
CBMapDrawerBase = SpatialMap.Class({
    initialize: function (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;
        }
    },
    
    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();
    },

    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">'+
                '                    <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;
    },

    setCallBack: function(callback){
        this.callback = callback;
    },

    onDrawSucces: function(map) {
        //this.pointsAsString = this.cbdraw.getPointsAsString();
        this.wkt = map.wkt;
        cbKort.mapObj.deleteFeature (map.id);
        this.radius = 0;//this.cbdraw.getRadius();
        if(this.wkt)
        {
            cbKort.setToolMode(_CBMapDrawerLastToolMode);
            this.cleanObjects();
        }
        this.callback.onDrawSuccess(this);
    },

    redrawObjects: function() {
        this.setColor();
        this.setSize();
        
        //this.cbdraw.drawAll(this.cbdraw.wpoints)
        cbKort.setToolMode(_CBMapDrawerToolMode);
    },

    dialogCancel: function () {
        cbKort.setToolMode(_CBMapDrawerLastToolMode);
        this.cleanObjects(true);
    },

    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;
    },
    
    getPointsAsString: function() {
        //TODO: 
        return this.wkt;
    },
    
    getWKT: function() {
        return this.wkt;
    },
    
    getRadius: function() {
        return this.radius;
    },
    
    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');
        }
    },
    
    setTitle: function(title) {
        var e = getElement("CBMapDrawer_dialog_titleBar");
        if(e)
            e.innerHTML = title;
    },
    
    setInfo: function(info) {
        var e = getElement("CBMapDrawer_info");
        if(e)
            e.innerHTML = info;
    },
    
    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;
        }
    },
    
    setInput: function() {
        var e = getElement("CBMapDrawerDialog_input");
        if(e)
            e.innerHTML = this.dialogInput;
    },
    
    setCursor: function () {
        cbKort.setCursor (this.cursor);
    },
    
    setColor: function(color) {
        if(color)
            this.color = color;
        //this.cbdraw.setColor(this.color,this.color);
    },
    
    setSize: function(size) {
        if(size)
            this.size = size;
        //this.cbdraw.setSize(this.size);
    },
    
    
    //Abstract member functions
    rawOK: function() {},
    downHandler: function(x1,y1){},
    //moveHandler: function(x1,y1){},
    upHandler: function(x1,y1){},
    initEnd: function(){},

    CLASS_NAME: "CBMapDrawerBase"
});


//************************************************
// Implementation - Private
// Classes
//  CBMapPolygonDrawer
//  CBMapPointDrawer
//  CBMapRectangleDrawer
//************************************************

//************************************************
//CBMapPolygonDrawer
//************************************************
CBMapPolygonDrawer = SpatialMap.Class(CBMapDrawerBase,{

    initialize: function (customOptions) {
        CBMapDrawerBase.prototype.initialize.apply(
            this, ["polygon", _cbMapDefault_PolygonOptions, customOptions]
        );
        cbKort.mapObj.drawPolygon (SpatialMap.Function.bind(this.onDrawSucces,this));
    },
    
    drawOK: function () {
    },
    
    downHandler: function (dx, dy) {
    },
    
    CLASS_NAME: "CBMapPolygonDrawer"
});


//************************************************
//CBMapLineDrawer
//************************************************
CBMapLineDrawer = SpatialMap.Class(CBMapDrawerBase,{

    initialize: function (customOptions) {
        CBMapDrawerBase.prototype.initialize.apply(
            this, ["line", _cbMapDefault_LineOptions, customOptions]
        );
        cbKort.mapObj.drawLine (SpatialMap.Function.bind(this.onDrawSucces,this));
    },

    drawOK: function () {
    },
    
    downHandler: function (dx, dy) {
    },

    CLASS_NAME: "CBMapLineDrawer"
});

//************************************************
//CBMapPointDrawer
//************************************************
CBMapPointDrawer = SpatialMap.Class(CBMapDrawerBase,{

    initialize: function (customOptions) {
        CBMapDrawerBase.prototype.initialize.apply(
            this, ["point", _cbMapDefault_PointOptions, customOptions]
        );
        cbKort.mapObj.drawPoint (SpatialMap.Function.bind(this.onDrawSucces,this));
    },
    
    downHandler: function (dx, dy) {
    },
    
    drawOK: function () {
    },
    
    CLASS_NAME: "CBMapPointDrawer"
});

//************************************************
//CBMapRectangleDrawer
//************************************************
CBMapRectangleDrawer = SpatialMap.Class(CBMapDrawerBase,{

    initialize: function (customOptions) {
        CBMapDrawerBase.prototype.initialize.apply(
            this, ["rectangle", _cbMapDefault_RectangleOptions, customOptions]
        );

        //TODO: MISSING
        var drawoptions = {
            draw: {
                sides:4,
                irregular: true
            }
        };
        cbKort.mapObj.drawRegularPolygon (SpatialMap.Function.bind(this.onDrawSucces,this),drawoptions);
        return;

        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();
    },
    
    downHandler: function (dx, dy) {
    },
    
    drawOK: function () {
        this.onDrawSuccess(this);
    },
    
    setAngle: function (angle) {
        this.angle = angle % 360;
        if(this.angle < 0) this.angle += 360;
    },
    
    rotate: function (angle) {
        return;
        
        
        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;
    },
    
    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;
    },
    
    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})
            }
        }
    },
    
    CLASS_NAME: "CBMapRectangleDrawer"
});

//************************************************
//CBMapCircleDrawer
//************************************************
CBMapCircleDrawer = SpatialMap.Class(CBMapDrawerBase,{

    initialize: function (customOptions) {
        CBMapDrawerBase.prototype.initialize.apply(
            this, ["circle", _cbMapDefault_CircleOptions, customOptions]
        );
        cbKort.mapObj.drawRegularPolygon (SpatialMap.Function.bind(this.onDrawSucces,this),{draw:{sides:40}});
    },
    
    downHandler: function (dx, dy) {
    },
    
    drawOK: function () {
        this.onDrawSuccess(this);
    },
    
    CLASS_NAME: "CBMapCircleDrawer"
});
    
