// MGeoRSS: GMaps API extension 
// copyright 2006 Mikel Maron (email: mikel_maron yahoo com)
// http://brainoff.com/gmaps/mgeorss.html
// This work is public domain

function MGeoRSS(){}

MGeoRSS.prototype.initialize=function(map) {
    this.map = map;
    this.rssurl = false;
    this.request = false;
	this.visible = true;
	this.polyLines = new Array();
	this.polyIndex = 0;
	this.aPolyline = "";
	this.iconN = new GIcon();
	this.iconN.image = "./images/icons/arrowN.png";
	this.iconN.iconSize = new GSize(16,16);
	this.iconN.iconAnchor = new GPoint (8,8)
	this.iconN.infoWindowAnchor = new GPoint(8,8);
	this.iconNE = new GIcon();
	this.iconNE.image = "./images/icons/arrowNE.png";
	this.iconNE.iconSize = new GSize(16,16);
	this.iconNE.iconAnchor = new GPoint (8,8)
	this.iconNE.infoWindowAnchor = new GPoint(8,8);
	this.iconE = new GIcon();
	this.iconE.image = "./images/icons/arrowE.png";
	this.iconE.iconSize = new GSize(16,16);
	this.iconE.iconAnchor = new GPoint (8,8)
	this.iconE.infoWindowAnchor = new GPoint(8,8);
	this.iconSE = new GIcon();
	this.iconSE.image = "./images/icons/arrowSE.png";
	this.iconSE.iconSize = new GSize(16,16);
	this.iconSE.iconAnchor = new GPoint (8,8)
	this.iconSE.infoWindowAnchor = new GPoint(8,8);
	this.iconS = new GIcon();
	this.iconS.image = "./images/icons/arrowS.png";
	this.iconS.iconSize = new GSize(16,16);
	this.iconS.iconAnchor = new GPoint (8,8)
	this.iconS.infoWindowAnchor = new GPoint(8,8);
	this.iconSW = new GIcon();
	this.iconSW.image = "./images/icons/arrowSW.png";
	this.iconSW.iconSize = new GSize(16,16);
	this.iconSW.iconAnchor = new GPoint (8,8)
	this.iconSW.infoWindowAnchor = new GPoint(8,8);
	this.iconW = new GIcon();
	this.iconW.image = "./images/icons/arrowW.png";
	this.iconW.iconSize = new GSize(16,16);
	this.iconW.iconAnchor = new GPoint (8,8)
	this.iconW.infoWindowAnchor = new GPoint(8,8);
	this.iconNW = new GIcon();
	this.iconNW.image = "./images/icons/arrowNW.png";
	this.iconNW.iconSize = new GSize(16,16);
	this.iconNW.iconAnchor = new GPoint (8,8)
	this.iconNW.infoWindowAnchor = new GPoint(8,8);
	this.iconQQ = new GIcon();
	this.iconQQ.image = "./images/icons/arrowQQ.png";
	this.iconQQ.iconSize = new GSize(16,16);
	this.iconQQ.iconAnchor = new GPoint (8,8)
	this.iconQQ.infoWindowAnchor = new GPoint(8,8);
}

MGeoRSS.prototype.setIcon=function(img) {
	this.icon = new GIcon();
	this.icon.image = "./images/icons/arrowstop.png";
	this.icon.iconSize = new GSize(10,10);
	this.icon.iconAnchor = new GPoint (5,5)
	this.icon.infoWindowAnchor = new GPoint(5,5);

}

MGeoRSS.prototype.showHide=function() {
	if (this.visible == true) {
		for (var i=0;i<this.markers.length;i++) {
			try { this.map.removeOverlay(this.markers[i]); } catch(e) {}
		}
		this.visible = false;
	} else {
		for (var i=0;i<this.markers.length;i++) {
			try { this.map.addOverlay(this.markers[i]); } catch(e) {}
		}
		this.visible = true;
	}
}
	
	
	
MGeoRSS.prototype.load=function(url,proxyurl,autozoom) {
	if (this.request != false) { return; }
 	this.rssurl = url;
	if (autozoom) {
		this.autozoom = autozoom;
		this.bounds = new GLatLngBounds();
	}
    this.request = GXmlHttp.create();
	if (proxyurl != undefined) {
      		this.request.open("GET",proxyurl + this.rssurl,true);
	} else {
		this.request.open("GET",this.rssurl, true);
	}
	var m = this;
	this.request.onreadystatechange = function() {
		m.callback();
	}
	this.request.send(null);
}

