﻿//global reference to Google Map object
var _gmap;
var _currentPoint;
var _regionalCenters;
var _corridors;
var _tripDisplay = new TripDisplay();

function loadMap() {

    if (GBrowserIsCompatible()) {
        var mapDiv = $("#TheMap");
        _gmap = new GMap2(mapDiv[0]);
        _gmap.setCenter(new GLatLng(45.471510, -122.760778), 11);
        _gmap.setMapType(G_NORMAL_MAP);

        new GKeyboardHandler(_gmap);
        _gmap.disableDoubleClickZoom();

        var backBoundary = new GLatLngBounds(new GLatLng(44.0, -124.0), new GLatLng(46.0, -121.0));
        var backMap = new GGroundOverlay("images/bb.gif", backBoundary);
        _gmap.addOverlay(backMap);

        var boundaries = new GLatLngBounds(new GLatLng(45.215729, -123.222594), new GLatLng(45.717593, -122.294691));
        var bmap = new GGroundOverlay("images/baseMap_AI_120.jpg", boundaries);
        _gmap.addOverlay(bmap);
        
        GEvent.addListener(_gmap, "mousemove", function(point) {
            _currentPoint = point;
            mouseOverCursor();
        });

        _gmap.addControl(new MapToolControl());

        var ovcontrol = new GOverviewMapControl(new GSize(175, 175));
        _gmap.addControl(ovcontrol);

        addDummyWindow();

        addRegionalCenters();
        addCorridors();     
    }
}

function ClearMap() {
    //Clear highlighted corridors
    _corridors.ClearSelected();
    _tripDisplay.Clear();
    _system = new TransitSystem(); //Start a new System Object.
    _tripDisplay.EditsMade(false);
    _maxCostReached = false;
}

function addDummyWindow() {
     var tab2 = new GInfoWindowTab("MapTab", "<div id='detailmap'></div>");
     var infoTabs = [tab2];
     try {
         _gmap.openInfoWindowTabs(_gmap.getCenter(), infoTabs);
         _gmap.closeInfoWindow();
     }
     catch (e) {

     }
}

/******** EVENT HANDLERS *********/

function handleRegionalCenterHover(regionalCenterName) {
    var regionalCenter = _regionalCenters.FindByName(regionalCenterName);
    
    if (regionalCenter) {
        if (!regionalCenter.IsHighlighted()) {
            regionalCenter.marker.setImage(_leftLeanHighlight);
        }
    }
}

function handleRegionalCenterHoverOut(regionalCenterName) {
    var regionalCenter = _regionalCenters.FindByName(regionalCenterName);
    if (regionalCenter) {
        if (!regionalCenter.IsHighlighted()) {
            regionalCenter.marker.setImage(_leftLean);
        }
    }
}

function handleCorridorMouseOver(corridorName, line) {
    var corridor = _corridors.FindByName(corridorName);
    document.body.style.cursor = 'pointer';
    if (corridor) {
        if (!corridor.selected) {
            corridor.Line.setStrokeStyle({ color: _secondaryHighlightedColor });
        }
   }
}

function handleCorridorMouseOut(corridorName, line) {
    var corridor = _corridors.FindByName(corridorName);
    document.body.style.cursor = 'default';
    if (corridor) {
        if (corridor.selected == true) {
            corridor.Line.setStrokeStyle({ color: _highlightedColor });
        }
        else {
            corridor.Line.setStrokeStyle({ color: _defaultColor });
        }
    }
}

/******** END EVENT HANDLERS *********/

/******** Map Toolbar **********/
/// <summary>
/// Create the basic control for the map tools
/// prototype: add it as a new control to google maps
/// prototype.initialize = construct the elements that are inside the control
/// </summary>
/// <returns>null</returns>
function MapToolControl() {
}
MapToolControl.prototype = new GControl();

MapToolControl.prototype.initialize = function(_gmap) {
    var container = document.createElement("div");

    // #### Clear tool
    var mapClear = document.createElement("div");
    mapClear.setAttribute("id", "clearMap");
    mapClear.setAttribute("title", "Clear current system");
    this.setButtonStyle_(mapClear);
    container.appendChild(mapClear);
    GEvent.addDomListener(mapClear, "click", function() {
        ClearMap();
    });

    var mapSave = document.createElement("div");
    mapSave.setAttribute("id", "saveSystem");
    mapSave.setAttribute("title", "Save current system");
    this.setButtonStyle_(mapSave);
    container.appendChild(mapSave);
    GEvent.addDomListener(mapSave, "click", function() {
        DoneWithSystem();
    });
    
    _gmap.getContainer().appendChild(container);
    return container;
}

// Sets the proper CSS for the given button element.
MapToolControl.prototype.setButtonStyle_ = function(button) {
    if (button.id == "clearMap") {
        button.style.backgroundImage = "url(images/clear.gif)";
        button.style.left = "97px";
    } else if (button.id == "saveSystem") {
        button.style.backgroundImage = "url(images/save.gif)";
        button.style.left = "5px";
    }

    button.style.position = "absolute";
    button.style.top = "5px";
    button.style.width = "90px";
    button.style.height = "26px";
    button.style.cursor = "pointer";
    button.style.zindex = "3";
}

function getIcon() {
    var icon = new GIcon();
    icon.image = _leftLean;
    icon.iconSize = new GSize(20, 20);
    icon.iconAnchor = new GPoint(18, 19);
    icon.infoWindowAnchor = new GPoint(12, 25);
    return icon;
}

function getJunctionIcon() {
    var icon = new GIcon();
    icon.image = _junctionIcon;
    icon.iconSize = new GSize(20, 26);
    icon.iconAnchor = new GPoint(10, 13);
    return icon;
}

function showPrevSystems() {
    $("#wrapperSystems").slideToggle();
}


function toggleInfoBox(name) {
    //turn on the name, turn off all others
    $('#divCapCost').hide('puff');
    $('#divRiders').hide('puff');
    $('#divBenefit').hide('puff');
    $('#divOpCost').hide('puff');
    if ($(name).is(':visible')) {
        $(name).hide('puff');
    }
    else {
        $(name).show('puff');
    }
}

function isWESCorridor(corridorId) {
    if (corridorId in {'10.14':0,'14.28':0, '6.8':0, '8.10':0, '4.6':0 })
        return true;
    else
        return false;
}

function mouseOverCursor() {
    if (_isIE) {
        $('shape').each(function() {
            this.style.cursor = "pointer";
        });
    } else {
        $('path').each(function() {
            this.style.cursor = "pointer";
        });
    }
}