var _SELECTED_OBJECT=null;
var _SELECTED_STRAIGHT=null;
var _LIST_OF_POINTS = new Array();
var _POLAR_FIGURE = null;
var _LINE_PATH = null;
var _POLYGON = null;
var _CNTR_GEOMETRIES_URL=_URL_MAP_VISIO+"geom/cntr_geometries.php";
var _UI_POLAR_FIGURE_URL=_URL_MAP_VISIO+"geom/ui_polar_figure.php";
var _SNAP_TOLERANCE=0.02;
var _ID_GEOMETRY = -1;
var _TEXT_DOMAIN = "";
var _LIST_OF_LINES = new Array();
var _LIST_OF_CIRCLES = new Array();

function evt_but_popup_line(event)
{
    show_popup_menu(event,new Popup_Menu_Line(this));
    if(_LINE_PATH == null)
    {
        _LINE_PATH = new Line_Path();
        _LIST_OF_EXTRA_GRAPHICS.push(_LINE_PATH);
    }
}
function evt_but_popup_polygon(event)
{
    show_popup_menu(event,new Popup_Menu_Polygon(this));
    if(_POLYGON == null)
    {
        _POLYGON = new Line_Path();
        _LIST_OF_EXTRA_GRAPHICS.push(_POLYGON);
    }
}
function evt_but_draw_line(event)
{
    process_mouse_down(event,new Evt_Draw_Line(_LINE_PATH));
}
function evt_but_draw_polygon(event)
{
    process_mouse_down(event,new Evt_Draw_Line(_POLYGON));
}
function evt_but_draw_point(event)
{
    process_mouse_down(event,new Evt_Draw_Point());
}
function evt_select_temporary_point(event)
{
    process_mouse_down(event,new Evt_Select_Temporary_Point);
}
function evt_snap_point(event)
{
    if(_LAYER_SELECTED.length  == "" )
    {
        _OP_USER = new Idle();
        alert(_MSG_SELECT_LAYER);
        return false;
    }
    process_mouse_down(event,new Evt_Snap_Point(false));
}

function evt_snap_new_point(event)
{
    if(_LAYER_SELECTED.length  == "" )
    {
        _OP_USER = new Idle();
        alert(_MSG_SELECT_LAYER);
        return false;
    }
    process_mouse_down(event,new Evt_Snap_Point(true));
}
function evt_but_select_object(event)
{
    if(_LAYER_SELECTED.length  == "" )
    {
        _OP_USER = new Idle();
        alert(_MSG_SELECT_LAYER);
        return false;
    }
    process_mouse_down(event,new Evt_Select_Object());
}
function evt_select_segment(event)
{
    if(_LAYER_SELECTED.length  == "" )
    {
        _OP_USER = new Idle();
        alert(_MSG_SELECT_LAYER);
        return false;
    }
    process_mouse_down(event,new Evt_Select_Segment());
}
function evt_move_straight_line(event)
{
    if(_LIST_OF_LINES.length  < 1)
    {
        _OP_USER = new Idle();
        alert(_MSG_ONE_LINE_REQUIRED);
        return false;
    }
    process_mouse_down(event,new Evt_Move_Straight_Line());
}
function evt_draw_circle(event)
{
    var circle = new Evt_Draw_Circle();
    process_mouse_down(event,circle);
}
function evt_draw_new_circle(event)
{
    if(_LIST_OF_POINTS.length  < 1)
    {
        _OP_USER = new Idle();
        alert(_MSG_ONE_POINT_REQUIRED);
        return false;
    }
    process_mouse_down(event,new Evt_Draw_New_Circle());
}
function evt_but_draw_polygon_2_points(event)
{
    if(!event)
        event=window.event;
    if(event.keyCode != 13)
        return false;
    draw_from_2_points(_POLYGON);
}
function draw_from_2_points(global_object)
{
    try{
        if(_LIST_OF_POINTS.length == 0)
        {
            alert(_MSG_SET_POINT);
            return false;
        }
        remove_from_extra_graphics(global_object);
        var len = global_object._list_of_graphics.length;
        if( len == 1 && global_object._list_of_graphics[0]._p2 == null)
        {
            global_object._list_of_graphics[0]._p2 = _LIST_OF_POINTS[0].clone();
        }
        else if(len == 0)
        {
            var line = new Line(_LIST_OF_POINTS[0].clone(),null);
            global_object.add_graphics(line);
        }
        else
        {
            var line = null;
            if(global_object._list_of_graphics[len-1].constructor == Line)
                line = new Line(global_object._list_of_graphics[len-1]._p2.clone(),
            _LIST_OF_POINTS[0].clone());
            else if(global_object._list_of_graphics[len-1].constructor == Arc)
                line = new Line(global_object._list_of_graphics[len-1]._p3.clone(),
            _LIST_OF_POINTS[0].clone());
            global_object.add_graphics(line);
        }
        clear_points();
        if(len > 0)
        {
            _LIST_OF_EXTRA_GRAPHICS.push(global_object);
            draw_extra_graphics(false);
        }	
    }catch(e)
    {
        alert("Vector_Graphics_Manager.draw_from_2_points(): "+e);
        return false;
    }
}
function evt_but_draw_line_2_points(event)
{
    if(!event)
        event=window.event;
    if(event.keyCode != 13)
        return false;
    draw_from_2_points(_LINE_PATH);
}
function evt_new_vertex(global_object)
{
    try{
        if(global_object == null || global_object._list_of_graphics.length == 0)
        {
            alert(_MSG_DEFINE_GEOMETRY);
            return false;
        }
        if(  _LIST_OF_POINTS.length !=1)
        {
            alert(_MSG_DEFINE_GEOMETRY);
            return false;
        }
        var obj = global_object._list_of_graphics[0];
        var distance=obj.distance_from_point(_LIST_OF_POINTS[0]._x,_LIST_OF_POINTS[0]._y,true);
        var index=0;
        for(var i=1; i< global_object._list_of_graphics.length;i++)
        {
            var o = global_object._list_of_graphics[i];
            if(o.distance_from_point(_LIST_OF_POINTS[0]._x,_LIST_OF_POINTS[0]._y,true) < distance)
            {
                distance = o.distance_from_point(_LIST_OF_POINTS[0]._x,_LIST_OF_POINTS[0]._y,true);
                obj = o;
                index=i;
            }
        }
        if(obj.constructor == Arc)
        {
            alert(_MSG_ONLY_LINE_MODIFICATION_VERTEX);
            return false;
        }
        var new_line = new Line(_LIST_OF_POINTS[0].clone(),obj._p2.clone());
        obj._p2 =_LIST_OF_POINTS[0].clone();
        global_object._list_of_graphics.splice(index+1,0,new_line);
        clear_points();
        draw_extra_graphics(false);
    }catch(e)
    {
        alert("Vector_Graphics_Manager.evt_new_vertex(): "+e);
        return false;
    }
}

