GGSB.modules.shotTrace = function(ggsb) {
	
	
	var ShotTrace = function(args) {
		var _self = this;
		
		_self.draggingMarker = null;
		_self.isDragging = false;
		
		this.updateLocation = true;
		
		// アイコンレイヤー
		_self.userLayer = null;
		
		// 軌跡レイヤー
		_self.polylineLayer = null;
		
		// 飛距離レイヤー
		_self.yardLayer = null;
		
		
		_self.userMessageWindow = null;
		
		
		
		_self.params = $.extend(_self.params, args, {
			notUpdateShot : true
		});
		
		this._dataObj = {};
		
	};
	
	var prot = {
		
		setupMap : function(arg) {
			var self = this;
			
			this.params = $.extend(true, {
			}, arg);
			
			this._dataObj = arg.data;
			
			this.map = arg.map;
			this.userLayer = new ZdcUserLayer();
			this.polylineLayer = new ZdcShape.Layer();
			this.yardLayer = new ZdcShape.Layer();
			
			this.geoObj = new ZdcGeometric();
			
			
			
			this.isOpenUserMessageWindow = false;
			
			this.userMessageWindow = new ZdcUserMsgWindow();
			
			this.userMessageWindow.setImage(
				'/content/images/common/parts/map/blank.png', new ZdcSize(235, 167),		// base
				'/content/images/common/parts/map/blank.png', new ZdcSize(235, 167),		// text
				'/content/images/common/parts/map/blank.png', new ZdcSize(0, 0),			// close
				'/content/images/common/parts/map/blank.png', new ZdcSize(0, 0)			// shadow
			);
			
			this.userMessageWindow.setDocPosition(
				0, 0,							// base_top, base_left
				0, 0,							// text_top, text_left
				0,								// close_top
				0, 0							// change_top, change_left
			);
			
			
			this.map.setZdcMsgHtmlOption(false, false, 'left', 'small', 'tag');
			this.map.setZdcMsgHtmlBorderType(1);
			
			
			if (this.params.isSimulator == true) {
				ZdcEvent.addListener(this.map, "clickmapnomove", function() {
					var pos = ZdcCommon.TKY2WGS(this.MouseLon, this.MouseLat);
					
					self._dataObj.appendShot({
						'holeId' : self.params.holeId,
						'scoreRecordId' : self.params.scoreRecordId,
						'courseId' : self.params.holeIndex < 10 ? $('#frontCourseID').val() : $('#backCourseID').val(),
						'lat' : pos.lat,
						'lon' : pos.lon
					});
					
					self.updateLocation = false;
					
					self.showHole({}, false);
				});
			}
			
			/*
			userLayer.setLayerScale(10, 18);
			userLayer.setLayerType('auto');
			*/
			
			this.map.addShapeLayer(this.polylineLayer);
			this.map.addUserLayer(this.userLayer);
			this.map.addShapeLayer(this.yardLayer);
			
			
			
			
			$(window).bind('updateTraceList', function() { self.showHole(); });
			
			$(window).bind('openShotInfo', function(e, args) {
				if (self.params.compareMode) {
					return;
				}
				
				//var _marker = self.findMarker(shotNo);
				var _marker = self.findMarker(args.shotTraceId);
				if (!_marker) {
					return;
				}
				var _shot = self.findShotFromMarker(_marker);
				if (!_shot) {
					return;
				}
				
				self.openShotInfo(_shot, _marker);
				
			});
			
			$(window).bind('closeOtherDialog', function() {
				self.closeShotInfo()
			});
			
			
			
			// ホール移動周りはここ
			$(window).bind('changeHole', function(e, obj) {
				
				
				self.params.holeId = obj.holeId;
				self.params.holeIndex = obj.index;
				self.params.scoreRecordId = obj.scoreRecordId;
				
				self.params.courseId = obj.index < 10 ? $('#frontCourseID').val() : $('#backCourseID').val();
				
				
				self.updateLocation = true;
				
				self.showHole(obj);
				
			});
		},
		
		
		
		showHole : function(arg) {
			if (arg) {
				this.params = $.extend(true, {
					editable : false,
					drawLine : false,
					showPanel : false,
					showIcon : true,
					showYard: true,
					compareMode: false
				}, this.params, arg);
			}
			
			
			var	charIcon,
				flagIcon,
				flagMarker,
				data,
				i,
				self = this;
			
			
			// 地図表示位置変更
			data = this._dataObj.getCourseHole({
				courseId : this.params.courseId,
				holeId : this.params.holeId
			});
			
			if (!data) {
				return;
			}
			
			
			// 既存マーカー除去
			this.userLayer.clearMarker(false);
			
			this.yardLayer.clearShape();
			
			this.polylineLayer.clearShape();
			
			
			if (this.updateLocation == true) {
				
				this.map.setMapLocation(ggsb.getMapCenter(this.map, ZdcCommon.WGS2TKY(data.LON, data.LAT)));
			}
			
			
			
			// ホール位置へアイコン設置
			flagIcon = new ZdcIcon();
			flagIcon.image = '/content/images/common/parts/map/flag.png';
			flagIcon.size = new ZdcSize(28, 30);
			flagIcon.offset = new ZdcPixel(-8, -28);
			flagIcon.id = 999;
			
			data = this._dataObj.getCourseHoleHazard({
				courseId : this.params.courseId,
				holeId : this.params.holeId
			});
			l = data.length;
			
			for (i = 0; i < l; i++) {
				// カップ位置のLAT・LNGを拾ってアイコン設置
				flagMarker = new ZdcMarker(ZdcCommon.WGS2TKY(data[i].LON, data[i].LAT), flagIcon);
				
				this.userLayer.addMarker(flagMarker);
			}
			
				
			// キャラクターアイコン
			charIcon = new ZdcIcon();
			charIcon.image = '/content/images/common/parts/map/blank.png';
			if (!this.params.compareMode) {
				charIcon.size = new ZdcSize(40, 32);
				charIcon.offset = new ZdcPixel(-20, -28);
			} else {
				charIcon.size = new ZdcSize(20, 17);
				charIcon.offset = new ZdcPixel(-8, -20);
			}
			
			
			// ショット位置取得
			data = this._dataObj.getShotTraceList({
				scoreRecordId:this.params.scoreRecordId
			});
			
			if (data) {
				// 各ショット位置へ配置
				for (i = 0; i < data.length; i++) {
					var shot = data[i],
						shot_prev,
						marker,
						pixel_point_prev,
						pixel_point;
					
					if (this.params.showIcon) {
						if (this.params.compareMode) {
							charIcon.image = '/content/images/common/parts/map/comp_shot'+shot.SHOT_NO+'.png';
							/*
							if (shot.COMMENT.length > 0) {
								charIcon.image = '/content/images/common/parts/map/comp_shot'+shot.SHOT_NO+'-comment.png';
							}
							*/
						} else {
							charIcon.image = '/content/images/common/parts/map/shot'+shot.SHOT_NO+'.png';
							if (shot.COMMENT.length > 0) {
								charIcon.image = '/content/images/common/parts/map/shot'+shot.SHOT_NO+'-comment.png';
							}
						}
					} else {
						charIcon.image = '/content/images/common/parts/map/blank.png';
					}
					marker = new ZdcMarker(ZdcCommon.WGS2TKY(shot.LON, shot.LAT), charIcon);
					
					//marker.id = shot.SHOT_TRACE_ID;
					marker.id = shot.SHOT_NO;
					marker.shot_trace_id = shot.SHOT_TRACE_ID;
					
					
					if (this.params.showPanel) {
						(function(marker, shot) {
							ZdcEvent.addListener(marker, 'mousedblclickmarker', function() {
								self.openShotInfo(shot, this);
								// 選択中のショットをハイライト
								$('#PopShotIndex ul.shotList>li').removeClass('shotSelected');
								$('#jsShotLi_'+marker.id).addClass('shotSelected');
							});
						})(marker, shot);
					}
					
					// 編集モード
					if (this.params.editable) {
						(function(marker, shot) {
							ZdcEvent.addListener(marker, "mousedownmarker", function() {
								self.isDragging = true;
								self.draggingMarker = this;
							});
							
							ZdcEvent.addListener(marker, "mouseupmarker", function() {
								self.isDragging = false;
								
								var point = this.Point;
								
								if (!self.params.notUpdateShot) {
									self.updateShotTrace(marker, ZdcCommon.TKY2WGS(point.lon, point.lat));
								} else {
									marker.setPoint(point);
								}
								
								
								self.yardLayer.clearShape();
								self.polylineLayer.clearShape();
								
								self.updatePolyline();
								self.updateYard({id:marker.id});
								
								
								$(window).trigger('updateShotList');
								
							});
							
							/*
							// 個別ショットパネル
							ZdcEvent.addListener(marker, 'mouseclickmarker', function() {
								self.map.openZdcMsgHtml('<p>testtesttesttesttesttest</p>', this.Point, "", 220, null, new ZdcPixel(5, -25));
							});
							*/
						})(marker, shot);
						
						ZdcEvent.addListener(this.map, "mousemove", function() {
							if (self.isDragging) {
								var newPoint = new ZdcPoint(this.MouseLon, this.MouseLat);
								
								self.draggingMarker.setPoint(newPoint);
								self.draggingMarker.redraw();
							}
						});
					}
					
					
					
					this.userLayer.addMarker(marker);
					
					
					// 2打目以降の場合、飛距離のヤード数を表示する
					if (i > 0) {
						shot_prev = data[i - 1];
						pixel_point_prev = this.map.convertPoint2Pixel(ZdcCommon.WGS2TKY(shot_prev.LON, shot_prev.LAT), 2);
						pixel_point = this.map.convertPoint2Pixel(ZdcCommon.WGS2TKY(shot.LON, shot.LAT), 2);
					}
				}
				
				// ポリライン引く
				if (this.params.drawLine) {
					this.updatePolyline();
				}
				
				
				// 飛距離更新
				if (this.params.showYard) {
					this.updateYard();
				}
				
				this.closeShotInfo();
				
			}
			
			ZdcEvent.addListener(this.map, "clickmap", function() {
				self.closeShotInfo();
			});
			
			
			
			$(window).trigger('updateHoleId', {
				holeId : this.params.holeId,
				holeIndex: this.params.holeIndex,
				scoreRecordId : this.params.scoreRecordId,
				courseId : this.params.courseId,
				courseHoleId: this.params.courseHoleId
			});
		},
		
		
		
		startDragging : function(i) {
			
		},
		
		
		
		updateShotTrace : function(marker, point) {
			var data = this._dataObj.getShotTraceList({
					holeId:this.params.holeId,
					scoreRecordId:this.params.scoreRecordId
				}),
				l = data.length,
				i;
			
			for (i = 0; i < l; i++) {
				if (data[i]['SHOT_TRACE_ID'] == marker.shot_trace_id) {
				//if (data[i].SHOT_NO == marker.id) {
					data[i].LAT = point.lat;
					data[i].LON = point.lon;
					
				}
			}
			
		},
		
		
		
		openShotInfo : function(shot, marker) {
			
			this.closeShotInfo();
			
			var self = this,
				myweapon = shot.MY_WEAPON_ID ? this._dataObj.getMyWeapon({myWeaponId:shot.MY_WEAPON_ID}) : null,
				node = 
				'<div class="shotInfo">'+
					'<div class="popBox" style="top:0 !important;left:0 !important;">'+
						'<div class="containerTop">'+
							'<div class="detail">'+
								'<div class="title">'+
									'<div class="flL">'+
										'<a href="javascript:void(0);">'+
											'<img src="/content/images/common/parts/map/panel/btn/left.gif" width="20" height="20" alt="前のショットへ" />'+
										'</a>'+
									'</div>'+
									'<div class="numYard">'+
										'<div class="num">'+shot.SHOT_NO+'</div>'+
										'<div class="yard"><strong>'+(shot.CARRY_YARD ? shot.CARRY_YARD : '0')+'</strong>yd</div>'+
									'</div>'+
									'<div class="flR">'+
										'<a href="javascript:void(0);">'+
											'<img src="/content/images/common/parts/map/panel/btn/right.gif" width="20" height="20" alt="前のショットへ" />'+
										'</a>'+
									'</div>'+
								'</div>'+
								'<div class="kind">'+
									(myweapon ?
									'<div class="title"><img src="/content/images/common/parts/icon/s/'+(ClubItems[shot.CLUB_NAME] ? 'club-'+ClubItems[shot.CLUB_NAME].name+'.png' : 'question.gif')+'" width="20" height="20" alt="'+myweapon.CLUB_NAME+'" /><strong>'+(myweapon ? myweapon.CLUB_NAME+' / '+myweapon.MAKER_NAME : '未設定')+'</strong></div>'+
									(myweapon ? '<p>'+myweapon.PRODUCT_NAME+'</p>' : '<p>&nbsp;</p>')
									:
									'<div class="title"><strong>&nbsp;</strong></div><p>&nbsp;</p>'
									)+
								'</div>'+
								'<div class="comment">'+
									'<div class="flL mgR10"><img src="/content/images/common/parts/icon/s/comment.png" width="16" height="16" alt="コメント" /></div>'+
									'<p>'+(shot.COMMENT ? shot.COMMENT : '&nbsp;')+'</p>'+
								'</div>'+
							'</div>'+
						'</div>'+
						'<div class="containerBottom"><img src="/content/images/common/parts/map/panel/bg/shotinfo-bottom.png" width="235" height="4" alt="" /></div>'+
					'</div>'+
				'</div>'
			,
			point = $.extend(true, {}, marker.Point),
			pixel = this.map.convertPoint2Pixel(marker.Point, 1),
			tmp;
			
			pixel.x -= 235 / 2;
			
			tmp = this.map.convertPixel2Point(pixel, 1);
			
			tmp.mx = ZdcCommon.DEG2MS(tmp.lon);
			tmp.my = ZdcCommon.DEG2MS(tmp.lat);
			
			point.lat = tmp.lat;
			point.lon = tmp.lon;
			point.mx = tmp.mx;
			point.my = tmp.my;
			
			
			
			this.map.openUserMsgWindow(this.userMessageWindow, point, $(node).get(0), new ZdcPixel((-235 / 2) - 20, -167/2), 2);
			
			
			
			$('div.popBox>.containerTop>.detail>.title>.flL').find('a').click(function() {
				var _marker = self.findMarker(marker.shot_trace_id, -1);
				if (!_marker) {
					return;
				}
				var _shot = self.findShotFromMarker(_marker);
				if (!_shot) {
					return;
				}
				
				self.openShotInfo(_shot, _marker);
			});
			$('div.popBox>.containerTop>.detail>.title>.flR').find('a').click(function() {
				var _marker = self.findMarker(marker.shot_trace_id, 1);
				if (!_marker) {
					return;
				}
				var _shot = self.findShotFromMarker(_marker);
				if (!_shot) {
					return;
				}
				
				self.openShotInfo(_shot, _marker);
			});
		},
		
		
		
		closeShotInfo : function() {
			if (this.map.usermsgopenstatus && this.userMessageWindow.openstatus == true) {
				this.map.closeUserMsgWindow(this.userMessageWindow);
			}
		},
		
		
		
		findShotFromMarker : function(m) {
			var data = this._dataObj.getShotTraceList({
				holeId:this.params.holeId,
				scoreRecordId:this.params.scoreRecordId
			}),
				i,
				l = data.length;
			
			for (i = 0; i < l; i++) {
				if (data[i].SHOT_NO == m.id) {
					return data[i];
				}
			}
			return null;
		},
		
		
		
		findMarker : function(shotTraceId, dir) {
			var markers = this.userLayer.markers,
				i,
				l = markers.length;
			
			for (i = 0; i < l; i++) {
				if (shotTraceId == markers[i].shot_trace_id) {
					if (!dir || dir == 0) {
						return markers[i];
					} else if (dir > 0) {
						return i + 1 < l ? markers[i+1] : null;
					} else {
						return i != 0 ? markers[i-1] : null;
					}
				}
			}
			return null;
		},
		
		
		
		updatePolyline : function() {
			
			var markers = this.userLayer.markers,
				i, 
				l = markers.length,
				point_prev,
				point,
				polyline = new ZdcShape.Polyline(),
				icon,
				user = this._dataObj.getUser();
			
			for (i = 0; i < l; i++) {
				if (!markers[i]) {
					break;
				}
				icon = markers[i].icon;
				if (icon.id == 999) {
					continue;
				}
				
				polyline.addPoint(markers[i].Point);
			}
			
			if (this.params.compareMode) {
				polyline.strokeColor = "#000000";
			} else
			if (user && user.COLOR_SHOT_LINE) {
				polyline.strokeColor = this.COLOR_SHOT_LINE[user.COLOR_SHOT_LINE - 1];
			} else {
				polyline.strokeColor = this.COLOR_SHOT_LINE[0];
			}
			polyline.strokeWeight = '5px';
			
			this.polylineLayer.addShape(polyline);
		},
		
		
		
		// 飛距離表示更新
		updateYard : function(args) {
			var markers = this.userLayer.markers,
				i,
				l = markers.length,
				dist,
				shapeText;
			
			for (i = 1; i < l; i++) {
				if (!markers[i]) {
					break;
				}
				
				if (markers[i-1].icon.id == 999 || markers[i].icon.id == 999) {
					continue;
				}
				
				// ショットNOが連続しない場合は飛距離を計算しない
				// ただし1の時は計算する
				if ((markers[i-1].id != markers[i].id && markers[i-1].id + 1 != markers[i].id) || i == l/* && markers[i - 1].id != 1*/) {
					dist = 0;
				} else {
					dist = this.meter2yard(this.geoObj.getPoint2PointDistance(markers[i-1].Point, markers[i].Point));
				}
				
				// データ更新
				this._dataObj.updateShotTraceYard(this.params, markers[i-1].shot_trace_id, dist);
				
				if (dist < 60) {
					continue;
				}
				
				shapeText = new ZdcShape.Text();
				
				//shapeText.textString = '<p style="height:1.75em;line-height:1.5em;vertical-align:middle;">'+dist+'yd</p>';
				shapeText.textString = '<div class="distanceNum"><strong>'+dist+'</strong>yd</div>';
				shapeText.setPoint(this.getPoint2PointMidPoint(markers[i-1].Point, markers[i].Point));
				
				shapeText.bgColor = "#000000";
				shapeText.textColor = "#FFFFFF";
				shapeText.textWidth = 80;
				shapeText.offsetx = -50;
				shapeText.offsety = -10;
				shapeText.opacity = 0.8;
				
				this.yardLayer.addShape(shapeText);
			}
		},
		
		
		
		// メートルをヤードに変換
		meter2yard : function(n) {
			return parseInt(n * 1.0936);
		},
		
		
		
		// 2点間に中点を返す
		getPoint2PointMidPoint : function(p1, p2) {
			return  new ZdcPoint( (p1.lon + p2.lon) / 2, (p1.lat + p2.lat) / 2) ;
		},
		
		
		COLOR_SHOT_LINE : [
							"#666666",
							"#CCCCCC",
							"#FF0000",
							"#990000",
							"#0099FF",
							"#0000CC",
							"#FF66FF",
							"#CC66FF",
							"#00CC33",
							"#006600",
							"#FFFF00",
							"#FF6600"
		]
	};
	
	ShotTrace.prototype = prot;
	
	ggsb.ShotTrace = $.extend(ShotTrace, {});
	
};

