// JavaScript Document


(function($){


//トップページランダム画像
	$('#topNakama').cycle({
		fx: 'fade',
		timeout: 3000
	});



//トップページストーリースライド
	$(function(){
		var 
		
		$stories = $('.storyBox'),
		page_width = 151,
		move_flag = true,
		
		// 動作を割り当てるメソッドを格納するオブジェクト
		bind = {
			button: {
				// next prev 共通の動作を定義
				_move: function(self, mode) {
					if (move_flag) {
						move_flag = false;
						var
						// 動かすべきページを取得
						_$storyPageList = $('.storyList'),
						_$storyPages = _$storyPageList.children('li'),
						// 現在の位置を取得
						_left_value = _$storyPageList.css('left'),
						// 限界値を定義
						_max = mode === 'next' ? page_width * (_$storyPages.size() -3) : 0,
						// 動かす方向を定義
						_operator = mode === 'next' ? '-=' : '+=',
						// アニメーションの動作
						_anime = function() {
							_$storyPageList.animate({left: _operator + 151 + 'px'}, 'normal', 'linear', function() { move_flag = true; });
						};
						
						// 現在位置が auto 以外の場合に絶対値を取得
						if (_left_value != 'auto') {
							_left_value = _left_value.replace('px', '');
							_current_left_value_abs = Math.abs(parseInt(_left_value));
						}
						
						// 各条件をクリアしたら動かす
						if (mode === 'next' && (_left_value == 'auto' || _current_left_value_abs < _max)) {
							_anime();
						} else if (mode === 'prev' && (_left_value != 'auto' && _current_left_value_abs > _max)) {
							_anime();
						} else {
							move_flag = true;
						}
					}
				},
				
				next: function(){
					// 動かす処理を取り込む
					var _move = this._move;
					$stories.each(function() {
						$(this).delegate('p.next', 'click', function(e) { e.preventDefault();
							_move($(this), $(this).attr('class'));
						});
					});
				},
				
				prev: function(){
					// 動かす処理を取り込む
					var _move = this._move;
					$stories.each(function() {
						$(this).delegate('p.prev', 'click', function(e) { e.preventDefault();
							_move($(this), $(this).attr('class'));
						});
					});
				}
			}
		},
		
		// 値を取得して格納するオブジェクト
		set = {
			// 号によって横幅を変える
			storyPageWidth: function() {
				$stories.each(function() {
					var _ul = $('.storyList');
					var _li_num = _ul.children().size();
					_ul.css({width: _li_num * page_width});
				});
			}
		};
		
		(function(){
			set.storyPageWidth();
			bind.button.next();
			bind.button.prev();
		})();
	});



//トップページニュースポップアップ
	$(function(){
		var 

		$stort_link = $('ul.newsList'),
		$wrapper = $('#wrapper'),
		$cover = $('#cover'),
		$on_cover = $('#onCover'),
		$stories = $on_cover.children('div.newsPop'),
		page_width = 675,
		move_flag = true,
		
		// 動作を割り当てるメソッドを格納するオブジェクト
		bind = {
			// ボタン類のイベント
			button: {
				// 絵本の起動
				starting: function(){
					$stort_link.children().children('span.txt').click(function(e) { e.preventDefault();
						// 絵本の号を取得
						var _story_number = $(this).parent().attr('class');
						// 黒い背景を表示
						set.coverHeight();

						$cover.fadeIn('fast', function() {
							if(!$.support.opacity) this.style.removeAttribute('filter');
							
							$on_cover.fadeIn('fast', function(){
								$stories.show();
								// 起動時のスライドの位置をセット
								$('.newsPage li').each(function(){
									var _liIndex = $(this).index();
									if ($(this).attr('id') === _story_number) 
										$(this).parent().css({left: - page_width * _liIndex + 'px'},function() { move_flag = true; });
								});
							});
						});
						// 閉じるボタンのイベントを適用
						bind.button.close();
					});
				},
				
				close: function() {
					// 共通で使える閉じる動作
					var _coversFadeOut = function() {
						// 絵本を非表示に
						$stories.each(function() { 
							if ($(this).css('display') === 'block') $(this).hide();
						});
						// カバーを非表示に
						$on_cover.fadeOut('fast', function() {
							$cover.fadeOut('fast', function(){});
						});
						// クリックイベントを解除
						$stories.find('p.close').unbind('click');
					};
					// 各クリック可能パーツに適用
					$stories.find('p.close').click(function(e) { e.preventDefault();
						_coversFadeOut();
					});
				}
			}
		},
		
		// 値を取得して格納するオブジェクト
		set = {
			// 黒い半透明のカバーの高さをセットする
			coverHeight: function() {
				var _height = $wrapper.height();
				$cover.css({height: _height});
			}
		};
		
		(function(){
			bind.button.starting();
		})();
	});



})(jQuery);