function evt_move_vertex(global_object)
{
    try{
        if(global_object == null || global_object._list_of_graphics.length == 0)
        {
            alert(_MSG_DEFINE_GEOMETRY);
            return false;
        }
        if(  _LIST_OF_POINTS.length !=1)
        {
            alert(_MSG_SET_POINT);
            return false;
        }
        var obj = global_object._list_of_graphics[0];
        var distance=obj.distance_from_point(_LIST_OF_POINTS[0]._x,_LIST_OF_POINTS[0]._y,true);
        for(var i=1; i< global_object._list_of_graphics.length;i++)
        {
            var o = global_object._list_of_graphics[i];
            if(o.distance_from_point(_LIST_OF_POINTS[0]._x,_LIST_OF_POINTS[0]._y,true) < distance)
            {
                distance = o.distance_from_point(_LIST_OF_POINTS[0]._x,_LIST_OF_POINTS[0]._y,true);
                obj = o;
            }
        }
        if(obj.constructor == Arc)
        {
            alert(_MSG_ONLY_LINE_MODIFICATION_VERTEX);
            return false;
        }
        var vertex=null;
        if(obj._p1.distance(_LIST_OF_POINTS[0])<obj._p2.distance(_LIST_OF_POINTS[0]))
            vertex=obj._p1;
        else
            vertex=obj._p2;
        for(var i=0; i< global_object._list_of_graphics.length;i++)
        {
            var l = global_object._list_of_graphics[i];
            if(l.constructor == Arc)
                continue;
            if(l._p1.equals(vertex))
                l._p1=_LIST_OF_POINTS[0].clone();
            else if(l._p2.equals(vertex))
                l._p2=_LIST_OF_POINTS[0].clone();
        }
        clear_points();
        draw_extra_graphics(false);
    }catch(e)
    {
        alert("Vector_Graphics_Manager.evt_move_vertex(): "+e);
        return false;
    }
	
}
function evt_but_remove_point(event)
{
    process_mouse_down(event,new Evt_Remove_Point());
}
function evt_but_remove_line(event)
{
    process_mouse_down(event,new Evt_Remove_Line(_LINE_PATH));
}
function evt_but_remove_straight_line(event)
{
    process_mouse_down(event,new Evt_Remove_Straight_Line());
}
function evt_but_remove_circle(event)
{
    process_mouse_down(event,new Evt_Remove_Circle());
}
function evt_but_remove_polygon(event)
{
    process_mouse_down(event,new Evt_Remove_Line(_POLYGON));
}

function evt_select_text(event){
    process_mouse_down(event,new Evt_Select_Interlis_Text());
}

function evt_show_attributes_text(event){
    var obj = new Evt_Select_Interlis_Text();
    obj.add_listener(new Listener_Show_Text_Attributes());
    process_mouse_down(event,obj);
}

