
var TrafficMap = Class.create({
  map: null,
  activeTab: 1,
  settings: null,
  markerManager: null,
  countyOutline: null,
  trafficMapSystemMessage: null,
  
  
  setMap: function(map) {
    this.map = map;
  },
  getMap: function() {
    return this.map;
  },
  setActiveTab: function(activeTab) {
    this.activeTab = activeTab;
  },
  getActiveTab: function() {
    return this.activeTab;
  },
  setSettings: function(settings) {
    this.settings = settings;
  },
  getSettings: function() {
    return this.settings;
  },
  getMarkerManager: function() {
    return this.markerManager;
  },
  setCountyOutline: function(countyOutline) {
    if(this.countyOutline && this.countyOutline.getPolygon()) {
      this.map.removeOverlay(this.countyOutline.getPolygon());
    }
    this.countyOutline = countyOutline;
    if(this.countyOutline.getPolygon()) {
      this.map.addOverlay(this.countyOutline.getPolygon());
    }
  },
  getCountyOutline: function() {
    return this.countyOutline;
  },
  setTrafficMapSystemMessage: function(trafficMapSystemMessage) {
    this.trafficSystemMessage = trafficMapSystemMessage;
  },
  getTrafficMapSystemMessage: function() {
    return this.trafficMapSystemMessage;
  },
  
  
  init: function() {
    this.settings = new TrafficMapSettings();
    this.settings.load();
    this.trafficMapSystemMessage = new TrafficMapSystemMessage();
    this.map = new GMap2(document.getElementById("trafficmap")); 
    var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,18));
    this.map.enableDoubleClickZoom();
    var uiOptions = this.map.getDefaultUI();
    uiOptions.controls.scalecontrol = false;
    this.map.setUI(uiOptions);
    this.map.addControl(new GScaleControl(), bottomRight);  
    this.map.setCenter(this.settings.getCenter(), this.settings.getZoomLevel());
    this.map.getInfoWindow().show();
    GEvent.addListener(this.map, "infowindowopen", iwOpen);
    GEvent.addListener(this.map, "infowindowclose", iwClose);
    GEvent.addListener(this.map, "click", function(marker, point) {
      processMapClick(marker, point);
    });
    GEvent.addListener(this.map, "dragend", function(marker, point) {
      updateSettingsBox();
    });
    GEvent.addListener(this.map, "zoomend", function(marker, point) {
      updateSettingsBox();
    });
    this.markerManager = new MarkerManager(this.map);
    toggleCountyOutline();
    this.setTimer();
    if(this.activeTab == 1) {
      retrieveTrafficSystemMessage();
    }
  },
  
  changeTab: function(newTabNumber) {
    hideElement('trafficmap_message');
    this.setActiveTab(newTabNumber);
    this.markerManager.clearMarkers();
    retrieveLegend(); 
    this.retrieveData();   
    updateSettingsBox();
    //$('trafficmap_label').innerHTML = this.getActiveTabTitle();
    updateMapLabel();
  },
  
  
  addMarkers: function(markers) {
    this.markerManager.clearMarkers();
    this.markerManager.addMarkers(markers, 0);
    this.markerManager.show();
  }, 
  
  addMarker: function(marker) {
    this.markerManager.addMarker(marker);
  },
  
  removeMarker: function(marker) {
    this.markerManager.removeMarker(marker);
  },
  
  getActiveTabTitle: function() {
    if(this.activeTab == 1) {
      return 'Traffic';
    }
    if(this.activeTab == 2) {
      return 'Traffic Cameras';
    }    
    if(this.activeTab == 3) {
      return 'Advisories';
    }
    if(this.activeTab == 4) {
      return 'Restrictions';
    }
    if(this.activeTab == 5) {
      return 'Message Signs';
    }
    if(this.activeTab == 6) {
      return 'RLE Cameras';
    }
    if(this.activeTab == 7) {
      return 'Weather';
    }    
    if(this.activeTab == 8) {
      return 'Recovery Projects';
    }
    if(this.activeTab == 9) {
      return 'Facilities';
    }
    if(this.activeTab == 10) {
      return 'Airports';
    }
    if(this.activeTab == 11) {
      return 'Bridge Restrictions';
    }
    
    return null;
  },
  
  defaultTabByTitle: function(title) {
    if(title != null) {
      var tab = null;
      if(title == 'Traffic') {
        tab = 1;
      }
      if(title == 'Cameras') {
        tab = 2;
      }
      if(title == 'Advisories') {
        tab = 3;
      }
      if(title == 'Restrictions') {
        tab = 4;
      }
      if(title == 'Message Signs') {
        tab = 5;
      }
      if(title == 'RLE') {
        tab = 6;
      }
      if(title == 'Weather') {
        tab = 7;
      }      
      if(title == 'Projects') {
        tab = 8;
      }
      if(title == 'Facilities') {
        tab = 9;
      }
      if(title == 'Airports') {
        tab = 10;
      }
      if(title == 'Bridges') {
        tab = 11;
      }
      if(tab != null) {
        map.getSettings().setActiveTab(tab);
      }
    }
    //$('tab1').tabber.tabShow(map.getSettings().getActiveTab()-1);
    map.changeTab(map.getSettings().getActiveTab());
  },
  
  retrieveData: function() {
    if(this.activeTab == 1) {
      retrieveTrafficFlow();    
    }
    else if(this.activeTab == 2) {
      retrieveMapItems('C');
    }    
    else if(this.activeTab == 3) {
      retrieveMapItems('R');
    }
    else if(this.activeTab == 4) {
      retrieveMapItems('S');
    }
    else if(this.activeTab == 5) {
      retrieveMapItems('V');
    }
    else if(this.activeTab == 6) {
      retrieveMapItems('Z');
    }
    else if(this.activeTab == 7) {
      retrieveMapItems('W');
    }
    else if(this.activeTab == 8) {
      retrieveMapItems('P');
    }
    else if(this.activeTab == 9) {
      retrieveMapItems('ODTB');
    }
    else if(this.activeTab == 10) {
      retrieveMapItems('A');
    }
    else if(this.activeTab == 11) {
      retrieveMapItems('UL');
    }
    
    retrieveSpecialTravelAlerts();
  },
  
  changeView: function(viewCode) {
    if(viewCode) {
      var view = views[viewCode];           
      this.settings.setCenter(new GLatLng(view.getLatitude(), view.getLongitude()));
      this.settings.setZoomLevel(view.getZoomLevel());
      this.settings.setView(view.getViewCode());
      this.map.setCenter(this.settings.getCenter(), this.settings.getZoomLevel());
      updateSettingsBox();
      updateMapLabel();
      toggleCountyOutline();
    }
  },
  
  setTimer: function() {
    clearInterval(updateMarkersTimerId);
    updateMarkersTimerId = setInterval('updateMarkers()', 300000);
    clearInterval(systemMessageTimerId);
    systemMessageTimerId = setInterval('retrieveTrafficSystemMessage()', 60000);
  },
  
  clearTimer: function() {
    clearInterval(updateMarkersTimerId);
    clearInterval(systemMessageTimerId);
  }
  
});

   
  
