
if (!bounds) var bounds = new GLatLngBounds();
if (!trip_shapes) var trip_shapes = new Array();
if (!shape_selected) var shape_selected = -1;
if (!point_selected) var point_selected = -1;

var startPoint;
var endPoint;

$(function () {

  if (!map) return;
  
  $(".leg[shape]").each(function () {
    var latlng_array = eval($(this).attr('shape'));
    if (latlng_array.length == 0) return;
    
    for (var i = 0 ; i < latlng_array.length ; i++)
    {
      latlng_array[i] = new GLatLng(parseFloat(latlng_array[i][0]), parseFloat(latlng_array[i][1]));
      bounds.extend(latlng_array[i]);
    }
    this.polyline = new GPolyline(latlng_array, $(this).hasClass('walking') ? '#33FF33' : '#3333FF');
    map.addOverlay(this.polyline);
    this.polyline.hide();
    this.polyline.parent = this;
    GEvent.addListener(this.polyline, "mouseover", function() { $(this.parent).addClass('current'); });
    GEvent.addListener(this.polyline, "mouseout", function() { $(this.parent).removeClass('current'); });
    
    var title = $(this).find('.start').attr('title');
    var icon = new GIcon(reddoticon);
    
    if (title && title.indexOf('Eastbound')!=-1) icon.image = "/images/red_10_dot_e.gif";
    else if (title && title.indexOf('Westbound')!=-1) icon.image = "/images/red_10_dot_w.gif";
    else if (title && title.indexOf('Northbound')!=-1) icon.image = "/images/red_10_dot_n.gif";
    else if (title && title.indexOf('Southbound')!=-1) icon.image = "/images/red_10_dot_s.gif";
    
    this.start_point = new GMarker(this.polyline.getVertex(0), {icon:icon, title: title, zIndexProcess: function() { return 10; }});
    //if (!$(this).hasClass('walking'))
    {
      map.addOverlay(this.start_point);
      this.start_point.hide();
      this.start_point.parent = this;
      GEvent.addListener(this.start_point, "mouseover", function() { $(this.parent).addClass('current'); });
      GEvent.addListener(this.start_point, "mouseout", function() { $(this.parent).removeClass('current'); });
    }
    
    var title = $(this).find('.end').attr('title');
    var icon = new GIcon(reddoticon);
    
    if (title && title.indexOf('Eastbound')!=-1) icon.image = "/images/red_10_dot_e.gif";
    else if (title && title.indexOf('Westbound')!=-1) icon.image = "/images/red_10_dot_w.gif";
    else if (title && title.indexOf('Northbound')!=-1) icon.image = "/images/red_10_dot_n.gif";
    else if (title && title.indexOf('Southbound')!=-1) icon.image = "/images/red_10_dot_s.gif";
    
    this.end_point = new GMarker(this.polyline.getVertex(this.polyline.getVertexCount()-1), {icon:icon, title: title, zIndexProcess: function() { return 20; }});
    map.addOverlay(this.end_point);
    this.end_point.hide();
    this.end_point.parent = this;
    GEvent.addListener(this.end_point, "mouseover", function() { $(this.parent).addClass('current'); });
    GEvent.addListener(this.end_point, "mouseout", function() { $(this.parent).removeClass('current'); });
  })
  
  $("#sidebar a.stop").mouseover(function () {
    p = $(this).parent();
    if (!p.hasClass('leg')) p = p.parent();
    if (!p.hasClass('leg')) p = p.parent();
    
    if ($(this).hasClass('start'))
      p = p[0].polyline.getVertex(0);
    else
      p = p[0].polyline.getVertex(p[0].polyline.getVertexCount()-1);
    
    highlightPoint.setLatLng(new GLatLng(parseFloat(p.lat()), parseFloat(p.lng())));
    highlightPoint.show();
    
  }).mouseout(function () {
    highlightPoint.hide();
  })
  
  $("#sidebar ul.options li h3").click(function() {
    $("#sidebar ul.options li").removeClass('selected');
    $(this).parent().addClass('selected');
    
    $(".walking[shape], .transit[shape]").each(function(){
      this.polyline.hide();
      this.start_point.hide();
      this.end_point.hide();
    })
    $(this).parent().find(".walking[shape], .transit[shape]").each(function () {
      this.polyline.show();
      this.start_point.show();
      this.end_point.show();
    }).eq(0).each(function () {
      p = this.polyline.getVertex(0);
      //startPoint.setLatLng(new GLatLng(p.lat(), p.lng()));
      //startPoint.show();
    });
    
    if (!map.getBounds().containsBounds(bounds))
    {
      map.setCenter(bounds.getCenter());
      map.setZoom(map.getBoundsZoomLevel(bounds));
    }
    return false;
  });
  
  map.setCenter(bounds.getCenter());
  map.setZoom(map.getBoundsZoomLevel(bounds));  

  
  $("#sidebar ul.options li h3").eq(0).click();
  
});
