﻿
var map = null;
var geocoder = null;

var baseIcon = new GIcon(G_DEFAULT_ICON);
//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(30, 33);
//baseIcon.shadowSize = new GSize(34, 27);
//baseIcon.iconAnchor = new GPoint(5, 25);
//baseIcon.infoWindowAnchor = new GPoint(9, 2);
var baseIcon1 = new GIcon(G_DEFAULT_ICON);
//baseIcon1.shadow = "http://www.google.com/mapfiles/shadow50.png";

var logoStops;
var stopRealIds;




function clearMap(mapConstainerId) {
    var i = 0;
    for (i = 0; i < 2; i++) {
        var rights_stuff = document.getElementById(mapConstainerId).getElementsByTagName("span")[i];
        if (typeof (rights_stuff) == "undefined") {
        }
        else {
            rights_stuff.style.display = "none";
        }
    }
}

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GLargeMapControl());
        //               map.addControl(new GOverviewMapControl());
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();

        //map.setCenter(new GLatLng(39.164141, -97.382812), 4);
        geocoder = new GClientGeocoder();

     

    }
}


function initializeSmallMap() {
    if (GBrowserIsCompatible()) {
       map = new GMap2(document.getElementById("map_canvas"));
       map.disableDragging();
       map.disableDoubleClickZoom();
       
        geocoder = new GClientGeocoder();

      //  GEvent.addListener(map, "addoverlay", OnLoad);

    }
}


function OnLoad() {
    clearMap("map_canvas");
}

function showMapByLatLong(lat, ln) {
   map.setCenter(new GLatLng(lat, ln), 14);
   if (bAddMarker) {
       var image = isIE6() ? "images/routenumbers/withoutlogo/1.gif" : "images/routenumbers/withoutlogo/1.png";
       var markerOverlay = createMarker(new GLatLng(lat, ln), "", image, 0, 66, -1, 46, 50, 0, 0);
      map.addOverlay(markerOverlay);
   }

}

function showSmallMapByLatLong(lat, ln, bLogo, iIndex) {
   map.setCenter(new GLatLng(lat, ln), 14);
   var image = null;
   var imageName = isIE6() ? iIndex + ".gif" : iIndex + ".png";

   // default for withoutlogo
   var iHeight = 50;
   var iWidth = 46;
   
   if (bLogo) {
      image = isIE6() ? "images/routenumbers/withlogo/" + imageName : "images/routenumbers/withlogo/" + imageName;
      iHeight = 61;
      iWidth = 58;
   }
   else
      image = isIE6() ? "images/routenumbers/withoutlogo/" + imageName : "images/routenumbers/withoutlogo/" + imageName;

   var markerOverlay = createMarker(new GLatLng(lat, ln), "", image, 0, 66, -1, iWidth, iHeight, 0, 0);
   map.addOverlay(markerOverlay);
}

function showMapByLatLongNoMaker(lat, ln) {
    map.setCenter(new GLatLng(lat, ln), 14);
}

function setLogos(logos) {
    logoStops = logos;
}

function setStopRealIds(stopIds) {

    stopRealIds = stopIds.split('|');
}

function showMap(corrdinates) {

    showMap2(corrdinates, false);
}


