﻿///<reference path="jquery-1.4.2.min.js"/>
///<reference path="jquery-ui-1.8rc3.custom.min.js"/>
///<reference path="jquery.updatepanel.js"/>

var ROUTE_LINE_WIDTH = 5;
var ROUTE_LINE_COLOR = "#000000";
var GEO_INPUT_WIDTH = 25;

$(document).ready(function () {
    numberOfRequests();
});

function ConflictMap(id, loc)
{
    var me = this;

	var map = null;

	this.showMap = function () {

	    if ($(id + "Placeholder").length == 0)
	        $(id).hide();
	    else {
	        var offset = $(id + "Placeholder").offset();
	        offset.left++;
	        offset.top++;
	        $(id).offset(offset);
	        $(id).show();

	        map.checkResize();

	        //map.setCenter(new GLatLng(loc.lat, loc.long), map.getZoom() );
	    }
	};

	this.hideMap = function ()
	{
		$(id).hide();
	};

	this.clearMap = function () {
	    map.clearOverlays();
	};

	this.zoomToRoute = function (route) {
	    var minLong, minLat, maxLong, maxLat;

	    for (var i = 0; i < route.points.length; i++) {
	        if (minLong == null || route.points[i].longitude > minLong)
	            minLong = route.points[i].longitude;

	        if (maxLong == null || route.points[i].longitude < maxLong)
	            maxLong = route.points[i].longitude;

	        if (minLat == null || route.points[i].latitude < minLat)
	            minLat = route.points[i].latitude;

	        if (maxLat == null || route.points[i].latitude > maxLat)
	            maxLat = route.points[i].latitude;
	    }

	    //var bounds = new GLatLngBounds(new GLatLng(minY, maxX), new GLatLng(maxY, minX));

	    //var zoomLevel = map.getBoundsZoomLevel(bounds);

	    //zoomLevel = Math.min(zoomLevel, 8);

	    //var center = bounds.getCenter();

	    $(id).show();

	    var bounds = new GLatLngBounds;
	    bounds.extend(new GLatLng(maxLat, maxLong));
	    bounds.extend(new GLatLng(minLat, minLong));
	    map.checkResize();

	    // map.getBoundsZoomLevel(bounds) will not properly unless the div
	    // containing the map is visible (the api can get bounds without the
        // div being visible)
	    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	    //	    var center = new GLatLng((maxLat + minLat) / 2, (maxLong + minLong) / 2);

	    //	    map.setCenter(center, 8);
	};

	this.drawRoute = function (route, color) {

	    if (!color)
	        color = "";

	    var points = [];

	    for (var i = 0; i < route.points.length; i++) {
	        points.push(new GLatLng(route.points[i].latitude, route.points[i].longitude));
	    }

	    var lineColor = ROUTE_LINE_COLOR;

	    if (color == "Red")
	        lineColor = "#ff0000";

	    var polyline = new GPolyline(points, lineColor, ROUTE_LINE_WIDTH);

	    map.addOverlay(polyline);

	    for (var i = 0; i < route.points.length; i++) {
	        var icon = new GIcon();
	        icon.iconSize = new GSize(35, 35);
	        icon.iconAnchor = new GPoint(17, 17);

	        if (i == 0)
	            icon.image = "RouteSymbolGenerator.aspx?type=0&typeCode=" + route.typeCode + "&designator=" + route.designator + "&color=" + color;
	        else if (i == route.points.length - 1)
	            icon.image = "RouteSymbolGenerator.aspx?type=2&symbol=" + route.points[i].name + "&color=" + color;
	        else
	            icon.image = "RouteSymbolGenerator.aspx?type=1&symbol=" + route.points[i].name + "&color=" + color;

	        var title = "Waypoint " + route.points[i].name + ": " + route.points[i].location;

	        var marker = new GMarker(new GLatLng(route.points[i].latitude, route.points[i].longitude), { icon: icon, title: title });

	        map.addOverlay(marker);
	    }

	    this.zoomToRoute(route);
	};

	this.drawSua = function (sua) {
	    var points = [];

	    for (var i = 0; i < sua.shapes.length; i++) {
	        points.push(new GLatLng(sua.shapes[i].latitude1, sua.shapes[i].longitude1));
	        points.push(new GLatLng(sua.shapes[i].latitude2, sua.shapes[i].longitude2));
	    }

	    var polygon = new GPolygon(points, "#0000ff", 2);

	    map.addOverlay(polygon);
	};

	this.drawConflicts = function (route, conflicts) {
	    me.drawRoute(route);

	    for (var i = 0; i < conflicts.length; i++) {
	        if (conflicts[i].sua)
	            me.drawSua(conflicts[i].sua);

	        if (conflicts[i].intruder)
	            me.drawRoute(conflicts[i].intruder, "Red");

	        //var icon = new GIcon();

	        //icon.iconSize = new GSize(35, 31);
	        //icon.iconAnchor = new GPoint(17, 15);
	        //icon.image = "RouteSymbolGenerator.aspx?type=3&symbol=" + (i + 1);

	        var icon = me.getIcon(3, "", (i + 1), "");

	        var marker = new GMarker(new GLatLng(conflicts[i].latitude, conflicts[i].longitude), { icon: icon });

	        map.addOverlay(marker);
	    }
	};

	var routeBuilding = false;
	var customMarkers = [];
	var customPoints = [];
	var customPolyline;

	this.beginBuildRoute = function () {
	    routeBuilding = true;
	    customPoints = [];
	    customMarkers = [];
	    customPolyline = null;
	};

	this.endBuildRoute = function () {
	    routeBuilding = false;
	};

	this.populateCustomRoute = function (points) {
	    me.clearTableRows();
	    me.clearMap();
	    me.beginBuildRoute();
	    customPoints = points;
	    me.addAllTableRows();
	    me.drawCustomRoute(false);
	};

	this.clearTableRows = function () {
	    $(id + "Table tr:not(tr:eq(0))").remove();
	};

	this.addAllTableRows = function () {
	    $.each(customPoints, function (i, latlng) {
	        me.addTableRow(i, latlng);
	    });
	};

	this.addTableRow = function (index, latlng) {
	    var table = $(id + "Table");
	    var tr = $("<tr/>").appendTo(table);

	    //point
	    var td = $("<td/>").html(index).appendTo(tr);

	    //latitude degrees
	    td = $("<td/>").appendTo(tr);
	    var latDegreesinput = $("<input type='text'/>").width(GEO_INPUT_WIDTH)
                                             .appendTo(td)
                                             .change(function () {
                                                 var degrees = parseFloat($(this).val());

                                                 var currentLatLng = customPoints[index];
                                                 var currentLatitude = me.parseLatLng(currentLatLng.lat());

                                                 var newLatLng = new GLatLng(degrees + (currentLatitude.minutes / 60), currentLatLng.lng());
                                                 customPoints[index] = newLatLng;
                                                 me.drawCustomRoute();
                                             });

	    //latitude minutes
	    td = $("<td/>").appendTo(tr);
	    input = $("<input type='text'/>").width(GEO_INPUT_WIDTH)
                                         .appendTo(td)
                                         .change(function () {
                                             var minutes = parseFloat($(this).val());

                                             var currentLatLng = customPoints[index];
                                             var currentLatitude = me.parseLatLng(currentLatLng.lat());

                                             if (minutes >= 60 || minutes < 0 ) {
                                                 alert("Latitude minutes must be between 0 and 60");
                                                 $(this).val(currentLatitude.minutes);
                                             }
                                             else {


                                                 var newLatLng = new GLatLng(currentLatitude.degrees + (minutes / 60), currentLatLng.lng());
                                                 customPoints[index] = newLatLng;
                                                 me.drawCustomRoute();
                                             }
                                         });

	    //latitude n/s
	    td = $("<td/>").appendTo(tr);
	    var select = $("<select><option value='N'>N</option><option value='S'>S</option><select>").width(40)
                                                                                                  .appendTo(td)
                                                                                                  .change(function () {
                                                                                                      var currentLatLng = customPoints[index];
                                                                                                      var newLatLng = new GLatLng(currentLatLng.lat() * -1, currentLatLng.lng());
                                                                                                      customPoints[index] = newLatLng;
                                                                                                      me.drawCustomRoute();
                                                                                                  }); 

	    //longitude degrees
	    td = $("<td/>").appendTo(tr);
	    var input = $("<input type='text'/>").width(GEO_INPUT_WIDTH)
                                             .appendTo(td)
                                             .change(function () {
                                                 var degrees = parseFloat($(this).val());

                                                 var currentLatLng = customPoints[index];
                                                 var currentLongitude = me.parseLatLng(currentLatLng.lng());

                                                 var newLatLng = new GLatLng(currentLatLng.lat(), degrees + currentLongitude.minutes );
                                                 customPoints[index] = newLatLng;
                                                 me.drawCustomRoute();
                                             }); 

	    //longitude minutes
	    td = $("<td/>").appendTo(tr);
	    input = $("<input type='text'/>").width(GEO_INPUT_WIDTH)
                                         .appendTo(td)
                                         .change(function () {
                                             var minutes = parseFloat($(this).val());

                                             var currentLatLng = customPoints[index];
                                             var currentLongitude = me.parseLatLng(currentLatLng.lng());

                                             if (minutes >= 60 || minutes < 0) {
                                                 alert("Longitude minutes must be between 0 and 60");
                                                 $(this).val(currentLongitude.minutes);
                                             }
                                             else {

                                                 var newLatLng = new GLatLng(currentLatLng.lat(), currentLongitude.degrees + (minutes / 60));
                                                 customPoints[index] = newLatLng;
                                                 me.drawCustomRoute();
                                             }
                                         }); 

	    //longitude e/w
	    td = $("<td/>").appendTo(tr);
	    var select = $("<select><option value='E'>E</option><option value='W'>W</option><select>").width(40)
                                                                                                  .appendTo(td)
                                                                                                  .change(function () {
                                                                                            	        var currentLatLng = customPoints[index];
                                                                                            	        var newLatLng = new GLatLng(currentLatLng.lng(), currentLatLng.lng() * -1);
                                                                                            	        customPoints[index] = newLatLng;
                                                                                            	        me.drawCustomRoute();
                                                                                            	    });

	    //delete button

	    me.updateTableValues(index, latlng);
	};

	this.deleteTableRow = function (index) {
	    $(id + "Table tr:eq(" + (index + 1) + ")").remove();

	    //renumber
	    $.each($(id + "Table tr"), function (i, tr) {
	        if (i != 0)
	            $(tr).children(":eq(0)").html(i-1);
	    });
	};

	this.parseLatLng = function (latlng) {
	    var value = Math.abs(latlng);

	    //grab the degrees which is the first part
	    var degrees = Math.floor(value);

	    var remainder = (value - Math.floor(value)) * 60.0;

	    //and now the minutes
	    var minutes = remainder;// Math.floor(remainder);

	    //remainder = (remainder - Math.floor(remainder)) * 60.0;

	    //and lastly the seconds
	    //var seconds = Math.round(remainder);

	    return { degrees: degrees, minutes: minutes };
	};

	this.updateTableValues = function (index, latlng) {
	    var tr = id + "Table tr:eq(" + (index + 1) + ") ";

	    $(tr).data("loc", latlng);

	    var latitude = me.parseLatLng(latlng.lat());
	    var longitude = me.parseLatLng(latlng.lng());

	    //latitude degrees
	    var input = $(tr + "td:eq(1) input");
	    input.val(latitude.degrees);

	    //latitude minutes
	    input = $(tr + "td:eq(2) input");
	    input.val(latitude.minutes);

	    var select = $(tr + "td:eq(3) select");
	    select.val(latlng.lat() >= 0 ? "N" : "S");

	    //latitude degrees
	    var input = $(tr + "td:eq(4) input");
	    input.val(longitude.degrees);

	    //latitude minutes
	    input = $(tr + "td:eq(5) input");
	    input.val(longitude.minutes);

	    var select = $(tr + "td:eq(6) select");
	    select.val(latlng.lng() >= 0 ? "E" : "W");
	};

	this.onMapRightClick = function (point, src, overlay) {
	    if (overlay) {
	        var index = me.getMarkerIndex(overlay);

	        if (index != -1) {
	            customPoints.splice(index, 1);
	            me.drawCustomRoute();
	            me.deleteTableRow(index);
	        }
	    }
	};	

	this.onMapClick = function (overlay, latlng, overlaylatlng) {
	    if (routeBuilding) {
	        if (latlng) {
	            customPoints.push(latlng);

	            me.drawCustomRoute();

	            me.addTableRow(customPoints.length - 1, latlng);
	        }
	        else if (overlay == customPolyline) {
	            for (var i = 0; i < customPoints.length - 1; i++) {
	                var bounds = new GLatLngBounds(new GLatLng(Math.min(customPoints[i].lat(), customPoints[i + 1].lat()),
                                                                 Math.min(customPoints[i].lng(), customPoints[i + 1].lng())),
                                                    new GLatLng(Math.max(customPoints[i].lat(), customPoints[i + 1].lat()),
                                                                 Math.max(customPoints[i].lng(), customPoints[i + 1].lng())));

	                if (bounds.containsLatLng(overlaylatlng)) {
	                    customPoints.splice(i + 1, 0, overlaylatlng);
	                    me.drawCustomRoute();
	                    break;
	                }
	            }
	        }
	    }
	};

	this.getMarkerIndex = function (marker) {
	    for (var i = 0; i < customMarkers.length; i++) {
	        if (customMarkers[i] == marker)
	            return i;
	    }

	    return -1;
	};

	this.drawCustomRoute = function (skipMarkers) {
	    //clear the polyline
	    if (customPolyline)
	        map.removeOverlay(customPolyline);

	    //redraw the polyline
	    customPolyline = new GPolyline(customPoints, ROUTE_LINE_COLOR, ROUTE_LINE_WIDTH);
	    map.addOverlay(customPolyline);

	    //clear the markers unless told to skip this step
	    //this step would be skipped during marker drag
	    if (!skipMarkers) {
	        //remove markers
	        for (var i = 0; i < customMarkers.length; i++) {
	            map.removeOverlay(customMarkers[i]);
	        }

	        //reset the array
	        customMarkers = [];

	        //redraw the markers
	        for (i = 0; i < customPoints.length; i++) {
	            //set the icon type
	            var type = 1; //turning point

	            if (i == 0) //start point
	                type = 0;
	            else if (i == customPoints.length - 1) //end point
	                type = 2;

	            //grab icon
	            var icon = me.getIcon(type, "CR", i, "");

	            //add the marker
	            var marker = new GMarker(customPoints[i], { icon: icon, draggable: true, dragCrossMove: false, bouncy: false });
	            map.addOverlay(marker);

	            //add marker events
	            GEvent.addListener(marker, "dragend", function (latlng) {
	                var index = me.getMarkerIndex(this);
	                customPoints[index] = latlng;
	                me.drawCustomRoute(true);
	                me.updateTableValues(index, latlng);
	            });

	            GEvent.addListener(marker, "drag", function (latlng) {
	                var index = me.getMarkerIndex(this);
	                customPoints[index] = latlng;
	                me.drawCustomRoute(true);
	                me.updateTableValues(index, latlng);
	            });

	            //keep track of the marker
	            customMarkers.push(marker);
	        }
	    }
	};

	this.getIcon = function (type, typeCode, symbol, color) {
	    var icon = new GIcon();

	    icon.iconSize = new GSize(35, 35);
	    icon.iconAnchor = new GPoint(17, 17);

	    switch (type) {
	        case 0:
	            icon.image = "RouteSymbolGenerator.aspx?type=0&typeCode=" + typeCode + "&designator=" + symbol + "&color=" + color;
	            break;
	        case 1:
	            icon.image = "RouteSymbolGenerator.aspx?type=1&symbol=" + symbol + "&color=" + color;
	            break;
	        case 2:
	            icon.image = "RouteSymbolGenerator.aspx?type=2&symbol=" + symbol + "&color=" + color;
	            break;	        
	        case 3:
	            icon.image = "RouteSymbolGenerator.aspx?type=3&symbol=" + symbol;
	            icon.iconSize = new GSize(35, 31);
	            icon.iconAnchor = new GPoint(17, 15);
	            break;
	    }

	    return icon;
	};

	if (GBrowserIsCompatible()) {
	    map = new GMap2($(id)[0]);
	    map.setCenter(new GLatLng(loc.lat, loc.long), 10);
	    map.setUIToDefault();

	    var copyright = new GCopyrightCollection("©");
	    copyright.addCopyright(new GCopyright("JLLDT",
			  new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)),
			                    0, '2010 Deconflict.org'));

	    var tilelayer = new GTileLayer(copyright, 3, 11);
	    tilelayer.getTileUrl = function (tile, zoom) { return "SectionalTiles.ashx?x=" + tile.x + "&y=" + tile.y + "&z=" + zoom; };
	    tilelayer.isPng = function () { return true; };
	    tilelayer.getOpacity = function () { return 1.0; }

	    var tileOverlay = new GTileLayerOverlay(tilelayer);

	    var customMap = new GMapType([tilelayer], new GMercatorProjection(18), "Sectional", { errorMessage: "No data available" });

	    map.addMapType(customMap);

	    map.setMapType(customMap);

	    GEvent.addListener(map, "click", me.onMapClick);
	    GEvent.addListener(map, "singlerightclick", me.onMapRightClick);
	}
}

