// イメージ読み込み
cxPreImages(
	'/shared/images/result/icon_map_arrow01.gif',
	'/shared/images/result/icon_map_arrow02.gif',
	'/shared/images/result/icon_map_arrow03.gif',
	'/shared/images/result/icon_map_arrow04.gif',
	'/shared/images/result/icon_map_arrow05.gif',
	'/shared/images/result/icon_map_arrow06.gif',
	'/shared/images/result/icon_map_arrow07.gif',
	'/shared/images/result/icon_map_arrow08.gif',
	'/shared/images/result/icon_map_arrow09.gif',
	'/shared/images/result/icon_map_list01.jpg',
	'/shared/images/result/icon_map_list02.jpg',
	'/shared/images/result/icon_map_list03.jpg',
	'/shared/images/result/icon_map_list04.jpg',
	'/shared/images/result/icon_map_list05.jpg',
	'/shared/images/result/icon_map_list06.jpg',
	'/shared/images/result/icon_map_list07.jpg',
	'/shared/images/result/icon_map_list08.jpg',
	'/shared/images/result/icon_map_list09.jpg'
);

/**
 * GMap2クラス
 * @param objId GooleMaps表示オブジェクトのID名:String
 * @param infoWindow 情報ウィンドウ内要素格納オブジェクト:Object
 */
//var GDMap2 = function(objId,point,sPurpose,infoWindow){
var GDMap2 = function(objId,infoWindow){
	//定数の宣言
	//GoogleMaps関係
	this.gmapArea				= $(objId);						//GoogleMaps表示オブジェクト
	this.zoom					= 14;							//初期ズームレベル
	this.searchOff_zoomLevel	= 11;							//検索ボタンをOFFにするズームレベル
	this.searchBtnOnId			= 'btn_on_ul';					//検索ボタンONのオブジェクト
	this.searchBtnOffId			= 'btn_off_ul';					//検索ボタンOFFのオブジェクト
	this.infoOption				= {maxWidth:400};				//情報ウィンドウオプション
	this.infoDataObj			= infoWindow;					//情報ウィンドウデータオブジェクト
	this.iconPath				= "/shared/images/result/";		//アイコンファイルへのパス
	this.icon_width				= "34";							//アイコン幅
	this.icon_height			= "37";							//アイコン高さ
	this.iconSelf				= "icon_map_arrow_self.gif";	//自身のアイコン
	this.iconFile				= {								//カテゴリごとのアイコンファイル
		 "1":"icon_map_arrow01.gif",
		 "2":"icon_map_arrow02.gif",
		 "3":"icon_map_arrow03.gif",
		 "4":"icon_map_arrow04.gif",
		 "5":"icon_map_arrow06.gif",
		 "6":"icon_map_arrow05.gif",
		 "7":"icon_map_arrow07.gif",
		 "8":"icon_map_arrow08.gif",
		 "9":"icon_map_arrow09.gif"
	};
	this.infoHtml				= {								//情報ウィンドウ内HTMLフォーマット
		title  :'<p style="margin:3px 0px;font-size:12pt;"><strong>#TITLE#</strong></p>',
		address:'<p style="margin:2px;font-size:10pt;">住所：#ADDRESS#</p>',
		tel    :'<p style="margin:2px;font-size:10pt;">電話：#TEL#</p>',
		url    :'<p align="right" style="margin:2px;font-size:10pt;"><a href="#URL#">詳細情報を見る</a></p>'
	};
	//Ajax関係
	this.s_method				= "POST";						//フォームデータのMETHOD属性値（GET/POST）
	this.s_async				= true;							//非同期通信（TRUE=有効 , FALSE=無効）
	this.s_path					= "/js/map_search.php";			//検索プログラムパス
	this.listHeader				= "dtl_map_list_selected";		//検索結果リスト検索種類ヘッダ
	this.listArea				= "resultList";					//検索結果リスト表示オブジェクトID名
	this.listNormal				= "normal";						//リストの通常クラス名称
	this.listMsover				= "msover";						//リストのマウスオーバー時クラス名称
	this.listIconPath			= "/shared/images/result/";		//アイコンファイルへのパス
	this.listHeaderIconFile		= {								//検索結果リスト検索種類ヘッダのアイコンファイル
		 "1":"icon_map_list01.jpg",
		 "2":"icon_map_list02.jpg",
		 "3":"icon_map_list03.jpg",
		 "4":"icon_map_list04.jpg",
		 "5":"icon_map_list06.jpg",
		 "6":"icon_map_list05.jpg",
		 "7":"icon_map_list07.jpg",
		 "8":"icon_map_list08.jpg",
		 "9":"icon_map_list09.jpg"
	};
	this.listHeaderAlt = {								//検索結果リスト検索種類ヘッダの画像ALT
		 "1":"観る",
		 "2":"温泉",
		 "3":"遊ぶ",
		 "4":"体験",
		 "5":"食べる",
		 "6":"泊まる",
		 "7":"買う",
		 "8":"富士山",
		 "9":"おすすめ"
	};

	//変数の宣言
	//GoogleMaps関係
	this.gmap			= null;							//GMap2インスタンス
	this.basemarker		= null;							//ページ自身のマーカー格納
	this.marker			= new Object();					//マーカー格納
	//Ajax関係
	this.resultData		= null;							//検索結果リスト格納オブジェクト

	//チェック
	if(!this.gmapArea){
		alert("System Error! [Constructor]\nGoogle Maps表示領域が存在しません。");
		return;
	}
	if(!this.infoDataObj.lat || !this.infoDataObj.lng){
		alert("System Error! [Constructor]\n緯度経度の取得に失敗しました。");
		return;
	}
	this.loading();
}

