var valueScript = window.location.protocol + '//' + window.location.host + window.location.pathname;
var valueGenre = 0;
var valuePoint = 0;
var valueZoom = 0;
var valueWindow = '';
var elementAnchor = null;
var map = null;
var markers = new Array();

function mapUnload() {
	GUnload();
}

function mapLoad(idAnchor, idWindow, idMap, x, y, z, g) {
	elementAnchor = document.getElementById(idAnchor);
	valueWindow = document.getElementById(idWindow).innerHTML;
	valueGenre = g;
	
	// マップ作成
	map = new GMap2(document.getElementById(idMap));
	GEvent.addListener(map, "moveend", function() {
		mapMove();
	});
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(y, x), Number(z));
}

function mapMove() {
	var mapBounds = map.getBounds();
	var maxX = mapBounds.getNorthEast().lng();
	var maxY = mapBounds.getNorthEast().lat();
	var minX = mapBounds.getSouthWest().lng();
	var minY = mapBounds.getSouthWest().lat();
	var request = GXmlHttp.create();
	var objResponse = null;
	valuePoint = map.getCenter();
	valueZoom = map.getZoom();
	changeAnchor();
	
	request.open('GET', 'map_loader.php?action=list&max_x=' + maxX + '&max_y=' + maxY + '&min_x=' + minX + '&min_y=' + minY + '&genre_id=' + valueGenre, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			objResponse = eval('(' + request.responseText + ')');
			if (!objResponse['error']) {
				for (var i = 0; i < objResponse['item'].length; i++) {
					var item = objResponse['item'][i];
					if (markers[item['shop_id']] != 1) {
						mapAddMarker(item['shop_id'], item['shop_info_genre_id'], item['shop_access_map_x'], item['shop_access_map_y']);
						markers[item['shop_id']] = 1;
					}
				}
			}
		}
	}
	request.send(null);
}

function mapAddMarker(valueShop, valueShopGenre, x, y) {
	var html = '';
	var objResponse = null;
	var mapIcon = new GIcon();
	mapIcon.iconSize = new GSize(20,20);
	mapIcon.shadowSize = new GSize(40,20);
	mapIcon.iconAnchor = new GPoint(2, 20);
	mapIcon.shadowAnchor = new GPoint(2, 20);
	mapIcon.infoWindowAnchor = new GPoint(10, 0);
	mapIcon.infoShadowAnchor = new GPoint(10, 0);
	mapIcon.image = 'images/search/icon.png';
//	mapIcon.image = 'images/mapicon/genre_' + valueShopGenre + '.png';
	mapIcon.shadow = 'images/search/shadow.png';
	
	var marker = new GMarker(new GLatLng(y, x), mapIcon);
	GEvent.addListener(marker, "click", function() {
		var request = GXmlHttp.create();
		request.open('GET', 'map_loader.php?action=shop&shop_id=' + valueShop, true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				html = valueWindow;
				objResponse = eval('(' + request.responseText + ')');
				if (!objResponse['error']) {
					html = html.replace(/%7B/g, '{');
					html = html.replace(/%7D/g, '}'); // FireFox対応
					html = replaceTag(html, objResponse['shop']);
//					alert(html);
					marker.openInfoWindowHtml(html);
				}
			}
		}
		request.send(null);
	});
	map.addOverlay(marker);
}

function changeGenre(g) {
	valueGenre = g;
	markers.length = 0;
	map.clearOverlays();
	mapMove();
	changeAnchor();
}

function changeAnchor() {
	elementAnchor.href = valueScript + '?x=' + valuePoint.lng() + '&y=' + valuePoint.lat() + '&z=' + valueZoom + '&g=' + valueGenre;
}


