////////////////////////////////////////////////////////////////
// for Widget
////////////////////////////////////////////////////////////////
PostLocation.prototype.load = function(_id, _y, _x, _z, _postID)
{
	if (GBrowserIsCompatible()) {
		var self = this;
		
		if (_postID && _postID != 0) this.postID = _postID;
		
		//全体表示ボタン
		var img1 = document.createElement("img");
		img1.style.position = "absolute";
		img1.style.left = "5px";
		img1.style.top = "7px";
		img1.src = this.pluginPath+"icons/return.png";
		img1.title = "初期表示位置に戻る";
		img1.onclick = function() { self.map.returnToSavedPosition(); };
		img1.style.cursor = "pointer";
		document.getElementById(_id).appendChild(img1);
		
		//ズームコントロール
		this.map.addControl(new GSmallZoomControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(6, 30)));

		var maptype = G_NORMAL_MAP;

		//////// MapType Cookie
		var maptypename = this.getCookie("gmaptype");
		if (maptypename != null) {
			if (maptypename == "G_SATELLITE_MAP") maptype = G_SATELLITE_MAP;
			else if (maptypename == "G_HYBRID_MAP") maptype = G_HYBRID_MAP;
			//else if (maptypename == "G_PHYSICAL_MAP") maptype = G_PHYSICAL_MAP;
		}
		GEvent.addListener(this.map, "maptypechanged", function(){
			var maptype = self.map.getCurrentMapType();
			var maptypename = "G_NORMAL_MAP";
			if (maptype == G_SATELLITE_MAP) maptypename = "G_SATELLITE_MAP";
			else if (maptype == G_HYBRID_MAP) maptypename = "G_HYBRID_MAP";
			//else if (maptype == G_PHYSICAL_MAP) maptypename = "G_PHYSICAL_MAP";
			document.cookie = "gmaptype="+maptypename+"; path=/;";
		} );
		////////

		if (!this.locations || this.locations.length == 0) {
			this.map.setCenter(new GLatLng(_y, _x), _z, maptype);
			return;
		}

		//Show Default Location
		if (_postID != undefined && _postID == 0) {
			//範囲変更しない
		} else if (_postID || this.locations.length == 1) {
			_x = this.locations[0][1];
			_y = this.locations[0][2];
			_z = this.locations[0][3];
			_z = Math.min(maptype.getMaximumResolution(), _z);
		} else {
			var bounds = this.getLocationsBounds();
			zoom = this.map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(bounds[1], bounds[0]), new GLatLng(bounds[3], bounds[2])));
			if (zoom > _z) {
				_x = (bounds[0]+bounds[2])/2;
				_y = (bounds[1]+bounds[3])/2;
				_z = zoom;
			}
		}
		this.map.setCenter(new GLatLng(_y, _x), _z, maptype);
		this.loadExtent = this.map.getBounds();

		//Overlay Location Icons
		this.createLocationOverlay();

		if (_postID) this.createConnection(this.getLocationConnection(0), this.getLocationGeometries(0));

		// popup close
		GEvent.addListener(this.map, "infowindowclose", function(){
			self.clearTmpMarker();
			if (self.tmpIdx >= 0) self.map.panTo(self.getLocationPoint(self.tmpIdx));
		} );

	}
}

PostLocation.prototype.setMapMoveCallback = function(func)
{
	var self = this;
	this.mapMoveListener = GEvent.addListener(this.map, "move", function(){
		func(self.map.getCenter());
	} );
}
PostLocation.prototype.removeMapMoveCallback = function()
{
	GEvent.removeListener(this.mapMoveListener);
	this.mapMoveListener = null;
}


////////////////////////////////////////////////////////////////
//初期表示範囲を表示
PostLocation.prototype.showLoadExtent = function()
{
	this.map.setZoom(this.map.getBoundsZoomLevel(this.loadExtent));
}

PostLocation.prototype.getLocationsBounds = function()
{
	var minLng = this.locations[0][1];
	var minLat = this.locations[0][2];
	var maxLng = minLng;
	var maxLat = minLat;
	for (var i=0; i<this.locations.length; i++) {
		if (this.locations[i].length > 4) {
			minLng = Math.min(minLng, this.locations[i][1]);
			minLat = Math.min(minLat, this.locations[i][2]);
			maxLng = Math.max(maxLng, this.locations[i][1]);
			maxLat = Math.max(maxLat, this.locations[i][2]);
		}
	}
	//Extend 10%
	var w = maxLng-minLng;
	var h = maxLat-minLat;
	minLng -= w/10;
	minLat -= h/10;
	maxLng += w/10;
	maxLat += h/10;
	return [minLng, minLat, maxLng, maxLat];
}
PostLocation.prototype.showLocationsBounds = function()
{
	var bounds = this.getLocationsBounds();
	_x = (bounds[0]+bounds[2])/2;
	_y = (bounds[1]+bounds[3])/2;
	_z = this.map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(bounds[1], bounds[0]), new GLatLng(bounds[3], bounds[2])));
	//地点が一つのときは最大縮尺になってしまうので少し縮小
	if (bounds[0] == bounds[2]) _z = _z-2;
	this.map.setCenter(new GLatLng(_y, _x), _z);
}

