var _markerManager = null;
var _icon = null;

var _icons = null;

var _mappingInProgress = false;

var _maxCount = 5;
var _currentCount = 1;
var _timerInterval = null;

function startUp() {
//    if (_timerInterval != null)
//        clearInterval(_timerInterval);        
    setIcons();
    getVehicles();
}
function setIcons()
{
    if (_icons == null)
    {
        _icons = new Array();
        
        for (var i=0; i<8; i++)
        {
            var icon = new GIcon(G_DEFAULT_ICON);
            icon.shadow = "";
            icon.iconSize = new GSize(16,16);
            icon.iconAnchor = new GPoint(8, 8);
            icon.image = 'Images/' + getDirectionFromInteger(i) + '.png';   
            
            _icons.push(icon);      
        }
    }
}
function getDirectionFromInteger(direction)
{
    switch (direction)
    {
        case 0:
            return "N";
        case 1:
            return "NE";
        case 2: 
            return "E";
        case 3:
            return "SE";
        case 4:
            return "S";
        case 5:
            return "SW";
        case 6:
            return "W";
        case 7:
            return "NW";
    }
}
function getIconFromDirection(direction)
{
    switch (direction)
    {
        case 'N':
            return _icons[0];
        case 'NE':
            return _icons[1];
        case 'E':
            return _icons[2];
        case 'SE':
            return _icons[3];
        case 'S':
            return _icons[4];
        case 'SW':
            return _icons[5];
        case 'W':
            return _icons[6];
        case 'NW':
            return _icons[7];
    }
}
function getValueFromDDL(ddl)
{
    return ddl.options[ddl.selectedIndex].value;
}
function getVehicles()
{
    var groupId = getVehicleType();
    var daysBack = getDaysBack();

    if (groupId == null)
        groupId = 1;
    
    if (daysBack == null)
        daysBack = 0;
      
    setAndShowAVLMessage('Loading Map Data...');    
        
    _mappingInProgress = true;
        
    $.get('AVLDataRetriever.axd', { groupId: groupId, daysBack: daysBack },
        function(mappables)
        {
            var mappablesCount = mappables.length;
            
            setAndShowAVLMessage('Found and mapping ' + mappablesCount + ' positions.');
            
            var currentPosition = 1;
            
            if (_markerManager == null)
            {
                var mgrOptions = { borderPadding: 50, trackMarkers: false };
                _markerManager = new MarkerManager(map, mgrOptions);
                
                _icon = new GIcon(G_DEFAULT_ICON);
                _icon.shadow = "";
                _icon.iconSize = new GSize(16,16);
                _icon.iconAnchor = new GPoint(8, 8);
                _icon.image = "Images/posCookie.png";
            }
            
            _markerManager.clearMarkers();
            
            $.each(mappables, function(i, mappable) {
                addCookieToMap(mappable, currentPosition, mappablesCount);
                currentPosition++;
            });
            
            setAndShowAVLMessage(mappablesCount + ' Positions successfully mapped.');
            
        }, "json"
      );
      
      _mappingInProgress = false;
      
      if (daysBack == 0)
      {
//        _timerInterval = setInterval('timerElapsed();', 60000);
//            
//        setTimeout("if (getDaysBack()==0) { setAndShowAVLMessage('Automatic update in 5 minute(s).'); }", 10000);
      }
}
function setAndShowAVLMessage(message) {
    $('#avlLoadingText:visible').show();
    $('#avlLoadingText').attr('innerHTML', message);
}
function addCookieToMap(mappable, currentPosition, totalPositions){
    
    var posn = new GLatLng(mappable.Latitude, mappable.Longitude);
    //var marker = new GMarker(posn, { icon: _icon });
    var marker = new GMarker(posn, { icon: getIconFromDirection(mappable.ShortHeading) });
    marker.bindInfoWindowHtml(getInfoWindowHtml(mappable));   
    
    _markerManager.addMarker(marker, 9); 
}
function getInfoWindowHtml(mappable)
{
    var html = '<div class="avlInfoWindow">';
    html += '<b>Report Date:</b> ' + mappable.ReportDate;
    html += '<br /><b>Report Time:</b> ' + mappable.ReportTime;
    html += '<br /><b>Speed:</b> ' + mappable.CurrentSpeed + ' mph';
    html += '<br /><b>Heading:</b> ' + mappable.Heading;
    html += '</div>'; 
    
    return html;
}
function timerElapsed() {
//    _currentCount++;
//   
//    if (!_mappingInProgress)
//         setAndShowAVLMessage("Automatic update in " + (_maxCount - _currentCount + 1) + " minute(s).");
//   
//    if (_currentCount > _maxCount)
//    {
//        _currentCount = 1;
//        
//        if (!_mappingInProgress)
//            getVehicles(1, 0);
//    }
}
