google.maps.Polyline.prototype.addPoint = addPoint;
function addPoint(LatLng) {
	var coords = this.getPath();
	coords.push(LatLng);
}

google.maps.Polyline.prototype.del = del;
function del() {
	this.setMap(null);
	try {
		this.window.close();
	} catch(err) {}
	
	try {
		for (var i = 0; i < this.tempMarkers.getLength();i++){
			var marker = this.tempMarkers.getAt(i);
			marker.setMap(null);
		}	
		} catch(err) {}
}

google.maps.Polyline.prototype.ClickAction = ClickAction;
function ClickAction(objNr) {		
			if (this.markers) {
		var erasemarker;
		for (var i=0; i < this.markers.getLength();i++) {
			erasemarker = this.markers.getAt(i);
			erasemarker.setMap(null);
		}
		for (var i=0; i < this.addmarkers.getLength();i++) {
			erasemarker = this.addmarkers.getAt(i);
			erasemarker.setMap(null);
		}	
	}
	var str = "<div id='inputFieldBOX' class='inputFieldBOX'>";
		str += '<div id="open" onclick="minimize(&#34;inputFieldBOX&#34;,this)" class="minimize"></div>';
		str += "<h2>Polyline, " + this.LineLength() + " m</h2>";
		for (var i in this.Info) {
			str += this.Info[i]["title"] + " : " + this.Info[i]["selected"];
			if (i < this.Info.length-1) str += "<br/>";
		}
		str += "<div class='infoWindowTool'>";
		str += '<img width="60" height="60" src="ICONS/FILL.png" title="Change polyline style" alt="Change polyline style" onclick="recreatePolyline(mapObjekts['+objNr+'],null,map);" />';
		str += '<img width="60" height="60" src="ICONS/EditPolyline.png" title="Edit polyline" alt="Edit polyline" onclick="editPoly(mapObjekts['+objNr+'],'+objNr+',-1);" />';
		str += '<img width="60" height="60" src="ICONS/DELETE.png" title="Delete polyline" alt="Delete polyline" onclick="deletObjekt(mapObjekts['+objNr+']);" />';
		str += "</div><h2 onclick='document.getElementById(&#34;inputField&#34;).innerHTML=&#34;&#34;'>[close]</h2>";
		str += "</div>";
		document.getElementById("inputField").innerHTML = str;	
}

google.maps.Polyline.prototype.LineLength = LineLength;
function LineLength() {
	var coords = this.getPath();
	var Circum = 0;
	for (var i = 0; i < coords.getLength()-1;i++){
		Circum += this.distance(coords.getAt(i),coords.getAt(i+1) );
	}
	return Math.round(Circum);
}

google.maps.Polyline.prototype.distance = distance;
function distance(c1,c2) { // returns distance in meters...
	var R = 6371; // km (change this constant to get miles)
	var dLat = (c2.lat()-c1.lat()) * Math.PI / 180;
	var dLon = (c2.lng()-c1.lng()) * Math.PI / 180;
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
			  Math.cos(c1.lat() * Math.PI / 180 ) * Math.cos(c2.lat() * Math.PI / 180 ) *
			  Math.sin(dLon/2) * Math.sin(dLon/2);
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
	var d = R * c;
	return d * 1000;
}

google.maps.Polyline.prototype.toKML = toKML;
function toKML() {
	var coords = this.getPath(); // how to find out what is what...
	var d = new Date();
	
	var id = (d.getTime() + "" + Math.floor(Math.random() *1000000)).substr(0, 18);
		
	var KMLString = "<Placemark id='" + id + clearentities(this.getKMLTitle())+"'>\n";
	KMLString += "<name>"+clearentities(this.getKMLTitle())+"</name>";
	//description
	KMLString += "<description>\n<![CDATA[";
	KMLString += "length: " + this.LineLength()+" m <br/>";

	for(var i in this.Info) {
		KMLString += this.Info[i]["title"] + " : " +  this.Info[i]["selected"]  + " <br/>";
	}
	
	KMLString += "<sub>created with <a href='http://drawamap.org'> DRAWaMAP</a> </sub>"
	KMLString += "]]> </description>\n";
	
	// addresse
	if (this.address) {
		KMLString += "<address>" + clearentities(this.address) + "</address>";
	} 
	
	// ExtendedData ...
	KMLString += "<ExtendedData>";
	for(var i in this.Info) {
		KMLString += "<Data name='" + clearentities(this.Info[i]["type"]) + "'><value>" + clearentities(this.Info[i]["selected"]) ;
		if (this.Info[i]["type"] == "select") {
			KMLString += ":";
			for (var j =0; j < this.Info[i]["options"].length;j++) {
				KMLString += clearentities(this.Info[i]["options"][j]);
				if (j+1 != this.Info[i]["options"].length) {
					KMLString += ",";
				}
			}
		} else if ( this.Info[i]["type"] == "text") {
			KMLString += ":";
		}
		
		KMLString += "</value>"
		KMLString += "<displayName><![CDATA["+ clearentities(this.Info[i]["title"])  +"]]></displayName></Data>";	
	}
	KMLString += "</ExtendedData>";

	KMLString += "<Style id='Style"+id+"'>\n";
	var strokeColor = this.strokeColor.replace("#","");
	KMLString += "<LineStyle>\n";	
   KMLString += "<color>ff"+strokeColor.substr(4,2)+strokeColor.substr(2,2)+strokeColor.substr(0,2)+"</color>\n";   // aarrbbgg     
   KMLString += "<colorMode>normal</colorMode>\n";
   KMLString += "<width>"+this.strokeWeight+"</width>\n"; 
   KMLString += "</LineStyle>\n";
   KMLString += "</Style>\n";
	
	KMLString += "<LineString id='"+window.document.title+"'>\n";
	KMLString += "<extrude>0</extrude>\n";
	KMLString += "<tessellate>0</tessellate>\n";                
	KMLString += "<altitudeMode>clampToGround</altitudeMode>\n";
	KMLString += "<coordinates>\n";
	for (var j =0; j < coords.length;j++){
		var latlng = coords.getAt(j);
		KMLString += latlng.lng()+","+latlng.lat()+" \n";
	}
	KMLString += "</coordinates>\n";
	KMLString += "</LineString>\n";
	KMLString += "</Placemark>";
	return KMLString;	
}

google.maps.Polyline.prototype.getKMLTitle = getKMLTitle;
function getKMLTitle() {
		return this.title;
}

