
// MGeoRSS: GMaps API extension 
// based on work by Mikel Maron (email: mikel_maron yahoo com)
// http://brainoff.com/gmaps/mgeorss.html
// This work is copyright of VTT

function MGeoRSS(){}
MGeoRSS.prototype.initialize=function(map) {
    this.map = map;
    this.rssurl = false;
    this.request = false;
	this.visible = true;
	this.markers = new Array();
	this.markerTrackers = new Array();
	this.arePoi = false;
}

MGeoRSS.prototype.showHide=function() {
	if (this.visible == true) {
		for (var i=0;i<this.markers.length;i++) {
			this.map.removeOverlay(this.markers[i]);
		}
		for (var j=0;j<this.markerTrackers.length;j++) {
			this.markerTrackers[j].disable();
		}
		this.visible = false;
	} else {
		for (var i=0;i<this.markers.length;i++) {
			this.map.addOverlay(this.markers[i]);
		}
		for (var j=0;j<this.markerTrackers.length;j++) {
			this.markerTrackers[j].enable();
		}
		this.visible = true;
	}
}
MGeoRSS.prototype.hide=function() {
    	if (this.visible == true) {
		for (var i=0;i<this.markers.length;i++) {
			this.map.removeOverlay(this.markers[i]);
		}
		for (var j=0;j<this.markerTrackers.length;j++) {
			this.markerTrackers[j].disable();
		}
		this.visible = false;
	}
}
MGeoRSS.prototype.show=function() {
    if(this.visible == false){
        		for (var i=0;i<this.markers.length;i++) {
			this.map.addOverlay(this.markers[i]);
		}
		for (var j=0;j<this.markerTrackers.length;j++) {
			this.markerTrackers[j].enable();
		}
		this.visible = true;
    }
}
MGeoRSS.prototype.load=function(url,update,poi,proxyurl,autozoom) {
	if (this.request != false) {
		return; 
	}
 	this.rssurl = url;
	if (autozoom) {
		this.autozoom = autozoom;
		this.bounds = new GLatLngBounds();
	}
	if(poi) {
		this.arePoi = true;
	}
    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(update);
	}
	this.request.send(null);
}

MGeoRSS.prototype.clear=function() {
	for (var i = 0; i < this.markers.length; i++) {
		this.map.removeOverlay(this.markers[i]);
	}
	this.markers = new Array();
}

MGeoRSS.prototype.callback = function(update) {
	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++) {
				//To make things a bit quicker, lets use the update
				var found = false;
				if(update) {
				var title = items[i].getElementsByTagName("title")[0].childNodes[0].data;
				for (var j = 0; j < this.markers.length; j++) {
					if (this.markers[j].getTitle() == title) {
						found = true;
						this.markers[j].setTouched(true);
						this.updateMarker(items[i], this.markers[j]);
						break;
					}
				}					
				}
				if(!found) {
				try {
					this.markers[this.markers.length] = this.createMarker(items[i]);
					if(this.visible) {
					this.map.addOverlay(this.markers[this.markers.length - 1]);
					}
					if (this.autozoom) {
						this.bounds.extend(this.markers[this.markers.length - 1].getLatLng());
					}
					this.markers[this.markers.length - 1].setTouched(true);
				} catch (e) {
				}
				}
			}
			var touchedMarkers = new Array();
			var marker;
			while((marker = this.markers.pop()) != null) {
				if(marker.isTouched()) {
					marker.setTouched(false);
					touchedMarkers.push(marker);
				}
				else {
					for(var k = 0; k < poiMarkers.length; k++) {
						if(marker == poiMarkers[k]) {
							//alert("removing "  +  marker.getTitle() + " Poi array length: " + poiMarkers.length + " MarkerTrackers array length: " + this.markerTrackers.length);
							poiMarkers.splice(k,1);
							//alert("removed, poi length: " + poiMarkers.length + " markertrackers length: " + this.markerTrackers.length);
							k--;
						}
					}
					for(var l = 0; l < this.markerTrackers.length; l++) {
						if(marker == this.markerTrackers[l].getMarker()) {
							this.markerTrackers[l].disable();
							this.markerTrackers.splice(l,1);
							l--;
						}
					}
					this.map.removeOverlay(marker);
				}
			}
			this.markers = touchedMarkers;
			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.panTo(new GLatLng(clat,clng));
				this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds) - 1);
			}
		}
		this.request = false;
	}
}

