﻿var oDirection = null;
var _fastResults = false;
var _HideDid = false;
var _Routetype = "TRAVELTIME"
var MainPath = "http://maps.yellowpages.co.za/"
function SearchAddress() {
    ClearResults();
    AddressSearch.SearchAddress($("#txtSearch").val());

}
function SetStartLocation(oMap, oPoint) {
    $("#imgLoadAni").show();
    directions.clickDirections = true;
    var oCoordOrigin = g_map.getCoordFromVisibleArea(oPoint);
    _zoomLevel = g_map.getZoomLevel();
    
    _image = "images/0g.png";
    var _iconAnchor = new AGPoint(-15, -39);
    var myHTMLObj = new Image();
    myHTMLObj.setAttribute("id", "Fast0");
    myHTMLObj.style.zIndex = 1;
    myHTMLObj.setAttribute("src", _image);
    myHTMLObj.setAttribute("title", "0");
    var objectSize = new AGSize(30, 39);
    var opts = new AGHTMLOverlayOptions(_iconAnchor, objectSize);


    var htmlOverlay = new AGHTMLOverlay(myHTMLObj, oCoordOrigin, opts);

    g_map.addOverlay(htmlOverlay, "Flags");

    //url = "ashx/ReverseGeocode.ashx?Lat=" + oCoordOrigin.latitude() + "&Lon=" + oCoordOrigin.longitude() + "&Level=" + _zoomLevel;
  //  $.getJSON(url, function (json) {
       $("#imgLoadAni").hide();
        //{ "NewDataSet": {"ResultSet": {"Dataset": "AG_TOWNS", "Description": "City of Tshwane Rural,Gauteng", "Latitude": "-25.477471", "Longitude": "28.209047" } }}
        directions.AddDirectionFrom(oCoordOrigin.latitude(), oCoordOrigin.longitude(),"Selected from map");// json.Description);

        if (divLeftHidden == true) {
            HideShowDivLeft();
        }
        $("#Tags").hide();
        if (directions.aSelected == "Selected" && directions.bSelected == "Selected") {
            directions.GetDirections();
        }

    //});
}
function BackToSearch() {
    $("#DirectionSearch").hide();
    $("#DefaultSearchlink").hide();
    $("#drivingDirections").html("");
    $("#DidExpander").show();
    directions.ShowDirections = false;
   

    $("#DirectionSearch").hide();
    $("#DefaultSearchlink").hide();
    if (_HideDid) {
        $("#didYouMean").hide();
    }
    try {    
          $("#PointA").val("");
          $("#PointB").val("");
          $("#PointC").val("");
          $("#PointD").val("");
          $("#PointE").val("");
          $("#drivingDirections").html("");
          directions.aSelected = null;
          directions.bSelected = null;
          directions.cSelected = null;
          directions.dSelected = null;
          directions.eSelected = null;
          directions.aArrayItem = null;
          directions.bArrayItem = null;
          directions.cArrayItem = null;
          directions.dArrayItem = null;
          directions.eArrayItem = null;
          directions.dirReady = null;
          g_map.removeGroupOverlay("GreenLine");
          g_map.removeGroupOverlay("Flags");
          g_map.removeGroupOverlay("AddressFlags");
      }
      catch (err) { }
      if (_fastResults) {
          $("#FastResults").show();
          AddressSearch.DrawFastPoints(AddressSearch.FastPageID);
      }
      else {

          if (AddressSearch.jsonAddress == null && _AutoResults == false) {
              Reset();
          }
          else {
              $("#addressResults").show();
              if (!_AutoResults) {
                  AddressSearch.DrawAddressPoints(AddressSearch.AddressPageID);
              }
              else {
              
                  AddressSearch.DrawAddressPointsAuto(_Autolat, _Autolon, _AutoDesc);
              }
          }
      }
  }
  function SetDefaultLocation(oMap, oPoint) {
      var oCoordOrigin = g_map.getCoordFromVisibleArea(oPoint);
      DefaultLocation.SaveDefaultLocationLatLng(oCoordOrigin.latitude(), oCoordOrigin.longitude());
  }

