// JScript ”j

var COLORS = [["red", "#ff0000"], ["orange", "#ff8800"], ["green","#008000"],

              ["blue", "#000080"], ["purple", "#800080"]];

var options = {};

var lineCounter_ = 0;

var shapeCounter_ = 0;

var markerCounter_ = 0;

var colorIndex_ = 0;

var featureTable_;

var map = new GMap2(document.getElementById("map"));

var latlngArray = null;

var ThisCarDeviceId = null;

var AutoScaleFlagBool = true;

var marker = new GMarker(new GLatLng(0, 0));

//var marker = new GMarker(new GLatLng(25.1235,51.452455),{draggable: true});

function select(buttonId) {

  document.getElementById("hand_b").className="unselected";

  document.getElementById("shape_b").className="unselected";

 // document.getElementById("line_b").className="unselected";

 // document.getElementById("placemark_b").className="unselected";

  document.getElementById(buttonId).className="selected";

}

function stopEditing() {

    select("hand_b");

}

function getColor(named) {

  return COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];

}

function getIcon(color) {

  var icon = new GIcon();

  icon.image = "http://google.com/mapfiles/ms/micons/" + color + ".png";

  icon.iconSize = new GSize(32, 32);

  icon.iconAnchor = new GPoint(15, 32);

  return icon;

}

function startShape() {

  select("shape_b");

  var color = getColor(false);

  var polygon = new GPolygon([], color, 2, 0.7, color, 0.2);

  startDrawing(polygon, "Shape " + (++shapeCounter_), function() {

      var cell = this;

      var area = polygon.getArea();

      cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";

  }, color);

 // testpoly(polygon);

}



function addFeatureEntry(name, color) {

  currentRow_ = document.createElement("tr");

  var colorCell = document.createElement("td");

  currentRow_.appendChild(colorCell);

  colorCell.style.backgroundColor = color;

  colorCell.style.width = "1em";

  var nameCell = document.createElement("td");

  currentRow_.appendChild(nameCell);

  nameCell.innerHTML = name;

  var descriptionCell = document.createElement("td");

  currentRow_.appendChild(descriptionCell);

  featureTable_.appendChild(currentRow_);

  return {desc: descriptionCell, color: colorCell};

}

function startDrawing(poly, name, onUpdate, color) {

  map.addOverlay(poly);

  poly.enableDrawing(options);

poly.enableEditing({onEvent: "mouseover"});

 poly.disableEditing({onEvent: "mouseout"});

 GEvent.addListener(poly, "endline", function() {

     select("hand_b");

     var cells = addFeatureEntry(name, color);

     GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);

     

 });

}

function testpoly(polygon,marker) {
   
    GPolygon.prototype.Contains = function(point) {
        var j = 0;
        var oddNodes = false;
        var x = point.lng();
        var y = point.lat();
        for (var i = 0; i < this.getVertexCount(); i++) {
            j++;
            if (j == this.getVertexCount()) { j = 0; }
            if (((this.getVertex(i).lat() < y) && (this.getVertex(j).lat() >= y))
          || ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {
                if (this.getVertex(i).lng() + (y - this.getVertex(i).lat())
            / (this.getVertex(j).lat() - this.getVertex(i).lat())
            * (this.getVertex(j).lng() - this.getVertex(i).lng()) < x) {
                    oddNodes = !oddNodes
                }
            }
        }
        return oddNodes;
    }

    // === A method for testing if a point is inside a polyline
    // === Add the GPolygon method to GPolyline
    GPolyline.prototype.Contains = GPolygon.prototype.Contains;
    GEvent.addListener(map, "mousemove", function() {
        var p = getpost(marker);
        if (polygon.Contains(p)) {
            document.getElementById("message").innerHTML = "The Car is INSIDE the Selected Area" + p;
        } else {
            document.getElementById("message").innerHTML = "The Car is outside the Selected Area."
        }
    });
 
}
function getpost(marker) {
    return marker.getLatLng();
}


// EWINDOW






function clickit(x, y) {



    function TextualZoomControl() {

    }

    TextualZoomControl.prototype = new GControl();

    TextualZoomControl.prototype.initialize = function(map) {

        var container = document.createElement("div");

        var spanTag = document.createElement("span");

        spanTag.innerHTML = "X :" + y + "</br>" + "Y :" + x;

        var zoomInDiv = document.createElement("div");

        this.setButtonStyle_(zoomInDiv);

        container.appendChild(zoomInDiv);

        zoomInDiv.appendChild(spanTag);

        map.getContainer().appendChild(container);

        return container;

    }

    TextualZoomControl.prototype.getDefaultPosition = function() {

        return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(1, 15));

    }

    TextualZoomControl.prototype.setButtonStyle_ = function(button) {

        button.style.color = "#0000cc";

        button.style.backgroundColor = "white";

        button.style.font = "small Arial";

        button.style.border = "1px solid black";

        button.style.padding = "2px";

        button.style.marginBottom = "3px";

        button.style.textAlign = "left";

        button.style.width = "6em";

        button.style.cursor = "pointer";

    }

    var mycontrol = new TextualZoomControl();

    map.addControl(mycontrol);

}

