
//	GoogleMap表示用 ベースクラス
var GMap = Class.create({
	
	currenticon: 0,
	mapobj : null,		//	地図オブジェクト
	geocoder : null,
	panorama : null,
	panoclient : null,
	markericon : [],
	mkricon: null,//test
	markers : [],		//	マーカー情報配列
	enableWheel: false,
	showPrintImage:true,	//	GoogleMapのアイコンに設定される印刷用のイメージを表示する
	
	/*
	initializeMap	: 地図の初期化処理(onloadに設定)
		mapobj : 地図ラッパクラスのインスタンス
		id : 地図を表示するhtml要素のid
		lat : 緯度
		lng : 経度
		zoom : 拡大率(大きいほどズームされる)
		showControl : 操作コントロールの表示
		cx : 地図の横幅
		cy : 地図の縦幅
	*/
	initializeMap : function( id, lat, lng, zoom, showControl, cx, cy) {
		var result = false;
		try
		{
			var elm = document.getElementById(id);
			if(!elm)
				throw new Error("element not found :"+id);
			var opt = null;
			if((cx>0) && (cy>0))
				opt = {size: new GSize(cx, cy)};
			// Map用初期化コード
			if(!this.mapobj)
			{
				this.mapobj = new GMap2(elm,opt);
			}
			if(!this.mapobj)
				throw new Error('initializeMap: cannot create GMap2 object.' );
				
			//	GLatLng()で緯度、輝度を指定
			//	setCenterの第2引数はズーム率(大きいほどズームされる)
			this.mapobj.setCenter(new GLatLng(lat, lng), zoom);
			if(showControl)
			{
				this.mapobj.addControl(new GMapTypeControl(),new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(0,0)));
				this.mapobj.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(0,20)));
			//	this.mapobj.addControl(new GMenuMapTypeControl(true,new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0,0))));
				this.mapobj.enableContinuousZoom();
				if(this.enableWheel)
					this.mapobj.enableScrollWheelZoom();
			}
			
			result = true;
		}
		catch(e)
		{
			throw new Error(e);
		}
		
		return result;
	},
	
	/*
		マーカー追加
		lat : 緯度
		lng : 経度
		mkrtitle : タイトル(ツールチップ)
		mkrbody : フキダシ内部の中身
	*/
	addNewMarker : function (lat, lng, mkrtitle, mkrbody) {
		var result = false;
		if(!this.mapobj)
			throw new Error('addNewMarker: object mapobj is empty. You should call initializeMap() first.' );
		var point = new GLatLng(lat, lng);
		var option = {};
		option.title = mkrtitle.replace(/&amp;/, '&');	//	titleだけHTML文字実体参照を展開しないため
		if(this.markericon[this.currenticon])
			option.icon = this.markericon[this.currenticon];
		var mrk = new GMarker(point,option);	//	titleはtooltip
		mrk.bindInfoWindowHtml(mkrbody);	//	フキダシの中身を指定、HTMLソースを指定
		//GEvent.addListener(mrk, "click", function(){mrk.openInfoWindowHtml(mkrbody);});	//	フキダシの中身を指定、HTMLソースを指定
		this.mapobj.addOverlay(mrk);
		return mrk;
	},
	/*
		マーカー管理用データを登録し、マーカーを追加
		data : 登録したいデータ
		lat : 緯度
		lng : 経度
		mkrtitle : タイトル(ツールチップ)
		mkrbody : フキダシ内部の中身
	*/
	addMarkerData : function(data ,lat, lng, mkrtitle, mkrbody) {
		result = false;
		var mrk = this.addNewMarker(lat, lng, mkrtitle, mkrbody);
		if(mrk){
			if(data)
				data.marker = mrk;
			this.markers.push(data);
		}
		
		result = true;
		return result;
	},
	/*
		登録済みマーカーデータを取得
	*/
	getMarkerData : function(id) {
		var info = null;
		if(typeof(this.markers)=='undefined')
			throw Error('No building data is specified.');
		info = this.markers[id];
		if(typeof(info)!='object')
			throw Error('No building data is specified with id:'+id);
		return info;
	},
	/*
		マーカーのアイコンを変更
		url : マーカー画像のURL
		cx : 横幅
		cy : 高さ
	*/
	setMarkerImage : function(url, cx, cy) {
		for(var n in this.markericon)
		{
			if(this.markericon[n].image==url)
				return;
		}
		var icon = new GIcon(G_DEFAULT_ICON);
		if(!this.showPrintImage)
		{
			//	GoogleMapのアイコンに設定される印刷用のイメージを表示しない場合
			icon.printImage = "";
			icon.mozPrintImage = "";
			icon.printShadow = "";
			icon.dragCrossImage = "";
		}
		if((cx>0) && (cy>0)) {
			icon.iconSize = new GSize(cx, cy);
			icon.shadowSize = new GSize(cx*2, cy);
			icon.iconAnchor = new GPoint(cx/2, cy);
		}
		icon.image = url;
		var order = this.markericon.length;
		this.markericon[order] = icon;
		this.switchIcon(order);
	},
	switchIcon : function(id) {
		this.currenticon = id;
	},
	clearAllMarkers :function () {
		if(typeof(this.markers)=='undefined')
			throw Error('No building data is specified.');
		this.mapobj.clearOverlays();
		for(var i in this.markers) {
			delete this.markers[i];
		}
		
	},
	/*
		地図の中心点を設定
		point : 中心点、(GLatLng)
		zoom : 拡大率(0-19、大きいほど詳細)
	*/
	setCenterPoint : function(point, zoom) {
		if(!this.mapobj)
			throw new Error('setCenterPoint: object mapobj is empty. You should call initializeMap() first.' );
		this.mapobj.setCenter(point, zoom);
	},
	
	// 住所から緯度経度に変換
	findPoint : function(address, handler) {
		if(!this.geocoder)
			this.geocoder = new GClientGeocoder();
		this.geocoder.getLatLng(address,handler);
	}.bind(this),
	
	//	StreetView初期化処理
	initStreetview : function(id) {
		var result = false;
		if(!this.panorama) {
			this.panorama = new GStreetviewPanorama(document.getElementById(id));
			GEvent.addListener(this.panorama, 'error', this.handleSVError);
		}
		if(!this.panorama)
			throw new Error('initStreetview: cannot create GStreetviewPanorama object.' );
		result = true;
		return result;
	}.bind(this),
	
	//	StreetView指定
	setView : function(point) {
		var result = false;
		if(!this.panorama)
			throw new Error('setView: object panorama is empty. You should call initStreetview() first.' );
			
		var ret = this.panorama.setLocationAndPOV(point);
		if(ret==200)
			result = true;
		
		if(!this.panoclient)
			this.panoclient = new GStreetviewClient();
		this.panoclient.getNearestPanorama(point,this.showView);
		
		return result;
	}.bind(this),
	
	//	StreetView表示
	showView : function(data) {
		var result = false;
		if (data.code != 200) {
			throw new Error('showView: Server rejected with code: ' + data.code);
		}
		if(!this.panorama)
			throw new Error('showView: object panorama is empty. You should call initStreetview() first.' );
		var ret = this.panorama.setLocationAndPOV(data.location.latlng);
		if(ret==200)
			result = true;
		else
			throw new Error('setLocationAndPOV: Server rejected with code: '+ret);
		return result;
	}.bind(this),
	
	//	フラッシュのインストールエラーハンドラ(StreetView用)
	handleSVError : function (errorCode) {
		if(!this.panorama)
			throw new Error('handleSVError: object panorama is empty. You should call initStreetview() first.' );
		if (errorCode == 600)
			throw new Error("Error: This area not supported in Streetview.");
		else if (errorCode == 603)
			throw new Error("Error: Flash doesn't appear to be supported by your browser");
	}.bind(this)
});

