/* ================================================================================
* 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.selfLink();
		$.ASconf.selfScroll();
		$.ASconf.btnRollover();
		$.ASconf.pulldown();
		$.ASconf.scrollCont();
		$.ASconf.changeBg();
	});
//---------------------------------------------------------------------
	$.ASconf =
	{
		//-----------------------------------------------------------------
		selfLink: function ()
		{//自ページリンクにクラス付与。子要素にイメージが含まれていたら半透明化
			
			var selfLinkClass = 'current';//Aタグに付与
			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
				{
					$('html').animate({scrollTop: hash.offset().top}, 300, 'swing');
					return false;
				}
			});
		}
		,
		//-----------------------------------------------------------------
		btnRollover: function()
		{//ボタン画像のロールオーバー
			$('a:not(.current):has(img)').each(function()
			{
				var target = $(this);
				target.hover(function()
				{
					target.not(':animated').fadeTo('fast', 0.6);
				}
				,function()
				{
					target.fadeTo('fast', 1);
				});
			});
		}
		,
		//-----------------------------------------------------------------
		pulldown: function()
		{//プルダウンメニュー
			var trigger = $('ul#cNav').find('li.pulldown');
			if(trigger.get(0))
			{
				trigger.each(function()
				{
					var target = $(this).find('ul');
					$(this).hover(function()
					{
						target.not(':animated').slideDown(400, 'easeOutCubic');
					}
					,function()
					{
						target.slideUp(300, 'easeInCubic');
					});
				});
			}//end if
		}
		,
		//-----------------------------------------------------------------
		scrollCont: function()
		{//カスタムスクロールバー
			var obj = $('div.scrollCont');
			if(obj.get(0))
			{
				obj.jScrollPane({hijackInternalLinks: true, animateScroll :true, showArrows: true});
			}
		}
		,
		//-----------------------------------------------------------------
		changeBg: function()
		{//背景切り替えスクリプト
			var obj = $('ul#bgImages');
			if(obj.get(0))
			{
				var contensWrap = $('div#container');//もろもろのコンテンツを囲む要素
				var maxNum = 5;//画像、テキストコンテンツなどの数。1からカウント
				var currentCont = 1;//現在選択されているコンテンツのフラグ
				//次へ戻るボタン			
				contensWrap.append('<p id="npNext"></p><p id="npPrev"></p>');
				var npNext = contensWrap.find('p#npNext');
				var npPrev = contensWrap.find('p#npPrev');
				
				//-----------------------------------------------------------------
				//ファンクション設定
				function changeBg(num)
				{//click：画像切り替えファンクション
					currentCont = num;
					firstBgTarget.attr('src', firstBgTarget.attr('src').replace(/[0-9]?(\.gif|\.jpg|\.png)$/, num+'$1'));
				}
				
				//-----------------------------------------------------------------
				//イベント設定
				npNext.click(function()
				{//次へボタン
					if(currentCont<maxNum) changeBg(currentCont + 1);
					else  changeBg(currentCont=1);
				})
				.hover(function()
				{//ホバー設定
					npNext.not(':animated').css('cursor','pointer').fadeTo('fast', 0.2);
				}
				,function()
				{
					npNext.fadeTo('fast', 1);
				});
				
				npPrev.click(function()
				{//前へボタン
					if(currentCont>1) changeBg(currentCont - 1);
					else changeBg(currentCont = maxNum);
				})
				.hover(function()
				{
					npPrev.not(':animated').css('cursor','pointer').fadeTo('fast', 0.2);
				}
				,function()
				{
					npPrev.fadeTo('fast', 1);
				});

				//-----------------------------------------------------------------
				//デフォルトで表示する背景画像の設定
				var firstBgTarget =  obj.find('img#bgmaximage');
				
				firstBgTarget.maxImage(
				{//全画面化
					isBackground: true,
					overflow: 'auto',
					verticalAlign:'bottom'
				})
				.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);
				}
				
				//-----------------------------------------------------------------
				//非同期で画像を読み込み
				
				$('div#news').each(function()
				{//homeのニュースコンテンツ
					var target = $(this);
					
					target.hover(function()
					{
						target.not(':animated').animate({ height: '138px' }, 500 );
						clearTimeout( to );
					}
					,function()
					{
						target.animate( { height: '35px' }, 500 );
					});
				});

			}//IF文終了
		}
		//-----------------------------------------------------------------
	};
//---------------------------------------------------------------------
})(jQuery);
