/*  MAP検索 ライブラリ
 *  (c) 2007 Kaneda brothers
 *  charset: UTF-8
 *--------------------------------------------------------------------------*/

var MapSystem = function(c) {
	this.initialize(c);
};

MapSystem.prototype = {
	map:       null,
	charset:   10,
	dir:       'jp',
	floter:    null,
	result:    null,
	form:      null,
	marker:    null,
	page:      1,
	load:      0,
	areaID:    null,
	closeName: null,
	resID:     null,
	category:  null,
	xa: null, ya: null, na: null, za: null,
	initialize: function(c) {
		if (!GBrowserIsCompatible()) return;
		this.charset  = c;
		switch(c) {
			case 30: this.dir = ''; break;
			default: this.dir = '';
		}
		this.marker   = new Array();
		this.category = new Array();
		this.xa       = new Array();
		this.ya       = new Array();
		this.na       = new Array();
		this.za       = new Array();
		
		this.map = new GMap2($("map"));
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.setCenter(new GLatLng(34.673068, 135.511271), 13);
		this.map.enableScrollWheelZoom();
		
		GEvent.addListener(this.map, "dblclick",  this.setLoadOn.bind(this));
		GEvent.addListener(this.map, "dragstart", this.setLoadOn.bind(this));
		GEvent.addListener(this.map, "moveend",   this.initResult.bind(this));
		
		this.setCenterFloter();
		
		// カテゴリースィッチ反映
		this.setCategory('sightseeing'  , 1);
		this.setCategory('accomodations', 0);
		this.setCategory('event'        , 0);
		this.setCategory('gourmet'      , 0);
		this.setCategory('shopping'     , 0);
		
		this.load = 1;
		this.initResult();
	},
	setCenterFloter: function() {
		var f = $('mapsearch_floter');
		var w = Math.round((xClientWidth() - 800) / 2);
		var h = Math.round((xClientHeight() - 600) / 2);
		if (w < 0) w = 0;
		if (h < 0) h = 0;
		xLeft(f, w);
		xTop(f, h);
		xDisplay(f, 'block');
	},
	createGicon: function(id){
		var icon = new GIcon();
		icon.image = "/common/gicon/gi_"+id+".png";
		icon.shadow = "/common/gicon/shadow.png";
		icon.iconSize = new GSize(20, 26);
		icon.shadowSize = new GSize(37, 26);
		icon.iconAnchor = new GPoint(10, 26);
		icon.infoWindowAnchor = new GPoint(10, 1);
		
		return icon;
	},
	createMarker: function(point, icon, html) {
		var marker = new GMarker(point, icon);
		var map    = this.map;
		GEvent.addListener(marker, 'click', function() {map.panTo(marker.getPoint()); marker.openInfoWindowHtml(html);});
		return marker;
	},
	setCategory: function(objName, flg) {
		this.category[objName] = flg;
		if (flg == 1) {
			$("chk_"+objName).src = this.dir+"/common/images_mapsearch/btn_c_" + objName + "_on.gif";
		} else {
			$("chk_"+objName).src = this.dir+"/common/images_mapsearch/btn_c_" + objName + "_off.gif";
		}
	},
	setLoadOn: function() {
		this.load = 1;
	},
	
	initResult: function() {
		if (this.load != 1) return;
		this.load = 0;
		this.page = 1;
		$('mapsearch_result_next').innerHTML = '';
		$('mapsearch_result_back').innerHTML = '';
		$('mapsearch_result_cnumber').innerHTML = '';
		
		var bnd = this.map.getBounds();
		var latlng;
		var num;
		var form;
		
		latlng = bnd.getSouthWest();
		num = latlng.lat() + '';  form  =  'from_lat=' + num.replace('.', '%2E');
		num = latlng.lng() + '';  form += '&from_lng=' + num.replace('.', '%2E');
		
		latlng = bnd.getNorthEast();
		num = latlng.lat() + '';  form += '&to_lat=' + num.replace('.', '%2E');
		num = latlng.lng() + '';  form += '&to_lng=' + num.replace('.', '%2E');
		
		latlng = this.map.getCenter();
		num = latlng.lat() + '';  form += '&center_lat=' + num.replace('.', '%2E');
		num = latlng.lng() + '';  form += '&center_lng=' + num.replace('.', '%2E');
		
		// カテゴリー
		if (this.category['sightseeing'] == 1)   { form += '&sightseeing=1';   }
		if (this.category['accomodations'] == 1) { form += '&accomodations=1'; }
		if (this.category['event'] == 1)         { form += '&event=1';         }
		if (this.category['gourmet'] == 1)       { form += '&gourmet=1';       }
		if (this.category['shopping'] == 1)      { form += '&shopping=1';      }
		
		this.form = form;
		this.loadResult();
	},
	
	loadResult: function() {
		
		// Loading...
		var loading = $("mapsearch_loading");
		xDisplay(loading, 'block');
		var w = Math.round((xClientWidth() - xWidth(loading)) / 2);
		var h = Math.round((xClientHeight() - xHeight(loading)) / 2);
		if (w < 0) w = 0;
		if (h < 0) h = 0;
		xLeft(loading, w);
		xTop(loading, h);
		
		$('mapsearch_result_list').innerHTML = 'Now Loading...';
		
		var opt = { method: 'get'
		          , parameters: this.form + '&page=' + this.page
		          , onSuccess: this.setResult.bind(this)
		          , onFailure: this.printFailure.bind(this)
		          , onException: this.printException.bind(this)
		          };
		new Ajax.Request(this.dir+'/search/map/list.php', opt);
	},
	printFailure: function(req, obj) {
		alert('Sorry, Too busy.');
	},
	printException: function(req, ex) {
		alert('ERROR： ' + ex);
	},
	
	setResult: function(req) {
		this.map.clearOverlays();
		this.marker = new Array();
		var txt = decodeURIComponent(req.responseText);
		eval('this.result = ' + txt);
		
		if (this.result.error != '0' || this.result.count == 0) {
			$('mapsearch_result_list').innerHTML = '';
			xDisplay('mapsearch_loading', 'none');
			return;
		}
		
		var cnt = this.result.list.length;
		var i;
		var htm = '';
		for (i = 0; i < cnt; i++) {
			htm += '<li id="re' + i + '"';
			htm += ' onmouseover="maps.mapListOv(' + i + ')"';
			htm += ' onmouseout="maps.mapListOut(' + i + ')"';
			htm += ' onclick="maps.mapListClick(' + i + ')">'
			htm += this.result.list[i].name + '</li>';
			
			this.xa[i] = this.result.list[i].longitude;
			this.ya[i] = this.result.list[i].latitude;
			this.na[i] = this.getInfoHTML(this.result.list[i]);
			var icon = this.createGicon(i);
			var point = new GPoint(this.xa[i], this.ya[i]);
			this.marker[i] = this.createMarker(point, icon, this.na[i]);
			this.map.addOverlay(this.marker[i]);
			this.za[i] = 13;
		}
		
		// Pager
		if (this.result.count > (15 * this.page)) {
			$('mapsearch_result_next').innerHTML = '<img src="'+this.dir+'/common/images_mapsearch/btn_next.gif" alt="next" width="31" height="5" />';
		} else {
			$('mapsearch_result_next').innerHTML = '';
		}
		if (this.result.count > 15 && this.page > 1) {
			$('mapsearch_result_back').innerHTML = '<img src="'+this.dir+'/common/images_mapsearch/btn_back.gif" alt="back" width="31" height="5" />';
		} else {
			$('mapsearch_result_back').innerHTML = '';
		}
		if (this.result.count > 15) {
			var total = Math.floor(this.result.count / 15);
			if ((this.result.count % 15) > 0) total++;
			$('mapsearch_result_cnumber').innerHTML = this.page + ' / ' + total;
		}
		
		$('mapsearch_result_list').innerHTML = htm;
		this.openResult();
		xDisplay('mapsearch_loading', 'none');
	},
	
	resBack: function() {
		if (this.page > 1) {
			this.page--;
			this.loadResult();
		}
	},
	resNext: function() {
		if (!this.result) return;
		if (this.result.count > (this.page * 15)) {
			this.page++;
			this.loadResult();
		}
	},
	
	getInfoHTML: function(rec) {
		var htm = '';
		htm += '<div class="mapsearch_fukidashi">';
		htm += '<a href="'+this.dir+'/search/detail/detail_hit.php?c=' + rec.contents_no + '&amp;n=' + rec.no + '">';
		htm += '<img src="/db/' + rec.image + '" alt="' + rec.seo_kw_alt + '" width="53" height="53" class="blockimg" />';
		htm += '</a>';
		htm += '<img src="/common/images/icon_rank' + rec.rank + '.gif" alt="" width="22" height="11" class="blockimg_mark" />';
		
		if (rec.barrierfree_flg == 1) {
			htm += '<img src="/common/images/icon_barrierfree.gif" alt="고령자 할인" width="11" height="11" class="blockimg_mark" />';
		}
		if (rec.parking_flg == 1 && rec.contents_no == 20) {
			htm += '<img src="/common/images/icon_parking.gif" alt="주차장" width="11" height="11" class="blockimg_mark" />';
		}
		if (rec.internet_flg == 1 && rec.contents_no == 20) {
			htm += '<img src="/common/images/icon_internet.gif" alt="인터넷" width="11" height="11" class="blockimg_mark" />';
		}
		if (rec.child_flg == 1) {
			htm += '<img src="/common/images/icon_child.gif" alt="특별 장애자 할인" width="11" height="11" class="blockimg_mark" />';
		}
		if (rec.disability_flg == 1) {
			htm += '<img src="/common/images/icon_disability.gif" alt="어린이 보호시설" width="11" height="11" class="blockimg_mark" />';
		}
		if (rec.senior_flg == 1) {
			htm += '<img src="/common/images/icon_senior.gif" alt="장애물 없음" width="11" height="11" class="blockimg_mark" />';
		}
		
		htm += '<h2><a href="'+this.dir+'/search/detail/detail_hit.php?c=' + rec.contents_no + '&amp;n=' + rec.no + '">' + rec.name + '</a></h2>';
		htm += rec.outline;
		htm += '</div>';
		
		return htm;
	},
	openResult: function() {
		clearTimeout(this.resID);
		$("mapsearch_result_title_base").style.display = 'none';
		$("mapsearch_result_base").style.display = 'block';
		$("mapsearch_result").style.height = '409px';
	},
	resCloseTime: function() {
		clearTimeout(this.resID);
		this.resID = setTimeout("maps.resCloseFinal()", 1500);
	},
	resCloseFinal: function() {
		$("mapsearch_result_title_base").style.display = 'block';
		$("mapsearch_result_base").style.display = 'none';
		$("mapsearch_result").style.height = '15px';
	},
	
	mapListClick: function(num){
		if (this.marker.length == 0) return;
		if (!this.marker[num])       return;
		this.map.closeInfoWindow();
		this.map.panTo(this.marker[num].getPoint());
		this.marker[num].openInfoWindowHtml(this.na[num]);
	},
	mapListOv: function(num){
		$("re"+num).style.backgroundColor = "#A6B53B";
	},
	mapListOut: function(num){
		$("re"+num).style.backgroundColor = "";
	},
	
	clickCategory: function(objName) {
		if (this.category[objName] == 1) {
			$("chk_"+objName).src = this.dir+"/common/images_mapsearch/btn_c_" + objName + "_off.gif";
			this.category[objName] = 0;
		} else {
			$("chk_"+objName).src = this.dir+"/common/images_mapsearch/btn_c_" + objName + "_on.gif";
			this.category[objName] = 1;
		}
		this.load = 1;
		this.initResult();
	},
	
	divOpen: function(num,clnum) {
		clearTimeout(this.areaID);
		$("area_"+clnum).style.display = 'none';
		$("area_"+num).style.display = 'block';
		$("area_base").style.display = 'block';
		
		$("btn_"+clnum).src = this.dir+"/common/images_mapsearch/btn_" + clnum + ".gif";
		$("btn_"+num).src   = this.dir+"/common/images_mapsearch/btn_" + num + "_on.gif";
		
		$("mapsearch_"+clnum).style.height = '30px';
		$("mapsearch_"+num).style.height = '45px';
	},
	closeTime: function(objNum) {
		this.closeName = objNum;
		clearTimeout(this.areaID);
		this.areaID = setTimeout("maps.closeFinal()", 400);
	},
	closeFinal: function() {
		$("area_base").style.display = 'none';
		$("area_"+this.closeName).style.display = 'none';
		$("btn_"+this.closeName).src = this.dir+"/common/images_mapsearch/btn_" + this.closeName + ".gif";
		$("mapsearch_"+this.closeName).style.height = '30px';
	},
	mapSetArea: function(num) {
		this.closeFinal();
		this.map.setCenter(new GLatLng(OSK_mmpY[num], OSK_mmpX[num]), 17 - OSK_mmpZ[num]);
		this.load = 1;
		this.initResult();
	}
}

