/**
 * GoogleMap Class
 * 
 * GoogleMapを管理するクラス
 * @version 1.0 2007/12/25
 */

/**
 * 定数
 */
var XML_PATH	= '../sys/api/map.api.php';			// ページ情報取得用API

/**
 * Mapクラス
 * @memberOf xml_data
 * @method load
 */
var Map = Class.create();
// フィールド定義 ----------------------------------------------------------- //
Map.prototype.xml_data	= null;
Map.prototype.map = null;
Map.prototype.geocoder = null;
// メソッド定義 ------------------------------------------------------------- //
// コンストラクタ-----------------------------------------------------

Map.prototype.initialize 		= function(){
	Event.observe(window, 'load', this.load.bindAsEventListener(this), false);
};
Map.prototype.load				= function(event){
	if (GBrowserIsCompatible()) {
		this.map = new GMap2($("gmap"));
		// GClientGeocoderを初期化
		this.geocoder = new GClientGeocoder();
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.addControl(new GOverviewMapControl(new GSize(100,100)));
        this.map.enableDoubleClickZoom();
		this.map.enableContinuousZoom();
		this.map.enableScrollWheelZoom();
//		this.setMarker2(36.287044,136.908099);
		this.loadMarkers(XML_PATH);
		
//		alert(this.dump(this.xml_data));
//		this.map.setCenter(new GLatLng(36.287041,136.905355), 14);
//		this.setMarker(36.287041,136.905355);
	}
};

Map.prototype.loadMarkers		= function(url){
	this.xml_data = this.execute(url);
	var markers = this.xml_data["result"]["markers"]["marker"];

	if(typeof(markers.length) == "undefined"){
		this.map.setCenter(new GLatLng(markers["lat"],markers["lng"]), 14);
	
		var marker = this.setMarker(
			parseFloat(markers["lat"], 10),
			parseFloat(markers["lng"],10),
			decodeURIComponent(markers["#text"]),
			markers["icon"],
			markers["shadow"]
		);
		GEvent.trigger(marker,'click');
	}else{
		this.map.setCenter(new GLatLng(markers[0]["lat"],markers[0]["lng"]), 14);
		for(i = 0; i < markers.length;i++){
			var marker = this.setMarker(
				parseFloat(markers[i]["lat"], 10),
				parseFloat(markers[i]["lng"],10),
				decodeURIComponent(markers[i]["#text"]),
				markers[i]["icon"],
				markers[i]["shadow"]
			);
			if(i == 0){
				GEvent.trigger(marker,'click');
			}
		}
	}
}

Map.prototype.setMarker = function(lat,lng,comment,icon_image,shadow){
    var icon = new GIcon();
    icon.image = icon_image;  //アイコン画像
    icon.shadow = shadow; //影の画像
    icon.iconSize = new GSize(40, 40);    //アイコンサイズ
    icon.shadowSize = new GSize(40, 40); //影のサイズ

    icon.printImage = icon_image;
    icon.mozPrintImage = icon_image;
    icon.printShadow = shadow;

    //アンカーポイントと画像の左上角からの相対オフセット値
    icon.iconAnchor = new GPoint(20,40);
	icon.infoWindowAnchor = new GPoint(20,20);

	var marker = 		new GMarker(new GLatLng(lat,lng),{title:'白川郷',draggable:false,icon:icon,maxWidth:200});

	this.map.addOverlay(marker);

    GEvent.addListener(marker, "click", function() {
       marker.openInfoWindowHtml(comment);
    });
	return marker;
}

Map.prototype.addMarker = function(event){
	if($F("lat") && $F("lng")){
		//alert($F("lat")+"/"+$F("lng"));
		this.setMarker($F("lat"),$F("lng"));
	}
}

Map.prototype.clearMarker = function(event){
	this.map.clearOverlays();
}

// XML関連読み込み処理------------------------------------------------------------------------------
Map.prototype.execute			= function(url,query){
	if(!this.isset(url)){
		return;
	}
	var result = null;
	
	if(this.isset(query)){
		result = new JKL.ParseXML(url,query);
	}else{
		result = new JKL.ParseXML(url);
	}
	
	result.async();
	return result.parse();
};

// MISC
Map.prototype.getOne			= function(url,query){
	return this.execute(url,query).result["#text"];
};

Map.prototype.dump				= function(xml){
	var dumper = new JKL.Dumper();
	return dumper.dump(xml);
}

Map.prototype.isset				= function(variable){
	return typeof variable == 'undefined' ? false : true;
};
Map.prototype.isnull			= function(variable){
	return variable == 'null' ? true : false;
};

// インスタンス作成
var googlemap = new Map();