MGeoRSS.prototype.callback = function() {
	if (this.request.readyState == 4) {
		if (this.request.status == "200") {
			var xmlDoc = this.request.responseXML;
			var items = xmlDoc.documentElement.getElementsByTagName("item");
			
			// Clean up any old stuff before we draw markers
			
			try {
				for (var i = 0; i < this.markers.length; i++) {
					try {
						//alert(this.markers[i].getTooltip());
					this.map.removeOverlay(this.markers[i]);
					//delete this.markers[i];
					} catch(e) { }
					try {
					if (typeof(this.markers[i].userData) != undefined) { this.map.removeOverlay(this.markers[i].userData); }
					} catch(e) { }
				}
			} catch(e) {  }
			
		
			delete this.markers;
			this.markers = new Array();
			// Now add new ones
			for (var i = 0; i < items.length; i++) {
				try {
					this.markers[i] = this.createMarker(items[i]);
					this.map.addOverlay(this.markers[i]);
					if (this.autozoom) {
						this.bounds.extend(this.markers[i].getPoint());
					}
				} catch (e) {
				}
			}
			if (this.autozoom) {
				var clat = (this.bounds.getNorthEast().lat() + this.bounds.getSouthWest().lat()) / 2;
				var clng = (this.bounds.getNorthEast().lng() + this.bounds.getSouthWest().lng()) / 2;
				this.map.setCenter(new GLatLng(clat,clng));
				this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds) - 1);
			}
		}
		this.request = false;
	}
}
MGeoRSS.prototype.createMarker = function(item) {
	var tooltip = item.getElementsByTagName("title")[0].childNodes[0].data;
	var description;
	try {
	 description = item.getElementsByTagName("description")[0].childNodes[0].nodeValue;
	} catch (e) {}
	var link;
	try {
	 link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
	} catch (e) {}

	/* namespaces are handled by spec in moz, not in ie */
	var latlng = ""; var lat; var lng; var box=""; var img=""; var direction=""; var line="";
	if (navigator.userAgent.toLowerCase().indexOf("msie") < 0) {
		try {
			latlng = item.getElementsByTagNameNS("http://www.georss.org/georss","point")[0].childNodes[0].nodeValue;
		} catch (e) { }
		try {
			lat = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","lat")[0].childNodes[0].nodeValue;
			lng = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","long")[0].childNodes[0].nodeValue;
		} catch (e) {}
		try {
			var rel = item.getElementsByTagNameNS("http://www.georss.org/georss","relationshiptag")[0].childNodes[0].nodeValue;
			if (rel == "image-extent") {
				box = item.getElementsByTagNameNS("http://www.georss.org/georss","box")[0].childNodes[0].nodeValue;
				img = item.getElementsByTagNameNS("http://search.yahoo.com/mrss","content")[0].getAttribute("url");
			}
		} catch (e) { }
		try {
				direction = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","altitude")[0].childNodes[0].nodeValue;
		} catch (e) {}
		try {
				line = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","line")[0].childNodes[0].nodeValue;
		} catch (e) {}
	} else {
		try {
			latlng = item.getElementsByTagName("georss:point")[0].childNodes[0].nodeValue;
		} catch (e) {}
		try {
			lat = item.getElementsByTagName("geo:lat")[0].childNodes[0].nodeValue;
			lng = item.getElementsByTagName("geo:long")[0].childNodes[0].nodeValue;
		} catch(e) {}
		try {
			box = item.getElementsByTagName("georss:box")[0].childNodes[0].nodeValue;
		} catch (e) {}
		try {
			var rel = item.getElementsByTagName("georss:box")[0].getAttribute("relationshiptag");
			if (rel == "image-extent") {
				box = item.getElementsByTagName("georss:box")[0].childNodes[0].nodeValue;
				img = item.getElementsByTagName("media:content")[0].getAttribute("url");
			}
		} catch (e) {}
		try {
				direction = item.getElementsByTagName("geo:altitude")[0].childNodes[0].nodeValue;
		} catch (e) {}
		try {
				line = item.getElementsByTagName("geo:line")[0].childNodes[0].nodeValue;
		} catch (e) {}
	}
	if (latlng.length > 0) {
		lat = latlng.split(" ")[0];
		lng = latlng.split(" ")[1];
	}
	
	
		

	if (box.length > 0 && img.length > 0) {
		var photo = new TPhoto();
		photo.id = img;
		photo.src = img;
		photo.percentOpacity = 80;
		b = box.split(' ');
		photo.anchorTopLeft = new GLatLng(b[3], b[0]);
		photo.anchorBottomRight = new GLatLng(b[1], b[2]);
		this.map.addTPhoto(photo);
		if (this.autozoom) {
			var nw = new GLatLng(parseFloat(b[3]),parseFloat(b[0]));
			var se = new GLatLng(parseFloat(b[1]),parseFloat(b[2]));
			this.bounds.extend(nw);
			this.bounds.extend(se);
		}
	}

	if (lat.length > 0 && lng.length > 0) {
		var point = new GPoint(parseFloat(lng), parseFloat(lat));
		var marker;
		if (direction.length != "") {
			marker = new PdMarker(point, eval("this.icon"+direction));
		} else if (this.icon) {
			marker = new PdMarker(point, this.icon);
		} else {
			marker = new PdMarker(point);
		}
		marker.setTooltip(tooltip);
		var html = "";
		if (link) {
			html = html + "<a href=\"" + link + "\">";
		}
		html = html + tooltip;
		if (link) {
			html = html + "</a>";
		}
		if (description) {
			html = html + "<p/>" + description;
		}
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		
		
		
		if (line.length > 0) {
			var ePolyline = new GPolyline.fromEncoded({
    			color: "#00FF00",
    			weight: 3,
   		 	points: line.split(" ")[0],  
		 	levels: line.split(" ")[1],
			zoomFactor: 32,
			numLevels: 4
				});
			GEvent.addListener(marker, "mouseover", function() {
				 map.addOverlay(ePolyline);
				});	
			GEvent.addListener(marker, "mouseout", function() {
				map.removeOverlay(ePolyline);
			});
			marker.userData = ePolyline; // Stash so we can delete 
			
			
		}
		return marker;
	}
}
GMap.prototype.addMGeoRSS=function(a) {
    a.initialize(this);
}
GMap2.prototype.addMGeoRSS=function(a) {
	a.initialize(this);
}


