var heights = null;
var points  = null;
var seton   = 0;
var pageOffset = 20;
var trackOrTrail = null;

// true if elevation is invisible
var dontShowElevation = false;
var topOffset = "380px";
/**
 * start method for elevations
 * creates the div and its content
 */
function createElevation(waypoints, trackID, table, idColumn, minimap)
{
	trackOrTrail = table;
		
	if (dontShowElevation == false)
	{
		generateHeightAndPoints(waypoints);
		if (minimap == false)
		{
			clearElevations();
			setElevationLink();
			createElevationDiv(waypoints, trackID, table, idColumn);
		}
	}
}

function saveTrackForPrint(trackID, idColumn)
{
	if (idColumn == "wanderwegID")
	{
		document.printForm.walkValue.value = trackID;
		document.printForm.trailValue.value = "";
	}
	else 
	{
		if (idColumn == "radwegID")
		{
			document.printForm.trailValue.value = trackID;
			document.printForm.walkValue.value = "";
		}
	}
}

function clearTrackForPrint()
{
	document.printForm.trailValue.value = "";
	document.printForm.walkValue.value = "";
}
/**
 * craetes the layer on the map with the fullscreen link
 */ 
function setElevationLink(position)
{
	var el = document.getElementById("elevation");
	if (el == null)	
	{
		if ( position === undefined ) 
			position = 5;
		
		var contextmenu = document.createElement("div");
		contextmenu.setAttribute("id", "elevation");
	  contextmenu.style.background="#ffffff";
	  contextmenu.style.border="1px solid #8888FF";
	  var img = "";
	  if (trackOrTrail == "radweg_t_radwege")
	  	img = "radwegClose.jpg"
	  else if (trackOrTrail == "wanderweg_t_wanderwege")
	  	img = "wanderwegClose.jpg"; 
	  
	  contextmenu.innerHTML = '<a href="javascript:void(0);" onclick="removeTrack(); return false;" ><img src="' + hostFiller + '/images/' + img + '" /></a>';
	  getMap().getContainer().appendChild(contextmenu);
	
	  var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize( 80, position));  
	  pos.apply(contextmenu);
	 
	}
}

/**
 * removes the elevation and the track from the map
 */
function removeTrack()
{
	setLoad(true);
	clearTrackForPrint();
	clearElevations();
	clearTrail();
	
	if (document.getElementById("elevation"))
		getMap().getContainer().removeChild(document.getElementById("elevation"));
	toggle("fold", "elevationDIV");
	document.exportForm.track.value = 0;
	document.exportForm.trail.value = 0;
	
	setLoad(false);

}

/**
 * removes the elevation from the map
 */
function clearElevations()
{
	//getMap().closeInfoWindow();
	var canvas = document.getElementById("map_canvas");
	var eleDiv = document.getElementById("elevationDIV");
	var cross  = document.getElementById("cross");
	var line   = document.getElementById("line");
	var ele    = document.getElementById("elevation");
	

	if (ele && canvas)
		canvas.removeChild(ele);
		
	if (canvas && eleDiv)
	{
		canvas.removeChild(eleDiv);
		
		if (cross)
			canvas.removeChild(cross);
		//if (line)
			//canvas.removeChild(line);
	}
}

/**
 * creates the heights array from all waypoints
 */
function generateHeightAndPoints(waypoints)
{
	heights = new Array();
	points = new Array();
	if (waypoints.length < 150)
	{
		for(var i = 0; i < waypoints.length; i++)
		{
			heights[i] = waypoints[i]["ele"];
			var p = waypoints[i]["point"];
			var newPoint = new Array();
			newPoint[0] = p.lat();
			newPoint[1] = p.lng();
			points[i] = p;
		}
	}
	else
	{
		for(var i = 0; i < waypoints.length; i+=3)
		{
			heights[i] = waypoints[i]["ele"];
			var p = waypoints[i]["point"];
			var newPoint = new Array();
			newPoint[0] = p.lat();
			newPoint[1] = p.lng();
			points[i] = p;
		}
	}
	
}

/**
 * creates the div and its contents for the elevation
 */
