// global variables and constants
var height;
var map = null;
var isIE = false;
var ie55 = false;
var isEnglish = location.href.indexOf('_e') > 0;
var mapCentre = new GLatLng(29.0, 84.0);
var grid = null;
var listeners = new Array();
var marker = null;
function addListener(obj, event, f) {
  listeners[event] = GEvent.addListener(obj, event, f);
}
function removeListener(event) {
  GEvent.removeListener(listeners[event]);
  listeners[event] = null;
}
var wpoints = new Array();
wpoints[201] = [28.8396176,83.7863088]
wpoints[202] = [28.8496074,83.7903548]
wpoints[203] = [28.8698753,83.7958467]
wpoints[204] = [28.8895428,83.8040811]
wpoints[205] = [28.8912806,83.8030457]
wpoints[206] = [28.9153581,83.8203353]
wpoints[207] = [28.9182599,83.8228083]
wpoints[208] = [28.9316132,83.8255066]
wpoints[209] = [28.9392562,83.8095582]
wpoints[210] = [28.9616599,83.8014756]
wpoints[211] = [29.0056163,83.8560784]
wpoints[212] = [29.0056163,83.8560784]
wpoints[213] = [29.0056163,83.8560784]
wpoints[214] = [29.0071458,83.8602948]
wpoints[215] = [29.0537031,83.8765705]
wpoints[216] = [29.061894,83.8729061]
wpoints[217] = [29.1159781,83.886956]
wpoints[218] = [29.1200272,83.8871974]
wpoints[219] = [29.1200272,83.8871974]
wpoints[220] = [29.1236778,83.8885385]
wpoints[221] = [29.1243479,83.8885492]
wpoints[222] = [29.1114367,83.9082313]
wpoints[223] = [29.092013,83.9326488]
wpoints[224] = [29.0915208,83.9334816]
wpoints[225] = [29.100905,83.933723]
wpoints[226] = [29.1004176,83.9342809]
wpoints[227] = [29.1775035,83.9567846]
wpoints[228] = [29.1814821,83.9569758]
wpoints[229] = [29.1836999,83.9571977]
wpoints[230] = [29.1839575,83.9569616]
wpoints[231] = [29.184379,83.9575839]
wpoints[232] = [29.18518,83.9563823]
wpoints[233] = [29.1902659,83.9506263]
wpoints[234] = [29.1904158,83.9469087]
wpoints[235] = [29.2046422,83.9410937]
wpoints[236] = [29.2050027,83.9408898]
wpoints[237] = [29.1810366,83.9570957]
wpoints[238] = [29.180857,83.9586246]
wpoints[239] = [29.1299618,83.9633775]
wpoints[240] = [29.0952096,83.9921038]
wpoints[241] = [29.0947458,83.9986271]
wpoints[242] = [29.0951583,83.9982247]
wpoints[243] = [29.0902176,84.0054131]
wpoints[244] = [29.0861538,84.0228612]
wpoints[245] = [29.0957538,84.019895]
wpoints[246] = [29.0732091,83.9396936]
wpoints[247] = [28.9637309,83.8029599]
wpoints[248] = [28.7827737,83.7248533]
wpoints[249] = [29.0681126,83.8830346]
wpoints[250] = [29.2030408,83.9416409]
wpoints[251] = [29.2089172,83.9434755]

// Map Type
GMapType.prototype.getMinimumResolution = function(latlng) {
  return 7;
}
GMapType.prototype.getMaximumResolution = function(latlng) {
  return 18;
}
G_HYBRID_MAP.gridColor = '#ccc';

