// 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(16, 28);
	//this.icon.shadowSize = new GSize(37, 34);
	this.icon.iconAnchor = new GPoint(8, 28);
	this.icon.infoWindowAnchor = new GPoint(8,28);
}
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) {
	if (this.request != false) { return; }
 	this.rssurl = url;
    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]);
				} catch (e) {
				}
			}
		}
		this.request = false;
	}
}
MGeoRSS.prototype.createMarker = function(item) {
	var title;
	try {
		title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
	} catch (e) {}
	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 */
	if (navigator.userAgent.toLowerCase().indexOf("msie") < 0) {
		var latlng = item.getElementsByTagNameNS("http://www.georss.org/georss","point")[0].childNodes[0].nodeValue;
		//var lat = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","lat")[0].childNodes[0].nodeValue;
		//var lng = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","long")[0].childNodes[0].nodeValue;
	} else {
		var latlng = item.getElementsByTagName("georss:point")[0].childNodes[0].nodeValue;
		//var lat = item.getElementsByTagName("geo:lat")[0].childNodes[0].nodeValue;
		//var lng = item.getElementsByTagName("geo:long")[0].childNodes[0].nodeValue;
	}
	var lat, lng;
	lat = latlng.split(" ")[0];
	lng = latlng.split(" ")[1];

	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 + "\">";
	}
	if (title) {
		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);
}