MGeoRSS.prototype.updateMarker = function(item, marker) {
	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 icn="";
	if (navigator.userAgent.toLowerCase().indexOf("msie") < 0) {
		try {
			icn = item.getElementsByTagNameNS("http://purl.org/rss/1.0/modules/content/", "encoded")[0].childNodes[0].nodeValue;
		} catch (e) {}
	
		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 {
			icn = item.getElementsByTagNameNS("http://purl.org/rss/1.0/modules/content/", "encoded")[0].childNodes[0].nodeValue;
		} catch (e) {}
	
		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 (lat.length > 0 && lng.length > 0) {
		var point = new GLatLng(parseFloat(lat), parseFloat(lng));
		marker.setLatLng(point);
		var resized = false;
		if(icn.length > 0) {
			if(icn.indexOf("!") != -1) {
				var temp = icn;
				icn = temp.substring(0, temp.indexOf("!")); //Icon url
				temp = temp.substring(temp.indexOf("!") + 1);
				var width = temp.substring(0, temp.indexOf("!"));
				temp = temp.substring(temp.indexOf("!") + 1);
				var height = temp.substring(0, temp.indexOf("!"));
				var oldSize = marker.getIcon().iconSize;
				marker.getIcon().iconSize = new GSize(width, height);
				if(!oldSize.equals(marker.getIcon().iconSize)) {
					resized = true;
				}
				temp = temp.substring(temp.indexOf("!") + 1);
				var x = temp.substring(0, temp.indexOf("!"));
				var y = temp.substring(temp.indexOf("!") + 1);
				marker.getIcon().iconAnchor = new GPoint(x,y);
				marker.getIcon().infoWindowAnchor = new GPoint(0,0); 
				//TODO: transparent for the non-compliant browser
				//Imagemap for the supported browser
			}
			marker.setImage(icn);
			marker.getIcon().image = icn;
		}
		
		var html = "";
		if (link) {
			html = html + "<a href=\"" + link + "\">";
		}
		html = html + marker.getTitle();
		if (link) {
			html = html + "</a>";
		}
		if (description) {
			html = html + "<p/>" + description;
		}
		marker.bindInfoWindowHtml(html);
		if (resized && this.visible) {
			this.map.removeOverlay(marker);
			this.map.addOverlay(marker);
		}
	}
		
}

MGeoRSS.prototype.createMarker = function(item) {
	var title = item.getElementsByTagName("title")[0].childNodes[0].data;
	//var isPoi = false;
	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) {}
	
	var pubDate;
	try {
	 pubDate = item.getElementsByTagName("pubDate")[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 icn=""; var meeting="";
	if (navigator.userAgent.toLowerCase().indexOf("msie") < 0) {
		
		try {
			icn = item.getElementsByTagNameNS("http://purl.org/rss/1.0/modules/content/", "encoded")[0].childNodes[0].nodeValue;
		} catch (e) {}
	
		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 {
			icn = item.getElementsByTagName("content:encoded")[0].childNodes[0].nodeValue;
		} catch (e) {}
	
		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 point = new GLatLng(parseFloat(lat), parseFloat(lng));
		var marker;
		
		if (icn.length > 0) {
			var icon = new GIcon();
			if(icn.indexOf("!") != -1) {
				var temp = icn;
				icn = temp.substring(0, temp.indexOf("!")); //Icon url
				temp = temp.substring(temp.indexOf("!") + 1);
				var width = temp.substring(0, temp.indexOf("!"));
				temp = temp.substring(temp.indexOf("!") + 1);
				var height = temp.substring(0, temp.indexOf("!"));
				icon.iconSize = new GSize(width, height);
				temp = temp.substring(temp.indexOf("!") + 1);
				var x = temp.substring(0, temp.indexOf("!"));
				var y = temp.substring(temp.indexOf("!") + 1);
				icon.iconAnchor = new GPoint(x,y);
				icon.infoWindowAnchor = new GPoint(0,0); 
				//TODO: transparent for the non-compliant browser
				//Imagemap for the supported browser
			} 
	 		icon.image = icn;
			marker = new GMarker(point, {title:title, icon:icon});
		} else {
			marker = new GMarker(point, {title:title});
		}
		var html = "";
		if (link) {
			html = html + "<a href=\"" + link + "\">";
		}
		html = html + title;
		if (link) {
			html = html + "</a>";
		}
		if (description) {
			html = html + "<p/>" + description;
		}
		if (this.arePoi) {
			poiMarkers.push(marker);
			if(pubDate) {
				var date = new Date(); 
				date.setTime(Date.parse(pubDate));
				marker.setDate(date);
			}
			var markerTracker = new MarkerTracker(marker, this.map, {padding:10, weight:5, opacity:1.0, length:10, updateEvent:'moveend', iconScale:1.0});
			if(!this.visible) {
				markerTracker.disable();
			}
			this.markerTrackers.push(markerTracker);
		}
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	}
}
MGeoRSS.prototype.getMarkers=function() {
	return this.markers;
}


GMap.prototype.addMGeoRSS=function(a) {
    a.initialize(this);
}
GMap2.prototype.addMGeoRSS=function(a) {
	a.initialize(this);
}
// Center on user
GMap2.prototype.setCenterOnUser=function(a) {
	this.centerOnUser = a;
}
GMap2.prototype.isCenterOnUser=function() {
	return this.centerOnUser;
}
//To find out if marker is to be removed in update
GMarker.prototype.isTouched=function() {
	return this.touched;
}
GMarker.prototype.setTouched=function(a) {
	this.touched = a;
}
//Date
GMarker.prototype.getDate=function() {
	return this.date;
}
GMarker.prototype.setDate=function(d) {
	this.date = d;
}
//Info window contents
GMarker.prototype.getInfo=function() {
    return this.info;
}
GMarker.prototype.setInfo=function(i) {
    this.info = i;
}