// Grid Overlay
function GridOverlay() {
}
GridOverlay.prototype = new GOverlay();
GridOverlay.prototype.gridSteps = [5400, 3600, 1800, 1200, 600, 300, 120, 60, 30, 20, 10, 5, 2, 1, 1, 1, 1, 1];
GridOverlay.prototype.initialize = function(map) {
  var div = document.createElement("div");
  div.style.position = "absolute";
  div.style.fontFamily = "Tahoma,Arial,Helvetica,sans-serif";
  div.style.fontSize = "xx-small";
  div.style.overflow = "hidden";
  div.style.visibility = "hidden";

  map.getPane(G_MAP_MAP_PANE).appendChild(div);
  addListener(map, "movestart", function() {
    div.style.visibility = "hidden";
  });
  var self = this;
  addListener(map, "moveend", function() {
    self.redraw(true);
  });
  addListener(map, "resize", function() {
    self.redraw(true);
  });

  this.map_ = map;
  this.div_ = div;

  return div;
}
GridOverlay.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
  removeListener("movestart");
  removeListener("moveend");
  removeListener("resize");
}
GridOverlay.prototype.copy = function() {
  return new GridOverlay();
}
function format(deg) {
  var pos = Math.abs(deg) + 0.0000005;
  var min = Math.floor(pos * 60) % 60;
  return '<div style="position:absolute; top:2px; left:2px;">' + Math.floor(pos) + '&deg;&nbsp;' + (min < 10 ? '0' : '') + min + "'</div>";
}
GridOverlay.prototype.redraw = function(force) {
  if (!force) { return; }

  while (this.div_.hasChildNodes()) { this.div_.removeChild(this.div_.firstChild); }
  var bounds = this.map_.getBounds();
  var sw = this.map_.fromLatLngToDivPixel(bounds.getSouthWest());
  var ne = this.map_.fromLatLngToDivPixel(bounds.getNorthEast());
  this.div_.style.color = this.map_.getCurrentMapType().gridColor;
  this.div_.style.top = ne.y + "px";
  this.div_.style.left = sw.x + "px";
  this.div_.style.width = this.map_.getSize().width + "px";
  this.div_.style.height = this.map_.getSize().height + "px";
  
  var step = this.gridSteps[this.map_.getZoom()] / 60;
  var w = Math.ceil(bounds.getSouthWest().lng() / step) * step;
  var s = Math.ceil(bounds.getSouthWest().lat() / step) * step;
  for (var next = w; next <= bounds.getNorthEast().lng() + 0.00005; next += step) {
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.top = "0px";
    div.style.left = (this.map_.fromLatLngToDivPixel(new GLatLng(s, next)).x - sw.x) + "px";
    div.innerHTML = '<img src="pixel.png" width="1" height="' + this.map_.getSize().height + '" />' + format(next);
    this.div_.appendChild(div);
  }
  for (var next = s; next <= bounds.getNorthEast().lat() + 0.00005; next += step) {
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.top = (this.map_.fromLatLngToDivPixel(new GLatLng(next, w)).y - ne.y) + "px";
    div.style.left = "0px";
    div.innerHTML = '<img src="pixel.png" width="' + this.map_.getSize().width + '" height="1" align="top" />' + format(next);
    this.div_.appendChild(div);
  }
  this.div_.style.visibility = "visible";
}

// OnOffControl
function OnOffControl(onImage, offImage, pos, onTrue, onFalse, initState, title) {
  this.on_ = onImage;
  this.off_ = offImage;
  this.pos_ = pos;
  this.onTrue_ = onTrue || function() { };
  this.onFalse_ = onFalse || function() { };
  this.st_ = initState;
  this.tit_ = title || "";
  this.grp_ = null;
}
OnOffControl.prototype = new GControl();
OnOffControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  container.style.cursor = ie55 ? "hand" : "pointer";
  container.style.width = '40px';
  container.style.height = '20px';
  container.title = this.tit_;
  container.innerHTML = '<div style="position:absolute; top:0px; left:0px; width:40px; height:20px; border-style:none;"><img id="' + this.on_ +
    '" src="' + (this.st_ ? this.on_ : this.off_) + '" /></div>';
  var self = this;
  GEvent.addDomListener(container, "click", function() {
    if (!self.grp_ || !self.st_) { self.changeState(); }
  });
  map.getContainer().appendChild(container);
  return container;
}
OnOffControl.prototype.getDefaultPosition = function() {
  return this.pos_;
}
OnOffControl.prototype.changeState = function() {
  if (!this.st_ && this.grp_) {
    this.grp_.getSelected().changeState();
    this.grp_.setSelected(this);
  }
  this.st_ = !this.st_;
  document.getElementById(this.on_).src = (this.st_ ? this.on_ : this.off_);
  this.st_ ? this.onTrue_() : this.onFalse_();
}
OnOffControl.prototype.getState = function() {
  return this.st_;
}
OnOffControl.prototype.addToGroup = function(grp) {
  this.grp_ = grp;
}

var icon = new GIcon();
icon.shadow = "pin_shadow.png";
icon.iconSize = new GSize(32, 32);
icon.shadowSize = new GSize(59, 32);
icon.iconAnchor = new GPoint(9, 31);
function createMarker(p) {
  var marker = new GMarker(new GLatLng(p[0], p[1]), {
    icon: new GIcon(icon, "pin" + Math.floor(Math.random() * 9) + ".png"),
    clickable: false
  });
  return marker;
}