PostLocation.prototype.setMaxBounds = function(minX, minY, maxX, maxY)
{
	this.maxBounds = new Array(minX, minY, maxX, maxY);
}

////////////////
// MapIcon Link
PostLocation.prototype.showMapLinkIcon = function(elem, icon_name)
{
	if (elem) {
		idx = this.locations.length-1;
		var link = '[map]';
		var icon  = this.getIcon(icon_name);
		if (icon) {
			var h = 14;
			var w = Math.ceil(icon.iconSize.width*h/icon.iconSize.height); 
			link = '<img src="'+icon.image.replace(/\.png$/, ".gif")+'" width="'+w+'" height="'+h+'" style="margin:0px;padding:0px;border:0px;"><img src="'+this.pluginPath+'link.png'+'" style="margin:0px;padding:0px;border:0px;">';
		}
		elem.innerHTML = '<a href="javascript:void(0);" onclick="postLocation.onClickMapLink(event, '+ idx +');"\
 onmouseover="postLocation.onMouseOverMapLink(event, '+ idx +');"\
 onmouseout ="postLocation.onMouseOutMapLink();">'+ link +'</a>';
		this.postLinks.push(elem);			
	}
}
PostLocation.prototype.onClickMapLink = function(e, idx)
{
	//地図の上のマージンを広げる
	this.setMapMarginTop(e, idx);
	this.showPostPopup(idx);
}
PostLocation.prototype.onMouseOverMapLink = function(e, idx)
{
	if (this.map.getInfoWindow().isHidden()) {
		//地図の上のマージンを広げる
		//this.setMapMarginTop(e, idx);
		this.closePopup();
		this.showPostCenter(idx, 0);
	}
}
PostLocation.prototype.onMouseOutMapLink = function()
{
	var self = this;
	window.onscroll = function(){ self.clearMapMarginTop(); };
	
	if (this.map.getInfoWindow().isHidden()) this.clearTmpMarker();
	//if (!this.postPopup) {
		//this.closePopup();
	//}
}


PostLocation.prototype.setMapMarginTop = function(e, idx)
{
	var pos = document.getElementById('postlocation_position');
	if (pos) {
		var xy = (document.all)?[window.event.clientX,window.event.clientY]:[e.clientX,e.clientY];	
		//地図のポジションを移動
		var link = document.getElementById('postlocation_'+idx);
		if (!link) return;

		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
		var mapTop = this.getOffsetTop(pos);
		var mapLeft = this.getOffsetLeft(pos);
		var mouseX = xy[0]+scrollLeft;
		//var mouseY = xy[1]+scrollTop;
		var w = this.map.getSize().width;
		var h = this.map.getSize().height;
		
		if (scrollTop > mapTop+20) {
			var top = scrollTop - mapTop;
			var left = 0;
			if (mouseX > mapLeft && mouseX < mapLeft+w && xy[1] < h+40) {
				left = -w-80;//(mouseX-mapLeft+20);
				pos.style.textAlign = 'right';
			} else {
				pos.style.textAlign = 'left';
			}
			pos.style.position = "relative";
			pos.style.top = top+"px";
			pos.style.left = left+"px";
			pos.style.border = "2px outset gray";
			pos.style.paddingBottom = "0px";
			//閉じるボタン
			var close = document.getElementById('postlocation_position_close');
			if (close) {
				close.style.display = "block";
			} else {
				close = document.createElement("label");
				close.id = 'postlocation_position_close';
				close.innerHTML = "閉じる";
				close.style.fontSize = '8pt';
				close.style.textDecoration = "underline";
				close.style.cursor = 'pointer';
				close.onclick = this.clearMapMarginTop;
				pos.appendChild(close);
			}
		}
	} else {
	
		var elem = document.getElementById('postlocation_margin_top');
		if (!elem) return;
		
		var top = document.documentElement.scrollTop || document.body.scrollTop;
		var h = top - this.getOffsetTop(elem);
		
		if (h > 0) elem.innerHTML = '<table><tr><td height='+h+'><br></td></tr></table>';
	}
}
PostLocation.prototype.clearMapMarginTop = function()
{
	var pos = document.getElementById('postlocation_position');
	if (pos) {
		pos.style.top = "";
		pos.style.left = "";
		pos.style.border = "2px solid white";
		//閉じるボタン
		var close = document.getElementById('postlocation_position_close');
		if (close) close.style.display = "none";
		pos.style.paddingBottom = "4px";
	}
	elem = document.getElementById('postlocation_margin_top');
	if (elem) {
		elem.innerHTML = '';
		window.onscroll = null;
	}
}

PostLocation.prototype.getOffsetTop = function(elem)
{
	var y = 0;
	while (elem) {
		y += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return y;
}
PostLocation.prototype.getOffsetLeft = function(elem)
{
	var x = 0;
	while (elem) {
		x += elem.offsetLeft;
		elem = elem.offsetParent;
	}
	return x;
}