function createElevationDiv(waypoints, trackID, table, idColumn)
{
	var elDiv = document.getElementById("elevationDIV");
	if (!elDiv)
	{
		var elevationDiv = document.createElement("div");
		//elevationDiv.setAttribute("style", "background-color: #ffffff");
		elevationDiv.style.backgroundColor = "#ffffff";
		elevationDiv.setAttribute("id", "elevationDIV");
		elevationDiv.style.height = "100px";
		elevationDiv.style.width  = "600px";
		elevationDiv.style.align = "right";
		//elevationDiv.style.top = topOffset;
		
		elevationDiv.style.position = "relative";
		elevationDiv.style.border = "1px solid #000";
	
		var img = document.createElement("img");
		source = hostFiller + "/images/track.php?trackID=" + trackID + "&table=" + table + "&idColumn=" + idColumn;
		
		img.setAttribute("src", source);
		img.setAttribute("onmouseover", "start_mousemove()");
		img.setAttribute("onmouseout", "stop_mousemove()");
		
		img.setAttribute("id", "elevationImg");
		
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			img.attachEvent("onmouseover", start_mousemove);
			img.attachEvent("onmouseout", stop_mousemove);
		}
		
		img.setAttribute("style", "border:1px;position:absolute;left:0px;width:600px;z-index:5");
		img.style.height = "100px";
		
		elevationDiv.appendChild(img);
	
		document.getElementById("map_canvas").appendChild(elevationDiv);
		
    var pos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0,0));  
    pos.apply(elevationDiv);
   
   }
   else
   {
   	var img = document.getElementById("elevationImg");
   	img.setAttribute("src", hostFiller + "/images/track.php?trackID=" + trackID + "&table=" + table + "&idColumn=" + idColumn);
		img.setAttribute("onmouseover", "start_mousemove()");
		img.setAttribute("onmouseout", "stop_mousemove()");
	
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			img.attachEvent("onmouseover", start_mousemove);
			img.attachEvent("onmouseout", stop_mousemove);	
		}
	
   }
}

/**
 * creates the line for mouseover on the elevation image
 */
function createLine()
{
	var isLine = document.getElementById("line");
	if (isLine == null)
	{
		var line = document.createElement("div");
		line.setAttribute("id", "line");
		
		line.style.height = 100;
		line.style.width  = 3;
		line.style.position = "absolute";
		
//		if (fullScreen == true)
//			line.style.top = document.getElementById("elevationDiv").style.top;
//		else
//			line.style.top = topOffset;
		
		line.style.visibility = "visible";
		line.style.zIndex = 10;
		
		var lineImg = document.createElement("img");
		if (navigator.appName == "Microsoft Internet Explorer")
			lineImg.src = hostFiller + "/images/icons/line.png";
		else
			lineImg.setAttribute("src", hostFiller + "/images/icons/line.png");
		
		lineImg.style.height = 100;
		lineImg.style.width  = 5;
		lineImg.style.zIndex = 15;
	
		line.appendChild(lineImg);
		//document.getElementById("map_canvas").appendChild(line);
			document.getElementById("elevationDIV").appendChild(line);
	} else {
		//isLine.style.visibility = "visible";
		toggle("unfold", "line");
	}
}

/**
 * creates the cross for the map center point
 */
function createCross()
{
	var isCross = document.getElementById("cross");
	if (isCross == null)
	{
		var cross = document.createElement("div");
		
		cross.setAttribute("id", "cross");
		cross.style.position = "absolute";
		cross.style.left = "350px";
		cross.style.top = "230px";
		cross.style.visiblity = "visible";
		cross.style.zIndex =   "15";
		
		var crossImg = document.createElement("img");
		crossImg.setAttribute("src", hostFiller + "/images/icons/cross.png");
		
		cross.appendChild(crossImg);
		document.getElementById("map_canvas").appendChild(cross);
	} else  {
		//isCross.style.visibility = "visible";
		toggle("unfold", "cross");
	}	
}

/**
 * sets the cross and the line and starts the mouse move
 */
function start_mousemove() 
{
	seton = 1;
	if(document.layers) 
		document.captureEvents(Event.MOUSEMOVE);

  document.onmousemove =	display_mousepos;
  createCross();
  createLine();
}

/**
 * hides the cross and the line
 */
function stop_mousemove() 
{	
  seton = 0;
	toggle("fold", "cross");
	toggle("fold", "line");
}

/**
 * displays the point depends on the mouse position
 */
function display_mousepos (evpos) 
{
	// position ermitteln
    var xPos    =  evpos? evpos.pageX : window.event.screenX;
	if (xPos < 445)
		return;
	
	if (fullScreenBool == false)	
	{
		if (xPos > 1020) 
			xPos = 1020;
	}
	else
	{
		if (xPos > (screen.availWidth - pageOffset - 12))
			xPos = screen.availWidth - pageOffset -15;
		
		if (xPos < (screen.availWidth - 585 - pageOffset) )	
			return;
	}	
  //if (seton==0)
  {
		var line = document.getElementById("line");
		
		if (fullScreenBool)
			line.style.left = xPos - (screen.availWidth - pageOffset - 25) + 575;
    else
    	line.style.left = xPos - 1010 + 575;
	
		if (fullScreenBool)
		{
			var leftPoint = screen.availWidth - pageOffset - 600;
			var mappoint_nr = Math.round(((xPos - leftPoint)*points.length/(575)));
		}
		else
			var mappoint_nr = Math.round(((xPos-450)*points.length/(590)));
		//var mappoint_nr = Math.round(((xPos-370)*points.length/(590)));
		
		var center = points[mappoint_nr];
		if (center)
		{
			var pixels = getMap().fromLatLngToContainerPixel(center);
			var cross = document.getElementById("cross");
			cross.style.left = pixels.x - 8;
			cross.style.top = pixels.y - 8;
	  		//getMap().panTo(center);
	  		//getMap().setCenter(center);  
		}
		//document.output.outputfeld1.value = " " + Math.round(heights[mappoint_nr]) + " m";	
	}
}