// GRoutes v0.6.5
// (c) 2006 by Holger "hihp" Pollmann, Linden, Germany.
// Free for noncommercial use and alteration.
// Not free for commercial use.
// Governing law is the law of Germany.

// unfortunately, in 2006 browser determination for "cross-browser scripting"
// still seems to be necessary. Of course because of IE.
var MSIE  = document.all;
var oldNS = document.layers;
var newNS = !document.all && document.getElementById;
var opera = window.opera;

var draggableDefault = true;

function GRouteMarker(img, pos, text)
{
  var markeroptions     = {icon: new GIcon(G_DEFAULT_ICON, img),
                           draggable: draggableDefault,
                           clickable: true};
  GMarker.call(this, pos, markeroptions);

  this.text             = text;
  this.clickhandler     = GEvent.addListener(this, 'click', this.openInfoWindow);
  this.dblclickhandler  = false;
  this.infowndclhandler = false;
}

GRouteMarker.prototype = new GMarker(new GLatLng(0,0), {draggable: draggableDefault, clickable: true});

GRouteMarker.prototype.copy = function()
{
  var newmarker = new GRouteMarker(this.getIcon().image, this.getPoint(), this.text);
  return newmarker;
}

GRouteMarker.prototype.openInfoWindow = function()
{
  this.openInfoWindowHtml('<div class="hinweis">'+this.text+'</div>');
}

function GRoute(name, points_array, color, weight, opacity)
{
  GPolyline.call(this, points_array, color, weight, opacity);

  this.name       = name;
  this.color      = color;
  this.markers    = new Array();
}

GRoute.prototype = new GPolyline();
GRoute.prototype.getLength = function() {
  var num = this.getVertexCount();
  if(num > 1){
    var ret = 0;

    for(var i = 0; i < (num-1); ++i){
      ret += this.getVertex(i).distanceFrom(this.getVertex(i+1));
    }

    return ret;
  }
  return NaN;
}

GRoute.prototype.addMarker = function(img, pos, text)
{
  if(img.constructor == GRouteMarker.prototype.constructor){
    // the first argument is a marker, so it will be inserted
    var marker = img;
  } else {
    // the first argument is not a marker, so we assume we got
    // an image source string, a GLatLng and some text
    var marker = new GRouteMarker(img, pos, text);
  }

  this.markers.push(marker);

  return marker;
}

function GRouteControl(printable, selectable, shown_elements)
{
  GControl.call(this, printable, selectable);
  this.routes_        = new Array();
  this.map_           = false;
  this.curRoute       = NaN;
  this.shown_elements = shown_elements;
  this.container_     = false;
}

GRouteControl.prototype = new GControl();


