// JavaScript Document
var map = 0;
var custom_icon = {
	url:"images/shop_detail/pointer_image.png",
	width:33,
	height:42,
	anchor_left:17,
	anchor_top:42,
	info_anchor_left:22,
	info_anchor_top:5
}
var custom_shadow = {
	url:"images/shop_detail/pointer_shadow.png",
	width:43,
	height:42
}
function WPGMClosure(obj, method, argsArray) {
	return function() {
		return method.apply(obj, argsArray);
	}
}

function GMap2_WheelZoomEnabled(pane) {
	this.map = new GMap2(pane);
	this.MAX = 19;
	this.mp = 0;
	this.ic = 0;
	this.oc = 0;
	this.wheel = false;
	this.wheeltimer = 0;
	GEvent.addListener(this.map, "mouseover", this.enableWheel.bind(this), false);
	GEvent.addListener(this.map, "mouseout", this.disableWheel.bind(this), false);
	GEvent.bind(this.map, "mousemove", this, this.mousemove);
	GEvent.bind(this.map, "moveend", this, function() {
		this.getZoomInCenter();
		this.getZoomOutCenter();
	});
	if (pane.addEventListener) pane.addEventListener('DOMMouseScroll', function(e) {
		window.event = e;
	}, false);
	if (pane.addEventListener) pane.addEventListener('DOMMouseScroll', WPGMClosure(this, this.getWheelDelta, []), false);
	else 
		pane.onmousewheel = WPGMClosure(this, this.getWheelDelta, []);
	return this.map;
}

GMap2_WheelZoomEnabled.prototype.enableWheel = function() {
	this.wheeltimer = setTimeout(this.doEnableWheel.bind(this), 500);
};

GMap2_WheelZoomEnabled.prototype.disableWheel = function() {
	clearTimeout(this.wheeltimer);
	this.wheel = false;
};

GMap2_WheelZoomEnabled.prototype.doEnableWheel = function() {
	this.wheel = true;
};

GMap2_WheelZoomEnabled.prototype.getWheelDelta = function() {
	if (!this.wheel) return false;
	var e = event;
	var d = 0;
	if (!e)
		e = (this.map.getPane()).event;
	if (e.wheelDelta) {
		d = e.wheelDelta/120;
		if (window.opera)
			d = -d;
	} else if (e.detail) {
		d = -e.detail/3;
	}
	if (d) {
		this.wheelZoom(d);
		if (e.preventDefault) {
			e.preventDefault();
		}
		e.returnValue = false;
	}
};

GMap2_WheelZoomEnabled.prototype.wheelZoom = function(d) {
	if (d < 0) {
		if (this.map.getZoom() > 0) {
			this.ic = this.map.getCenter();
			this.map.setCenter(this.oc);
			this.map.zoomOut();
			this.getZoomOutCenter();
		}
	} else {
		if (this.map.getZoom() < this.MAX) {
			this.oc = this.map.getCenter();
			this.map.setCenter(this.ic);
			this.map.zoomIn();
			this.getZoomInCenter();
		}
	}
};

GMap2_WheelZoomEnabled.prototype.mousemove = function(latlng) {
	this.mp = latlng;
	this.getZoomInCenter();
	this.getZoomOutCenter();
};

GMap2_WheelZoomEnabled.prototype.getZoomInCenter = function() {
	if (!this.mp) return null;
	var a = this.mp; var b = this.map.getCenter();
	if (Math.max(a.lng(), b.lng()) - Math.min(a.lng(), b.lng()) > Math.max(a.lng(), b.lng())) {
		if (a.lng() < 0) a = new GLatLng(a.lat(), a.lng() + 360, true);
		else b = new GLatLng(b.lat(), b.lng() + 360, true);
	}
	var ne = new GLatLng(Math.max(a.lat(),b.lat()), Math.max(a.lng(),b.lng()));
	var sw = new GLatLng(Math.min(a.lat(),b.lat()), Math.min(a.lng(),b.lng()));
	var nb = new GLatLngBounds(sw, ne);
	this.ic = nb.getCenter();
};

GMap2_WheelZoomEnabled.prototype.getZoomOutCenter = function() {
	if (!this.mp) return null;
	var cp = this.map.getCenter();
	this.oc = new GLatLng(2*cp.lat()-this.mp.lat(),2*cp.lng()-this.mp.lng());
};

function createMarker(target, listen) {
	var icon = new GIcon();
	icon.image = custom_icon.url;
	icon.shadow = custom_shadow.url;
	icon.iconSize = new GSize(custom_icon.width, custom_icon.height);
	icon.shadowSize = new GSize(custom_shadow.width, custom_shadow.height);
	icon.iconAnchor = new GPoint(custom_icon.anchor_left, custom_icon.anchor_top);
	icon.infoWindowAnchor = new GPoint(custom_icon.info_anchor_left, custom_icon.info_anchor_top);
	var marker = new GMarker(new GLatLng(parseFloat(target.meta.lat), parseFloat(target.meta.lng)), icon);
	if (listen)
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("<h4><a href=\"shop"+target.code+".html\">"+target.name+"</a></h4><span>"+target.meta.loc+"</span>"
			);
		});
	return marker;
}

function dispwholemap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2_WheelZoomEnabled(document.getElementById("map_main"));
		var point = new GLatLng(parseFloat(posts_params[0].meta.lat), parseFloat(posts_params[0].meta.lng));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		var maptype = G_NORMAL_MAP;
		map.setCenter(point, 15, maptype);
		pointsBounds = map.getBounds();
		map.addOverlay(posts_params[0].marker = createMarker(posts_params[0]), true);
		if (posts_params.length > 0) {
			var marker = 0;
			for (var i = 1; i < posts_params.length; i++) {
				marker = createMarker(posts_params[i], true);
				posts_params[i].marker = marker;
				map.addOverlay(marker);
				point = marker.getPoint()
				if (!pointsBounds.contains(point)) {
					pointsBounds.extend(point);
				}
			}
			map.setCenter(pointsBounds.getCenter(), map.getBoundsZoomLevel(pointsBounds), maptype);
		}
	}
}

function update_shop_detail(request) {
	$('shop_detail').innerHTML = request.responseText;
	dispmap($('shop_detail').firstChild.firstChild.id);
}

function onLoad() {
	dispwholemap();
}

Event.observe(window, "load", onLoad, false);