function numberOfConflicts(element, missionScheduleID) {
    $.ajax({
        async: true,
        type: "POST",
        url: "SchedulingService.asmx/NumberOfConflicts",
        data: JSONstring.make({ missionScheduleID: parseInt(missionScheduleID) }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var conflictCount = msg.d;

            $(element).attr("onload", "");

            if (conflictCount > 0) {
                $(element).attr("src", "RouteSymbolGenerator.aspx?type=4&symbol=" + conflictCount);
                var title = conflictCount + " conflict" + (conflictCount > 1 ? "s" : "") + " found.  Click to view.";
                $(element).attr("alt", title);
                $(element).attr("title", title);
                $(element).parent().attr("title", title);
            }
            else {
                $(element).parent().remove();
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            //swallow error
            $(element).remove();
        }
    });
}

function numberOfRequests() {
    
    $.ajax({
        async: true,
        type: "POST",
        url: "SchedulingService.asmx/NumberOfRequests",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var requestCount = msg.d;

            if (requestCount == 0)
                $("#spanRequestCount").html("");
            else
                $("#spanRequestCount").html("(" + requestCount + ")");
        },
        error: function (xhr, textStatus, errorThrown) {
            //swallow error
            $("#spanRequestCount").html("");
        }
    });


    setTimeout("numberOfRequests()", 10000);
}