
var TrafficMap = Class.create({
  map: null,
  activeTab: 1,
  activeTabs: null,
  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;
  },
  setActiveTabs: function(activeTabs) {
    this.activeTabs = activeTabs;
  },
  getActiveTabs: function() {
    return this.activeTabs;
  },
  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();
    }
    populateCategories(this.getSettings().getActiveTabs());
  },
  
  changeTab: function(newTabNumber, tabs) {
    hideElement('trafficmap_message');
    this.setActiveTab(newTabNumber);
    this.setActiveTabs(tabs); //map- string
    this.markerManager.clearMarkers();
    retrieveLegend();
    this.retrieveData();
    updateSettingsBox();
    updateMapLabel();
  },
  
  addLayer: function(field, newTabNumber) {
    hideElement('trafficmap_message');
    this.populateActiveTabs(field, newTabNumber);
    this.markerManager.clearMarkers();
    retrieveLegend();
    this.retrieveMultipleData(field);
    updateSettingsBox();
    updateMapLabel();
  },

  populateActiveTabs: function(field, newTabNumber) {
      var activeTabSet = false;
    if (field[newTabNumber-1].checked) {
      this.setActiveTab(newTabNumber);
        activeTabSet = true;
    }
      var foundOne = false;
      var tabs = "";
      var index = 0;
      for (var i = 0; i < field.length; i++) {
        if (!activeTabSet && field[i].checked && !foundOne) {
          this.setActiveTab(field[i].value);
          foundOne = true;
        }
        if (field[i].checked) {
          if (tabs.length == 0) {
              tabs = field[i].value;
          } else {
          tabs = tabs + "," + field[i].value;
          }
        }
      }
      this.setActiveTabs(tabs); //map- string
      this.getSettings().setActiveTabs(tabs);
      this.getSettings().setActiveTab(this.getActiveTab());
      createCookie(this.getSettings().prefix + 'activeTab', this.activeTab, null);
      createCookie(this.getSettings().prefix + 'activeTabs', this.activeTabs, null);
  },
    
  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, field) {
    if (title != null) {
      var tab = null;
      if (title == 'Traffic') {
        tab = 1;
      }
      else if(title == 'Cameras') {
        tab = 2;
      }
      else if (title == 'Advisories') {
        tab = 3;
      }
      else if (title == 'Restrictions') {
        tab = 4;
      }
      else if (title == 'Message Signs') {
        tab = 5;
      }
      else if (title == 'RLE') {
        tab = 6;
      }
      else if (title == 'Weather') {
        tab = 7;
      }      
      else if (title == 'Projects') {
        tab = 8;
      }
      else if (title == 'Facilities') {
        tab = 9;
      }
      else if (title == 'Airports') {
        tab = 10;
      }
      else if (title == 'Bridges') {
        tab = 11;
      }

      if (tab != null) {
        map.getSettings().setActiveTab(tab);
        map.getSettings().setActiveTabs("" + tab); // settings- string
      }
    }

    map.changeTab(map.getSettings().getActiveTab(), map.getSettings().getActiveTabs());
    if (field != null) {
      if (tab && tab != null) {
        for (var i = 0; i < field.length; i++) {
          field[i].checked = false;
        }
        field[tab-1].checked = true;
      }
      //} else {
//        field[0].checked = true;
//      }
    }
  },

  retrieveData: function() {
    var tmpArray = this.activeTabs.split(',');
    var numLayers = tmpArray.length;

    if (numLayers == 1) {
        switch (parseInt(this.activeTab)) {
            case 1:
                retrieveTrafficFlow();
                break;
            case 2:
                retrieveMapItems('C');
                break;
            case 3:
                retrieveMapItems('R');
                break;
            case 4:
                retrieveMapItems('S');
                break;
            case 5:
                retrieveMapItems('V');
                break;
            case 6:
                retrieveMapItems('Z');
                break;
            case 7:
                retrieveMapItems('W');
                break;
            case 8:
                retrieveMapItems('P');
                break;
            case 9:
                retrieveMapItems('ODTB');
                break;
            case 10:
                retrieveMapItems('A');
                break;
            case 11:
                retrieveMapItems('UL');
                break;
        }
    } else {
        var tabs = new Array();
        for (var i = 0; i < numLayers; i++) {
          tabs[i] = parseInt(tmpArray[i]);
        }
        var categories = getCategories(tabs);
        if (categories.length > 0) retrieveMapItems(categories);
    }
    
    retrieveSpecialTravelAlerts();
  },
    
  retrieveMultipleData: function(field) {
    var tabs = new Array();
    var index = 0;
    for (var i = 0; i < field.length; i++) {
      if (field[i].checked) {
        tabs[index++] = parseInt(field[i].value);
      }
    }
     
    var categories = getCategories(tabs);
    if (categories.length > 0) retrieveMapItems(categories);
    
    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,
  activeTabs: null,
  center: null,
  zoomLevel: null,
  view: null,
  prefix: 'gov.deldot.traffic.trafficmap.',
  
  setActiveTab: function(activeTab) {
    this.activeTab = activeTab;
  },
  getActiveTab: function() {
    return this.activeTab;
  },
  setActiveTabs: function(activeTabs) {
    this.activeTabs = activeTabs;
  },
  getActiveTabs: function() {
    return this.activeTabs;
  },
  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.activeTabs = readCookie(this.prefix + 'activeTabs');
    if(!this.activeTabs) {
      this.activeTabs = '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 + 'activeTabs', this.activeTabs, 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;
  }
});