function SetStopLocation(oMap, oPoint) {
//    $("#imgLoadAni").show();
//    var oCoordOrigin = g_map.getCoordFromVisibleArea(oPoint);
//    _zoomLevel = g_map.getZoomLevel();
//    url = "ashx/ReverseGeocode.ashx?Lat=" + oCoordOrigin.latitude() + "&Lon=" + oCoordOrigin.longitude() + "&Level=" + _zoomLevel;
//    $.getJSON(url, function (json) {
//        $("#imgLoadAni").hide();
//        //{ "NewDataSet": {"ResultSet": {"Dataset": "AG_TOWNS", "Description": "City of Tshwane Rural,Gauteng", "Latitude": "-25.477471", "Longitude": "28.209047" } }}
//        directions.AddDirectionTo(json.Latitude, json.Longitude, json.Description);

//        if (divLeftHidden == true) {
//            HideShowDivLeft();
//        }
//        $("#Tags").hide();
//        if (directions.aSelected == "Selected" && directions.bSelected == "Selected") {
//            directions.GetDirections();
//        }
//    });
    $("#imgLoadAni").show();
    directions.clickDirections = true;
    var oCoordOrigin = g_map.getCoordFromVisibleArea(oPoint);
    _zoomLevel = g_map.getZoomLevel();

    _image = "images/1g.png";
    var _iconAnchor = new AGPoint(-15, -39);
    var myHTMLObj = new Image();
    myHTMLObj.setAttribute("id", "Fast1");
    myHTMLObj.style.zIndex = 1;
    myHTMLObj.setAttribute("src", _image);
    myHTMLObj.setAttribute("title","1");
    var objectSize = new AGSize(30, 39);
    var opts = new AGHTMLOverlayOptions(_iconAnchor, objectSize);


    var htmlOverlay = new AGHTMLOverlay(myHTMLObj, oCoordOrigin, opts);

    g_map.addOverlay(htmlOverlay, "Flags");

    //url = "ashx/ReverseGeocode.ashx?Lat=" + oCoordOrigin.latitude() + "&Lon=" + oCoordOrigin.longitude() + "&Level=" + _zoomLevel;
   // $.getJSON(url, function (json) {
        $("#imgLoadAni").hide();
        //{ "NewDataSet": {"ResultSet": {"Dataset": "AG_TOWNS", "Description": "City of Tshwane Rural,Gauteng", "Latitude": "-25.477471", "Longitude": "28.209047" } }}
        directions.AddDirectionTo(oCoordOrigin.latitude(), oCoordOrigin.longitude(), "Selected from map");  //json.Description);
        if (divLeftHidden == true) {
            HideShowDivLeft();
        }
        $("#Tags").hide();
        if (directions.aSelected == "Selected" && directions.bSelected == "Selected") {
            directions.GetDirections();
            $("#imgLoadAni").show();
        }

   // });
}
function DisplayCoordFromClick(oMap, oPoint) {
    var oCoordOrigin = g_map.getCoordFromVisibleArea(oPoint);
    alert("Latitude : " + oCoordOrigin.latitude() + " Longitude : " + oCoordOrigin.longitude());
}
var ShowResults = false;
function ShowAddressResults() {
    if (ShowResults) {
        $("#addressResults").fadeOut();
        ShowResults = false;
    } else {
        $("#addressResults").fadeIn();
        ShowResults = true;

    }
}
function PrintPreview() {
    var bb = g_map.getBoundingBox();
    var _sw = bb.getSouthWest();
    var _ne = bb.getNorthEast();
    var _centerPoint = g_map.getCentre();
    var DestCoords = "";
    var DestDesc = "";
    var _description = "none";
    if (directions.aArrayItem != null) {
        _item = directions.aArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.bArrayItem != null) {
        _item = directions.bArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.cArrayItem != null) {
        _item = directions.cArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.dArrayItem != null) {
        _item = directions.dArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.eArrayItem != null) {
        _item = directions.eArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
//    if ($("#_luceneSearchText").val() != "") {
//        _description = $("#_luceneSearchText").val();
//    }
    var _url = "printpreview.aspx?Owidth=" + parseInt($("#_mapContainer").width()) + "&Oheight=" + parseInt($("#_mapContainer").height()) + "&origindescription=" + _description + "&_type=" + $("#cboType").val() + "&origincoords=" + _centerPoint.latitude() + "|" + _centerPoint.longitude() + "&printboundingbox=" + _sw.longitude() + "," + _sw.latitude() + "," + _ne.longitude() + "," + _ne.latitude() + "&scale=" + g_map.getZoomLevel();
    if (DestCoords != "") {
        _url += "&destinationcoords=" + DestCoords;
        _url += "&destinationdescription=" + DestDesc;
        _url += "&RouteType=" + directions.stype;
    }
    window.open(_url);
    //?origindescription=GAUTENG&origincoords=-26.014866533871675|28.2618&destinationdescription=&destinationcoords=&boundingbox=27.4252,-26.9238,29.0984,-25.1096&province=GAUTENG&printboundingbox=26.30074,-27.258155,30.22286,-24.775245&scale=8


}
function ShowSMS()
{
    $('#SMSModal').modal();
}
function changeType() {
    if (document.getElementById("rbTime").checked == 'checked') {
        _Routetype = "TRAVELTIME";
    }
    else {
        _Routetype = "LENGTH";
        
    }
    if (directions.dirReady == true) {
        //ReSearch();
        
        directions.GetDirections();
    }

}
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
function DrawPoints() {
    RemovePreviousPoints();

    var scaleIndex = g_map.getZoomLevel();
    if (scaleIndex >= 15) {
        xmlHttp = GetXmlHttpObject()
        if (xmlHttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var bb = g_map.getBoundingBox();
        var _sw = bb.getSouthWest();
        var _ne = bb.getNorthEast();
        var url = "ajax_search_points.aspx?bb=" + _sw.longitude() + "," + _sw.latitude() + "," + _ne.longitude() + "," + _ne.latitude() + "&num=" + Math.random();
        xmlHttp.onreadystatechange = RecievePoints;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }
}
function RecievePoints() {
    if (xmlHttp.readyState == 4) {
        var arr_received = xmlHttp.responseText;
        if (arr_received != "") {
            arr = arr_received.split("|")
            for (var i = 0; i < arr.length; i++) {
                str = arr[i].split(";");

                //                w = document.createElement('div');
                //                w.setAttribute('id', 'map_property' + i);
                //                w.setAttribute('name', 'map_property');
                //                w.style.cursor = "pointer";
                //                w.style.zIndex = 1000;

                //                //url = PoiUrl + '' +str[0];

                //                var img = document.createElement("img");
                //                //img.setAttribute("src","map_images/white_space.gif");
                //                img.setAttribute("src", str[4]);
                //                img.style.cursor = "pointer";
                //                img.style.width = "25px";
                //                img.style.height = "25px";


                //                //img.style.borderColor="red";
                //                //img.style.borderWidth="1px";
                //                //img.style.borderStyle="solid";
                //                //img.onclick = new Function('ShowPOINewPage("'+url+'");');
                //                img.setAttribute("title", str[1]);

                //                w.appendChild(img);
                //                var myHTMLOptions = new AGHTMLOverlayOptions(new AGPoint(0, -2));
                //                var myHTMLOverlay = new AGHTMLOverlay(w, new AGCoord(parseFloat(str[2]), parseFloat(str[3])), myHTMLOptions);
                //                g_map.addOverlay(myHTMLOverlay, "Points");


                var oAGIcon = new AGIcon();
                oAGIcon.image = str[4];
                oAGIcon.iconSize = new AGSize(25, 25);
                oAGIcon.iconAnchor = new AGPoint(0, 0);
                oAGIcon.infoDisplayAnchor = new AGPoint(10, 0);
                var markerOption = new AGMarkerOptions(oAGIcon);
                markerOption.title = str[1];
                markerOption.bouncy = true;

                var simpleMarker = new AGMarker(new AGCoord(parseFloat(str[2]), parseFloat(str[3])), markerOption);
                g_map.addOverlay(simpleMarker, "Points");



            }
        }
    }
}

function RemovePreviousPoints() {
    g_map.removeGroupOverlay("Points");
}
function SMS() {
    _number = $("#txtSMS").val()
    _number = _number.replace(/\s|-/g, "");
    _number = _number.replace("+", "");
    var _validPhoneNo = new RegExp("^(?!\\+.*\\+)\\+?(\\(\\+?\\d+\\))?\\d{7,15}$");
    var _valid = _validPhoneNo.test(_number);

    if (_valid == false) {
    alert("Please enter a valid cell number");
}
else{
    var bb = g_map.getBoundingBox();
    var _sw = bb.getSouthWest();
    var _ne = bb.getNorthEast();
    var _centerPoint = g_map.getCentre();
    var DestCoords = "";
    var DestDesc = "";
    var _description = "none";
    if (directions.aArrayItem != null) {
        _item = directions.aArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.bArrayItem != null) {
        _item = directions.bArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.cArrayItem != null) {
        _item = directions.cArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.dArrayItem != null) {
        _item = directions.dArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.eArrayItem != null) {
        _item = directions.eArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    //    if ($("#_luceneSearchText").val() != "") {
    //        _description = $("#_luceneSearchText").val();
    //    }
    var _url = MainPath + "ViewWap.aspx?Owidth=" + parseInt($("#_mapContainer").width()) + "&Oheight=" + parseInt($("#_mapContainer").height()) + "&origindescription=" + _description + "&_type=" + directions.sType + "&origincoords=" + _centerPoint.latitude() + "|" + _centerPoint.longitude() + "&printboundingbox=" + _sw.longitude() + "," + _sw.latitude() + "," + _ne.longitude() + "," + _ne.latitude() + "&scale=" + g_map.getZoomLevel();
    if (DestCoords != "") {
        _url += "&destinationcoords=" + DestCoords;
        _url += "&destinationdescription=" + DestDesc;
        _url += "&RouteType=" + _Routetype;
    }
    _url = escape(_url);
    var _dirSMS = "F";
    if (DestCoords != "") {
        _dirSMS = "T"
    }

    smsMap = "ashx/SendSMS.ashx?MSISDN=" + _number + "&directions="+_dirSMS+"&url=" + _url;
    $.getJSON(smsMap, function (json) {
        $.modal.close();
        alert("SMS sent");
    });
    //?origindescription=GAUTENG&origincoords=-26.014866533871675|28.2618&destinationdescription=&destinationcoords=&boundingbox=27.4252,-26.9238,29.0984,-25.1096&province=GAUTENG&printboundingbox=26.30074,-27.258155,30.22286,-24.775245&scale=8
    }

}
function zoomtoLocationFast(lat, lon) {
    var newCoord = new AGCoord(parseFloat(lat), parseFloat(lon));
   
    g_map.centreAndScale(newCoord, 14);
}
function addToFav() {

    if (!window.event) {
        window.sidebar.addPanel("Yellow Pages Maps & Directions - South Africa's premium maps & directions site", "http://maps.yellowpages.co.za", "");
    }
    else {
        window.external.AddFavorite("http://maps.yellowpages.co.za", "Yellow Pages Maps & Directions - South Africa's premium maps & directions site");
    }
}
function SendEmail() {
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   _bit = emailPattern.test($("#emailAddress").val());  
if(_bit == false) {
    alert("Please enter a valid email address");
}
else{
    var bb = g_map.getBoundingBox();
    var _sw = bb.getSouthWest();
    var _ne = bb.getNorthEast();
    var _centerPoint = g_map.getCentre();
    var DestCoords = "";
    var DestDesc = "";
    var _description = "none";
    if (directions.aArrayItem != null) {
        _item = directions.aArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.bArrayItem != null) {
        _item = directions.bArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.cArrayItem != null) {
        _item = directions.cArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.dArrayItem != null) {
        _item = directions.dArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
    if (directions.eArrayItem != null) {
        _item = directions.eArrayItem.split("|");
        DestCoords += _item[0] + "|" + _item[1] + "$";
        DestDesc += _item[2] + "|";
    }
//    if ($("#_luceneSearchText").val() != "") {
//        _description = $("#_luceneSearchText").val();
//    }
    var _url = "JSONMAILER.aspx?Owidth=" + parseInt($("#_mapContainer").width()) + "&Oheight=" + parseInt($("#_mapContainer").height()) + "&Email=" + $("#emailAddress").val() + "&origindescription=" + _description + "&RouteType=" + directions.stype + "&origincoords=" + _centerPoint.latitude() + "|" + _centerPoint.longitude() + "&printboundingbox=" + _sw.longitude() + "," + _sw.latitude() + "," + _ne.longitude() + "," + _ne.latitude() + "&scale=" + g_map.getZoomLevel();
    if (DestCoords != "") {
        _url += "&destinationcoords=" + DestCoords;
        _url += "&destinationdescription=" + DestDesc;
    }

    $.getJSON(_url, function () {
        $.modal.close();
        alert("Mail sent");
    });
}
}
function Email() {
    
    $('#EmailModal').modal();
}
function ClearResults() {

    $("#imgLoadAni").hide();
    directions.ShowDirections = false;
    $("#PointA").val("");
    $("#PointB").val("");
    $("#PointC").val("");
    $("#PointD").val("");
    $("#PointE").val("");
    
    $("#FastResults").hide();
    $("#DirectionSearch").hide();
    $("#DefaultSearchlink").show();
    AddressSearch.RemoveBubble();
    g_map.removeGroupOverlay("Flags");
    g_map.removeGroupOverlay("AddressFlags");
    $("#_message").html("Welcome to Yellow Pages Maps & Directions");
    try {
        curvyCorners.redraw();
    }
    catch (e) {

    }
    
    $("#drivingDirections").html("");
    directions.aSelected = null;
    directions.bSelected = null;
    directions.cSelected = null;
    directions.dSelected = null;
    directions.eSelected = null;
    directions.aArrayItem = null;
    directions.bArrayItem = null;
    directions.cArrayItem = null;
    directions.dArrayItem = null;
    directions.eArrayItem = null;
    directions.dirReady = null;
    $("#tdC").hide();
    $("#tdD").hide();
    $("#tdE").hide();
    $("#remA").hide();
    $("#remB").hide();
    AddressSearch.jsonAddress = null;
    AddressSearch.jsonFast = null;


    try {
        g_map.removeGroupOverlay("GreenLine");
    }
    catch (err) { }
    $.getJSON("ashx/GetTopSearches.ashx", function (json) {
        var _html = "<ul class='ulClass'>";
        _icnt = 50;
        for (x in json) {
            _random = Math.random();
            if (_icnt < 49) {
                _name = x;
                _name = _name.replace("'", "");
                _html += "<li  class=\"liClass\" onclick=\"doSearchPop('" + _name + "');\" >" + _name + " </li>";
            }
            _icnt--;

        }
        _html += "</ul>";
        $("#tagCload").html(_html);
        AG_tagCloud.CreateCloud("liClass");
     //   $("#Tags").show();

    });
}
function ResetMap() {
    var oLatLng = new AGCoord(-26.17028, 28.053534);
    g_map.centreAndScale(oLatLng, 5);
    DefaultLocation.LoadCookie();
    Reset();
}
function Reset() {

   
    
    $("#PointA").val("");
    $("#PointB").val("");
    $("#PointC").val("");
    $("#PointD").val("");
    $("#PointE").val("");
    $("#DidExpander").hide();
    $("#addressResults").hide();
    $("#FastResults").hide();
    $("#DirectionSearch").hide();
    $("#DefaultSearchlink").show();
    AddressSearch.RemoveBubble();
    g_map.removeGroupOverlay("Flags");
    g_map.removeGroupOverlay("AddressFlags");
    $("#_message").html("Welcome to Yellow Pages Maps & Directions");
    try {
        curvyCorners.redraw();
    }
    catch (e) {

    }
    $("#txtSearch").val("");
    $("#drivingDirections").html("");
      directions.aSelected =  null;
      directions.bSelected =  null;
      directions.cSelected =  null;
      directions.dSelected =  null;
      directions.eSelected =  null;
      directions.aArrayItem =  null;
      directions.bArrayItem =  null;
      directions.cArrayItem =  null;
      directions.dArrayItem =  null;
      directions.eArrayItem =  null;
      directions.dirReady = null;
      $("#tdC").hide();
      $("#tdD").hide();
      $("#tdE").hide();
      $("#remA").hide();
      $("#remB").hide();
      AddressSearch.jsonAddress = null;
      AddressSearch.jsonFast = null;
    
      
    try {
        g_map.removeGroupOverlay("GreenLine");
    }
    catch (err) { }
    $.getJSON("ashx/GetTopSearches.ashx", function (json) {
        var _html = "<ul class='ulClass'>";
        _icnt = 50;
        for (x in json) {
            _random = Math.random();
            if (_icnt < 49) {
                _name = x;
                _name = _name.replace("'", "");
                _html += "<li  class=\"liClass\" onclick=\"doSearchPop('" + _name + "');\" >" + _name + " </li>";
            }
            _icnt--;

        }
        _html += "</ul>";
        $("#tagCload").html(_html);
        AG_tagCloud.CreateCloud("liClass");
        $("#Tags").show();

    });
}
function clickFunction(id) {
    alert(id);
}
function ClearMap() {
    directions.ShowDirections = false;
    $("#DidExpander").hide();
    $("#imgLoadAni").hide();
    $("#addressResults").hide();
    $("#FastResults").hide();
    $("#DirectionSearch").hide();
    $("#PointA").val("");
    $("#PointB").val("");
    $("#PointC").val("");
    $("#PointD").val("");
    $("#PointE").val("");

}
function directionloadhandler(oLatLong, caller) {
    $("#imgLoadAni").hide();
    var arrCoords = oDirection.getVertices();
    if (directions.clickDirections == true) {

        url = "ashx/ReverseGeocode.ashx?Lat=" + parseFloat(arrCoords[0].latitude()) + "&Lon=" + parseFloat(arrCoords[0].longitude()) + "&Level=18";
        $.getJSON(url, function (json) {
            if ($("#PointA").val() == "Selected from map") {
                $("#PointA").val(json.Description);
            }
            directions.aArrayItem = arrCoords[0].latitude() + "|" + arrCoords[0].longitude() + "|" + json.Description;
            url = "ashx/ReverseGeocode.ashx?Lat=" + parseFloat(arrCoords[arrCoords.length - 1].latitude()) + "&Lon=" + parseFloat(arrCoords[arrCoords.length - 1].longitude()) + "&Level=18";
            $.getJSON(url, function (json) {
                if ($("#PointB").val() == "Selected from map") {
                    $("#PointB").val(json.Description);
                    directions.bArrayItem = arrCoords[arrCoords.length - 1].latitude() + "|" + arrCoords[arrCoords.length - 1].longitude() + "|" + json.Description;
                }

                
                directions.clickDirections = false;
            });
        });
    }
   

    var oVectorStyleOpts = new AGVectorStyleOptions("Green", 8, 0.5, "Green", 0.5);
    var oLine = new AGPolyline(arrCoords, oVectorStyleOpts);

    g_map.addOverlay(oLine,"GreenLine");

    oDirection.clear();
}
function ShowBubble(style, height, width, offsetx, offsety, lat, lon, map, content) {
    openPoint = g_map.getPointFromVisibleArea(new AGCoord(lat, lon));
    var moveAmountx = 0;
    var moveAmounty = 0;
    openPointX = _pageWidth - openPoint.x - width;
    if (openPointX < width) {
        moveAmountx = width - openPointX + 10;

    }
    openPointy = openPoint.y - (height + 58);

    if (openPointy < height + 58) {
        moveAmounty = openPointy - 30;

    }
    if (moveAmounty > 0)
        moveAmounty = 0;
    var oPoint = new AGPoint(moveAmountx * -1, moveAmounty * -1);

    g_map.panBy(oPoint);
    g_map.removeGroupOverlay("BubbleOverLay");
    var _image = "";
    var _color = "";
    var _bordercolor
    var _mozopac = "";
    var _opac = "";

    if (style == 1) //white
    {
        _image = "BubbleSpriteWhite5.png";
        _color = "#FFF";
        _bordercolor = "#A2A2A2";
        _mozopac = "1";
        _opac = "100";
        _closeImage = "cancel.png";

    }
    if (style == 2) {
        _image = "BubbleSpriteBlueTrans.png";
        _color = "#3447FF";
        _bordercolor = "#959595";
        _mozopac = "0.7";
        _opac = "70";
        _closeImage = "cancel.png";
    }
    var topWidth = width - 72;
    var contentWidth = width - 75;
    var contentHeight = height - 70;
    var bottomwidth = width - 150;
    var shaddowWidth = width - 200;
    var shaddowheight = height - 160;
    Bubble = "<div style='width:" + width + "px; height: 30px;position:relative;z-index:10;'>";
    Bubble += "<div style='float:left;background-image:url(\"images/" + _image + "\");background-repeat:no-repeat; width: 35px; height: 38px;margin-top:-1px;position:relative;z-index:10;'></div>";
    Bubble += "<div style='opacity:" + _mozopac + ";filter:alpha(opacity=" + _opac + ");background-color:" + _color + ";border-top:1px solid " + _bordercolor + ";float:left; width:" + topWidth + "px; height: 36px;position:relative;z-index:10;'></div>";
    Bubble += "<div style='width: 35px; height: 37px; float: left; background-image: url(\"images/" + _image + "\"); background-repeat: no-repeat; background-position:-122px -1px; top: 0px; z-index: 10; opacity: 1;position:relative;z-index:10;'><img id='imgRemButton' src='images/" + _closeImage + "' style='float:left;margin-left:2px;margin-top:15px;position:relative;z-index:10;' onclick=' RemoveBubble()' /></div>";
    Bubble += "<div style='height:" + contentHeight + "px;opacity:" + _mozopac + ";filter:alpha(opacity=" + _opac + ");background-color:" + _color + ";border-left:1px solid " + _bordercolor + ";float:left;width:35px;margin-left:1px;position:relative;z-index:10;'></div>";
    Bubble += "<div style='height:" + contentHeight + "px;opacity:" + _mozopac + ";filter:alpha(opacity=" + _opac + ");background-color:" + _color + ";float:left;width:" + contentWidth + "px;overflow:auto;position:relative;z-index:10;' id='Info'>";
    Bubble += content;
    Bubble += "</div>";
    Bubble += "<div style='height:" + contentHeight + "px;opacity:" + _mozopac + ";filter:alpha(opacity=" + _opac + ");background-color:" + _color + ";border-right:1px solid " + _bordercolor + ";float:left;width:35px;position:relative;z-index:10;'></div>";
    Bubble += "<div style='float:left;opacity:" + _mozopac + ";filter:alpha(opacity=" + _opac + ");background-color:" + _color + ";border-left:1px solid " + _bordercolor + "; width: 35px; height: 35px;margin-left:1px;position:relative;z-index:10;'></div>";
    Bubble += "<div style='opacity:" + _mozopac + ";filter:alpha(opacity=" + _opac + ");background-color:" + _color + ";float:left;width:" + contentWidth + "px; height: 37px;position:relative;z-index:10;'></div>";
    Bubble += " <div style='width: 36px; height: 37px; float: left; background-image: url(\"images/" + _image + "\"); background-repeat: no-repeat; background-position:-121px -119px;position:relative;z-index:10;'></div>";
    Bubble += "<div style='float:left;background-image:url(\"images/" + _image + "\");background-repeat:no-repeat; width: 112px; height: 55px;margin-top:-3px;background-position:-0px -161px;position:relative;z-index:10;'></div>";
    Bubble += "<div style='float:left;width:" + bottomwidth + "px;border-top:1px solid " + _bordercolor + ";margin-top:-1px;position:relative;z-index:10;'></div>";
    Bubble += "<img src='Images/shadow.png' style='width:" + (width * 1.5) + "px; height:" + (height * 0.7) + ";float:left;margin-top:-" + (height * 0.7) + "px;z-index:1;position:relative; top: 0px; left: 0px;margin-left:10px;'/></div>";
    Bubble += "</div>";
    _x = -18 + offsetx;
    _y = height + 68 + offsety;
    var _iconAnchor = new AGPoint(_x, -1 * (_y));
    var _size = new AGSize(width, height);
    //            var _size = new AGSize(width, 25);
    var myHTMLObj = document.createElement('div');
    myHTMLObj.style.zIndex = 99999999;
    myHTMLObj.innerHTML = Bubble;
    var _options = new AGCustomOverlayOptions(_iconAnchor, _size);
    var myHTMLOverlay = new AGCustomOverlay(myHTMLObj, new AGCoord(lat, lon), _options);
    map.addOverlay(myHTMLOverlay, "BubbleOverLay");

    //            AGEvent.addListener(myHTMLOverlay,"onclick",HTMLOverlayClicked);
}
function RemoveBubble() {
    g_map.removeGroupOverlay("BubbleOverLay");
}
