/* ----------------------------------
| $RCSfile: infrastructure.js,v $
| $Date: 2007/10/21 16:15:46 $
| $Revision: 1.9 $
 ---------------------------------- */

var ginfracons = [];
for (t=0; t<=infra_types.length; t++)
   ginfracons["gmap_infratype_"+infra_types[t]] = g_create_infra_icon(IMAGES_DIR, "gmap_infratype_"+infra_types[t]+".png");

// tmp infra icon
// infra_icon = g_create_icon(IMAGES_DIR, "gmap_info.png");
// cache
var infra_overlays = new Array();
// global var with currently selected region
var curr_sel_reg_id = -1;
// current infra switch state (on/off)
var infra_shown = false;
// handle for event handler (add infra overlay)
var ev_add_last_infra_handle;

function show_region_infra_on_map(infxmlresult) {
    g_objs = infxmlresult.getElementsByTagName("object");
    g_objs_num = g_objs.length;
    if (g_objs_num > 0) {
        ev_add_last_infra_handle = GEvent.addListener(map, "addoverlay", ev_add_last_infra);
    }
    for (i=0; i<g_objs_num; i++) {
        desc = "";
        obj_id = g_objs[i].getElementsByTagName("id").item(0).firstChild.data;
        lat = g_objs[i].getElementsByTagName("latitude").item(0).firstChild.data;
        lng = g_objs[i].getElementsByTagName("longitude").item(0).firstChild.data;
        infra_type = g_objs[i].getElementsByTagName("type").item(0).firstChild.data;
        if (g_objs[i].getElementsByTagName("name").item(0).firstChild)
            desc+= g_objs[i].getElementsByTagName("name").item(0).firstChild.data;
        if (g_objs[i].getElementsByTagName("address").item(0).firstChild)
            desc+= (desc != '' ? ', ' : '')+g_objs[i].getElementsByTagName("address").item(0).firstChild.data;        
        if (desc == "") {
            if (g_objs[i].getElementsByTagName("description").item(0).firstChild)
                desc+= g_objs[i].getElementsByTagName("description").item(0).firstChild.data;
            else
                desc+= g_objs[i].getElementsByTagName("infra_name").item(0).firstChild.data;
        }
        infra_marker = g_create_infra_marker(new GLatLng(lat, lng), obj_id, infra_type, desc);
        cache_infra_overlay(infra_marker, infra_type); // cache by region and infra type
    }    
    // display infrastructure according to "infra_types_show" var (browse.php)
    g_display_infrastructure();
}  

function g_display_infrastructure(infra_type) {
    // bail out if all infra types are shut off (checkbox)
    if (!infra_type && infra_types_show.length < 1) {
        setTimeout('hide_loading_notice_small()',10);
        return;
    }
    curr_sel_reg_id = get_current_region_id();
    // mark last overlay in cache as last (to remove loading notice)
    if (infra_type) last_type_in_cache = infra_type;
    else last_type_in_cache = infra_types_show[infra_types_show.length - 1];
    infra_overlays[curr_sel_reg_id][last_type_in_cache][infra_overlays[curr_sel_reg_id][last_type_in_cache].length-1].cm_is_last = true;
    // add event listener on last overlay add
    ev_add_last_infra_handle = GEvent.addListener(map, "addoverlay", ev_add_last_infra);
    if (infra_type) {
        for (i=0; i<infra_overlays[curr_sel_reg_id][infra_type].length; i++) {
            map.addOverlay(infra_overlays[curr_sel_reg_id][infra_type][i]);    
        }          
    } else {
        // itterate over "infra_types_show" array to see which types are enabled for display
        for (k=0; k<infra_types_show.length; k++) {
            for (i=0; i<infra_overlays[curr_sel_reg_id][infra_types_show[k]].length; i++) {
                map.addOverlay(infra_overlays[curr_sel_reg_id][infra_types_show[k]][i]);    
            }          
        }
    }
}

// removes infrastructure from map by type
function g_remove_infra_by_type(infra_type) {
    for (i=0; i<infra_overlays[curr_sel_reg_id][infra_type].length; i++) {
        map.removeOverlay(infra_overlays[curr_sel_reg_id][infra_type][i]);    
    }    
}

// activated by chedckbox on bottom of map
function g_toggle_infra_type(chck_elem, infra_type) {
    if (chck_elem.checked) {
        g_display_infrastructure(infra_type);
        infra_types_show.push(infra_type);
    } else {
        g_remove_infra_by_type(infra_type);
        remove_show_infra_type_from_arr(infra_type);
    }
}

function remove_show_infra_type_from_arr(infra_type) {
    for (i=0; i<infra_types_show.length; i++) {
        if (infra_types_show[i] == infra_type)
            infra_types_show.splice(i, 1);
    }
}

function get_current_region_id() {
    return(frm.elements["g_object[region_id]"].options[frm.elements["g_object[region_id]"].selectedIndex].value);    
}

function load_region_infrastructure() {
    var aif = new AJAXInteraction(WEB_ROOT_RELATIVE+"ajax/sinfra.php?lang_code="+
									frm.elements["g_app[lang_code]"].value+
									"&region_id="+curr_sel_reg_id+"&infra_gis_version="+INFRA_GIS_VERSION,
                                  show_region_infra_on_map);
    aif.doGet();    
}

