// LIST MARKER DEFINITION
function ListMarker(map,iconManager) {
	this.map = map;
	this.markers = new Array();
	this.markerHTMLInfo = new Array();
	this.currentMarker = null;
	this.currentIndex = 0;
	if (typeof(iconManager) == "undefined") {
		iconManager = new MarkerIconManager();
		iconManager.addIcon(G_DEFAULT_ICON,"");
	}
	this.iconManager = iconManager;
}
// Add Marker
ListMarker.prototype.addMarker = function(marker, markerContent) {
	this.currentIndex = this.markers.push(marker) - 1;
	this.markerHTMLInfo.push(markerContent);
	this.currentMarker = marker;
	this.map.addOverlay(this.currentMarker);
	this.focusCurrent();
}

// Remove Marker
ListMarker.prototype.removeMarker = function() {
	if (this.markers.length == 0) {
		return;
	}
	this.map.removeOverlay(this.currentMarker);
	this.markers.splice(this.currentIndex, 1); //Remove current marker
	this.markerHTMLInfo.splice(this.currentIndex, 1); //Remove current marker content
	if (this.currentIndex == this.markers.length) {
		this.currentIndex--;
	}
	this.currentMarker = this.markers[this.currentIndex];	
	this.focusCurrent();
}

ListMarker.prototype.focusNext = function() {
	if (this.currentIndex < this.markers.length - 1) {
		this.currentIndex++;
		this.currentMarker = this.markers[this.currentIndex];				
	}
	this.focusCurrent();
}

ListMarker.prototype.focusIndex = function(i) {
	if (i<0 || i>=this.markers.length) {
		return;
	}
	this.currentIndex = i;
	this.currentMarker = this.markers[this.currentIndex];
	this.focusCurrent();
}

ListMarker.prototype.focusPrevious = function() {
	if (this.currentIndex > 0) {
		this.currentIndex--;
		this.currentMarker = this.markers[this.currentIndex];
	}
	this.focusCurrent();
}

// Focus to current marker
ListMarker.prototype.focusCurrent = function() {
	if (this.currentMarker == undefined || this.currentMarker == null) {
		return;
	}
	this.map.panTo(this.currentMarker.getLatLng());
}

ListMarker.prototype.updateCurrentContent = function(content) {
	content=content.replace(/\n/g,' ');

	/***** IE and Opera need this line also *****/
						 
	content=content.replace(/\s/g,' ').replace(/  ,/g,' ');  

	this.markerHTMLInfo[this.currentIndex] = content;
}

ListMarker.prototype.getCurrentContent = function() {
	return this.markerHTMLInfo[this.currentIndex];
}

ListMarker.prototype.toXML = function() {
	var strXML = "<listMarker>";
	for (var i=0; i<this.markers.length;i++) {
		strXML += "<marker>";
		strXML += "<lat>" + this.markers[i].getLatLng().lat() + "</lat>";
		strXML += "<lng>" + this.markers[i].getLatLng().lng() + "</lng>";
		strXML += "<info>" + this.markerHTMLInfo[i] + "</info>";
		strXML += "</marker>";
	}
	strXML += "<mapType>" + this.map.getCurrentMapType().getName() + "</mapType>";
	strXML += "<mapZoom>" + this.map.getZoom() + "</mapZoom>";
	strXML += "</listMarker>";	
	return strXML;
}

ListMarker.prototype.parseXML = function(xmlString, allowDrag, isPixel) {
	if( typeof(allowDrag) == "undefined" ){
		allowDrag = false;
	}
	
	if( typeof(isPixel) == "undefined" ){
		isPixel = false;
	}
	
	if (xmlString == null || xmlString == "") {
		alert("Không thể phân tích được nội dung XML rỗng");
		return;
	}
 	 try //Internet Explorer
  	{
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async="false";
	  xmlDoc.loadXML(xmlString);
  	}
 	 catch(e)
 	 {
		  try //Firefox, Mozilla, Opera, etc.
		  {
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString(xmlString,"text/xml");
		  }
		  catch(e)
		  {
			  alert(e.message);
			  return;
		  }
	}
		
	var reg_mapTypes = this.map.getMapTypes();
	if (xmlDoc.getElementsByTagName("mapType")[0]) {
		for (var i=0; i<reg_mapTypes.length;i++) {
			var mapType = xmlDoc.getElementsByTagName("mapType")[0].childNodes[0].nodeValue;
			if (reg_mapTypes[i].getName() == mapType) {
				this.map.setMapType(reg_mapTypes[i]);		
				break;
			}
		}
	} else {
		this.map.setMapType(reg_mapTypes[0]);
	}
	
	if (xmlDoc.getElementsByTagName("mapZoom")[0]) {
		var zoomLevel = xmlDoc.getElementsByTagName("mapZoom")[0].childNodes[0].nodeValue;
		this.map.setZoom(parseInt(zoomLevel));
	}
	
	// Parse into listmarker;
	var markerTags = xmlDoc.getElementsByTagName("marker");	
	this.markers = new Array();	
	this.markerHTMLInfo = new Array;
	this.currentIndex = 0;
	
	for (var i=0; i<markerTags.length;i++) {
		var lat = xmlDoc.getElementsByTagName("lat")[i].childNodes[0].nodeValue;
		var lng = xmlDoc.getElementsByTagName("lng")[i].childNodes[0].nodeValue;
		var info = xmlDoc.getElementsByTagName("info")[i].childNodes[0].nodeValue;
		
		var point = new GLatLng(lat,lng);
		if (isPixel) {
			point = _proj.fromPixelToLatLng(new GPoint(lat,lng),defaultZoomLevel);
		}
		
		var gIcon = this.iconManager.getRelateIcon(info);
		var newMarker = new GMarker(point, {icon:gIcon, draggable: allowDrag});
		this.addMarker(newMarker, info);
	}
	
	if (this.markers.length > 0) {
		this.currentMarker = this.markers[0];
		this.currentIndex = 0;
		this.focusCurrent();
	}
}

// *** END LIST MARKER DEFINITION ******
function MarkerIconManager() {
	this.icons = new Array();
	this.iconLabels = new Array();	
}

MarkerIconManager.prototype.addIcon = function(gIcon, iconLabel) {	
	this.icons.push(gIcon);
	this.iconLabels.push(iconLabel);
}

MarkerIconManager.prototype.getRelateIcon = function(markerInfo) {
	markerInfo = markerInfo.toLowerCase();	
	var pos = markerInfo.length;
	var selectedIndex = this.iconLabels.length - 1; // Mac dinh la lay icon cuoi cung
	
	for (var i=0; i < this.iconLabels.length; i++) {		
		var found = markerInfo.indexOf(this.iconLabels[i].toLowerCase());		
		if (found > 0 && found < pos) {			
			pos = found;
			selectedIndex = i;			
		}
	}
	
	return this.icons[selectedIndex];
}