var main_south = [
  {'latitude' : 50.660, 'longitude' : -.951},
  {'latitude' : 50.684, 'longitude' : -.945},
  {'latitude' : 50.703, 'longitude' : -.956},
  {'latitude' : 50.705, 'longitude' : -.960},
  {'latitude' : 50.706, 'longitude' : -.963},
  {'latitude' : 50.707, 'longitude' : -.966},
  {'latitude' : 50.708, 'longitude' : -.975},
  {'latitude' : 50.73, 'longitude' : -1.066},
  {'latitude' : 50.739, 'longitude' : -1.093},
  {'latitude' : 50.755, 'longitude' : -1.136},
  {'latitude' : 50.759, 'longitude' : -1.186},
  {'latitude' : 50.764, 'longitude' : -1.258},
  {'latitude' : 50.773, 'longitude' : -1.291},
  {'latitude' : 50.77, 'longitude' : -1.311},
  {'latitude' : 50.784, 'longitude' : -1.323},
  {'latitude' : 50.806, 'longitude' : -1.295},
  {'latitude' : 50.813, 'longitude' : -1.295},
  {'latitude' : 50.818, 'longitude' : -1.3},
  {'latitude' : 50.85, 'longitude' : -1.34},
  {'latitude' : 50.868, 'longitude' : -1.376},
  {'latitude' : 50.880, 'longitude' : -1.398}
];

var main_north = [
  {'latitude' : 50.660, 'longitude' : -.916},
  {'latitude' : 50.687, 'longitude' : -.940},
  {'latitude' : 50.703, 'longitude' : -.951},
  {'latitude' : 50.7055, 'longitude' : -.953},
  {'latitude' : 50.709, 'longitude' : -.956},
  {'latitude' : 50.712, 'longitude' : -.963},
  {'latitude' : 50.715, 'longitude' : -.974},
  {'latitude' : 50.737, 'longitude' : -1.064},
  {'latitude' : 50.765, 'longitude' : -1.135},
  {'latitude' : 50.765, 'longitude' : -1.2},
  {'latitude' : 50.768, 'longitude' : -1.235},
  {'latitude' : 50.773, 'longitude' : -1.260},
  {'latitude' : 50.782, 'longitude' : -1.295},
  {'latitude' : 50.786, 'longitude' : -1.308},
  {'latitude' : 50.798, 'longitude' : -1.296},
  {'latitude' : 50.808, 'longitude' : -1.285},
  {'latitude' : 50.813, 'longitude' : -1.287},
  {'latitude' : 50.817, 'longitude' : -1.292},
  {'latitude' : 50.826, 'longitude' : -1.303},
  {'latitude' : 50.853, 'longitude' : -1.338},
  {'latitude' : 50.871, 'longitude' : -1.373},
  {'latitude' : 50.878, 'longitude' : -1.383}
];

var north_north = [
  {'latitude' : 50.765, 'longitude' : -1.135},
  {'latitude' : 50.817, 'longitude' : -1.292}
];

var north_south = [
  {'latitude' : 50.755, 'longitude' : -1.136},
  {'latitude' : 50.813, 'longitude' : -1.295}
];

var portsmouth_east = [
  {'latitude' : 50.737, 'longitude' : -1.064},
  {'latitude' : 50.754, 'longitude' : -1.081},
  {'latitude' : 50.758, 'longitude' : -1.086},
  {'latitude' : 50.768, 'longitude' : -1.086},
  {'latitude' : 50.773, 'longitude' : -1.088},
  {'latitude' : 50.778, 'longitude' : -1.096},
  {'latitude' : 50.783, 'longitude' : -1.102},
  {'latitude' : 50.79, 'longitude' : -1.109}
];

var portsmouth_west = [
  {'latitude' : 50.739, 'longitude' : -1.093},
  {'latitude' : 50.759, 'longitude' : -1.091},
  {'latitude' : 50.768, 'longitude' : -1.091},
  {'latitude' : 50.773, 'longitude' : -1.093},
  {'latitude' : 50.777, 'longitude' : -1.099},
  {'latitude' : 50.782, 'longitude' : -1.1035},
  {'latitude' : 50.789, 'longitude' : -1.111}
];

