var _MAP_LOADED = 0;
function change_ti_sentieri_selected_tool(active){
    document.getElementById("pan").className="";
    document.getElementById("zoomin").className="";
    document.getElementById("zoomout").className="";
    document.getElementById("fullextent").className="";
    document.getElementById("select_segment").className="";
    document.getElementById("profile").className="";
    document.getElementById("start_finish_point").className="";
    //document.getElementById("measure").className="";
    
    if(active){
        active.className = "tool_selected";
    }
}
function evt_select_segment(event){
    process_mouse_down(event,new Evt_Select_Segment());
}
function clear_composition(){
    try{
        var params="type=segment&action=clear";
        loading_message(true);
        process_ajax_call( params,_CNTR_COMPOSITION,
            function(error){
                if(!error)
                {
                    reset_extra_graphics();
                    reload_map("type=map&action=load");
                    return true;
                }
                else
                {
                    loading_message(false);
                    return false;
                }
            } );
    }catch(e)
    {
        alert("TI_Sentieri_Events_Manager.clear_composition(): "+e);
        return false;
    }
}
function evt_but_create_profile(){
    try{
        remove_all_events();
        if(_LINE_PATH == null){
            alert(_MSG_SELECT_SEGMENT);
            return false;
        }
        
        if(_LIST_OF_POINTS.length != 2){
            clear_points();
            calculate_auto_start_finish_points();
        }
        var cntr_url = _UI_PROFILE+"?type=porfile&action=create&x1="+_LIST_OF_POINTS[0]._x+"&y1="+_LIST_OF_POINTS[0]._y+
        "&x2="+_LIST_OF_POINTS[1]._x+"&y2="+_LIST_OF_POINTS[1]._y;
        var dhtml_profile = new DHTML_Profile(cntr_url, '100%', '100%');
    }catch(e)
    {
        alert("TI_Sentieri_Events_Manager.evt_but_create_profile(): "+e);
        return false;
    }
}
function evt_select_route_segments(id_route){
    try{
		
        var params="type=route&action=select_segments&id_route="+id_route;
        loading_message(true);
        process_ajax_call( params,_CNTR_COMPOSITION,
            function(error){
                if(!error)
                {
                    reset_extra_graphics();
                    if(  this.responseXML.normalize ){
                        this.responseXML.normalize();
                    }

                    _LINE_PATH = _MAP_WRAPPER.decode_linestring_from_xml(this.responseXML);
                    _LINE_PATH._color="#ffd202";
                    _LINE_PATH._stroke=2;
                    //Get the first point of the first line
                    var pxl = _MAP_WRAPPER.convert_to_pixel(_LINE_PATH._list_of_graphics[0]._p1._x,
                        _LINE_PATH._list_of_graphics[0]._p1._y,true);
                    _LIST_OF_EXTRA_GRAPHICS.push(_LINE_PATH);
                    loading_message(true);
                    reload_map("type=map&action=zoom_to_scale&x="+pxl[0]+"&y="+pxl[1]+
                        "&scale="+100000);
                    calculate_auto_start_finish_points();
                    loading_message(true);
                    return true;
                }
                else
                {
                    loading_message(false);
                    return false;
                }
            } );
		
    }catch(e)
    {
        alert("TI-Sentieri_Events_Manager.evt_select_route_segments(): "+e);
        return false;
    }
}

function evt_set_point(event)
{
    _LAYER_SELECTED="Segmenti";
    process_mouse_down(event,new Evt_Set_Point(true));
}

function evt_but_search(){
    try{
		
        var locality = document.getElementById("locality").value;
        if(locality == ""){
            alert(_MSG_SPECIFY_LOCALITY);
            return false;
        }
        
        var params="type=route&action=search_by_locality&locality="+encodeURIComponent(locality);
        var count=0;    
        loading_message(true);
        process_ajax_call( params,_CNTR_SEARCH,
            function(error){
                if(!error)
                {
                    var container = document.createElement("div");
                    container.id = 'result';

                    clear_node(document.getElementById("result_cell"));
                    //container.onclick=evt_select_route;
                    if (container.addEventListener){
                        container.addEventListener("mouseout", hide_details, false);
                    } else if (container.attachEvent){
                        container.attachEvent("onmouseout", hide_details);
                    }
                    var p = document.createElement("p");
                    p.className="titolo_paragrafo";
                    
                    /*
                    var select = document.createElement("select");
                    select.id="route_select";
                    select.size=10;
                    select.onclick=evt_select_route;
                    select.className="testo_normale";
                    select.style.width="240px";
                    container.appendChild(select);
                    */
                    if(  this.responseXML.normalize ){
                        this.responseXML.normalize();
                    }
                    var list_of_routes = this.responseXML.getElementsByTagName("route");
                    var custom_select = new Custom_Select(container);
                    for(var i=0; i< list_of_routes.length; i++){
                        var route = list_of_routes[i];
                        custom_select.add_option(route.getElementsByTagName("name")[0].firstChild.nodeValue,
                            route.getElementsByTagName("id")[0].firstChild.nodeValue);
                        
                        /*var option = document.createElement("option");
                        select.appendChild(option);
                        var id = route.getElementsByTagName("id")[0].firstChild.nodeValue;
                        option.value=id;
                        function show_details () {
                            //document.getElementById("details").style.visibility = 'visible';
                            document.getElementById("details").innerHTML = this.firstChild.nodeValue;
                        }
                        function hide_details () {
                            //document.getElementById("details").style.visibility = 'hidden';
                            document.getElementById("details").innerHTML = '';
                        }
                        if (option.addEventListener){
                            option.addEventListener("mouseover", show_details, false);
                        } else if (option.attachEvent){
                            option.attachEvent("onmouseover", show_details);
                        }
                        if (option.addEventListener){
                            option.addEventListener("mouseout", hide_details, false);
                        } else if (option.attachEvent){
                            option.attachEvent("onmouseout", hide_details);
                        }
                        var name = document.createTextNode("("+id+") "+route.getElementsByTagName("name")[0].firstChild.nodeValue);
                        option.appendChild(name);*/
                        count++;
                    }
                    custom_select.draw();
                    p.appendChild(document.createTextNode(count+" "+_MSG_ROUTES));
                                        document.getElementById("result_cell").appendChild(p);
                    document.getElementById("result_cell").appendChild(container);
                    loading_message(false);
                    return true;
                }
                else
                {
                    loading_message(false);
                    return false;
                }
            } );
		
    }catch(e)
    {
        alert("UI_Search.evt_but_search(): "+e);
        return false;
    }
}
function evt_key_but_search(event) {
    if(!event)
        event=window.event;
    if(event.keyCode != 13){
        return false;
    }

    evt_but_search();
    
}

