﻿var map = null;
var geocoder = null;
//var point = [];
//var point2= [];
var markerColours = new Array ("red","yellow","green","blue","purple","pink","turquoise","orange","white","brown","aqua");
var marker = [];
var markerCounter = 0;
var bounds = new GLatLngBounds();
var sidebar;

function loadGMap(pageID) {

    if (GBrowserIsCompatible()) {

        if (document.getElementById("RouteMapDisplay") != null) {

            map = new GMap2(document.getElementById("RouteMapDisplay"));
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(new GLatLng(0, 0), 3);
            sidebar = "";
            
            getRoutes()
            
//          var mgr = new MarkerManager(map);

//          for (markerCounter; markerCounter < 3; ++markerCounter) {

//              var lat = 31.578535 - markerCounter;
//              var lng = -8.041992 + markerCounter;
//              var html = "<b>Node " + markerCounter + "</b><br/><a href='?id=1'>link</a>"
//              createMarker('location' + (markerCounter + 1), point, markerCounter, lat, lng, html)
//            }

//            var polyline = new GPolyline(point, "#ff0000", 3);
//            map.addOverlay(polyline);

//            for (markerCounter; markerCounter < 6; ++markerCounter) {

//                var lat = 27.137368 + markerCounter;
//                var lng = 2.548828 - markerCounter;
//                var html = "<b>Node " + markerCounter + "</b><br/><a href='?id=1'>link</a>"
//                createMarker('location' + (markerCounter + 1), point2, markerCounter, lat, lng, html)
//            }
//            
//            var polyline2 = new GPolyline(point2, "#ffff00", 3);
//            map.addOverlay(polyline2);
//                  
            var bounds = new GLatLngBounds();
            for (var i = 0; i < marker.length; i++) {
                bounds.extend(marker[i].getLatLng()); 
            }
            
            map.setZoom(map.getBoundsZoomLevel(bounds));
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
 
            //alert(bounds)

            //map.setZoom(map.getBoundsZoomLevel(polyline.getBounds()));
            //map.setCenter(polyline.getBounds().getCenter());
                                 
            //showAddress("peru")
            //ecuador -1.831239,-78.183406
            //peru -9.189967,-75.015152
            //chile -35.675147,-71.542969
            //venezuela 6.42375, -66.58973
            //colombia 4.570868, -74.297333
            //Shanghai, China 31.2243531,121.4759159
            //6 Bale close 51.5560635, -1.8465067  
            //31.578535, -8.041992 marakesh
            //27.137368, 2.548828 algreia
            //30.050077, 31.223145 Cairo
        }
        
    }
}

function createMarker(name, current_point, index, Lat, Long, html, newRoute, colour) {

    var markerColour = colour;
    if (colour == ''){markerColour = 'red';}
    current_point[index] = new GLatLng(Lat, Long);
    marker[index] = new GMarker(current_point[index]);
    map.addOverlay(marker[index]);
    
    marker[index].setImage("css/images/marker_" + markerColour + ".png");
    if (index == 0){marker[index].setImage("css/images/marker_start.png");}
     
    if (html != ''){
        GEvent.addListener(marker[index], "click", function() {
            marker[index].openInfoWindowHtml(name + "<br />" + html);
            GEvent.trigger(marker[index], "mouseout");
        });
    }

    GEvent.addListener(marker[index], 'mouseover', function() {
        marker[index].setImage("css/images/marker_on.png");
        document.getElementById("RouteMapSideBar").getElementsByTagName("div")[index].style.background = "#035003";
        document.getElementById("RouteMapSideBar").getElementsByTagName("div")[index].style.color = "white";
    });

    GEvent.addListener(marker[index], 'mouseout', function() {
    marker[index].setImage("css/images/marker_" + markerColour + ".png");
    if (index == 0){marker[index].setImage("css/images/marker_start.png");}
    document.getElementById("RouteMapSideBar").getElementsByTagName("div")[index].style.background = "white";
        document.getElementById("RouteMapSideBar").getElementsByTagName("div")[index].style.color = "black";
    });

    GEvent.addListener(marker[index], "infowindowclose", function() {
        //map.removeOverlay(marker);
        //ZMarker(point, label, count(), 4, i, marker[i].visited);
    })

    //Now add Sidebar link
    createSideBarLink(name, index, newRoute)

}