var CountyOutline = Class.create({
  points: null,
  levels: null,
  polygon: null,
  
  setPoints: function(points) {
    this.points = points;
  },
  getPoints: function() {
    return this.points;
  },
  setLevels: function(levels) {
    this.levels = levels;
  },
  getLevels: function() {
    return this.levels;
  },
  getPolygon: function() {
    return this.polygon;
  },
  
  init: function(points, levels) {
    this.polygon = new GPolyline.fromEncoded({
      color: "#0000ff",
      weight: 3,
      opacity: 0.5,
      points: points,
      levels: levels,
      zoomFactor: 2,
      numLevels: 18
    });
  }

});  
  
var TrafficMapSettings = Class.create({
  activeTab: null,
  center: null,
  zoomLevel: null,
  view: null,
  prefix: 'gov.deldot.traffic.trafficmap.',
  
  setActiveTab: function(activeTab) {
    this.activeTab = activeTab;
  },
  getActiveTab: function() {
    return this.activeTab;
  },
  setCenter: function(center) {
    this.center = center;
  },
  getCenter: function() {
    return this.center;
  },
  setZoomLevel: function(zoomLevel) {
    this.zoomLevel = zoomLevel;
  },
  getZoomLevel: function() {
    return this.zoomLevel;
  },
  setView: function(view) {
    this.view = view;
  },
  getView: function() {
    return this.view;
  },
  isCountyView: function() {
    return this.view == 'N' || this.view == 'K' || this.view == 'S';
  },
  
  load: function() {
    var lat = readCookie(this.prefix + 'centerLatitude');
    var lng = readCookie(this.prefix + 'centerLongitude');
    if(lat && lng) {
      this.center = new GLatLng(lat, lng);
    }
    else {
      this.center = defaultCenter;
    }
    
    this.zoomLevel = readCookie(this.prefix + 'zoomLevel');
    if(this.zoomLevel) {
      this.zoomLevel = parseInt(this.zoomLevel);
    }
    else {
      this.zoomLevel = 9;
    }
    
    this.activeTab = readCookie(this.prefix + 'activeTab');
    if(!this.activeTab) {
      this.activeTab = 1;
    }
    
    this.view = readCookie(this.prefix + 'view');
    if(!this.view) {
      this.view = 'ST';
    }
  },  
  
  save: function() {
    createCookie(this.prefix + 'activeTab', this.activeTab, null);
    createCookie(this.prefix + 'centerLatitude', this.center.lat(), null);
    createCookie(this.prefix + 'centerLongitude', this.center.lng(), null);
    createCookie(this.prefix + 'zoomLevel', this.zoomLevel, null);
    createCookie(this.prefix + 'view', this.view, null);
  }
  
}); 


var TrafficMapView = Class.create({
  viewCode: null,
  viewName: null,
  latitude: null,
  longitude: null,
  zoomLevel: null,
  
  setViewCode: function(viewCode) {
    this.viewCode = viewCode;
  },
  getViewCode: function() {
    return this.viewCode;
  },
  setViewName: function(viewName) {
    this.viewName = viewName;
  },
  getViewName: function() {
    return this.viewName;
  },
  setLatitude: function(latitude) {
    this.latitude = latitude;
  },
  getLatitude: function() {
    return this.latitude;
  },
  setLongitude: function(longitude) {
    this.longitude = longitude;
  },
  getLongitude: function() {
    return this.longitude;
  },
  setZoomLevel: function(zoomLevel) {
    this.longitude = zoomLevel;
  },
  getZoomLevel: function() {
    return this.zoomLevel;
  },
  
  init: function(viewCode, viewName, latitude, longitude, zoomLevel) {
    this.viewCode = viewCode;
    this.viewName = viewName;
    this.latitude = latitude;
    this.longitude = longitude;
    this.zoomLevel = zoomLevel;    
  }
  
}); 


var TrafficMapSystemMessage = Class.create({
  message: null,
  closedByUser: false,
  
  getMessage: function() {
    return this.message;
  },
  setMessage: function(message) {
    this.message = message;
  },
  getClosedByUser: function() {
    return this.closedByUser;
  },
  setClosedByUser: function(closedByUser) {
    this.closedByUser = closedByUser;
  },

  differs: function(newMessage) {
    return this.message != newMessage;
  }
  

}); 

