// JavaScript Document

var map = "";

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(36.07748516109143, 140.11884570121765), 17);
		//print_marker();
		load_data("all");
	}else{
		document.write("Google Mapsは利用できません");
	}
}

//クロスブラウザ用XlHttpRequestオブジェクト生成関数
function createHttpRequest(){
	if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	}else if(window.ActiveXOvject){
		try{
			return new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e2){
				return null;
			}
		}
	}else{
		return null;
	}
}

//データベースから店舗情報を取得
function load_data(cate){
	//マーカーを削除
	map.clearOverlays();
	//XMLHttpRequestオブジェクトを生成
	var request = createHttpRequest();
	//URIエンコード
	//cate = encodeURI(cate);
	
	//request.open("GET", "../lib/return_data.php?flag=1&category="+cate);
	request.open("GET", "./lib/return_data.php?flag=1&category="+cate);
	//コールバック関数
	request.onreadystatechange = function(){
		if(request.readyState == 4){
			//レスポンスの受信
			var res = request.responseXML;
			//地図上に表示
			print_marker(res);
		}
	}
	
	request.send("");
}

//地図上にマーカを表示する関数
function print_marker(res){
	
	//xmlデータからマーカーを作成
	var shopdata = res.getElementsByTagName("shopData");
	
	for(i=0;i<shopdata.length;i++){
		var img = "";
		//xmlからデータを取得
		var name = shopdata[i].getElementsByTagName("name")[0].firstChild.nodeValue;
		var category = shopdata[i].getElementsByTagName("tel")[0].firstChild.nodeValue;
		var tel = shopdata[i].getElementsByTagName("category")[0].firstChild.nodeValue;
		var address = shopdata[i].getElementsByTagName("address")[0].firstChild.nodeValue;
		var lat = shopdata[i].getElementsByTagName("lat")[0].firstChild.nodeValue;
		var lon = shopdata[i].getElementsByTagName("lon")[0].firstChild.nodeValue;
		if(shopdata[i].getElementsByTagName("img")[0].firstChild != null)
		var img = shopdata[i].getElementsByTagName("img")[0].firstChild.nodeValue;
		var shopid = shopdata[i].getElementsByTagName("shopid")[0].firstChild.nodeValue;
		//htmlデータの作成
		var html = "<div style='width:400'><div style='float:left;text-align: left'>"+name;
		html += "<br />"+category;
		html += "<br />"+tel;
		html += "<br />"+address;
		html += "<br /><a href=\"./contents.php?id="+shopid+"\">詳細ページへ</a>";
		html += "</div><div style='float:right'><img src=\"./photo/"+img+"\" height=\"90\" width=\"140\"/></div>";
		html += "</div>";
		//alert(html);
		//マーカーの作成、登録
		var marker= create_marker(new GLatLng(lat,lon),html, shopid, name);
 		map.addOverlay(marker);
	}
}

//マーカーを作成
function create_marker(point, html, shopid, name){
	var icon = new GIcon(G_DEFAULT_ICON);
	var dir_name;
	
	switch(shopid.substring(0,1)){
		case "i":
				dir_name = "tavern";
				break;
		case "w":
				dir_name = "jpn";
				break;
		case "b":
				dir_name = "bar";
				break;
		case "g":
				dir_name = "foreign";
				break;
		case "k":
				dir_name = "cafe";
				break;
		case "s":
				dir_name = "snack";
				break;
	}
	icon.image = "./images/ch/gourmet/pin/"+dir_name+"/"+shopid.substring(1, shopid.length)+".gif";
	icon.shadow = "./images/ch/gourmet/pin/shadow.png"
	icon.iconSize = new GSize(19, 23);
	icon.shadowSize = new GSize(31, 23);
	icon.iconAnchor = new GPoint(20,37);
	var opts = {
		icon:icon,
		title:name
	}
	
	var marker = new GMarker(point, opts);
	GEvent.addListener(marker, 'click', function(){ 
		marker.openInfoWindowHtml(html);
	});
	return marker;
}
