var markers = [];
var map = null;
var polyline = null;
var contextmenu;
function load() {
    try {
        document.getElementById('map-dsply').style.width = screen.width - 35;
        document.getElementById('map-dsply').style.height = (screen.height / 2) - 75;
        map = new GMap2(document.getElementById("map-dsply"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GOverviewMapControl());
        //To enable the Mouse wheel Scroll
        map.enableScrollWheelZoom();
        map.setCenter(new GLatLng(23.805450, 78.486328), 4);
    }
    catch (Error) {
        alert('Please check your Internet Connection. Map cannot be loaded');
    }
}
var lt;
var count = '';
//To get the array of LatLong values
var collectdata = null;
var lntlngs = new Array();
var geocoder;
var address = new Array();
var disArray = new Array();
var googleResults;
var lastvalue = null;
var positioncurrent = null;
function DisplayLocation(latlng) {
    map = new GMap2(document.getElementById("map-dsply"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());
    //To enable the Mouse wheel Scroll
    map.enableScrollWheelZoom();
    map.setCenter(new GLatLng(23.805450, 78.486328), 4);
    lt = new Array();
    var clusterer = new Clusterer(map);
    var icon1 = G_START_ICON;
    clusterer.SetIcon(icon1);

    googleResults = new Array();
    lntlngs = new Array();
    collectdata = new GLatLngBounds();
    if (GBrowserIsCompatible()) {
        if (latlng.indexOf('&*') > 0) {
            var currentpos = latlng.split('&*');
            lt = currentpos[0].split('&');
            positioncurrent = currentpos[1];
        }
        else {
            lt = latlng.split('&');
            lt.length = lt.length - 1;
        }
        for (var i = 0; i < lt.length; i++) {
            var lng = new Array();
            lng = lt[i].split('#');
            if (i == lt.length - 1) {
                lastvalue = new GLatLng(lng[4], lng[5]);
            }
            lntlngs.push(new GLatLng(lng[4], lng[5]));
            collectdata.extend(new GLatLng(lng[4], lng[5]));
        }
        //create our new class
        var work = new threadedLoop(lntlngs);
        //create the action to compare each item with
        work.action = function(item, index, total) {
            var lng = new Array();
            if (index <= total) {
                lng = lt[index - 1].split('#');
                clusterer.AddMarker(createMarker(lntlngs[index - 1], lng[0], lng[1], lng[2], lng[3], lng[4], lng[5]));
            }
        };
        //another action to use when our loop is done
        work.finish = function(thread) {
        };
        work.start();
        map.setCenter(collectdata.getCenter(), map.getBoundsZoomLevel(collectdata) - 1);
    }
}

function createMarker(point, displayName, userName, strAddress, gender, latitude, Longitude) {
    var icon1 = G_START_ICON;
    var icon2 = G_END_ICON;
    var icon = new GIcon();
    icon.iconSize = new GSize(20, 20);
    icon.image = '../images/icn-map-it.gif';
    icon.shadowSize = new GSize(1, 1);
    icon.iconAnchor = new GPoint(12, 12);
    icon.infoWindowAnchor = new GPoint(12, 5);
    var marker = new GMarker(point, { draggable: false, icon: icon });
    GEvent.addListener(marker, "mouseover", function() { marker.openInfoWindowHtml('<table cellpadding=5 cellspacing=5><tr><td colspan="2"><u><b>Employee Details</b></u></td></tr><tr><td width="25px">' + '<b>DisplayName</b></td><td>' + displayName + '</td></tr>' + '<tr><td><b>UserName</b></td><td>' + userName + '</td></tr>' + '<tr><td><b>Address</b></td><td>' + strAddress + '</td></tr>' + '<tr><td width="40px"><b>Gender</b></td><td>' + gender + '</td></tr></table>'); });
    markers.push(marker);
    return marker;
}


//------------------------------Get Directions page---------------------------------------------------------//


var map;
var gdir;
var geocoder = null;
var addressMarker;
function loadMap() {
    mapWidth = screen.width - 35;
    document.getElementById('map-dsply').style.height = (screen.height) / 1.75;
    document.getElementById('search-box').style.height = (screen.height) / 1.75;
    map = new GMap2(document.getElementById('map-dsply'));
    map.setCenter(new GLatLng(23.805450, 78.486328), 4);
    map.enableScrollWheelZoom();
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());

    // define minimum and maximum zoom levels
    G_NORMAL_MAP.getMinimumResolution = function() { return 0; }
    G_NORMAL_MAP.getMaximumResolution = function() { return 19; }

    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
    document.getElementById('btnGetDirections').click();    
}
var toaddr = 0;
var fromaddr = 0;
function setDirections(toAddress, locale) {
    //Via points
    var latarray = new Array();
    map.clearOverlays();
    document.getElementById("directions").innerHTML = '';
    toAddress = toAddress.substring(0, toAddress.length - 1);
    if (toAddress.indexOf('#') > 0) {
        toaddr = toAddress.split('#');
    }
    if (toAddress.indexOf('#') > 0) {
        for (var i = 0; i < toaddr.length; i++) {

            gdir = new GDirections(map, document.getElementById("directions"));
            GEvent.addListener(gdir, "load", onGDirectionsLoad);

            latarray.push(toaddr[i]);
        }
        gdir.loadFromWaypoints(latarray);
    }
}
function handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);


    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

    else alert("An unknown error occurred.");

}
var dist;
function onGDirectionsLoad() {  
}

function GetDistance(distance) {    
}
        
        
       