// 初期化
var maps = null;
function onLoadArea(c) {
	maps = new MapSystem(c);
}


var OSK_mmpX = new Array();
var OSK_mmpY = new Array();
var OSK_mmpZ = new Array();

OSK_mmpX[0]=135.422042;		OSK_mmpY[0]=34.967543;		OSK_mmpZ[0]=5;
OSK_mmpX[1]=135.455662;		OSK_mmpY[1]=34.90623; 		OSK_mmpZ[1]=5;
OSK_mmpX[2]=135.435608;		OSK_mmpY[2]=34.818319;		OSK_mmpZ[2]=5;
OSK_mmpX[3]=135.482126;		OSK_mmpY[3]=34.830188;		OSK_mmpZ[3]=5;
OSK_mmpX[4]=135.564423;		OSK_mmpY[4]=34.820004;		OSK_mmpZ[4]=5;
OSK_mmpX[5]=135.623265;		OSK_mmpY[5]=34.849905;		OSK_mmpZ[5]=5;
OSK_mmpX[6]=135.646305;		OSK_mmpY[6]=34.902264;		OSK_mmpZ[6]=4;
OSK_mmpX[7]=135.470644;		OSK_mmpY[7]=34.777522;		OSK_mmpZ[7]=5;
OSK_mmpX[8]=135.517502;		OSK_mmpY[8]=34.776058;		OSK_mmpZ[8]=5;
OSK_mmpX[9]=135.56767;		OSK_mmpY[9]=34.777734;		OSK_mmpZ[9]=4;
OSK_mmpX[10]=135.648814;	OSK_mmpY[10]=34.816171;		OSK_mmpZ[10]=5;
OSK_mmpX[11]=135.675595;	OSK_mmpY[11]=34.786924;		OSK_mmpZ[11]=4;
OSK_mmpX[12]=135.620603;	OSK_mmpY[12]=34.763893;		OSK_mmpZ[12]=4;
OSK_mmpX[13]=135.639297;	OSK_mmpY[13]=34.730128;		OSK_mmpZ[13]=4;
OSK_mmpX[14]=135.565273;	OSK_mmpY[14]=34.735323;		OSK_mmpZ[14]=4;
OSK_mmpX[15]=135.583224;	OSK_mmpY[15]=34.73809;		OSK_mmpZ[15]=4;
OSK_mmpX[16]=135.626211;	OSK_mmpY[16]=34.713536;		OSK_mmpZ[16]=4;
OSK_mmpX[17]=135.511271;	OSK_mmpY[17]=34.673068;		OSK_mmpZ[17]=3;
OSK_mmpX[18]=135.599854;	OSK_mmpY[18]=34.667731;		OSK_mmpZ[18]=4;
OSK_mmpX[19]=135.607866;	OSK_mmpY[19]=34.620031;		OSK_mmpZ[19]=4;
OSK_mmpX[20]=135.550887;	OSK_mmpY[20]=34.579895;		OSK_mmpZ[20]=4;
OSK_mmpX[21]=135.603618;	OSK_mmpY[21]=34.572391;		OSK_mmpZ[21]=4;
OSK_mmpX[22]=135.631383;	OSK_mmpY[22]=34.577983;		OSK_mmpZ[22]=4;
OSK_mmpX[23]=135.592767;	OSK_mmpY[23]=34.554722;		OSK_mmpZ[23]=4;
OSK_mmpX[24]=135.498644;	OSK_mmpY[24]=34.577765;		OSK_mmpZ[24]=4;
OSK_mmpX[25]=135.557234;	OSK_mmpY[25]=34.504643;		OSK_mmpZ[25]=2;
OSK_mmpX[26]=135.422042;	OSK_mmpY[26]=34.967543;		OSK_mmpZ[26]=5;
OSK_mmpX[27]=135.644299;	OSK_mmpY[27]=34.518682;		OSK_mmpZ[27]=5;
OSK_mmpX[28]=135.635912;	OSK_mmpY[28]=34.488138;		OSK_mmpZ[28]=4;
OSK_mmpX[29]=135.626768;	OSK_mmpY[29]=34.45066;		OSK_mmpZ[29]=5;
OSK_mmpX[30]=135.438518;	OSK_mmpY[30]=34.524075;		OSK_mmpZ[30]=4;
OSK_mmpX[31]=135.414097;	OSK_mmpY[31]=34.502596;		OSK_mmpZ[31]=4;
OSK_mmpX[32]=135.39965;		OSK_mmpY[32]=34.488375;		OSK_mmpZ[32]=4;
OSK_mmpX[33]=135.477733;	OSK_mmpY[33]=34.429000;		OSK_mmpZ[33]=5;
OSK_mmpX[34]=135.565446;	OSK_mmpY[34]=34.442027;		OSK_mmpZ[34]=4;
OSK_mmpX[35]=135.392889;	OSK_mmpY[35]=34.460904;		OSK_mmpZ[35]=5;
OSK_mmpX[36]=135.363079;	OSK_mmpY[36]=34.434026;		OSK_mmpZ[36]=5;
OSK_mmpX[37]=135.356741;	OSK_mmpY[37]=34.398479;		OSK_mmpZ[37]=5;
OSK_mmpX[38]=135.323078;	OSK_mmpY[38]=34.403792;		OSK_mmpZ[38]=5;
OSK_mmpX[39]=135.286643;	OSK_mmpY[39]=34.396086;		OSK_mmpZ[39]=5;
OSK_mmpX[40]=135.27356;		OSK_mmpY[40]=34.369735;		OSK_mmpZ[40]=5;
OSK_mmpX[41]=135.239728;	OSK_mmpY[41]=34.348678;		OSK_mmpZ[41]=5;
OSK_mmpX[42]=135.153897;	OSK_mmpY[42]=34.321014;		OSK_mmpZ[42]=5;

