// GRoutes v0.6.4
// (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;

function GRouteMarker(img, pos, text)
{
  GMarker.call(this, pos, new GIcon(G_DEFAULT_ICON, img), false);

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

GRouteMarker.prototype = new GMarker(new GLatLng(0,0));

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';

  
  

  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) {      
	    var option    = document.getElementById('curroute' + index);
	    
    }
  } else {
    alert('Unspecified route cannot be made current route');
  }
}
