/* ================================================================================
* Title: Jquery Function
*
* Copyright (c) 2011 Astra The Studio Inc.
* http://www.studio.co.jp/
* Date: Oct.07, 2011
* @author: Chihiro Mori
* @version: 9.9.6
*
* jQuery 1.6.3
*
* パス判定用に以下の外部CSSと併用すること
* <link href="*css/base.css" rel="stylesheet" type="text/css" id="baseCSS" />
* Macで作業するときの注意：バックスラッシュ\を入力するときはoptionを押しながらエンマーク
================================================================================ */
var shrPath = document.getElementById('baseCSS').href.replace('css/base.css','');
//---------------------------------------------------------------------
$script(shrPath+'js/jquery.js', function()
{
//---------------------------------------------------------------------
(function($)
{
//---------------------------------------------------------------------
$(function()
{
	$.AS.P =
	{//共通パラメータ
		'selfClass': 'current',
		'links': $('a'),
		'noBtnFilter': 'ul#sNav'
	};
	$.AS.selfLink();
	$.AS.selfScroll();
	$.AS.btnRollover();
	$.AS.colorbox('div#singlePostWrap', 'js/colorbox/jquery.colorbox.js');
	$.AS.formCheck('form#form');
	$.AS.autoHeight('div#container', ($('body.home')[0]? 350: 143)); //トップビジュアルの分高さが違う
	$.AS.customFunction();	
});
//---------------------------------------------------------------------
$.AS =
{
//-----------------------------------------------------------------
selfLink: function ()
{
	var dom = $.AS.P.links;
	var num = dom.length;
	var uri = location.href.replace(location.hash, '').replace(/(\/|\/#)$/, '/');
	
	for(var i=0;i<num;i++)
	{//自ページリンクにクラス付与。子要素にイメージが含まれていたら半透明化
		var target = dom.eq(i);
		//var childImg = target.find('img');
		var tmp = document.createElement('span');
		tmp.innerHTML = '<a href="' + target.attr('href') + '" />';
		var path = tmp.firstChild.href;
		if (path == uri)
		{//パスを比較して同じだったら
			target.addClass($.AS.P.selfClass).removeAttr('href');
			//childImg.fadeTo(0, 0.6);
		}
	}
}
,
//-----------------------------------------------------------------
selfScroll: function ()
{
	$.AS.P.links.filter('[href*=#]').click(function()
	{
		var hash = this.hash;
		if(hash != '')
		{//#のみでは無かったら
			$(!$.support.checkOn ? 'body' : 'html')
			.animate({scrollTop: $(hash).offset().top}, 600, 'easeOutBack');
			return false;
		}
	});
}
,
//-----------------------------------------------------------------
btnRollover: function()
{//ボタン画像のロールオーバー
	var notFilter = $($.AS.P.noBtnFilter).find('a');
	var dom = $.AS.P.links.not(notFilter).filter(':not(.'+ $.AS.P.selfClass +'):has(img)');
	var num = dom.length;
	
	for(var i=0;i<num;i++)
	{
		var target = dom.eq(i);
		target.hover(function()
		{
			$(this).stop(true, true).fadeTo('fast', 0.6);
		}
		,function()
		{
			$(this).stop(true, true).fadeTo('fast', 1);
		});
	}//endfor
}
,
//-----------------------------------------------------------------
colorbox: function(tmp, jsPath)
{//Examples of how to assign the ColorBox event to elements
	var dom = $(tmp);
	if(dom.get(0))
	{
		$script(shrPath+jsPath, function()
		{
//					var head = document.getElementsByTagName('head')[0];
//					var link = document.createElement('link');
//					link.rel = 'stylesheet';
//					link.type = 'text/css';
//					link.href = shrPath+cssPath;
//					head.appendChild(link);
			dom.find('a:has(img)[target!="_blank"]').colorbox();
		});
	}//endif
}
,
//-----------------------------------------------------------------
autoHeight: function(tmp, otherHeight)
{
	var target = $(tmp);
	var defHeigh = target.height();
	var minHeight = defHeigh+otherHeight;
	function heightFix()
	{
		var nowHeight = $(window).height();
		if(nowHeight > minHeight)
		{
			target.height(nowHeight-otherHeight);
		}
		else if(nowHeight <= minHeight)
		{
			//target.height(defHeigh);
		}
	}
	$(window).bind('resize', heightFix);
	heightFix();
}
,
//-----------------------------------------------------------------
formCheck: function(tmp)
{
	var dom = $(tmp);
	if(dom.get(0))
	{
		$script(shrPath+'js/form/validate.js', function()
		{
			function addValidate(tmp, mess)
			{
				var num = tmp.length;
				for(var i=0;i<num;i++)
				{
					var target = tmp.eq(i);
					target.focus(function()
					{
						$(this).removeClass('error');
					})
					.blur(function()
					{
						var validateType = $(this).attr('class').split(' ');
						var num = validateType.length;
						for(var i=0;i<num;i++)
						{
							var ret = validateAndSet(this, validateType[i], mess);
							if(!ret) return false;
						}//endfor
					});
				}//endfor
			}
			var validate = dom.find('.required, .email, .tel');
			addValidate(validate, true);

			$script(shrPath+'js/form/ajaxzip2.js', function()
			{//住所検索
				dom.find('input#zip').keyup(function()
				{
					AjaxZip2.JSONDATA = shrPath+'js/form/data';
					AjaxZip2.zip2addr(this,'pref','addr');
				});
			});
			
		});//endscript
	}//endif
}
,
//-----------------------------------------------------------------
customFunction: function ()
{
	//次の月へリンクの表示設定
	var dom = $('div#monthNav').find('a.current').parent();
	if(dom.get(0))
	{
		dom.prev().addClass('prev').end().next().addClass('next');
	}
	
	//サイドメニューの年別一覧のアコーデオン
	var acc = $('#sNav').find('a.yearNav');
	if(acc.get(0))
	{
		var target = acc.next();
		var triggerClass = 'open';
		if(target.find('a.current').get(0))
		{
			acc.toggleClass(triggerClass);
		}
		else
		{
			target.hide(); //予めメニューを閉じる
		}
		acc.click(function()
		{
			var $this = $(this);
			target.slideToggle(function()
			{
				$this.toggleClass(triggerClass);
			});
			return false;
		});
	}
	
	var myCalendar = $('p#newCalendar').find('img');
	if(myCalendar.get(0))
	{
		//カレンダーの年別切り替え
		var targetYear = 2012;
		var oldYear = 2011;
		var today = new Date();
		myYear= today.getFullYear();
		if(myYear == oldYear)
		{
			myCalendar.attr('src', myCalendar[0].src.replace(targetYear, oldYear));
		}
	}
}
//-----------------------------------------------------------------
};
//---------------------------------------------------------------------
})(jQuery);
//---------------------------------------------------------------------
});
