/**
 * 物件マップ
 */
 
var estatemap;
var addresslist = [];
var buildings = [];
var markers = [];
var infowindow = [];
var notfoundcount = 0;
var guyMarker = null;

$(function(){

	estatemap = new PBmap({container:'googlemap'});
	estatemap.show();
	var option = {};
	option.size = 'large';
	option.zoom = 11;
	estatemap.setControl(option);
	
	markers = [];
	
	estatemap.getLatLngByAddress($('#init_place').val(), function(latlng){
		estatemap.showPoint(latlng);
		
		var addresslist = [];
		var tmpbuildings = $('a.buildingsname');
		var tmpaddress = $('span.addresslist');
		var tmplat = $('span.lat');
		var tmplng = $('span.lng');
		var tmptoshihojin = $('span.toshihojin');
		var tmptype = $('span.estatetype');
		
		for (var i=0; i<tmpbuildings.length; i++) {
			addresslist[i] = {
				building:tmpbuildings[i].innerHTML,
				address:tmpaddress[i].innerHTML,
				lat:tmplat[i].innerHTML,
				lng:tmplng[i].innerHTML,
				toshihojin:tmptoshihojin[i].innerHTML,
				type:tmptype[i].innerHTML
			};
			markers[i] = estatemap.addMarker(new GLatLng(addresslist[i].lat, addresslist[i].lng));
			markers[i].number = i;
			
			// InfoWindow 内の html
			infowindow[i] = '<div class="infowindow">';
			infowindow[i] += '<h1>'+addresslist[i].building+'周辺</h1>';
			infowindow[i] += '<div class="type">'+addresslist[i].toshihojin+'／物件種別：'+addresslist[i].type+'</div>';
			infowindow[i] += '</div>';
			
			GEvent.addListener(markers[i], 'click', viewEstate);
			
			$(tmpbuildings[i]).data('number', i);
			$(tmpbuildings[i]).click(function(){
				viewEstate($(this).data('number'));
			});
			
		}
		
	});
	
});

var draglng;
var estateinfo = null;
function viewEstate(number) {
	if (typeof(number)!='number')
		number = this.number;
	estateinfo = infowindow[number];
	var tmpbuildings = $('a.buildingsname');
	$('#estatelist li').css('background', 'white');
	$(tmpbuildings[number]).parent().css('background', '#DAE5FF');
	var marker = markers[number];
	marker.openInfoWindowHtml(estateinfo);
	estatemap.setZoom(15);
	
	// 物件マーカーの InfoWindow が開いたとき
	GEvent.addListener(marker, "infowindowopen", function() {
		var client = new GStreetviewClient();
		client.getNearestPanorama(marker.getLatLng(), function(data){
			if (data.code != 200){
				return false;
			}
			var newpoint = data.location.latlng;
			
			if (guyMarker)
				estatemap.removeMarker(guyMarker);
			
			var guyIcon = new GIcon();
			guyIcon.image = "http://maps.google.co.jp/intl/ja_jp/mapfiles/cb/man-0.png";
			guyIcon.transparent = "http://maps.google.co.jp/intl/ja_jp/mapfiles/cb/man-pick.png";
			guyIcon.imageMap = [ 26, 13, 30, 14, 32, 28, 27, 28, 28, 36, 18, 35, 18, 27, 16, 26, 16, 20, 16, 14, 19, 13, 22, 8 ];
			guyIcon.iconSize = new GSize(49, 52);
			guyIcon.iconAnchor = new GPoint(25, 35); // near base of guy's feet
			guyIcon.infoWindowAnchor = new GPoint(25, 5); // top of guy's head
			guyIcon.shadow = null;
			
			var option = {icon:guyIcon, draggable:true};
			guyMarker = estatemap.addMarker(newpoint, {option:option});
			guyMarker.number = number;
			GEvent.addListener(guyMarker, 'click', viewEstate);
			
			lastMarkerLocation = newpoint;
			
			if ($('#panorama').length>0)
				$('#panorama').remove();
				
			estatemap.setZoom(18);
			guyMarker.openInfoWindowHtml(estateinfo+'<div id="panorama" style="text-align:center;width:450px;height:250px;"></div>');
			
			// guyIconのInfoWindowが開いたとき
			GEvent.addListener(guyMarker, "infowindowopen", function(){
				
				var panoramaObj = new GStreetviewPanorama($('#panorama')[0]);
				panoramaObj.setLocationAndPOV(lastMarkerLocation);
				
				// guyIcon がドラッグ開始されたとき
				GEvent.addListener(guyMarker, "drag", function(){
					if (!draglng || draglng < guyMarker.getLatLng().lng()) {
						guyMarker.setImage("http://www.google.co.jp/intl/ja_jp/mapfiles/cb/man_fly_right.png");
					} else if (draglng > guyMarker.getLatLng().lng()) {
						guyMarker.setImage("http://www.google.co.jp/intl/ja_jp/mapfiles/cb/man_fly_left.png");
					}
					draglng = guyMarker.getLatLng().lng();
				});
				
				// guyIcon がドロップされたとき
				GEvent.addListener(guyMarker, "dragend", function(){
					guyMarker.setImage(guyMarker.getIcon().image);
					client.getNearestPanorama(guyMarker.getLatLng(), function(response){
						if (response.code != 200) {
							guyMarker.setLatLng(lastMarkerLocation);
						} else {
							var latlng = new GLatLng(response.Location.lat, response.Location.lng);
							guyMarker.setLatLng(latlng);
							lastMarkerLocation = latlng;
							$('#panorama').remove();
							guyMarker.openInfoWindowHtml('<div id="panorama" style="text-align:center;width:450px;height:270px;"></div>');
							// （以降、guyIconのInfoWindowが開いたときのイベントの処理に移る）
						}
					});
				});
				
				// パノラマ内で移動されたとき、guyIcon もその場所へ移動
				GEvent.addListener(panoramaObj, "initialized", function(location){
					guyMarker.setLatLng(location.latlng);
				});
				
				// パノラマ内で方向が変わったとき、guyIcon の座布団も回転
				GEvent.addListener(panoramaObj, "yawchanged", function(newYaw){
					var GUY_NUM_ICONS = 16;
					var GUY_ANGULAR_RES = 360 / GUY_NUM_ICONS;
					if (newYaw < 0) {
						newYaw += 360;
					}
					guyImageNum = Math.round(newYaw / GUY_ANGULAR_RES) % GUY_NUM_ICONS;
					guyImageUrl = "http://maps.google.co.jp/intl/ja_jp/mapfiles/cb/man_arrow-"+guyImageNum+".png";
					guyMarker.setImage(guyImageUrl);
				});
			
			});
		});
	});
}