function MapSetCenter(y, x, caronoroff, speed, deviceid, direction, carnumber, AutoScaleBool) {

    if (GBrowserIsCompatible()) {


        var tinyIcon = new GIcon();

        var strimg;

        if (22.5 < direction && direction < 67.5) {

            strimg = "img/car_45.png";

        }

        else if (67.5 < direction && direction < 112.5) {

            strimg = "img/car_90.png";

        }

        else if (112.5 < direction && direction < 157.5) {

            strimg = "img/car_135.png";

        }

        else if (157.5 < direction && direction < 202.5) {

            strimg = "img/car_180.png";

        }

        else if (202.5 < direction && direction < 247.5) {

            strimg = "img/car_225.png";

        }

        else if (247.5 < direction && direction < 292.5) {

            strimg = "img/car_270.png";

        }

        else if (292.5 < direction && direction < 337.5) {

            strimg = "img/car_315.png";

        }

        else {

            strimg = "img/car_0.png";

        }



        tinyIcon.image = strimg;

        tinyIcon.iconSize = new GSize(24, 24);

        tinyIcon.iconAnchor = new GPoint(6, 20);

        tinyIcon.infoWindowAnchor = new GPoint(5, 1);



        var latlng = new GLatLng(y, x);

        if (!isDefined(latlngArray)) {

            latlngArray = new Array();

        }



        latlngArray.push(latlng);

        ew = new EWindow(map, E_STYLE_7);

        map.addOverlay(ew);

        // This function just makes a pretty table for the EWindow contents

        function pretty(a, b, c) {

            return '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="100%" class="EWTitle" nowrap>' + a +



               '</td></tr>' +

               '<tr><td class="Ebody" nowrap>' + b + '</td></tr><tr><td class="Ebody">' + c + '</td></tr></table>';

        }



        markerOptions = { icon: tinyIcon };

        var html = pretty(carnumber, '<b>S: ' + speed + '<b> Km/hr</b>', '<b>I:' + caronoroff);



        marker = new GMarker(latlng, markerOptions);

        marker.carx = x;

        marker.cary = y;

        marker.carspeed = speed;

        marker.id = deviceid;

        map.addOverlay(marker);

        // ========== Open the EWindow instead of a Google Info Window ==========

        GEvent.addListener(marker, 'click', function(marker) {

        GEvent.addListener(marker, "test", clickit(x, y));

        GEvent.trigger(marker, "test");



        });

        ew.openOnMarker(marker, html);



        if (latlngArray.length == 1) {

            map.setCenter(new GLatLng(y, x));

        }



        else {

            var bounds = new GLatLngBounds();

            for (var i = 0; i < latlngArray.length; i++) {

                bounds.extend(latlngArray[i]);

            }

            map.setZoom(map.getBoundsZoomLevel(bounds));

            map.setCenter(bounds.getCenter());

        }

        featureTable_ = document.getElementById("featuretbody");
    }

}

function CarStatus(CarNumber) {

    PPG_STATUS_CONTROL.setCarNo(CarNumber);

}

function ClearCar() {

    map.clearOverlays();

    if (isDefined(latlngArray)) {

        latlngArray.length = 0;

    }

}

function initialize() {

    if (GBrowserIsCompatible()) {

        var mapOptions = {

        googleBarOptions: {

        adsOptions: { client: "partner-google-maps-api",

                      channel: "AdSense for Search channel",

                      adsafe: "high",

                      language: "en"}} }

                      map = new GMap2(document.getElementById("map"), mapOptions);

        map.setCenter(new GLatLng(25.46387, 51.27544), 13);

        map.addControl(new GLargeMapControl3D());

        map.addControl(new GMapTypeControl());

        map.addMapType(G_PHYSICAL_MAP);

        map.addControl(new GOverviewMapControl());

        map.enableGoogleBar();

featureTable_ = document.getElementById("featuretbody");
    select("hand_b");
  }

}



function googleTrackingHistory(latlngs) {

    var points = [];

    var kilo;

    var bounds = new GLatLngBounds();

    for (var i = 0; i < latlngs.length; i += 2) {

        if (typeof latlngs[i + 1] == 'number' && typeof latlngs[i] == 'number') {

            points[i] = new GLatLng(latlngs[i + 1] / 1000000, latlngs[i] / 1000000);

            bounds.extend(points[i]);

        }

    }



    var polyline = new GPolyline(points, "#ff00ff", 10);

    kilometer = polyline.getLength() / 1000;

    ValuePopulate("The Total Distance : </b>" + kilometer.toFixed(2) + "</b>km");

    GEvent.addListener(polyline, 'click', function(kilo)

    { map.openInfoWindowHtml(kilo, "<b>The Total Distance : </b>" + kilometer.toFixed(2) + "km") });

    map.addOverlay(polyline);

    map.setZoom(map.getBoundsZoomLevel(bounds));

    map.setCenter(bounds.getCenter());

}

function ValuePopulate(n) {

    document.getElementById("displayScreen").innerHTML = n;

}

function goto(x, y) {

    map.clearOverlays();

    map.setCenter(new GLatLng(x, y), 13);

    var marker = new GMarker(new GLatLng(x, y));

    map.addOverlay(marker);



}












