// 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.markers = new Array();
}
MGeoRSS.prototype.setIcon=function(img) {
	this.icon  = new GIcon();
	this.icon.image = img;
	this.icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	this.icon.iconSize = new GSize(20, 34);
	this.icon.shadowSize = new GSize(37, 34);
	this.icon.iconAnchor = new GPoint(9, 34);
	this.icon.infoWindowAnchor = new GPoint(18,25);
}
MGeoRSS.prototype.showHide=function() {
	if (this.visible == true) {
		for (var i=0;i<this.markers.length;i++) {
			this.map.removeOverlay(this.markers[i]);
		}
		this.visible = false;
	} else {
		for (var i=0;i<this.markers.length;i++) {
			this.map.addOverlay(this.markers[i]);
		}
		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");
			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 title = 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="";
	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) { }
	} 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) {}
	}
	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 (this.icon) {
			marker = new GMarker(point, this.icon);
		} else {
			marker = new GMarker(point);
		}
		var html = "";
		if (link) {
			html = html + "<a href=\"" + link + "\">";
		}
		html = html + title;
		if (link) {
			html = html + "</a>";
		}
		if (description) {
			html = html + "<p/>" + description;
		}
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	}
}
GMap.prototype.addMGeoRSS=function(a) {
    a.initialize(this);
}
GMap2.prototype.addMGeoRSS=function(a) {
	a.initialize(this);
}
