GGSB.modules.shotList = function(ggsb) {
	
	function ShotList(args) {
		var _self = this;
		
		this.params = {};
		
		this.page = 0;
		this.page_num = 0;
		
		this.data = [];
		
		this.editable = false;
		
		
		this._dataObj = {};
		
		this._myWeaponObj = {};
		
		this._sortShotObj = {};
		
		this._setupShotObj = {};
		
		this.baseDOM = $('#PopShotIndex');
		
		if (this.baseDOM.length == 0) {
			this.baseDOM = $('#PopShotIndexS');
		}
		if (!this.baseDOM) {
			return;
		}
		
		
		// 並び替え
		this.baseDOM.find('.detail>.btnList>li:eq(1)>a').click(function () {
			_self.showSortShot({holeId:_self.params.holeId, scoreRecordId:_self.params.scoreRecordId});
			return false;
		} );
		
		// ショット追加
		this.baseDOM.find('.detail>.btnList>li:eq(0)>a').unbind('click').click(function () {
			_self.appendShot({holeId:_self.params.holeId, scoreRecordId:_self.params.scoreRecordId, courseId:_self.params.courseId});
			return false;
		} );
		
		// ページング
		this.baseDOM.find('.detail>.btnSelect>a:eq(0)').bind('click', function() { _self.showPrev(); return false; });
		this.baseDOM.find('.detail>.btnSelect>a:eq(1)').bind('click', function() { _self.showNext(); return false; });
		
		
		$(window).bind('updateShotList', function() { _self.showList(); });
		$(window).bind('updateTraceList', function() { _self.showList(); });
		
		$(window).bind('updateHoleId', function(e, obj) {
			_self.page = 0;
			_self.showList(obj);
		});
		
		$(window).bind('closeOtherDialog', function() {
			_self.baseDOM.find('.cursor').hide();
		});
	};
	
	var prot = {
		_myweapon : null,
		_club : null,
		
		setupShotList : function(args) {
			this._dataObj = args.data;
			
			this.editable = args.editable;
			this.params = $.extend(true, {
				openPanel : true
			}, args);
			
			
			if (this.editable) {
				
				if (this._myWeaponObj) {
					delete this._myWeaponObj;
				}
				
				this._myWeaponObj = new ggsb.MyWeapon({
					data : args.data
				});
				
				if (this._sortShotObj) {
					delete this._sortShotObj;
				}
				
				this._sortShotObj = new ggsb.SortShot({
					data : args.data
				});
				
				if (this._setupShotObj) {
					delete this._setupShotObj;
				}
				
				this._setupShotObj = new ggsb.SetupShot({
					data: args.data
				});
				
				
				_myweapon = this._dataObj.getMyWeapon();
				_club = this._dataObj.getClub();
			}
			
			this.baseDOM.find('.cursor').hide();
		},
		
		showList : function(args) {
			
			this.baseDOM.find('.detail>ul>li.weaponSelected').removeClass('weaponSelected');
			this.baseDOM.find('.detail>ul>li.shotSelected').removeClass('shotSelected');
			
			
			if (args) {
				this.params = $.extend({}, args);
			}
			
			
			this._data = this._dataObj.getShotTraceList(this.params);
			
			if (!this._data) {
				this.page_num = 0;
			} else {
				//this.page = 0;
				this.page_num = Math.ceil(this._data.length / this.PER_PAGE);
			}
			
			this.updateList();
		},
		
		
		
		updateList : function() {
			
			var i,
				shot,
				clubName,
				dom = this.baseDOM.find('.detail>.shotList'),
				self = this;
			
			
			dom.empty();
			
			
			// ページングボタン更新
			var btnDown = this.baseDOM.find('.detail>.btnSelect>a:eq(1)>img');
			var btnUp = this.baseDOM.find('.detail>.btnSelect>a:eq(0)>img');
			
			if (this.page == 0) {
				if (!btnUp.attr('src').match(/-off.gif$/)) {
					btnUp.attr('src', btnUp.attr('src').replace('.gif', '-off.gif'));
				}
			} else {
				btnUp.attr('src', btnUp.attr('src').replace(/-off.gif$/, '.gif'));
			}
			if (this.page_num <= 1 || this.page == (this.page_num - 1)) {
				if (!btnDown.attr('src').match(/-off.gif$/)) {
					btnDown.attr('src', btnDown.attr('src').replace('.gif', '-off.gif'));
				}
			} else {
				btnDown.attr('src', btnDown.attr('src').replace(/-off.gif$/, '.gif'));
			}
			
			if (!this._data) {
				return;
			}
			
			
			
			for (i = this.page * this.PER_PAGE; i < this.PER_PAGE * (this.page + 1); i++) {
				shot = this._data[i];
				
				if (!shot) {
					break;
					/*
					dom.append('<li></li>');
					continue;
					*/
				}
				
				clubName = shot.CLUB_NAME;
				if (clubName == null) {
					//continue;
				}
				
				
				dom.append(
					$('<li/>')
					.attr('id', 'jsShotLi_'+shot.SHOT_NO)
					.append(
						$('<span class="num">'+shot.SHOT_NO+'</span>'+
						'<span class="yard">'+(shot.CARRY_YARD ? '<strong class="ft13">'+shot.CARRY_YARD+'</strong>yd'  : '<strong class="ft13">0</strong>yd')+'</span>').click(
							(function(shot) {
								return function() {
									//if (self.params.openPanel) {
//										$(window).trigger('openShotInfo', shot.SHOT_NO);
										$(window).trigger('openShotInfo', {shotNo:shot.SHOT_NO, shotTraceId:shot.SHOT_TRACE_ID});
									//}
								}
							})(shot)
						//).css('cursor', this.params.openPanel ? 'pointer' : '')
						).css('cursor', 'pointer')
					)
					.append(
						this.editable ? 
							$('<a/>').attr(
								'href', 'javascript:void(0);'
							)
							.addClass(
								'kind'
							)
							.append(
								clubName ? 
								'<img src="/content/images/common/parts/icon/s/club-'+ClubItems[clubName].name+'.png" width="20" height="20" alt="'+clubName+'" />'+
								'<strong>'+clubName+'</strong>' : 
								'<img src="/content/images/common/parts/icon/s/question.gif" width="20" height="20" alt="" />'
							)
							.click((function(shot) {
								return function() {
									self.clearSelected(this);
									$(this).parent('li').addClass('weaponSelected');
									self.showCursor($(this).parent('li'));
									self.showMyWeapon({'holeId' : self.params.holeId, 'scoreRecordId' : self.params.scoreRecordId, 'shotNo' : shot.SHOT_NO, 'myWeaponId' : shot.MY_WEAPON_ID, 'shot' : shot});
								}
							})(shot))
						:
							$('<span/>').addClass(
								'kind'
							)
							.append(
								clubName ? 
								'<img src="/content/images/common/parts/icon/s/club-'+ClubItems[clubName].name+'.png" width="20" height="20" alt="'+clubName+'" />'+
								'<strong>'+clubName+'</strong>' : 
								''
							)
					)
					.append(
						this.editable ? 
							$('<a/>').attr(
								'href', 'javascript:void(0);'
							)
							.addClass(
								'edit'
							)
							.append(
								'<span>編集</span>'
							)
							.click((function(shot) {
								return function() {
									self.clearSelected(this);
									$(this).parent('li').addClass('shotSelected');
									self.showCursor($(this).parent('li'));
									self.showSetupShot({holeId:self.params.holeId, scoreRecordId:self.params.scoreRecordId, shot:shot, index:self.params.holeIndex});
								}
							})(shot))
						:
							''
					)
				);
			}
		},
		
		
		
		showPrev : function() {
			var btnImg = this.baseDOM.find('.detail>.btnSelect>a:eq(0)>img');
			
			$(window).trigger('closeOtherDialog');
			
			if (btnImg.attr('src').match(/-off.gif$/)) {
				return false;
			}
			
			this.page--;
			
			
			this.updateList();
			
			
		},
		
		
		showNext : function() {
			var btnImg = this.baseDOM.find('.detail>.btnSelect>a:eq(1)>img');
			
			$(window).trigger('closeOtherDialog');
			
			if (btnImg.attr('src').match(/-off.gif$/)) {
				return false;
			}
			
			this.page++;
			
			
			this.updateList();
			
			
		},
		
		
		
		showCursor : function(obj) {
			var index = obj.index(),
				height = obj.height();
			
			// イベント発生の関係で、実際の表示処理は各パネル表示箇所で
			// ここでは位置の計算のみ
			this.baseDOM.find('.cursor').css('top', (46 + (index * height)));
		},
		
		
		
		showMyWeapon : function(args) {
			$(window).trigger('closeOtherDialog');
			this._myWeaponObj.showDialog(args);
		},
		
		
		
		showSetupShot : function(args) {
			$(window).trigger('closeOtherDialog');
			this._setupShotObj.showDialog(args);
		},
		
		
		
		showSortShot : function(args) {
			$(window).trigger('closeOtherDialog');
			this._sortShotObj.showDialog(args);
		},
		
		
		
		appendShot : function(args) {
			this._dataObj.appendShot(args);
			
			$(window).trigger('updateTraceList');
		},
		
		
		
		clearSelected : function(obj) {
			var targets = $(obj).parent().siblings();
			$(obj).parent('li')
				.removeClass('shotSelected').removeClass('weaponSelected')
				.siblings().removeClass('shotSelected').removeClass('weaponSelected');
		},
		
		PER_PAGE : 8
	};
	
	ShotList.prototype = prot;
	
	ggsb.ShotList = $.extend(ShotList, {});
	
	
	ggsb.getClubName = function(myWeaponId) {
		var clubId,
			i, il,
			l, ll;
		
		il = _myweapon.length;
		ll = _club.length;
		for (i = 0; i < il; i++) {
			if (_myweapon[i].MY_WEAPON_ID == myWeaponId) {
				for (l = 0; l < ll; l++) {
					if (_club[l].CLUB == _myweapon[i].CLUB) {
						return _club[l].CLUB_NAME;
					}
				}
			}
		}
		return null;
	};

};

