var Map = { }

Map.Poi = Class.create({
	initialize: function(element, lat, lng, zoom, options) {
		this.element = $(element);
		this.options = options;
		this.heightTarget = 0;
		this.marker_count = 0;
		this.zoom = zoom;
		
		this.map = new GMap2(this.element);
		this.map.initzoom = 0;
		this.geocoder = new GClientGeocoder();
		this.marker_manager = null;
		this.poi_icon = new GIcon();
		this.directions = null;
		this.markers = new Array();
		
		this.options = options || { };
		this.options.typecontrol = this.options.typecontrol ? true : false;
		this.options.zoomcontrol = this.options.zoomcontrol ? true : false;
		this.options.iconwidth = this.options.iconwidth || 17;
		this.options.iconheight = this.options.iconheight || 21;
		this.options.anchorx = this.options.anchorx || 8;
		this.options.anchory = this.options.anchory || 21;
		
		this.map.setCenter(new GLatLng(lat, lng), zoom);
		//this.map.setMapType(G_PHYSICAL_MAP);

		if(this.options.typecontrol){
			this.map.addControl(new GMapTypeControl);
		}
		if(this.options.zoomcontrol){
			this.map.addControl(new GSmallMapControl);
		}
		
		if(this.options.zoomend){
			GEvent.addListener(this.map, "zoomend", this.options.zoomend); 
		}
						
		this.poi_icon.iconSize = new GSize(this.options.iconwidth,this.options.iconheight);
		this.poi_icon.iconAnchor = new GPoint(this.options.anchorx,this.options.anchory);
		this.poi_icon.transparent = null;
		this.poi_icon.shadow = null;
		
		Event.observe(window, 'unload', this.cleanup.bindAsEventListener(this));
	},
	
	zoomIn: function() {
		this.map.zoomIn();
		return false;
	},
	
	zoomOut: function() {
		this.map.zoomOut();
		return false;
	},
				
	addPoi: function(Icon,Lat,Lng,Title,Html,Url,Id) {
		if(this.marker_manager==null){
			this.marker_manager = new MarkerManager(this.map, {trackMarkers:true});
			this.marker_manager.refresh();
		}
		
		var tmp_icon = new GIcon(this.poi_icon);
		tmp_icon.image = Icon;
		var marker_options = { icon: tmp_icon };
		
		var tmp = new GMarker(new GLatLng(Lat,Lng),marker_options);
		var panel = new Map.Panel(this.map, tmp, Id, this);
		panel.addTooltip(Title);
		tmp.panel = panel;
		
		if(Html!=''){
			panel.addDetailWindow(Html);
		}
		
		if(Url!=''){
			tmp.url = Url;
			GEvent.addListener(tmp, "mouseup",function(){
				document.location.href = Url;
			});
		}
					
		this.markers[this.marker_count] = tmp;
		this.marker_count++; 
		this.marker_manager.addMarker(tmp,0);
		
		return tmp;
	},
		
	extendArea: function(Padding) {
		var boundary = new GLatLngBounds();
		
		for (var i=0;i<this.marker_count;i++){
			boundary.extend(this.markers[i].getLatLng());
		}
		
		var sw = boundary.getSouthWest();
		var ne = boundary.getNorthEast();
		var center = boundary.getCenter();
		
		var min_lat = Math.min(2*center.lat() - ne.lat(), sw.lat());                
		var max_lat = Math.max(2*center.lat() - sw.lat(),  ne.lat());        
		var min_lng = Math.min(2*center.lng() - ne.lng(), sw.lng());               
		var max_lng = Math.max(2*center.lng() - sw.lng(), ne.lng());
				
		boundary.extend(new GLatLng(min_lat-Padding, min_lng-Padding));
		boundary.extend(new GLatLng(max_lat+Padding, max_lng+Padding));

		this.map.setCenter(boundary.getCenter(), this.map.getBoundsZoomLevel(boundary)>this.zoom ? this.zoom : this.map.getBoundsZoomLevel(boundary));
		this.map.initzoom = this.map.getBoundsZoomLevel(boundary)>this.zoom ? this.zoom : this.map.getBoundsZoomLevel(boundary);
	},
		
	cleanup: function() {
		GUnload();
	}
});