function createSideBarLink(text, index, newRoute) {

    if (newRoute != ''){
        sidebar += "<h3>" + newRoute + "</h3>";
        document.getElementById("RouteMapSideBar").innerHTML = sidebar;
    }

    sidebar += "<div class='sidebar'";
    sidebar += "onclick='GEvent.trigger(marker[" + index + "],\"click\")' ";
    sidebar += "onmouseover='GEvent.trigger(marker[" + index + "],\"mouseover\")' ";
    sidebar += "onmouseout='GEvent.trigger(marker[" + index + "],\"mouseout\")' ";
    //sidebar += "><img src='css/images/marker_on.png' height='15px' valign='middle'>&nbsp;";
    sidebar += ">";
    sidebar += text;
    sidebar += "</div>";
    document.getElementById("RouteMapSideBar").innerHTML = sidebar;
}

function loadHomePageMap() {

    if (GBrowserIsCompatible()) {

        if (document.getElementById("PageSummary_MapDisplay") != null) {

            map = new GMap2(document.getElementById("PageSummary_MapDisplay"));
            map.disableScrollWheelZoom();
            map.disableDoubleClickZoom();
            //map.addControl(new GLargeMapControl());
            //map.addControl(new GMapTypeControl());
            map.setCenter(new GLatLng(0, 0), 3);

            getRoutes()

            //            var bounds = new GLatLngBounds();
            //            for (var i = 0; i < marker.length; i++) {
            //                bounds.extend(marker[i].getLatLng()); 
            //            }
            //            
            //            map.setZoom(map.getBoundsZoomLevel(bounds));
            //            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

        }

    }
}

function createRightNowMarkers(current_point, index, Lat, Long, html, type) {

    current_point[index] = new GLatLng(Lat, Long);
    marker[index] = new GMarker(current_point[index]);
    map.addOverlay(marker[index]);
    if (type == 'MostRecentPoint') {
        marker[index].setImage("css/images/marker_red.png");
        if (html != '') {
            GEvent.addListener(marker[index], "mouseover", function() {
                marker[index].setImage("css/images/marker_yellow.png");
                marker[index].openInfoWindowHtml(html + "<br />Current time: <span id='timecontainer'><%= Now()%></span><script type='text/javascript'>new showLocalTime('timecontainer', 'server-asp', 0, 'short')</script>");
                GEvent.trigger(marker[index], "mouseout");
            });

            GEvent.addListener(marker[index], 'mouseout', function() {
                marker[index].setImage("css/images/marker_red.png");
                //document.getElementById("RouteMapSideBar").getElementsByTagName("span")[index].style.background = "yellow";
            });
        }

    }
    else if (type == 'UpComingPoint') {
    //alert(index)
        if (index == 0){marker[index].setImage("css/images/marker_start.png");}

            GEvent.addListener(marker[index], "click", function() {
            marker[index].openInfoWindowHtml(html);
            GEvent.trigger(marker[index], "mouseout");
        });
        
            GEvent.addListener(marker[index], 'mouseover', function() {
            marker[index].setImage("css/images/marker_on.png");
            //document.getElementById("RouteMapSideBar").getElementsByTagName("span")[index].style.background = "turquoise";
        });

            GEvent.addListener(marker[index], 'mouseout', function() {
            marker[index].setImage("css/images/marker_red.png");
            if (index == 0){marker[index].setImage("css/images/marker_start.png");}
            //document.getElementById("RouteMapSideBar").getElementsByTagName("span")[index].style.background = "yellow";
        });
        
    } else {
    
        marker[index].setImage("css/images/Marker_grey.png");
    }

    //    GEvent.addListener(marker[index], 'mouseover', function() {
    //        marker[index].setImage("css/images/Marker_Turquoise.png");
    //        //document.getElementById("RouteMapSideBar").getElementsByTagName("span")[index].style.background = "turquoise";
    //    });

    //    GEvent.addListener(marker[index], 'mouseout', function() {
    //        marker[index].setImage("css/images/Marker_Yellow.png");
    //        //document.getElementById("RouteMapSideBar").getElementsByTagName("span")[index].style.background = "yellow";
    //    });

    GEvent.addListener(marker[index], "infowindowclose", function() {
        //map.removeOverlay(marker);
        //ZMarker(point, label, count(), 4, i, marker[i].visited);
    })
}








