
/*********************************************************************\
*                                                                     *
* egeoxml.js                                         by Mike Williams *
*                                                                     *
* A Google Maps API Extension                                         *
*                                                                     *
* Renders the contents of a My Maps (or similar) KML file             *
*                                                                     *
* Documentation: http://econym.googlepages.com/egeoxml.htm            * 
*                                                                     *
***********************************************************************
*                                                                     *
*   This Javascript is provided by Mike Williams                      *
*   Blackpool Community Church Javascript Team                        *
*   http://www.commchurch.freeserve.co.uk/                            *
*   http://econym.googlepages.com/index.htm                           *
*                                                                     *
*   This work is licenced under a Creative Commons Licence            *
*   http://creativecommons.org/licenses/by/2.0/uk/                    *
*                                                                     *
\*********************************************************************/
// Version 2.6   08 Feb 2008 - Trailing whitespace wasn't removed in the previous change

// Constructor

function EGeoXml(myvar, map, url, opts){
	// store the parameters
	this.myvar = myvar;
	this.map = map;
	this.url = url;
	if (typeof url == "string") {
		this.urls = [url];
	}
	else {
		this.urls = url;
	}
	this.opts = opts || {};
	// other useful "global" stuff
	this.bounds = new GLatLngBounds();
	this.gmarkers = [];
	this.gpolylines = [];
	this.gpolygons = [];
	this.groundoverlays = [];
	this.styles = []; // associative array
	this.iwwidth = this.opts.iwwidth || 250;
	this.progress = 0;
	this.lastmarker = {};
	this.myimages = [];
	this.imageNum = 0;
}
               
// uses GXml.value, then removes leading and trailing whitespace
EGeoXml.value = function(e){
	a = GXml.value(e);
	a = a.replace(/^\s*/, "");
	a = a.replace(/\s*$/, "");
	return a;
}

// Create Polygon

EGeoXml.prototype.createPolygon = function(points, color, width, opacity, fillcolor, fillopacity, pbounds, name, desc, last){
	var thismap = this.map;
	var iwoptions = {};
	var polygonoptions = {};
	var p = new GPolygon(points, color, width, opacity, fillcolor, fillopacity, polygonoptions)
	this.map.addOverlay(p);
	this.gpolygons.push(p);
	GEvent.addListener(p, "click", function(){
		//thismap.openInfoWindowHtml(pbounds.getCenter(),html,iwoptions);
		$("#region_check input").removeAttr("checked");
		$("#check_region_" + desc).attr("checked", "checked");
		updateCarto('xml_gmap_terrain.asp');
	});
	if (last) $("#updating").hide();
}

  
// Request to Parse an XML file

EGeoXml.prototype.parse = function(){
	// clear some variables
	this.gmarkers = [];
	this.gpolylines = [];
	this.gpolygons = [];
	this.groundoverlays = [];
	this.styles = []; // associative array
	this.lastmarker = {};
	this.myimages = [];
	this.imageNum = 0;
	var that = this;
	this.progress = this.urls.length;
	for (u = 0; u < this.urls.length; u++) {
		GDownloadUrl(this.urls[u], function(doc){
			that.processing(doc);
		});
	}
}

