/* ================================================================================
* Title: Jquery Function
*
* Copyright (c) 2009 Astra The Studio Inc.
* http://www.studio.co.jp/
* Date: Feb.13, 2009
* @author: Chihiro Mori
* @version: 1.0.5
*
* jQuery 1.6.1
*
* 確認済ブラウザ: [Mac] FireFox3.0, Safari3.1.2 [Win] IE6.0, 7.0
* Macで作業するときの注意：バックスラッシュ\を入力するときはoptionを押しながらエンマーク
================================================================================ */

(function($)
{
//---------------------------------------------------------------------
	$(function()
	{
		$.ASconf.extend();
		$.ASconf.selfLink();
		$.ASconf.selfScroll();
		$.ASconf.scrollCont();
		$.ASconf.guidePage();
		$.ASconf.randMagBg();
		$.ASconf.mNavBtn1();
		$.ASconf.mNavBtn2();
		$.ASconf.mNavBtn3();
		$.ASconf.btnRollover();
		$.ASconf.categoryScript();
		$.ASconf.pngFix();
		$.ASconf.fixLayoutEventArchive();
	});
//---------------------------------------------------------------------
	$.ASconf =
	{
		extend:function(){   
			$.fn.extend({
                    //.log()をつけてチェーン途中でもログ出力
                    log: function(str) {
                         str = str || "";
                           console.log(str,$(this));
                           return this;
                    }
			})

		},
		//-----------------------------------------------------------------
		selfLink: function ()
		{//自ページリンクにクラス付与。子要素にイメージが含まれていたら半透明化
			
			var selfLinkClass = 'current';//Aタグに付与
			var btnClass = 'btn';//IMGタグに付与
			var theUrl = location.href.replace(location.hash, '').replace(/(\/|\#)$/, '/index.html');
			
			//他のファンクションから「shr」フォルダのパスを参照できる「$.ASconf.shrUrl」を生成
			$.ASconf.shrUrl = document.getElementById('baseCSS').href.replace('css/base.css', '');
			
			$('a[href]').each(function()
			{
				var target = $(this);
				var childImg = target.find('img');
				var i = document.createElement('span');
				i.innerHTML = '<a href="' + target.attr('href') + '" />';
				var absolutePath = i.firstChild.href;				
				if (absolutePath == theUrl)
				{//パスを比較して同じだったら
					target.addClass(selfLinkClass).removeAttr('href');
					childImg.fadeTo(0, 0.7);
					// opacityに非対応なブラウザに適用
					if(!$.support.opacity) childImg.addClass(btnClass).fixPng();
				}
			});
		}
		,
		//-----------------------------------------------------------------
		selfScroll: function()
		{//^は初めの文字が#だった場合。*にするとhoge.html#hogeもマッチ
			$('a[href^=#]').click(function(e)
			{
				var hash = $(this).hash;
				if(!hash || hash == "#")
				{//#のみの場合はデフォルト動作を止める。
					e.preventDefault();
					//e.stopPropagation();
				}
				else
				{//safariとchromeを判定
					$(!$.support.checkOn ? 'body' : 'html')
					.animate({scrollTop: hash.offset().top}, 300, 'swing');
					return false;
				}
			});
		}
		,
		//-----------------------------------------------------------------
		scrollCont: function()
		{//カスタムスクロールバー
		
			//newsページの画像の横並び調整　:nth-child（数式）を使って3要素ごとにclass付与
			//先に調整しないとダメ
			$('div#imgWrap').find('li:nth-child(3n)').addClass('last');
		
			var obj = $('body:not(.event.archive) div.scrollCont');
			if(obj.get(0))
			{
				obj.jScrollPane({hijackInternalLinks: true, animateScroll :true});
			}
			
		}
		,
		//-----------------------------------------------------------------
		guidePage: function()
		{//ガイドページのスクリプト
			var obj = $('ul#bgImages');
			if(obj.get(0))
			{
				var contensWrap = $('body.guide').find('div#container');//もろもろのコンテンツを囲む要素
				var maxNum = 8;//画像、テキストコンテンツなどの数。1からカウント
				var currentCont = 1;//現在選択されているコンテンツのフラグ
				
				//-----------------------------------------------------------------
				//トリガーの表示設定
				var menuWrap = contensWrap.find('div#sNavWrap');
				//divのコンテンツの無い領域にマウスが反応しないよう、要素別にイベントを設定
				var menuTrigger = menuWrap.find('p#sNavBt, div#sNavInner');
				var menuHeight = menuWrap.height();
				var menuMinHeight = menuTrigger.eq(0).height();//ボタン分の高さ
				//あらかじめメニューを閉じる
				menuWrap.height(menuMinHeight);
				var to;
				
				//メニューの開閉ファンクション
				menuTrigger.hover(function()
				{//メニューを開く
					menuWrap.not(':animated').animate({height: menuHeight}, 400, 'easeOutCubic' );
					clearTimeout( to );
				}
				,function()
				{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
					to = setTimeout( function (){ menuWrap.animate({height: menuMinHeight}, 300, 'easeInCubic' ); }, 600);
				});
				
				//-----------------------------------------------------------------
				//テキストコンテンツの切り替え
				var txtCont = contensWrap.find('div.guideCont');
				var txtClass = 'off';//テキストコンテンツに付与するクラス名
				txtCont.not(':first').addClass(txtClass).end().css('left','20px');//予め非表示（jScrollPaneより後に非表示にする）
				//トリガーとなる画像サムネール
				var trigger = menuTrigger.eq(1).find('li');
				var triName = 'sNav';//トリガーの数字を除いたクラス名
				var triClass = 'over';//トリガーに付与するクラス名		
				
				//ファンクション設定
				function changeCont(tmp)
				{//click：画像切り替えファンクション
					var num = tmp.attr('id').replace(triName,'');//どのサムネールをクリックしたかの判定
					if(num!=currentCont)
					{//何度も押せないようにする
						currentCont = num;//現在のコンテンツ番号を代入
						firstBgTarget.attr('src', firstBgTarget.attr('src').replace(/[0-9]?(\.gif|\.jpg|\.png)$/, num+'$1'));
						
						//テキストコンテンツの表示設定
						txtCont.addClass(txtClass);//一旦すべて非表示
						txtCont.eq(num-1).removeClass(txtClass).fadeIn();
						
						//トリガーのhoverの調整
						trigger.not(tmp).removeClass(triClass).fadeTo(0, 1);
						
						//次へ戻るボタンの調整
						npNextCtrl(npNext);
						npPrevCtrl(npPrev);
					}
				}
				
				function thumHover(tmp)
				{//hover：サムネールの表示設定
					var num = tmp.attr('id').replace(triName,'');//どのサムネールをクリックしたかの判定
					if(num!=currentCont)
					{//何度もhoverしないように
						tmp.not(':animated').addClass(triClass).fadeTo(0, 0.6);
					}
				}

				function thumOut(tmp)
				{//Out：サムネールの表示設定
					var num = tmp.attr('id').replace(triName,'');//どのサムネールをクリックしたかの判定
					if(num!=currentCont)
					{//何度もOutしないように
						tmp.removeClass(triClass).fadeTo(0, 1);//カレントコンテンツは除外
					}
				}
				
				//イベント設定
				trigger.click(function(){ changeCont($(this)); })
				.hover(function()       { thumHover($(this)); }
				,function()             { thumOut($(this)); });

				//一番始めの画像サムネールを予め半透明にする
				trigger.eq(0).fadeTo(0, 0.6).addClass(triClass);

				//-----------------------------------------------------------------
				//次へ戻るボタン
				
				contensWrap.append('<p id="npNext"></p><p id="npPrev"></p>');
				var npNext = contensWrap.find('p#npNext');
				var npPrev = contensWrap.find('p#npPrev');
				
				npPrev.fadeTo(0, 0);//予めフェードアウトしておく。

				//-----------------------------------------------------------------
				//ファンクション設定
				function npNextCtrl(tmp)
				{//次のコンテンツが無い場合はフェードアウトしたまま
					if(currentCont>=maxNum)
						tmp.css('cursor','default').fadeTo('fast', 0);
					else
						tmp.fadeTo('fast', 1);
				}
				
				function npPrevCtrl(tmp)
				{//前のコンテンツが無い場合はフェードアウトしたまま
					if(currentCont<=1)
						tmp.css('cursor','default').fadeTo('fast', 0);
					else
						tmp.fadeTo('fast', 1);
				}
				
				//イベント設定
				npNext.click(function()
				{//次へボタン
					if(currentCont<maxNum)
					{//次のコンテンツ
						var target = trigger.eq(currentCont);
						thumHover(target);//サムネールの調整
						changeCont(target);//コンテンツ切り替え
					}
				})
				.hover(function()
				{//ホバー設定
					if(currentCont<maxNum)
						$(this).not(':animated').css('cursor','pointer').fadeTo('fast', 0.6);
					else
						$(this).css('cursor','default');
				}
				,function()
				{
					if(currentCont<maxNum)
						$(this).fadeTo('fast', 1);
				});

				npPrev.click(function()
				{//前へボタン
					if(currentCont>1)
					{//一つ前のコンテンツ
						var target = trigger.eq(currentCont-2);
						thumHover(target);//サムネールの調整
						changeCont(target);//コンテンツ切り替え
					}
				})
				.hover(function()
				{
					if(currentCont>1)
						$(this).not(':animated').css('cursor','pointer').fadeTo('fast', 0.6);
					else
						$(this).css('cursor','default');
				}
				,function()
				{
					if(currentCont>1)
					{//前のコンテンツが無い場合はフェードアウトしたまま
						$(this).fadeTo('fast', 1);
					}
				});

				//-----------------------------------------------------------------
				//デフォルトで表示する背景画像の設定
				var firstBgTarget =  obj.find('img#bgmaximage');
				
				firstBgTarget.maxImage(
				{//全画面化
					isBackground: true,
					overflow: 'auto',
					verticalAlign:'top'
				})
				.load(function()
				{
					firstBgTarget.not(':animated').hide().fadeIn('slow');
				});
				
				//-----------------------------------------------------------------
				//非同期で画像を読み込み
				for(var i=2;i<=maxNum;i++)
				{
					var src = firstBgTarget.attr('src').replace(/[0-9]?(\.gif|\.jpg|\.png)$/, i+'$1');
					var img = $(new Image());
					img.load(function()
					{//ロードが完了したら
						obj.append(this);
						//console.log(parent.children(':eq('+ maxNum +')'));
						if(obj.children()[maxNum-1])
						{//すべてロード完了したらローディング画像表示を解除
							$('body').css('backgroundImage','none');
						}
					})
					//.error(function() { /*alert('error');*/ })
					.attr('src', src);
				}
			}//IF文終了
		}
		,
		//-----------------------------------------------------------------
		randMagBg: function()
		{//ランダム背景画像
			var obj = $('p#randomBgImages');
			if(obj.get(0))
			{
				//背景読み込みと連動してナビゲーションを表示させる
//				var nav = $('div#nav');
//				var navHeight = nav.height();
//				nav.height(0);//予め非表示
				var maxNum = 4;//背景画像の数。1からカウント 例：hoge1.jpg
		
				obj.find('img').each(function()
				{
					var target = $(this);
					var num = Math.floor(Math.random()*maxNum+1);
					//ファイル書き換え
					target.attr('src', target.attr('src').replace(/[0-9]?(\.gif|\.jpg|\.png)$/, num+'$1'));
					
					//ローディング設定
					target.maxImage(
					{
						isBackground: true,
						overflow: 'auto',
						verticalAlign:'top'
					})
//					.load(function()
//					{
//						target.hide().fadeIn('slow', function()
//						{//ディレイ効果
//							nav.animate({height: navHeight}, 500, 'easeOutBack' );
//						});
//					});
					
				});//each終了
			}//IF文終了
		}
		,
		//-----------------------------------------------------------------
		mNavBtn1: function()
		{//カスタムスクロールバー

			var trigger = $('dd#cNav1-1').find('a');
			var target = $('ul#mNav1');
			//var targetFlag = false;
			var menuHeight = target.height();
			//あらかじめメニューを閉じる
			target.height(0);
			var to;

			//メニューの開閉ファンクション
			trigger.hover(function()
			{//メニューを開く
				target.not(':animated').animate({height: menuHeight}, 400, 'easeOutCubic' );
				clearTimeout( to );
			}
			,function()
			{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
					to = setTimeout( function (){ target.animate({height: 0}, 300, 'easeInCubic' ); }, 600);
			});
			
			target.hover(function()
			{//メニューを開く
				clearTimeout( to );
			}
			,function()
			{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
				to = setTimeout( function (){ target.animate({height: 0}, 300, 'easeInCubic' ); }, 600);
			});

		}
		,
		mNavBtn2: function()
		{//カスタムスクロールバー

			var trigger = $('dd#cNav1-2').find('a');
			var target = $('ul#mNav2');
			//var targetFlag = false;
			var menuHeight = target.height();
			//あらかじめメニューを閉じる
			target.height(0);
			var to;

			//メニューの開閉ファンクション
			trigger.hover(function()
			{//メニューを開く
				target.not(':animated').animate({height: menuHeight}, 400, 'easeOutCubic' );
				clearTimeout( to );
			}
			,function()
			{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
					to = setTimeout( function (){ target.animate({height: 0}, 300, 'easeInCubic' ); }, 600);
			});
			
			target.hover(function()
			{//メニューを開く
				clearTimeout( to );
			}
			,function()
			{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
				to = setTimeout( function (){ target.animate({height: 0}, 300, 'easeInCubic' ); }, 600);
			});

		}
		,
		mNavBtn3: function()
		{//カスタムスクロールバー

			var trigger = $('dd#cNav1-3').find('a');
			var target = $('ul#mNav3');
			//var targetFlag = false;
			var menuHeight = target.height();
			//あらかじめメニューを閉じる
			target.height(0);
			var to;

			//メニューの開閉ファンクション
			trigger.hover(function()
			{//メニューを開く
				target.not(':animated').animate({height: menuHeight}, 400, 'easeOutCubic' );
				clearTimeout( to );
			}
			,function()
			{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
					to = setTimeout( function (){ target.animate({height: 0}, 300, 'easeInCubic' ); }, 600);
			});
			
			target.hover(function()
			{//メニューを開く
				clearTimeout( to );
			}
			,function()
			{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
				to = setTimeout( function (){ target.animate({height: 0}, 300, 'easeInCubic' ); }, 600);
			});

		}
		,
		//-----------------------------------------------------------------
		btnRollover: function()
		{//PNG対応ロールオーバー
			var filter = $('div#sNavInner').find('a');//除外するコンテンツ
			var addClassName = 'btn';
			//console.log(filter);
			//$('a:not(.current, .noDefHover):has(img)').each(function()
			$('a:not(.current):has(img)').not(filter).each(function()
			{//IE8で要素のイベントが無効になる対策のため回りくどいことしている↓
				var target = $(this);
				var childImg = target.find('img');
				childImg.addClass(addClassName);
				
				// opacityに非対応なブラウザに適用
				if(!$.support.opacity) target.css('cursor','pointer').find('img').fixPng();
				
				target.hover(function()
				{
					childImg.not(':animated').fadeTo('fast', 0.6);
				}
				,function()
				{
					childImg.fadeTo('fast', 1);
				});
			});
		}
		,
		//-----------------------------------------------------------------
		categoryScript: function()
		{//ページごとのスクリプト
			$('body.home div#news').each(function()
			{//homeのニュースコンテンツ
				var target = $(this);
				
				target.hover(function()
				{
					target.not(':animated').animate({ marginLeft: 0 }, 500 );
					clearTimeout( to );
				}
				,function()
				{//ロールアウトしてもすぐにメニューが引っ込まないようにする。
					to = setTimeout( function (){ target.animate( { marginLeft: '-702px' }, 500 ); }, 700); 
				});				
			});			
		}
		,
		//-----------------------------------------------------------------
		pngFix: function()
		{//IE6のPNG対策。
			if (typeof document.documentElement.style.maxHeight == 'undefined')
			//if (!typeof document.documentElement.style.maxHeight)
			{//IE6判定（IE８で要素のイベントが無効になるので除外する）
				$('img:not(.btn)[src$=".png"]').each(function()
				{
					$(this).fixPng();
				});
			}
		},
		fixLayoutEventArchive:function(){
			if( !$('body').hasClass('event') || !$('body').hasClass('archive')) return false;
			
			$('#eventSchoolContents div.monthBox').each(function(){
				var o = {}
				
				o['monthBox'] = $(this);
				o['posts'] = $(this).find('div.post');
				
				var eventCount = 0;
				var schoolCount = 0;
				
				o.posts.each(function(n){
					var target = $(this);
					if(target.hasClass('catEvent')){
						var pos ={
							top:target.height() * eventCount
						}
						
						
						eventCount++;
						
						
					
					}else if($(this).hasClass('catSchool')){
						var pos ={
							top:target.height() * schoolCount
						}
						schoolCount++;
					
					}
					
					target.css(pos);
					
					if(eventCount >= schoolCount){
						var height = {
							height:target.height()*eventCount
						}
					}else{
						var height = {
							height:target.height()*schoolCount
						}
						
					}					
					o.monthBox.css(height);
				
				})
				var obj = $('div.scrollCont');
				if(obj.get(0))	{
					obj.jScrollPane({hijackInternalLinks: true, animateScroll :true});
				}
			
			})
			
			$('#monthList div.monthBox').each(function(n){
				$(this).css({
					height:$('#eventSchoolContents div.monthBox').eq(n).height()
				})
			})
			
			$('#eventSchoolContents div.monthBox:last').addClass('last')
			
			
			
			
		}
		//-----------------------------------------------------------------
	};
//---------------------------------------------------------------------
})(jQuery);
if (!window.console) {
  (function() {
      var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
        "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
      window.console = {};
      for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
  })();
}
