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

/**
 * 定数
 */
var XML_PATH	= '../sys/api/search.api.php';			// ページ情報取得用API
var CENTER_LAT	= 36.287041;
var CENTER_LNG	=136.905355;
var CENTER_ZOOM	=14;

/**
 * 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();
		
		if (navigator.userAgent.match("Safari")) $("gmap").onmousewheel = this.mouseWheelZoom.bindAsEventListener(this);
		else if (navigator.userAgent.match("Gecko")) $("gmap").addEventListener("DOMMouseScroll", this.mouseWheelZoom.bindAsEventListener(this), false);
		else $("gmap").attachEvent("onmousewheel", this.mouseWheelZoom.bindAsEventListener(this));

//		this.map.setCenter(new GLatLng(CENTER_LAT,CENTER_LNG), CENTER_ZOOM);
		this.map.setCenter(new GLatLng(37.287044,136.908099), CENTER_ZOOM);
		this.map.panTo(new GLatLng(CENTER_LAT,CENTER_LNG))

	
	var list = $("gmapt").getElementsByClassName("search_category");
	for(i = 0; i < list.length;i++){
		Event.observe(list[i], 'click', this.search.bindAsEventListener(this), false)
	}

	var list = $("gmapt").getElementsByClassName("search_type");
	for(i = 0; i < list.length;i++){
		Event.observe(list[i], 'click', this.search.bindAsEventListener(this), false)
	}

//	Event.observe($("send"),'click',this.search.bindAsEventListener(this),false);


//		this.loadMarkers(XML_PATH);
	}
};

Map.prototype.loadMarkers		= function(url){
	this.clearMarker();
	this.xml_data = this.execute(url);
//	this.write("gmap",this.dump(this.xml_data));
	var markers = typeof(this.xml_data["result"]["markers"]) == "undefined" ?
		null :this.xml_data["result"]["markers"]["marker"];

	if(markers == null){
		return;
	}

	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();
}

Map.prototype.mouseWheelZoom = function(event) {
	var delta = event.wheelDelta;
	if (navigator.userAgent.match("MSIE") || navigator.userAgent.match("Safari")) {
	delta = event.wheelDelta;
	event.returnValue = false;
	} else {
	delta = event.detail * -1;
	event.preventDefault();
	}
	this.map.setZoom(this.map.getZoom() + (delta > 0 ? 1 : -1));
}


Map.prototype.search = function(event){
	var category	= $("gmapt").getElementsByClassName("search_category");
	var type		= $("gmapt").getElementsByClassName("search_type");
	
	var category_array = new Array();
	for(i = 0; i < category.length; i++){
		if(category[i].checked){
			category_array.push(category[i].value);
		}
	}
	
	var type_array = new Array();
	for(i=0;i < type.length;i++){
		if(type[i].checked){
			type_array.push(type[i].value);
		}
	}
	
	var query = "?c="+encodeURIComponent(category_array.join(","))+"&t="+encodeURIComponent(type_array.join(","))
//	this.xml_data = this.execute(XML_PATH+query);
	this.loadMarkers(XML_PATH+query);
//	this.write("gmap",this.dump(this.xml_data));
}

// 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.write				= function(target,src){
	if($(target) == null){
		alert("出力先が見つかりません。");
	}else{
		Element.update($(target),src);
	}
};

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();