EGeoXml.prototype.processing = function(doc){
	var that = this;
	var xmlDoc = GXml.parse(doc)
	// Read through the Styles
	var styles = xmlDoc.documentElement.getElementsByTagName("Style");
	for (var i = 0; i < styles.length; i++) {
		var styleID = styles[i].getAttribute("id");
		
		// is it a LineStyle ?
		var linestyles = styles[i].getElementsByTagName("LineStyle");
		if (linestyles.length > 0) {
			var width = parseInt(GXml.value(linestyles[0].getElementsByTagName("width")[0]));
			if (width < 1) {
				width = 5;
			}
			var color = EGeoXml.value(linestyles[0].getElementsByTagName("color")[0]);
			var aa = color.substr(0, 2);
			var bb = color.substr(2, 2);
			var gg = color.substr(4, 2);
			var rr = color.substr(6, 2);
			color = "#" + rr + gg + bb;
			var opacity = parseInt(aa, 16) / 256;
			if (!that.styles["#" + styleID]) {
				that.styles["#" + styleID] = {};
			}
			that.styles["#" + styleID].color = color;
			that.styles["#" + styleID].width = width;
			that.styles["#" + styleID].opacity = opacity;
		}
		// is it a PolyStyle ?
		var polystyles = styles[i].getElementsByTagName("PolyStyle");
		if (polystyles.length > 0) {
			var fill = parseInt(GXml.value(polystyles[0].getElementsByTagName("fill")[0]));
			var outline = parseInt(GXml.value(polystyles[0].getElementsByTagName("outline")[0]));
			var color = EGeoXml.value(polystyles[0].getElementsByTagName("color")[0]);
			
			if (polystyles[0].getElementsByTagName("fill").length == 0) {
				fill = 1;
			}
			if (polystyles[0].getElementsByTagName("outline").length == 0) {
				outline = 1;
			}
			var aa = color.substr(0, 2);
			var bb = color.substr(2, 2);
			var gg = color.substr(4, 2);
			var rr = color.substr(6, 2);
			color = "#" + rr + gg + bb;
			
			var opacity = parseInt(aa, 16) / 256;
			if (!that.styles["#" + styleID]) {
				that.styles["#" + styleID] = {};
			}
			that.styles["#" + styleID].fillcolor = color;
			that.styles["#" + styleID].fillopacity = opacity;
			if (!fill) 
				that.styles["#" + styleID].fillopacity = 0;
			if (!outline) 
				that.styles["#" + styleID].opacity = 0;
		}
	}
	
	// Read through the Placemarks
	var placemarks = xmlDoc.documentElement.getElementsByTagName("Placemark");
	for (var i = 0; i < placemarks.length; i++) {
		var name = EGeoXml.value(placemarks[i].getElementsByTagName("name")[0]);
		var desc = EGeoXml.value(placemarks[i].getElementsByTagName("description")[0]);
		var style = EGeoXml.value(placemarks[i].getElementsByTagName("styleUrl")[0]);
		var coords = GXml.value(placemarks[i].getElementsByTagName("coordinates")[0]);
		coords = coords.replace(/\s+/g, " "); // tidy the whitespace
		coords = coords.replace(/^ /, ""); // remove possible leading whitespace
		coords = coords.replace(/ $/, ""); // remove possible trailing whitespace
		coords = coords.replace(/, /, ","); // tidy the commas
		var path = coords.split(" ");
		
		// Is this a polyline/polygon?
		if (path.length > 1) {
			// Build the list of points
			var points = [];
			var pbounds = new GLatLngBounds();
			for (var p = 0; p < path.length - 1; p = p + 1) {
				var bits = path[p].split(",");
				var point = new GLatLng(parseFloat(bits[1]), parseFloat(bits[0]));
				points.push(point);
				that.bounds.extend(point);
				pbounds.extend(point);
			}
			var polygons = placemarks[i].getElementsByTagName("Polygon");
			if (polygons.length) {
				// it's a polygon grab the info from the style
				if (!!that.styles[style]) {
					var width = that.styles[style].width;
					var color = that.styles[style].color;
					var opacity = that.styles[style].opacity;
					var fillopacity = that.styles[style].fillopacity;
					var fillcolor = that.styles[style].fillcolor;
				}
				else {
					var width = 5;
					var color = "#0000ff";
					var opacity = 0.45;
					var fillopacity = 0.25;
					var fillcolor = "#0055ff";
				}
				
				if(i == placemarks.length -1)
					that.createPolygon(points, color, width, opacity, fillcolor, fillopacity, pbounds, name, desc, true);
				else
					that.createPolygon(points, color, width, opacity, fillcolor, fillopacity, pbounds, name, desc, false);
			}
		}
		
	}
	GEvent.trigger(that, "parsed");
}