//function evt_select_route(){
//    var select = document.getElementById("route_select");
//    for(var i=0; i<select.options.length;i++){
//        var opt = select.options[i];
//        if(opt.selected){
//            evt_select_route_segments(opt.value);
//            break;
//        }
//    }
//}

function calculate_auto_start_finish_points(){
    try{
        if(_LINE_PATH == null){
            alert(_MSG_SELECT_SEGMENT);
            return false;
        }
        var params="type=point&action=calculate_start_finish_points";
        process_ajax_sync_call( params,_CNTR_COMPOSITION,
            function(error){
                if(!error)
                {
                    clear_extra_graphics();
                    var p1 = this.responseXML.getElementsByTagName("start_point")[0];
                    var p2 = this.responseXML.getElementsByTagName("finish_point")[0];
                    var start_point = new Flag_Symbol(p1.getElementsByTagName("x")[0].firstChild.nodeValue,
                        p1.getElementsByTagName("y")[0].firstChild.nodeValue );
                    start_point._color="#00ff00";
                    var finish_point = new Flag_Symbol(p2.getElementsByTagName("x")[0].firstChild.nodeValue,
                        p2.getElementsByTagName("y")[0].firstChild.nodeValue );
                    finish_point._color="#ff0000";
                    _LIST_OF_POINTS.push(start_point);
                    _LIST_OF_EXTRA_GRAPHICS.push(start_point);
                    _LIST_OF_POINTS.push(finish_point);
                    _LIST_OF_EXTRA_GRAPHICS.push(finish_point);
                    draw_extra_graphics(false);
                    return true;
                }
                else
                {
                    return false;
                }
            } );
		
    }catch(e)
    {
        alert("TI_Sentieri_Events_Manager.calculate_auto_start_finish_points(): "+e);
        return false;
    }
}

function evt_but_print() {

    //configure selected segments
    if(_LINE_PATH != null){
        try{
            var params="type=report&action=prepare_map_report";

            if(_LIST_OF_POINTS.length == 2){
                params+="&start_x="+_LIST_OF_POINTS[0]._x+"&start_y="+_LIST_OF_POINTS[0]._y+
                "&finish_x="+_LIST_OF_POINTS[1]._x+"&finish_y="+_LIST_OF_POINTS[1]._y;
            }else if(_LIST_OF_POINTS.length == 1){
                params+="&start_x="+_LIST_OF_POINTS[0]._x+"&start_y="+_LIST_OF_POINTS[0]._y;
            }

            loading_message(true);
            process_ajax_call( params,_CNTR_REPORT,
                function(error){
                    if(!error)
                    {
                        loading_message(false);
                        gen_dhtml_profile();
                        
                        return true;
                    }
                    else
                    {
                        loading_message(false);
                        return false;
                    }
                } );

        }catch(e)
        {
            alert("TI_Sentieri_Events_Manager.evt_but_print(): "+e);
            loading_message(false);
            return false;
        }
        
    } else {
        gen_dhtml_profile();
    }

}
function gen_dhtml_profile()
{
    var min = _MAP_WRAPPER.convert_to_pixel(_MAP_WRAPPER._minx,_MAP_WRAPPER._miny);
    var max = _MAP_WRAPPER.convert_to_pixel(_MAP_WRAPPER._maxx,_MAP_WRAPPER._maxy);
    var cntr_url=_URL_PLANIMETRY_URL+"?&min_x="+min[0]+"&min_y="+min[1]+
    "&max_x="+max[0]+"&max_y="+max[1]+"&type=auto_planimetry";

    var dhtml_profile = new DHTML_Profile(cntr_url, '100%', '100%');
}
function ti_sentieri_startup() {
    var route = document.getElementById('route').value;

    _MAP_LOADED++;
    if (route != '' && !isNaN(route) && (_MAP_LOADED == 3))
    {
        evt_select_route_segments(route);
    }
}