function update_point_geometry_to_db(type,action,id_geom_name,cntr_url,other_params){
    try{
        remove_popup();
		
        if(_LIST_OF_POINTS.length != 1)
        {
            alert(_MSG_SET_POINT);
            return false;
        }
        if(_ID_GEOMETRY == -1)
        {
            alert(_MSG_SELECT_OBJECT);
            return false;
        }
        var params="type="+type+"&action="+action+"&x="+_LIST_OF_POINTS[0]._x+
            "&y="+_LIST_OF_POINTS[0]._y+"&"+id_geom_name+"="+_ID_GEOMETRY+other_params;
        execut_map_ajax_call(params,cntr_url);
    }catch(e)
    {
        alert("Vector_Graphics_Events_Manager.update_point_geometry_to_db: "+e);
        loading_message(false);
        return false;
    }
}
function update_line_geometry_to_db(type,action,id_geom_name,cntr_url){
     try{
        remove_popup();
		
        if(_LINE_PATH == null)
        {
            alert(_MSG_DEFINE_GEOMETRY);
            return false;
        }
        if(_ID_GEOMETRY == -1)
        {
            alert(_MSG_SELECT_OBJECT);
            return false;
        }
        var params="type="+type+"&action="+action+"&geometry="+_LINE_PATH.to_post_request()+
            "&"+id_geom_name+"="+_ID_GEOMETRY;
        alert(params);
        execut_map_ajax_call(params,cntr_url);
    }catch(e)
    {
        alert("Vector_Graphics_Events_Manager.update_point_geometry_to_db: "+e);
        loading_message(false);
        return false;
    }
    
}
function delete_geometry_from_db(type,action,id_geom_name,cntr_url){
    try{
        remove_popup();
        if(_ID_GEOMETRY == -1)
        {
            alert(_MSG_SELECT_OBJECT);
            return false;
        }
        var params="type="+type+"&action="+action+"&"+id_geom_name+"="+_ID_GEOMETRY;
        execut_map_ajax_call(params,cntr_url);
    }catch(e)
    {
        alert("Evt_Select_Hydraulic_Node.configure_items(): "+e);
        loading_message(false);
        return false;
    }
}

function insert_text_to_db(type){
    try{
        remove_popup();
		
        if(_LIST_OF_POINTS.length != 1)
        {
            alert(_MSG_SET_POINT);
            return false;
        }
        if(_ID_GEOMETRY == -1)
        {
            alert(_MSG_SELECT_OBJECT);
            return false;
        }
		
        _WIN = window.open(_UI_TEXT+"?type="+type+"&action=insert&x="+
            _LIST_OF_POINTS[0]._x+"&y="+_LIST_OF_POINTS[0]._y+"&id_geometry="+_ID_GEOMETRY,
        "","width="+get_width(0.30)+",height="+get_height(0.40)+
            ",scrollbars=yes,status=yes,resizable=yes");
        _WIN.focus();
		
    }catch(e)
    {
        alert("Evt_Select_Hydraulic_Node.mouse_up(): "+e);
        loading_message(false);
        return false;
    }

}
function clear_points()
{
    for(var i=0; i< _LIST_OF_POINTS.length;i++)
        remove_from_extra_graphics(_LIST_OF_POINTS[i]);
    _LIST_OF_POINTS.splice(0,_LIST_OF_POINTS.length);
}
function clear_linepath()
{
    if(_LINE_PATH != null)
    {
        remove_from_extra_graphics(_LINE_PATH);
        _LINE_PATH.clear();
        register_key_enter_event_listenter(null);
        _LINE_PATH = null;
        draw_extra_graphics(false);
    }
}
function clear_polygon()
{
    if(_POLYGON != null)
    {
        remove_from_extra_graphics(_POLYGON);
        _POLYGON.clear();
        register_key_enter_event_listenter(null);
        _POLYGON = null;
        draw_extra_graphics(false);
    }
}
function clear_polar_figure()
{
    clear_points();	
    if(_POLAR_FIGURE == null)
        return false;
    remove_from_extra_graphics(_POLAR_FIGURE);
    _POLAR_FIGURE.clear();
    _POLAR_FIGURE = null;
    draw_extra_graphics(false);	
}
function clear_lines() {
        for(var i=0; i< _LIST_OF_LINES.length;i++)
        remove_from_extra_graphics(_LIST_OF_LINES[i]);
        _LIST_OF_LINES.splice(0,_LIST_OF_LINES.length);
}
function clear_circles() {
        for(var i=0; i< _LIST_OF_CIRCLES.length;i++)
        remove_from_extra_graphics(_LIST_OF_CIRCLES[i]);
        _LIST_OF_CIRCLES.splice(0,_LIST_OF_CIRCLES.length);
}