/*
	http://googlemapsbook.com/2007/01/22/extending-gmarker/
*/
gmap.Marker = (function(){
	
	// ---- CONSTRUCTOR --------------------------
	var Marker=function(latlng, options){
		var I=this;
		
		I.options = j.extend({}, I.defaults, options);	
		
		//get icon based on category if no icon is explicitly passed
		if(!I.options.icon) I.options.icon = createIcon(I.options.category);
	
		I.map = I.options.map || gmap.map;
		I.latlng = latlng;
		I.labelText = I.options.labelText || "";
		I.labelClass = I.options.labelClass || "markerLabel";
		I.labelOffset = I.options.labelOffset || new GSize(0, 0);

		
		GMarker.apply(I, [I.latlng, I.options]);
		
	}

	/*
		Get an icon based on the type of location it is.
		http://code.google.com/p/google-maps-icons/
	*/
	function createIcon(iconType){
		var returnIcon = new GIcon();
		iconType = (iconType)? iconType.toLowerCase() : "default";

		//iconType = "asdfadf"; // makes icons default - too much logic required for mixed locations. not in scope.
		if(iconType=="apartments" || iconType=="communities"){
			returnIcon.image="/common/images/google/marker-communities.png";
			returnIcon.shadow="/common/images/google/marker-shadow.png";
			returnIcon.iconSize = new GSize(19, 33);	
			returnIcon.shadowSize = new GSize(28, 33);
			returnIcon.iconAnchor = new GPoint(9, 32);
			returnIcon.infoWindowAnchor = new GPoint(9, 15);	
		}else if(iconType=="dining"){
			returnIcon.image="/common/images/google/marker-dining.png";
			returnIcon.shadow="/common/images/google/marker-shadow.png";
			returnIcon.iconSize = new GSize(19, 33);
			returnIcon.shadowSize = new GSize(28, 33);
			returnIcon.iconAnchor = new GPoint(9, 23);
			returnIcon.infoWindowAnchor = new GPoint(9, 15);		
		}else if(iconType=="lodging"){
			returnIcon.image="/common/images/google/marker-lodging.png";
			returnIcon.shadow="/common/images/google/marker-shadow.png";
			returnIcon.iconSize = new GSize(19, 33);	
			returnIcon.shadowSize = new GSize(28, 33);
			returnIcon.iconAnchor = new GPoint(0, 32);
			returnIcon.infoWindowAnchor = new GPoint(9, 15);	
		}else if(iconType=="shopping"){
			returnIcon.image="/common/images/google/marker-shopping.png";
			returnIcon.shadow="/common/images/google/marker-shadow.png";
			returnIcon.iconSize = new GSize(19, 33);	
			returnIcon.shadowSize = new GSize(28, 33);
			returnIcon.iconAnchor = new GPoint(0, 23);
			returnIcon.infoWindowAnchor = new GPoint(9, 15);	
		}else{
			returnIcon.image="/common/images/google/mm_20_gray.png";
			returnIcon.shadow="/common/images/google/mm_20_shadow.png";
			returnIcon.iconSize = new GSize(12, 20);			
			returnIcon.shadowSize = new GSize(22, 20);
			returnIcon.iconAnchor = new GPoint(6, 20);
			returnIcon.infoWindowAnchor = new GPoint(6, 10);
		}
		return returnIcon;
	}
	
	
	
	// ---- PUBLIC MEMBERS --------------------------
	Marker.prototype = new GMarker(new GLatLng(0, 0));
							
	j.extend(Marker.prototype, {
		defaults:{
			map:gmap.map,
			locations:[], //Array - gmap.Location(s)
			window:null, //gmap.MarkerWindow
			category:"default" //String - indicates the category and controls the offset and icon of the marker
		},
		initialize:function(map) {
			var I=this;
			GMarker.prototype.initialize.call(I, map);
			
			I.map = map;
			I.div = document.createElement("div");
			I.div.className = I.labelClass;
			I.div.innerHTML = I.labelText;
			I.div.style.position = "absolute";
			map.getPane(G_MAP_MARKER_PANE).appendChild(I.div);
		
			I.getLocations();
			I.window = new gmap.MarkerWindow({marker:I});
			
		},
		getLocations:function(){
			var I=this;
			I.locations = tbelt.array.searchObjects({marker:I}, gmap.locations);
			return I.locations;
		},
		redraw:function(force) {
			var I=this;
			GMarker.prototype.redraw.call(this, I.map);
		
			// We only need to do anything if the coordinate system has changed
			if (!force) return;
		
			var pos = I.map.fromLatLngToDivPixel(I.latlng);
			var zidx = GOverlay.getZIndex(I.latlng.lat());			
			
			I.div.style.left = (pos.x + I.labelOffset.width) + "px";
			I.div.style.top = (pos.y + I.labelOffset.height) + "px";
			I.div.style.zIndex = zidx + 1; // Directly in front of the marker image
			
		},
		remove:function() {
			
			this.div.parentNode.removeChild(this.div);
			this.div = null;
			GMarker.prototype.remove.call(this);
		},
		setIcon:function(iconType){
			var I=this;
			if(iconType==null) iconType = I.options.category;
			if(iconType!=I.options.category){
				I.options.icon=createIcon(iconType);
				var newMarker = new gmap.Marker(I.latlng, I.options);
				I.map.removeOverlay(I);
				I.map.addOverlay(newMarker);			
				I = newMarker;
			}
		}
	});
	
	// ---- RETURN TO ENCAPSULATORS --------------------------
	return Marker;
	
})();