// map setup
function loadMap() {
  if (GBrowserIsCompatible()) {
    // create new Map object
    map = new GMap2(document.getElementById("map"), {mapTypes:[G_HYBRID_MAP], logoPassive:true});
    map.resize = function() {
      this.savePosition();
      this.checkResize();
      this.returnToSavedPosition();
    }
    map.setCenter(mapCentre, 10, G_HYBRID_MAP);
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    // position the map
    if (!ie55) {
      // add polyline in all browsers but IE 6-
      var points = "istnDio_~N_Sc{Bgt@_}AarAar@mpAun@}h@y]{r@}OcTyH{fAye@eiBeQajCibA}kCa~Aw`Byp@aSlIsXpqAemBlbAeiAeKqxAaj@itDkeHcyCt]kqBsmBoaAsKsh@a~@eoBxUylAeRuiBm[iz@c_@z{A_hBlsBisCiiAqg@waD}m@keBpIwzEcjAxwDkQ~aFcXlkBauAdPgeC";
      var levels = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
      var routeTo = new GPolyline.fromEncoded({
        color: "#0000ff",
        weight: 5,
        points: points,
        levels: levels,
        zoomFactor: 32,
        numLevels: 4
      });
      map.addOverlay(routeTo);
      points = "yuqpD{au_Oig@h`DfmDt`Ea@`pBrm@jkDiD|bAxd@zvAhcC|m@bkAdaBjfBt[n~B~`@dF`k@vY~mBllAdZzhAzo@ldCwi@r{@wsBzzBpq@`tArmBnhBti@j|GfeA";
      levels = "BBBBBBBBBBBBBBBBBBBBB";
      var routeFrom = new GPolyline.fromEncoded({
        color: "#00003f",
        weight: 5,
        points: points,
        levels: levels,
        zoomFactor: 32,
        numLevels: 4
      });
      map.addOverlay(routeFrom);
      points = "gobqDazl_OkVvBwb@tf@zAtO}a@x@gaAtn@xa@_bApOgGvf@nBhaA{d@";
      levels = "BBBBBBBBBB";
      var routeLo = new GPolyline.fromEncoded({
        color: "#00001f",
        weight: 5,
        points: points,
        levels: levels,
        zoomFactor: 32,
        numLevels: 4
      });
      map.addOverlay(routeLo);
      points = "yuqpD{au_Ot\\wa@hEiKhPojA`DmX~Nqi@qJy^yWCfIT_`@nv@{Ozc@tLdj@xDv[aNdh@yAhO";
      levels = "BBBBBBBBBBBBBBB";
      var routeLuri = new GPolyline.fromEncoded({
        color: "#00001f",
        weight: 5,
        points: points,
        levels: levels,
        zoomFactor: 32,
        numLevels: 4
      });
      map.addOverlay(routeLuri);
    }
    grid = new GridOverlay();
    map.addOverlay(grid);
    map.addControl(new GSmallMapControl());
    map.addControl(new GScaleControl());
    map.addControl(new OnOffControl((isIE ? "collapse.gif" : "collapse.png"), (isIE ? "enlarge.gif" : "enlarge.png"), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 0)),
      toggleMapSize, toggleMapSize, false, (isEnglish ? 'Toggle map size' : 'Vollbild ein/aus')));
  } else {
    var warntext = isEnglish ?
      "Unfortunately, your browser isn't supported by Google Maps. A list of <a href='http://maps.google.com/support/bin/answer.py?answer=16532&amp;topic=1499'>compatible browser versions</a> is available from a Google site." :
      "Leider wird Ihr Browser von Google Maps nicht unterst&uuml;tzt. Eine <a href='http://maps.google.com/support/bin/answer.py?answer=16532&amp;topic=1499'>Liste unterst&uuml;tzter Browserversionen</a> ist direkt bei Google einsehbar.";
    document.getElementById("map").innerHTML = warnText;
  }
}

function toggleMapSize() {
	if (document.getElementById) {
		var containerDiv = document.getElementById("map")
		var contentTop = document.getElementById("content").offsetTop
		var contentHeight = document.getElementById("content").clientHeight
	} else if (document.all) {
		var containerDiv = document.all.map
		var contentTop = document.all.content.offsetTop
		var contentHeight = document.all.content.clientHeight
	}
	if (containerDiv.maximized) {
		containerDiv.style.top = (contentTop + contentHeight + 10) + 'px'
		containerDiv.style.height = (height - contentTop - contentHeight - 20) + 'px'
	} else {
		containerDiv.style.top = contentTop + 'px'
		containerDiv.style.height = (height - contentTop - 10) + 'px'
	}
	containerDiv.maximized = !containerDiv.maximized
	map.resize()
}

function resizeMapContainer() {
	var imgIx = (isCS(runner) ? max_pic_index - idx_index - 1 : 0)
	var ready = (document.images[imgIx].complete == true)
	if (!ready) {
		setTimeout("resizeMapContainer()", 100)
		return
	}
	if (document.getElementById) {
		var contentDiv = document.getElementById("content")
		var mapDiv = document.getElementById("map")
	} else if (document.all) {
		var contentDiv = document.all.content
		var mapDiv = document.all.map
	}
	var topOffset = contentDiv.offsetTop + contentDiv.clientHeight + 10
	mapDiv.style.top = topOffset + 'px'
	mapDiv.style.height = (height - topOffset - 10) + 'px'
	if (!map)
		loadMap()
	else {
		if (marker) {
			map.removeOverlay(marker)
			marker = null
		}
		map.resize()
	}
	var ix = seq[runner]
	if (wpoints[ix]) {
		marker = createMarker(wpoints[ix])
		map.addOverlay(marker)
		map.panTo(marker.getLatLng())
	}
}