function showMap2(corrdinates,isRouteView) {
    //alert(corrdinates);

    map.clearOverlays();
    var pointsArr = new Array();

    //var data = "-83.5101638,35.7142590|-81.0945805,32.0724856";
    var data = corrdinates;
    var values = corrdinates.split('|');

    //    alert(values.length);

    if (values.length > 1) {
        var i = 0;
        var j = 65;
        for (i = 0; i < values.length; i++) {
            var lnglat = values[i].split(',');
            var lat = lnglat[1];
            var lng = lnglat[0];

            pointsArr.push(lat + "," + lng);
        }

        directions = new GDirections(map);
        GEvent.addListener(directions, "error", handleErrors);
        GEvent.addListener(directions, "addoverlay", onGDirectionsLoad);
        //GEvent.addListener(directions, "load", onGDirectionsLoad);
        directions.loadFromWaypoints(pointsArr);
    }

    else if (values.length == 1) {
        var lnglat = values[0].split(',');
        var lat = parseFloat(lnglat[1]);
        var lng = parseFloat(lnglat[0]);

        var imagelogo = isIE6() ? "images/routenumbers/withoutlogo/1.gif" : "images/routenumbers/withoutlogo/1.png";
        var imagechat = isIE6() ? "images/routeimg/chatbubbleselectday.gif" : "images/routeimg/chatbubbleselectday.png";

        var point = null;
        /*
        if (!isRouteView) {
           point = new GLatLng(lat + parseFloat(7 / (11 * 69.1)), // 7/11 of a mile of latitude
                  lng - parseFloat(1 / (2 * 53)));   // 1/2 of a mile of longitude
        }
        else
        */
            point = new GLatLng(lat, lng);

        var markerOverlay = isRouteView ? createMarker(point, "", imagelogo, 1, j - 66, -1, 30, 33, 0, 0) : createMarker(point, "", imagechat, 0, j - 66, -1, 222, 152, 111, 160);
       
        map.addOverlay(markerOverlay);
        //map.addOverlay(new GMarker(point));
        showMapByLatLongNoMaker(lat, lng);

    }
    else {
        //alert("less 1");

    }
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, text, iconImg, t, n, id, width, height, offsetx, offsety) {
   size = new GSize(width, height);
   
    //    // Create a lettered icon for this point using our icon class
    if (t == 0) var letteredIcon = new GIcon(baseIcon);
    else var letteredIcon = new GIcon(baseIcon1);

    letteredIcon.image = iconImg;
    letteredIcon.iconSize = size;
    if (offsetx != 0 && offsety != 0)
       letteredIcon.iconAnchor = new GPoint(offsetx, offsety);

    // Set up our GMarkerOptions object
    markerOptions = { icon: letteredIcon };
    var marker = new GMarker(point, markerOptions);
//    GEvent.addListener(marker, "click", function() {
//        //tpoint=marker.getLatLng();
//        ppoint = marker.getLatLng();
//        if (id != -1) pid = id;
//        if (t == 0) marker.openInfoWindowHtml(text, infoWindowOptions);
//        if (t == 1) { if (n != -1) showinfo(n); }
//    });
    
    markerA = marker;
    return marker;
}


function handleErrors() {
    if (directions.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: " + directions.getStatus().code);
    else if (directions.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: " + directions.getStatus().code);

    else if (directions.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: " + directions.getStatus().code);

    else if (directions.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: " + directions.getStatus().code);

    else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);

    else alert("An unknown error occurred.");

}

function needLogo(index) {

    var logos = logoStops.split('|');

    var i;
    for (i = 0; i < logos.length; i++) {
        if (index == logos[i]) {
            return true;
        }
    }

    return false;
}

function haveReadIds(index) {

    if (stopRealIds.length <= index)
        return false;

    return true;
}

function getRealIds(index) {

    var ids = stopRealIds.split('|');
    return ids[index];
}

function onGDirectionsLoad() {

    var markerNum = directions.getNumGeocodes();
    var i;
   
    for (i = 0; i < markerNum; i++) {
        // Create our own marker icon
        var blueIcon = new GIcon(baseIcon);

        var imageType = isIE6() ? ".gif" : ".png";
        blueIcon.image = "images/routenumbers/withoutlogo/" + (i + 1) + imageType;

        if (needLogo(i)) {
            blueIcon.image = "images/routenumbers/withlogo/" + (i + 1) + imageType;
        }
        

        // Set up our GMarkerOptions object
        markerOptions = { icon: blueIcon };

        var mark = directions.getMarker(i);
        var newMarker = new GMarker(mark.getLatLng(), markerOptions);
        map.removeOverlay(mark);
        //alert(newMarker);

        if (haveReadIds(i)) {
            var id = stopRealIds[i];
            addMakerListener(newMarker, id, i + 1);
        }
        
        map.addOverlay(newMarker);


    }

    clearMap("map_canvas");

    //clearMap("map_canvas");
}

function addMakerListener(pointMarker, stopId, index) {
    GEvent.addListener(pointMarker, "click", function() {
        viewStop(stopId, index);
    });
}  



function EndRequestHandler(sender, args) {
    //            alert("Finish Request");

    //    var j = 65;
    //    var pointsArr = new Array();
    //    $(".spanCoor").each(function(i) {
    //        //alert($(this).html());

    //        var corr = $(this).html();
    //        var lnglat = corr.split(',');
    //        var lat = lnglat[1];
    //        var lng = lnglat[0];

    //        //                alert(lat);
    //        var point = new GLatLng(lat, lng);

    //        var markerOverlay = createMarker(point, "", "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_green" + String.fromCharCode(j++) + ".png", 1, j - 66, -1);
    //        map.addOverlay(markerOverlay);

    //        pointsArr.push(lat + "," + lng);

    //    });



    //    directions = new GDirections(map);

    //    directions.clear();
    //            GEvent.addListener(directions, "error", handleErrors);
    //            GEvent.addListener(directions, "addoverlay", onGDirectionsLoad);
    //            directions.loadFromWaypoints(pointsArr);
}
function load() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