GRouteControl.prototype.initialize = function(map) {
  this.map_ = map;

  var c  = document.createElement('div');
  if(MSIE){
    // special code only for Microsoft Internet Explorer... sigh
    c.className = 'RouteControl';
  } else {
    // the regular code is for the rest
	  c.setAttribute('class', 'RouteControl');
  }
  this.container_ = c;
  c.RouteControl = this;

  c.style.height   = 'auto';
  c.style.width    = 'auto';

  var controlStyle = 'radio';
  if(this.shown_elements == 1)
    controlStyle = 'select';

  if(controlStyle == 'select'){
    if(MSIE){
	    var s = document.createElement(
          '<select name="RouteSelector" id="RouteSelector" size="1" '
        + 'onchange="this.parentNode.RouteControl.routeSelected();"></select>');
	    this.RouteSelector = s;
    } else {
	    var s = document.createElement('select');
	    this.RouteSelector = s;
	    s.id = 'routeSelector';
	    s.setAttribute('name', 'RouteSelector');
	    s.setAttribute('size', '1');
	    s.setAttribute('onchange',
	                   'this.parentNode.RouteControl.routeSelected();');
    }
    c.appendChild(s);
  }
  for(var i = 0; i < this.routes_.length; ++i){
    var route      = this.routes_[i];
    var route_text = route.name
                   + ' (' + (Math.round(route.getLength())/1000) + ' km)';

    if(controlStyle == 'select'){
	    var o          = document.createElement('option');
	    o.style.color  = route.color;
	    o.appendChild(document.createTextNode(route_text));
	    o.id = 'curroute' + i;
      o.setAttribute('value', i);
      s.appendChild(o);
    } else {
	    var p          = document.createElement('p');
	    p.style.color  = route.color;
	    var rb_label   = document.createElement('label');
      if(MSIE){
        // special code only for Microsoft Internet Explorer... sigh
	      var rb         = document.createElement(
	        '<input type="radio" name="curroute="'
	      + 'id="curroute'+i+'" onclick="'
	      + 'this.parentNode.parentNode.parentNode.RouteControl.setCurrentRoute('+i+');'
	      + '">');
      } else {
        // code for the rest
	      var rb         = document.createElement('input');
	      rb.setAttribute('type', 'radio');
	      rb.setAttribute('name', 'curroute');
	      rb.id = 'curroute' + i;
	      rb.setAttribute('value', i);
	      rb.setAttribute('onclick',
	                      'this.parentNode.parentNode.parentNode.RouteControl.setCurrentRoute('
	                     + i + ');');
      }
	    rb_label.appendChild(rb);
	    rb_label.appendChild(document.createTextNode(route_text));
	    p.appendChild(rb_label);
	    c.appendChild(p);
    }
  }

  map.getContainer().appendChild(c);

  if(this.routes_.length > this.shown_elements){
    if(controlStyle == 'select'){
      c.style.height   = '22px';
	    c.appendChild(s);
    } else {
      c.style.height   = (this.shown_elements * 20) + 'px';
      c.style.overflow = 'auto';
      if(MSIE){
        c.onresize = resizeRouteControlContainer;
      }
    }
  }

  return c;
}

// IE hack to accomodate size of container so no horizontal scrollbar appears
function resizeRouteControlContainer()
{
  var bcr = this.getBoundingClientRect();
  var w   = bcr.right - bcr.left;
  this.style.width = w + 16 + 'px';
  this.onresize = '';
}

GRouteControl.prototype.addRoute = function(route)
{
  this.routes_.push(route);
}

GRouteControl.prototype.applyRoute = function(index)
{
  var route = this.routes_[index];
  this.map_.addOverlay(route);

  if(this.routes_[index].markers.length > 0){
    for(var i = 0; i < this.routes_[index].markers.length; ++i){
      this.map_.addOverlay(this.routes_[index].markers[i]);
    }
  }
}

GRouteControl.prototype.unapplyRoute = function(index)
{
  if(this.routes_[index].markers.length > 0){
    for(var i = 0; i < this.routes_[index].markers.length; ++i){
      this.map_.removeOverlay(this.routes_[index].markers[i]);
    }
  }
  this.map_.removeOverlay(this.routes_[index]);
  this.map_.closeInfoWindow();
}

GRouteControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

GRouteControl.prototype.routeSelected = function()
{
  if(typeof(this.RouteSelector) == 'undefined')
    return;

  var new_index = this.RouteSelector.selectedIndex;
  var option    = document.getElementById('curroute' + new_index);
  this.RouteSelector.style.color = option.style.color;

  this.setCurrentRoute(new_index);
}

GRouteControl.prototype.setCurrentRoute = function(index)
{
  if(index == this.curRoute)
    return;

  if((index >= 0) && (index < this.routes_.length)){
    if(!isNaN(this.curRoute))
      this.unapplyRoute(this.curRoute);
    this.curRoute = index;
    this.applyRoute(index);
    if(this.shown_elements > 1){
	    var rb = document.getElementById('curroute'+index);
	    rb.checked = true;
    } else if(this.shown_elements == 1) {
      this.RouteSelector.selectedIndex = index;
	    var option    = document.getElementById('curroute' + index);
	    this.RouteSelector.style.color = option.style.color;
    }
  } else {
    alert('Unspecified route cannot be made current route');
  }
}