// displays all scheduled types on map
function show_infra_overlays() {
    curr_sel_reg_id = get_current_region_id();
    if (infra_overlays[curr_sel_reg_id]) {
        if (infra_overlays[curr_sel_reg_id].length > 0) {
            g_display_infrastructure(); // display infra scheduled for display (infra_types_show var)  
        } else load_region_infrastructure(); // first display, go on server
    } else load_region_infrastructure(); // first display, go on server
    infra_shown = true;
}


function ev_add_last_infra(infra_overlay) {
    if (infra_overlay.cm_is_last) {
        setTimeout('hide_loading_notice_small()',10);
        infra_overlay.cm_is_last = false;
        GEvent.removeListener(ev_add_last_infra_handle);   
    }
}

// removes all types from map
function remove_infra_overlays() {
    if (infra_overlays[curr_sel_reg_id]) {
        for (k=0; k<infra_types_show.length; k++) {
            for (i=0; i<infra_overlays[curr_sel_reg_id][infra_types_show[k]].length; i++) {
                map.removeOverlay(infra_overlays[curr_sel_reg_id][infra_types_show[k]][i]);    
            }          
        }        
    }
    infra_shown = false;
}

function cache_infra_overlay(marker, infra_type) {
    if (!infra_overlays[curr_sel_reg_id])
        infra_overlays[curr_sel_reg_id] = new Array();
    if (!infra_overlays[curr_sel_reg_id][infra_type])
        infra_overlays[curr_sel_reg_id][infra_type] = new Array();
    infra_overlays[curr_sel_reg_id][infra_type][infra_overlays[curr_sel_reg_id][infra_type].length] = marker;
}

// activated by "Infrastructure button"
function toggle_infrastructure() {
    if (!infra_shown) {
        g_el("MAP_INFRA_BTTN").className = "activated";
        g_el("g_map_infra_tool").style.display = "";
        show_loading_notice("small", "infrastructure");
        setTimeout('show_infra_overlays()',1);
        self.scrollTo(0, 220); // make infra types checkboxes visible
    } else { 
        remove_infra_overlays();   
        g_el("MAP_INFRA_BTTN").className = "";
        g_el("g_map_infra_tool").style.display = "none";
    }
}

function g_create_infra_marker(point, obj_id, infra_type, desc) {
  var marker = new GMarker(point, ginfracons["gmap_infratype_"+infra_type]);
  marker.tooltip = "<div class='obj_info_tooltip'>"+desc+"</div>";
  GEvent.addListener(marker, "mouseover", function() {
    g_show_tool_tip(marker);
  });
  GEvent.addListener(marker, "mouseout", function() {
    g_hide_tool_tip(marker);
  });    
  return marker;
}

function g_create_infra_icon(img_path, icon_image) {
    var iicon = new GIcon();
    iicon.image = img_path + icon_image;
    iicon.iconSize = new GSize(16, 16);
    iicon.iconAnchor = new GPoint(8, 16);
    return iicon;
}

function infra_handle_region_change() {
    remove_infra_overlays();
    g_el("g_map_infra_tool").style.display = "none";
}

// Custom map type and infrastructure control
function InfraStructureControl() {}

function g_create_map_type_control() {
    InfraStructureControl.prototype = new GControl();
    InfraStructureControl.prototype.initialize = function (map) {
        container = g_el("customMapTypeCtrl");
        map.getContainer().appendChild(container);
        g_activate_map_type_bttn();
        container.style.display = "";
        return container;
    }
    InfraStructureControl.prototype.getDefaultPosition = function () {
        return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
    }
    return new InfraStructureControl();
}

function g_activate_map_type_bttn() {
    cached_map_type = g_readCookie("g_app[map_type]");
    g_el("MT_MAP_BTTN").className = "";
    g_el("MT_HYB_BTTN").className = "";
    g_el("MT_SAT_BTTN").className = "";
	switch (cached_map_type) {
	    case("G_NORMAL_MAP"):
		    g_el("MT_MAP_BTTN").className = "activated";
			break;
        case("G_HYBRID_MAP"):
            g_el("MT_HYB_BTTN").className = "activated";
            break;
        case("G_SATELLITE_MAP"):
            g_el("MT_SAT_BTTN").className = "activated";
			break;
		default: 
		    g_el("MT_MAP_BTTN").className = "activated";
	}                
}

/* This function is obsolete now, infrastructure is allways shown */
function ev_handle_map_zoom_infra(oldLevel, newLevel) {
    infra_btn = g_el("infrastructure_ctrl");
    if (newLevel >= 13) {
        infra_btn.style.display = "";
    } else {
        if (infra_btn.style.display != "none")
            remove_infra_overlays();
            infra_btn.style.display = "none";
            g_el("g_map_infra_tool").style.display = "none";
    }
}

// called from control
function g_set_map_type(type) {
    map.setMapType(type);
    g_activate_map_type_bttn();
}

function g_activate_bg(td_elem) {
    td_elem.className+= "_hover";   
}

function g_deactivate_bg(td_elem) {
    td_elem.className = td_elem.className.replace(/_hover/,"");   
}