var icon, map, gtraffic, gext, gdir, geocoder; var markers = new Array(); var tmarkers = new Array(); var tmark = 0; var omark = 0; var plines = new Array();
function gmap_load() {
    if (!pagem()) return;
    display('top_map_button',0)
    display('top_map_button2',1,1)
    if (!GBrowserIsCompatible()) { document.write('browser incompatible'); return; }
    int_places();
    icon = new GIcon;
    icon.iconSize = new GSize(34, 34);
    icon.iconAnchor = new GPoint(18, 17);
    map = new GMap2(document.getElementById('map'));
    gotoll(40, -100, 3);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.addControl(new ctext());
    map.addControl(new map_max());
    map.addControl(new printvc());
    map.addControl(new linkicon());
    map.addControl(new GOverviewMapControl(new GSize(92,92)));
    geocoder = new GClientGeocoder();
    gtraffic = new GTrafficOverlay();
    gtraffic.hide();
    map.addOverlay(gtraffic);
    gdir = new GDirections(map, gE('extra'));
    GEvent.addListener(gdir, "load", gdir_load);
    GEvent.addListener(gdir, "error", gdir_error);

    var gmovi = gE('map_overview').firstChild.firstChild;
    gmovi.style.left = '0px'; gmovi.style.top = '0px'; gmovi.style.width = '100%'; gmovi.style.height = '100%'; gmovi.style.border = '0px';
    gE('map_overview').style.border = '1px solid #000';
    gE('map_overview').firstChild.style.border = '0px'; 

}
function gdir_error() {
    var gdc = gdir.getStatus().code;
    var gdm;
    if (gdc == G_GEO_UNKNOWN_ADDRESS) gdm = 'Atleast one of the provided addresses could not be located, either do to it being incorrect or too new to be in the database.';
    else if (gdc == G_GEO_SERVER_ERROR) gdm = 'For some reason this request could not be processed, sorry!';
    else if (gdc == G_GEO_MISSING_QUERY) gdm = 'Missing input in directions request.';
    else if (gdc == G_GEO_BAD_REQUEST) gdm = 'Bad request, couldn\'t parse.';
    else gdc = 'Uknown error occured.';
    gE('extra').innerHTML = gdm;
}
function gdir_load() {
    display('extra', 1);
    markers_done();
}
function gotoll(lat, lng, zoom, type) { map.setCenter(new GLatLng(lat, lng), zoom || map.getZoom()); if(type=='sat')map.setMapType(G_SATELLITE_MAP);else if (type=='norm')map.setMapType(G_NORMAL_MAP);else if(type=='hyb')map.setMapType(G_HYBRID_MAP); }
function add_marker(lat, lng, itext, title, html, hover, large) {
    lat = parseFloat(lat); lng = parseFloat(lng);
    var ico = new GIcon(icon);
    if (itext) ico.image = '/images/icon.pl?txt='+itext;
    if (!itext) ico.image = '/images/star.gif';
    var marker = new GMarker(new GLatLng(lat, lng), { icon: ico, draggable: false, clickable: true });
    marker.infobox(title, html);
    marker.ibox.large = large; marker.ibox.hover = hover;
    if (large && html) GEvent.addListener(marker, "click", function() { extra_box(title, large == 1 ? html : large); display('extra',1); });
    else if (html && hover != 3) GEvent.addListener(marker, "click", function() { marker.ibox.toggle(); });
    if (hover == 1) {
        GEvent.addListener(marker, "mouseover", function() { marker.ibox.tshow(); });
        GEvent.addListener(marker, "mouseout", function() { marker.ibox.thide(); });
    } else if (hover == 2) {
        GEvent.addListener(marker, "mouseover", function() { map.set_ctext(title); });
        GEvent.addListener(marker, "mouseout", function() { map.clear_ctext(); });
    } else if (hover == 3) {
        GEvent.addListener(marker, "mouseover", function() { map.set_ctext(html); });
        GEvent.addListener(marker, "mouseout", function() { map.clear_ctext(); });
    } else if (hover == 4) {
        GEvent.addListener(marker, "mouseover", function() { map.set_ctext(html); marker.ibox.tshow(); });
        GEvent.addListener(marker, "mouseout", function() { map.clear_ctext(); marker.ibox.thide(); });
    }
    tmarkers[tmarkers.length] = marker;
}
function add_marker2(lat, lng, title) {
    lat = parseFloat(lat); lng = parseFloat(lng);
    var ico = new GIcon(icon);
    ico.image = '/images/star.gif';
    var marker = new GMarker(new GLatLng(lat, lng), { icon: ico, draggable: false, clickable: true });
    marker.infobox(title,'');
    marker.ibox.hover = 1; marker.ibox.large = 1;
    GEvent.addListener(marker, "mouseover", function() { marker.ibox.tshow(); });
    GEvent.addListener(marker, "mouseout", function() { marker.ibox.thide(); });
    GEvent.addListener(marker, "click", function() { map.panTo(marker.getPoint()); });
    map.addOverlay(marker.ibox); map.addOverlay(marker);
}
function markers_done() {
    if (tmarkers.length < 1) { eloading(); return };
    omark = tmark;
    tmark = (tmarkers.length <= (tmark + 5) ? tmark = tmarkers.length : tmark + 5);
    for (var i = omark; i < tmark; i++) {
        var marker = tmarkers[i];
        if (!marker.ibox.large || (marker.ibox.large && (marker.ibox.hover == 1 || marker.ibox.hover == 4))) map.addOverlay(marker.ibox);
        map.addOverlay(marker);
        markers[markers.length] = marker;
    }
    if (tmark >= tmarkers.length) { tmarkers.length = 0; tmark = 0; omark = 0; lowzoom(); eloading(); return; }
    if (tmark < tmarkers.length) setTimeout('markers_done()', 1);
}
function polyline_done(a) {
    if (!a && plines.length > 0) a = 0;
    else if (plines.length == 0) return;
var gpoly = new GPolyline(plines[a]);
map.addOverlay(gpoly);
//    if (plines[a].length > 1) map.addOverlay(new GPolyline(plines[a]));
    if (a != (plines.length - 1)) setTimeout('polyline_done('+(a+1)+')', 1);
    else plines.length = 0;
}