OSK_mmpX[50]=135.528774;	OSK_mmpY[50]=34.743964;		OSK_mmpZ[50]=3;
OSK_mmpX[51]=135.48523;		OSK_mmpY[51]=34.729057;		OSK_mmpZ[51]=3;
OSK_mmpX[52]=135.450991;	OSK_mmpY[52]=34.708756;		OSK_mmpZ[52]=3;
OSK_mmpX[53]=135.547795;	OSK_mmpY[53]=34.723158;		OSK_mmpZ[53]=3;
OSK_mmpX[54]=135.529564;	OSK_mmpY[54]=34.708058;		OSK_mmpZ[54]=3;
OSK_mmpX[55]=135.573759;	OSK_mmpY[55]=34.701183;		OSK_mmpZ[55]=3;
OSK_mmpX[56]=135.548478;	OSK_mmpY[56]=34.696794;		OSK_mmpZ[56]=3;
OSK_mmpX[57]=135.503466;	OSK_mmpY[57]=34.702154;		OSK_mmpZ[57]=3;
OSK_mmpX[58]=135.477554;	OSK_mmpY[58]=34.692964;		OSK_mmpZ[58]=3;
OSK_mmpX[59]=135.451866;	OSK_mmpY[59]=34.682382;		OSK_mmpZ[59]=3;
OSK_mmpX[60]=135.454237;	OSK_mmpY[60]=34.663187;		OSK_mmpZ[60]=3;
OSK_mmpX[61]=135.487148;	OSK_mmpY[61]=34.679135;		OSK_mmpZ[61]=3;
OSK_mmpX[62]=135.508207;	OSK_mmpY[62]=34.678835;		OSK_mmpZ[62]=3;
OSK_mmpX[63]=135.545977;	OSK_mmpY[63]=34.672565;		OSK_mmpZ[63]=3;
OSK_mmpX[64]=135.472534;	OSK_mmpY[64]=34.614354;		OSK_mmpZ[64]=3;
OSK_mmpX[65]=135.470956;	OSK_mmpY[65]=34.650479;		OSK_mmpZ[65]=3;
OSK_mmpX[66]=135.497981;	OSK_mmpY[66]=34.660026;		OSK_mmpZ[66]=3;
OSK_mmpX[67]=135.521496;	OSK_mmpY[67]=34.661054;		OSK_mmpZ[67]=3;
OSK_mmpX[68]=135.493543;	OSK_mmpY[68]=34.63846;		OSK_mmpZ[68]=3;
OSK_mmpX[69]=135.542859;	OSK_mmpY[69]=34.653552;		OSK_mmpZ[69]=3;
OSK_mmpX[70]=135.514038;	OSK_mmpY[70]=34.633276;		OSK_mmpZ[70]=3;
OSK_mmpX[71]=135.503999;	OSK_mmpY[71]=34.606683;		OSK_mmpZ[71]=3;
OSK_mmpX[72]=135.531956;	OSK_mmpY[72]=34.621434;		OSK_mmpZ[72]=3;
OSK_mmpX[73]=135.559149;	OSK_mmpY[73]=34.618902;		OSK_mmpZ[73]=3;