//function ZMarker(point, label, n, imInd, i, visited) {
//    function sendBack(marker, b) {
//        return GOverlay.getZIndex(marker.getPoint().lat()) - n * 10000;
//    }
//    marker[i] = new GMarker(point, { title: label, zIndexProcess: sendBack });
//    map.addOverlay(marker[i]);
//    marker[i].setImage(markerImage[imInd]);
//    marker[i].visited = visited;

//    GEvent.addListener(marker[i], "click", function() {
//        marker[i].openInfoWindowHtml(label);
//        marker[i].visited = true;
//        GEvent.trigger(marker[i], "mouseout");
//    });
//    GEvent.addListener(marker[i], 'mouseover', function() {
//        marker[i].setImage(markerImage[3]);
//        document.getElementById("sidebar").getElementsByTagName("span")[i].style.background = "yellow";
//    });
//    GEvent.addListener(marker[i], 'mouseout', function() {
//        if (marker[i].visited) {
//            marker[i].setImage(markerImage[4]);
//            document.getElementById("sidebar").getElementsByTagName("span")[i].style.color = "gray";

//        } else {
//            marker[i].setImage(markerImage[0]);
//            document.getElementById("sidebar").getElementsByTagName("span")[i].style.color = "black";
//        }
//        document.getElementById("sidebar").getElementsByTagName("span")[i].style.background = "white";
//    });
//    GEvent.addListener(marker[i], "infowindowclose", function() {
//        map.removeOverlay(marker[i]);
//        ZMarker(point, label, count(), 4, i, marker[i].visited);
//    })
//}

function showAddress(address) {
    
    var geocoder = new GClientGeocoder();
    if (geocoder) {
        geocoder.getLatLng(address,
        function(point) {
            if (!point) {
                alert(address + " not found");
            } else {
                map.setCenter(point, 2);
                var marker = new GMarker(point);
                alert(point.x + ',' + point.y)
                map.addOverlay(marker);
                marker.openInfoWindowHtml(address);
            }
        });
    } else { alert("no geocoder")}
}


//<![CDATA[
var mmap, exml;
function initMap(PageID) {
    mmap = new GMap2(document.getElementById("GMap" + PageID), { draggableCursor: 'hand', draggingCursor: 'move' });
    //mmap.addMapType(G_PHYSICAL_MAP);
    mmap.addMapType(G_SATELLITE_3D_MAP);
    mmap.setCenter(new GLatLng(20, 0), 2);
    //mmap.addControl(new GLargeMapControl());
    //mmap.addControl(new GMapTypeControl());
    //mmap.addControl(new GSmallMapControl());
    var mt = mmap.getMapTypes();
    mmap.setMapType(mt[1]);
    mmap.disableScrollWheelZoom();
    mmap.disableDoubleClickZoom();
    mmap.enableContinuousZoom();
    exml = new GeoXml("exml", mmap, "test.xml", { iwwidth: 250, sidebarid: "the_side_bar", sortbyname: true, nozoom: true, polylabeloffset: new GSize(-40, 0), polylabelopacity: 50, polylabelclass: "style1", domouseover: true, allfoldersopen: true, messagebox: document.getElementById('messagearea') });
    exml.parse();
    GEvent.addDomListener(mmap.getContainer(), "DOMMouseScroll", wheelblock);
    mmap.getContainer().onmousewheel = wheelblock;
}

function wheelblock(e) {
    if (!e) { e = window.event }
    if (e.preventDefault) { e.preventDefault() }
    e.returnValue = false;
}

//]]>