Map.Panel = Class.create(GOverlay.prototype,{
	initialize: function(map,marker,id,Parent) {
		if(marker){
			this.marker = marker;
			this.map = map;
			this.id = id;
			this.tooltip = null;
			this.detailWindow = null;
			this.marker.panel = this;
			this._parent = Parent;
			
			this.map.addOverlay(this);
		}
	},
	
	addTooltip: function(Tooltip) {
		Element.insert(this.map.getPane(G_MAP_FLOAT_PANE), '<div id="tt-'+this.id+'" class="map-tooltip" style="display: none;">'+Tooltip+'</div>');
		this.tooltip = $('tt-'+this.id);
		this.tooltip.style.position = 'absolute';
		
		GEvent.addListener(this.marker, "mouseover", function(){
			this.panel.showtip();
		});
		
		GEvent.addListener(this.marker, "mouseout", function(){
			this.panel.hidetip();
		});
	},
	
	addDetailWindow: function(Html) {
		Element.insert(this.map.getPane(G_MAP_FLOAT_SHADOW_PANE), '<div id="dw-'+this.id+'" class="map-panel" style="display: none;">'+Html+'</div>');
		this.detailWindow = $('dw-'+this.id);
		this.detailWindow.style.position = 'absolute';
		
		GEvent.addListener(this.marker, "mouseup",function(){
			if(this.panel.visible()){
				this.panel.hide();
			}else{
				for (var i=0;i<this.panel._parent.marker_count;i++){
					if(this.panel._parent.markers[i].panel.detailWindow!=null && this.panel._parent.markers[i].panel.detailWindow!=this.panel.detailWindow){
						this.panel._parent.markers[i].panel.hide();
					}
				}
				this.panel.show();
			}
		});
	},
	
	redraw: function(force) {
		if (!force) return;
		var pos = null;
		
		if(this.tooltip){
			pos = this.getPos(this.tooltip);
			this.tooltip.style.top = pos.y + 'px';
			this.tooltip.style.left = pos.x + 'px';
		}
		
		if(this.detailWindow){
			pos = this.getPos(this.detailWindow);
			this.detailWindow.style.top = pos.y + 'px';
			this.detailWindow.style.left = pos.x + 'px';
		}
	},
	
	show: function() {
		this.redraw(true);
		
		var marker = this.map.fromLatLngToContainerPixel(this.marker.getPoint());
		var anchor = this.marker.getIcon().iconAnchor;
		var pane_dim = this.detailWindow.getDimensions();
		var d_x = 0;
		var d_y = 0;

		if(marker.x-anchor.x+pane_dim.width+5>this.map.getSize().width){
			d_x = -1*(marker.x+anchor.x+pane_dim.width-this.map.getSize().width-5);
		}
		
		if(marker.y-anchor.y-pane_dim.height-5<0){
			d_y = pane_dim.height-marker.y+anchor.y+5;
		}
		
		this.hidetip();
		this.detailWindow.show();
		this.detailWindow.zIndex = 60000;
		
		this.map.panBy(new GSize(d_x,d_y));
	},
	
	hide: function() {
		this.detailWindow.hide();
	},
	
	showtip: function() {
		if(!this.visible()){
			this.redraw(true);
			
			this.tooltip.show();
			this.tooltip.zIndex = 60001;
		}
	},
	
	hidetip: function() {
		this.tooltip.hide();
	},
	
	visible: function() {
		if(this.detailWindow){
			return this.detailWindow.visible();
		}else{
			return false;
		}
	},
	
	getPos: function(Pane){
		var pos = new Object();
		var marker = this.map.fromLatLngToDivPixel(this.marker.getPoint());
		var anchor = this.marker.getIcon().iconAnchor;
		var pane_dim = Pane.getDimensions();
		
		pos.x = marker.x-anchor.x;
		pos.y = marker.y-anchor.y-pane_dim.height;
			
		return pos;
	}
})