
var MapManager = Class.create();
MapManager.prototype = {
	idname_: null,	
	initial_lat_: null,
	initial_lng_: null,
	initial_scale_: null,
	opts_: null,
	size_controller_: null,
	size_controller_type_: null,
	
	initialize: function(idname, initial_lat, initial_lng, initial_scale, opts) {
		this.idname_ = idname;
		this.initial_lat_ = initial_lat;
		this.initial_lng_ = initial_lng;
		this.initial_scale_ = initial_scale;
		this.opts_ = opts;
	},
	map_: null,
	marker_: null,
	
	setMapSize: function() {
	    var height = this.getMapElementHeight_();
		var divmap = $(this.idname_);
	    Element.setStyle(divmap, {
	        'height': height + 'px'
	    });
		var old_controller = null;
		if (height < 130) {
			if (this.size_controller_type_ != 1) {
				old_controller = this.size_controller_;
				this.size_controller_ = new GSmallZoomControl();
				this.size_controller_type_ = 1;
			}
		} else if (height < 310) {
			if (this.size_controller_type_ != 2) {
				old_controller = this.size_controller_;
				this.size_controller_ = new GSmallMapControl();
				this.size_controller_type_ = 2;
			}
		} else {
			if (this.size_controller_type_ != 3) {
				old_controller = this.size_controller_;
				this.size_controller_ = new GLargeMapControl();
				this.size_controller_type_ = 3;
			}					
		}
	    if (this.map_) {
	        this.map_.checkResize();
			if (old_controller) {
				this.map_.removeControl(old_controller);
				this.map_.addControl(this.size_controller_);
			}
	        this.map_.panTo(this.marker_.getPoint());
	    }
	},
	
	start: function() {
		if (GBrowserIsCompatible()) {
		    this.setMapSize();
		    var marker_location = new GLatLng(this.initial_lat_, this.initial_lng_);
		    this.map_ = new GMap2($(this.idname_));
			var scale = 5;
			if (this.initial_scale_) {
				scale = this.initial_scale_;
			}
		    this.map_.setCenter(marker_location, scale);
		    this.map_.addControl(this.size_controller_);
		    this.map_.addControl(new GMapTypeControl());
		    this.map_.addControl(new GScaleControl());
			this.map_.enableDoubleClickZoom();
			this.map_.enableContinuousZoom();
			this.map_.enableScrollWheelZoom();
		    this.marker_ = new GMarker(marker_location, {
		        draggable: true,
		        bouncy: true,
		        title: 'このマークをドラッグして位置を変更してください'
		    });
		    this.map_.addOverlay(this.marker_);
			if (this.dispatcher) {
				this.dispatcher.send('center_changed', this.map_);
			}
		    GEvent.addListener(this.marker_, 'dragend', this.fireDragEnd.bind(this));
			GEvent.addListener(this.map_, 'zoomend', this.fireZoomEnd.bind(this));
		    this.fireDragEnd();
		}
	},

	getMapElementHeight_: function () {
		if (this.opts_ && this.opts_.height_excludes) {
			var ex_height = 0;
			this.opts_.height_excludes.each(function (value) {
				var height = $(value).getHeight();
				ex_height += height; 
				
			});
			if (this.opts_.height_excludes_margin) {
				ex_height = ex_height + this.opts_.height_excludes_margin;
			}
			return this.getBodyHeight_() - ex_height;
		}
		return 300;
//	    return this.getBodyHeight_();
	},
	
	getBodyHeight_: function() {
		var y;
		if (self.innerHeight) { // all except Explorer
		    y = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		    y = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
		    y = document.body.clientHeight;
		}
		return y;
	},
	
	createIcon: function () {
	    return G_DEFAULT_ICON;
	}
	,
	on_locate: function(lat, lng, label) {
		if (this.map_.getZoom() == 5) {
			this.map_.setZoom(16);
		} 
		this.marker_.setPoint(new GLatLng(lat, lng));
		if (this.dispatcher) {this.dispatcher.send('center_changed', lat, lng);}
       	this.map_.panTo(this.marker_.getPoint());
	}
	,
	fireDragEnd: function() {
    	var marker_location = this.marker_.getPoint();
		if (this.dispatcher) {
			this.dispatcher.send('center_changed', marker_location.lat(), marker_location.lng());
		}
	}
	,
	fireZoomEnd: function(oldLevel, newLevel) {
		if (this.dispatcher) {
			this.dispatcher.send('zoom_end', newLevel, oldLevel);
		}
	}
}
