/*
	Gmapの基本クラス
	amano
*/
var GmapBase = Class.create();
GmapBase.prototype = {
	initialize : function () {
		this.map;
		this.bigIcon = this.createBigIcon();
		this.smallIcon = this.createSmallIcon();
		this.isInfoOpened = false;
	},
	load : function(lat, lng, size) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(lat, lng), size);
		this.map = map;
		return map;
	},
	move : function(lat, lng) {
		this.map.panTo(new GLatLng(lat, lng));
	},
	closeInfoWindow : function() {
		if (this.isInfoOpened) {
			this.map.closeInfoWindow();
			this.isInfoOpened = false;
		}
	},
	setMarker : function(lat, lng, icon, html) {
		var self = this;
		var point = new GPoint(lng, lat);
		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "click", function() {
			self.closeInfoWindow();
			self.move(lat, lng);
			if (html != undefined) {
				self.isInfoOpened = true;
				marker.openInfoWindowHtml(html);
			} 
		});
		this.map.addOverlay(marker);
		return marker;
	},
	createBigIcon : function() {
		var icon = new GIcon();
		icon.image = "http://www.google.com/mapfiles/marker.png";
		icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon.iconSize = new GSize(20, 34);
		icon.shadowSize = new GSize(37, 34);
		icon.iconAnchor = new GPoint(9, 34);
		icon.infoWindowAnchor = new GPoint(9, 2);
		icon.infoShadowAnchor = new GPoint(18, 25);
		return icon;
	},
	createSmallIcon : function() {
		var icon = new GIcon();
    		icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	    icon.iconSize = new GSize(12, 20);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(6, 20);
	    icon.infoWindowAnchor = new GPoint(5, 1);
		return icon;
	},
	getBigIcon : function() {
		return this.bigIcon;
	},
	getSmallIcon : function() {
		return this.smallIcon;
	}
};

/*
	検索結果ページ表示
*/
var ServiceGmap = Class.create();
ServiceGmap.prototype = {
	initialize : function () {
		this.base = new GmapBase();
		this.smallIcon = this.base.createSmallIcon();
		this.bigIcon = this.base.createBigIcon();
		this.map;
	},
	load : function() {
		this.map = this.base.load(38.17, 139.17, 4);
	},
	clear : function() {
		this.map.clearOverlays();
	},
	setService : function(lat, lng, size) {
		this.clear();
		this.map.setCenter(new GLatLng(lat, lng), size);
		this.base.setMarker(lat, lng, this.bigIcon);
	}
};

var AdminGmap = Class.create();
Object.extend(AdminGmap.prototype, ServiceGmap.prototype);
Object.extend(AdminGmap.prototype, {
	initialize : function() {
		ServiceGmap.prototype.initialize.apply(this);
	},
	load : function() {
		this.map = this.base.load(38.17, 139.17, 4);
		gmap = this.map;
		icon = this.smallIcon;
		GEvent.addListener(this.map, "click", function(marker, point) {
			html = "<a href=\"#\" onclick=\"adminGmap.setServiceAndForm(" + point.lat() + "," + point.lng() + ")\">この場所に設定</a>";
			newmarker = new GMarker(point,icon);
			gmap.addOverlay(newmarker);
			newmarker.openInfoWindowHtml(html);
		});
	},
	setServiceAndForm : function(lat, lng) {
		this.setService(lat, lng);
		$('service_lat').value = lat;
		$('service_lng').value = lng;
	}
});