/**
 * 処理関数郡の宣言
 * GoogleMaps表示に使用する関数郡
 */
GDMap2.prototype = {
	/**
	 * ロード画面を表示
	 * GoogleMaps表示エリアにロード中であることを示す文字を描画する
	 */
	loading : function(){
		if(this.gmapArea) this.gmapArea.innerHTML = '<div style="font-size:80%;padding:10px;">Now Loading...</div>';
		this.initGmap();
	},

	/**
	 * GoogleMaps初期化
	 * GoogleMapsを初期化する
	 */
	initGmap : function(){
		//ブラウザチェック
		if(!GBrowserIsCompatible()){
			alert("ご利用のブラウザではGoogleMapsを表示することができません。");
			return;
		}
		//GoogleMapsの初期セット
		this.gmap = new GMap2(this.gmapArea);
		var point = new GLatLng(this.infoDataObj.lat,this.infoDataObj.lng);
		this.gmap.setCenter(point, this.zoom);
		this.gmap.addControl(new GLargeMapControl());
		this.gmap.addControl(new GScaleControl());
		new GKeyboardHandler(this.gmap);
		this.gmap.enableContinuousZoom();		//連続した滑らかなズーム処理
		this.gmap.enableDoubleClickZoom();		//ダブルクリックによるズーム処理
		//情報ウィンドウ表示処理
		this.basemarker = this.createMarker(this.infoDataObj);
		if(this.basemarker){
			this.gmap.addOverlay(this.basemarker);
			var msg = this.createInfoWindowHtml(this.infoDataObj);
			this.basemarker.openInfoWindowHtml(msg,this.infoOption);
		}
		//結果表示
		this.resultData = new Object();
		this.resultData[this.infoDataObj.id] = this.infoDataObj;	//検索結果情報のセット
		this.createResultList(this.infoDataObj,0);					//検索結果リストのセット
		//検索ボタン用イベント登録
		GEvent.addListener(this.gmap,"zoomend",
			function(oldLevel,newLevel){
				//検索ボタンOFF
				if(gmap2.searchOff_zoomLevel > newLevel){
					$(gmap2.searchBtnOnId).style.display = "none";
					$(gmap2.searchBtnOffId).style.display = "block";
				}
				//検索ボタンON
				else{
					$(gmap2.searchBtnOffId).style.display = "none";
					$(gmap2.searchBtnOnId).style.display = "block";
				}
			}.bind(this)
		);
	},

	/**
	 * GoogleMaps表示領域範囲の緯度経度取得
	 * GoogleMaps表示領域範囲の緯度経度を取得する
	 * @return lat_min:最小緯度 lat_max:最大緯度 lng_min:最小経度 lng_max:最大経度
	 */
	getFourLatLng : function(){
		var bounds  = this.gmap.getBounds();	//矩形領域を取得
		var ne      = bounds.getNorthEast();	//矩形領域の北東ポイントを取得
		var sw      = bounds.getSouthWest();	//矩形領域の南西ポイントを取得
		var lat_min = sw.lat();					//最小緯度
		var lat_max = ne.lat();					//最大緯度
		var lng_min = sw.lng();					//最小経度
		var lng_max = ne.lng();					//最大経度
		return{
			lat_min:lat_min,
			lat_max:lat_max,
			lng_min:lng_min,
			lng_max:lng_max
		};
	},

	/**
	 * GoogleMapsマーカー生成
	 * GoogleMaps表示用のマーカーを生成する
	 * @param obj マーカー作成用オブジェクト
	 * @return 作成したマーカー
	 */
	createMarker : function(obj){
		//引数チェック
		var key = ((obj.id) ? obj.id : "");
		if(key == ""){
			alert("System Error! [Func:createMarker]\nデータIDの取得に失敗しました。");
			return;
		}
		if(this.basemarker && key == this.infoDataObj.id) return;
		//アイコンの作成
		var icon;
		if(obj.purpose && this.iconFile[obj.purpose]) icon = this.createIcon((key == this.infoDataObj.id ? this.iconSelf : this.iconFile[obj.purpose]));
		//マーカーの作成
		var marker;
		if(icon) marker = new GMarker(new GLatLng(obj["lat"],obj["lng"]),{icon:icon,title:obj["title"]});
		else marker = new GMarker(new GLatLng(obj["lat"],obj["lng"],{title:obj["title"]}));
		//クリックイベントの登録
		GEvent.addListener(marker, "click",
			function(){
				//メッセージの作成
				var msg = this.createInfoWindowHtml(obj);
				//ウィンドウ表示
				marker.openInfoWindowHtml(msg,this.infoOption);
			}.bind(this)
		);
		this.marker[key] = marker;
		return marker;
	},

	/**
	 * GoogleMapsアイコン生成
	 * GoogleMapsマーカー作成用のアイコンを生成する
	 * @param iconFileName アイコンファイルのファイル名
	 * @return 作成したアイコン
	 */
	createIcon : function(iconFileName){
		//引数チェック
		if(!this.iconPath || !this.icon_width || !this.icon_height || !iconFileName) return false;
		//アイコン作成
		var icon = new GIcon();
		icon.image = this.iconPath + iconFileName;
		icon.iconSize = new GSize(this.icon_width,this.icon_height);
		icon.iconAnchor = new GPoint(this.icon_width / 2,this.icon_height);	//アイコンと地図ポイントの位置関係：中央×下
		icon.infoWindowAnchor = new GPoint(this.icon_width / 2,0);
		return icon;
	},

	/**
	 * GoogleMaps情報ウィンドウ内HTML生成
	 * GoogleMaps情報ウィンドウ内のHTMLを生成する
	 * @param obj 情報ウィンドウに必要な値を持ったオブジェクト
	 * @return 作成したhtml
	 */
	createInfoWindowHtml : function(obj){
		var html = '';
		if(obj.title)   html += this.infoHtml.title.replace("#TITLE#",obj.title);
		if(obj.address) html += this.infoHtml.address.replace("#ADDRESS#",obj.address);
		if(obj.tel)     html += this.infoHtml.tel.replace("#TEL#",obj.tel);
		if(obj.url)     html += this.infoHtml.url.replace("#URL#",obj.url);
		return html;
	},

	/**
	 * 検索クエリ
	 * 検索処理を実行する
	 * @param query 検索に使用する値を持った文字列
	 * @return 作成したhtml
	 */
	doQuery : function(query){
		var chache = true;						//キャッシュ無効スイッチ(true/false)
		var xmlhttpobj = createHttpRequest();	//XMLHttpRequestオブジェクト
		//Ajaxで検索開始
		xmlhttpobj.open(this.s_method,this.s_path,this.s_async);
		if(this.s_method == 'POST') xmlhttpobj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		//キャッシュを見に行かないようにする
		if(chache) xmlhttpobj.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
		xmlhttpobj.onreadystatechange = function(){
			if(xmlhttpobj.readyState == 4) this.getResult(xmlhttpobj);
		}.bind(this);
		xmlhttpobj.send(query);
	},

	/**
	 * 結果取得
	 * 検索結果を取得する
	 * @param query 検索に使用する値を持った文字列
	 * @return 作成したhtml
	 */
	getResult : function(obj){
		if(obj.responseText != "") eval('var data=' + obj.responseText);
		else var data = new Object();
//res = '{"1":{"id":"1","title":"静岡県庁","purpose":"4","address":"静岡県静岡市葵区追手町9番6号","tel":"054-221-2455","url":"http://192.168.0.220","lat":"34.976917","lng":"138.383012"},"2":{"id":"2","title":"test_id2_name","purpose":"4","address":"test_id2_add_01","tel":"000-0000-0002","url":"http://192.168.0.220","lat":"34.956917","lng":"138.393012"},"4":{"id":"4","title":"test_id4_name","purpose":"4","address":"test_id4_add_01","tel":"","url":"http://192.168.0.220","lat":"34.986917","lng":"138.583112"}}';
//res = '{"1":{ "id":"1", "title":"静岡県庁", "purpose":"8", "address":"静岡県静岡市葵区追手町9番6号", "tel":"054-221-2455", "url":"http://192.168.0.220", "lat":"34.976917", "lng":"138.383012" } ,"4":{ "id":"4", "title":"test_id4_name", "purpose":"8", "address":"test_id4_add_01", "tel":"", "url":"http://192.168.0.220", "lat":"34.986917", "lng":"138.583112" } }';
//res = '';
//if(res != "") eval('var data=' + res);
//else var data = new Object();
		this.resultData = data;
		//リストの初期化
		$(this.listArea).innerHTML = "";
		//マーカーの作成
		var key,id,marker,result;
		var loop_cnt = 0;
		//自身のマーカーを表示
		if(this.basemarker) this.gmap.addOverlay(this.basemarker);
		//その他のマーカーを表示
		for(key in data){
			//マーカーの作成
			marker = this.createMarker(data[key]);
			if(marker) this.gmap.addOverlay(marker);
			//検索結果リスト表示
			this.createResultList(data[key],loop_cnt);
			loop_cnt++;
		}
		//リストエリアに何も表示が無ければ、指定文字を表示
		if($(this.listArea).innerHTML == "") $(this.listArea).innerHTML = '<p style="font-size:12px;">該当する情報はありません</p>';
	},

	/**
	 * 結果リスト生成
	 * 検索結果リストを生成する
	 * @param obj 検索結果が入っているオブジェクト
	 * @param loop_cnt 何件目の結果か分かるようにするためのカウンター
	 * @return 作成したhtml
	 */
	createResultList : function(obj,loop_cnt){
		//表示エリアの存在チェック
		if(!$(this.listArea)) return;
		//objのIDがページ自身のIDと同じならreturnする
		if(obj.id == this.infoDataObj.id) return;
		//リストに表示するデータを作成
		var data = document.createElement('li');
		data.id = obj.id;
		if(loop_cnt % 2 == 0) data.className = 'odd';
		data.innerHTML = '<a href="javascript:void(0)">' + obj.title + '</a>';
		$(this.listArea).appendChild(data);
		//onClickイベントの処理登録
		data.onclick = function(){
			gmap2.listClickEventListener(this.id);
		};
	},

	/**
	 * 結果リストクリックイベント
	 * 検索結果リストをクリックしたときのイベント
	 * @param key クリックされたリストのID
	 */
	listClickEventListener : function(key){
		//データのチェック
		if(!this.marker[key] || !this.resultData[key]){
			alert("System Error! [Func:listClickEventListener]\nマーカー情報の取得に失敗しました。");
			return;
		}
		//InfoWindowを表示
		this.marker[key].openInfoWindowHtml(this.createInfoWindowHtml(this.resultData[key]),this.infoOption);
	},

	/**
	 * 表示範囲内の検索処理
	 * 表示範囲内で検索を行った際の処理
	 * @param purpose_id クリックされたボタンのID
	 * @return false:処理終了
	 */
	reSearch : function(purpose_id){
		//情報ウィンドウ非表示
		gmap2.gmap.closeInfoWindow();
		//全マーカー削除
		gmap2.gmap.clearOverlays();
		gmap2.marker = new Object();
		//緯度経度範囲の抽出
		var pointRange = gmap2.getFourLatLng();
		var lat_min = pointRange.lat_min;
		var lat_max = pointRange.lat_max;
		var lng_min = pointRange.lng_min;
		var lng_max = pointRange.lng_max;
		//リストのヘッダー画像を変更
		if(this.listHeaderIconFile[purpose_id] && this.listHeaderAlt[purpose_id]){
			$(this.listHeader).innerHTML = '<img src="' + this.listIconPath + this.listHeaderIconFile[purpose_id] + '" alt="' + this.listHeaderAlt[purpose_id] + '" />';
		}
		//引数の作成
		var query   = "lat_min=" + lat_min + "&lat_max=" + lat_max + "&lng_min=" + lng_min + "&lng_max=" + lng_max + "&purpose=" + purpose_id;
		//検索処理
		gmap2.doQuery(query);
		return false;
	}
}

//画像のプリロード
function cxPreImages(){
	var d = document;
	if(d.images){
		if(!d.MM_p) d.MM_p = new Array();
		var i;
		var j = d.MM_p.length;
		var a = cxPreImages.arguments;
		for(i = 0;i < a.length;i++){
			if(a[i].indexOf("#")!=0){
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
		}
	}
}