var nab_tower = [
  {'latitude' : 50.684, 'longitude' : -.945},
  {'latitude' : 50.6842, 'longitude' : -.9452}
];

var fawley_jetty = [
  {'latitude' : 50.835, 'longitude' : -1.324},
  {'latitude' : 50.843, 'longitude' : -1.334}
];

var AIS = Class.create({
  initialize : function(element, wrapper, options){
    if(!(this.element=$(element))) return;
    if(!(this.wrapper=$(wrapper))) return;

    this.options = Object.extend({
      'controls' : false
    }, options||{});

    if(this.options.names){
      this.names = $A(this.options.names);
    }

    this.map = new dd.ui.Map(this.element, {
      'height' : this.options.height,
      'width' : this.options.width
    });

    var bounds = new GLatLngBounds(new GLatLng(50.669483540876115, -1.52435302734375), new GLatLng(50.92987248611602, -0.97503662109375));
    var point = bounds.getCenter();
    var zoom = this.map.map.getBoundsZoomLevel(bounds);
    this.map.setCenter(point, zoom);

    this.map.addPath(main_south, {'color' : '#0000FF', 'width' : 2});
    this.map.addPath(main_north, {'color' : '#0000FF', 'width' : 2});
    this.map.addPath(north_north, {'color' : '#2D426D', 'width' : 2});
    this.map.addPath(north_south, {'color' : '#2D426D', 'width' : 2});
    this.map.addPath(portsmouth_east, {'color' : '#2D426D', 'width' : 2});
    this.map.addPath(portsmouth_west, {'color' : '#2D426D', 'width' : 2});
    this.map.addPath(nab_tower, {'color' : '#FF0000', 'width' : 10});
    this.map.addPath(fawley_jetty, {'color' : '#FF8000', 'width' : 5});

    this.markerCache = {};
    this.pathCache = {};

    this.list = new Element('ul');
    this.wrapper.insert(this.list);

    document.observe('dom:loaded', function(){
      this.p = new PeriodicalExecuter(this.refreshData.bind(this), 60);
      this.refreshData();
    }.bindAsEventListener(this));
  },
  refreshData : function(){
    if(!Ajax.activeRequestCount){
      var ajax = new Ajax.Request(this.options.url.uncache(), {
        'method' : 'get',
        'onSuccess' : this.updateMap.bind(this)
      });
    }
  },
  updateMap : function(t){
    var markers = $A(t.responseXML.documentElement.getElementsByTagName('marker'));
    markers.each(function(marker, i){
      var name = marker.getAttribute('name');
      if(!this.names || (this.names && this.names.include(name))){
        if(!this.markerCache[name]){
          this.addMarker(name, marker, i);
        }else{
          this.markerCache[name].update({
            'latitude' : marker.getAttribute('lat'),
            'longitude' : marker.getAttribute('lon')
          });
        }
      }
    }, this);
  },
  addMarker : function(name, marker, i){
    this.markerCache[name] = this.map.addMarker({
      'latitude' : marker.getAttribute('lat'),
      'longitude' : marker.getAttribute('lon'),
      'label' : name.toLowerCase().toTitleCase(),
      'icon' : new dd.ui.Map.icon('/ais/icon.png', {
        'height' : 20,
        'width' : 20
      })
    });

    if(this.options.controls){
      var l = new Element('label').update(name.toLowerCase().toTitleCase());
      var c = new Element('input', {type : 'checkbox', checked : 'checked'});
      c.observe('change', this.toggle.bindAsEventListener(this, name));
      l.insert({top : c});
      this.list.insert(new Element('li').insert(l));
    }
  },
  toggle : function(e, name){
    this.markerCache[name].toggle();
  }
});

Object.extend(String.prototype, (function() {
  return {
    toTitleCase : function(){
      return this.replace(/([\w&`'‘’"“.@:\/\{\(\[<>_]+-? *)/g, function(match, p1, index, title) {
        if (index > 0 && title.charAt(index - 2) !== ":" && match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1) return match.toLowerCase();
        if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1) return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
        if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || title.substring(index - 1, index + 1).search(/[\])}]/) > -1) return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    },
    uncache : function(){
      return this+((this.indexOf('?')!=-1) ? '&' : '?')+'uncache='+new Date().getTime();
    }
  }
})());







