var mailAddress ='mailto:'+'mail'+'@'+'sasaomiku.jp';
var toTopP = '<div class=\"toTopJS\"><a href=\"#top\"><img src=\"./common/image/toTop.gif\" alt=\"Page Top\" title=\"このページの先頭へ\" class=\"mouseon\" /></a></div>';


$(function(){
	/* fist-child 最初の.sectionH2には、class="first-child"を付与 */
	$('#article .sectionH2:first-child').addClass('first-child');
	/* 最初の.sectionH3には、class="first-child"を付与 */
	$('#article .sectionH2 .sectionH3:first-child').addClass('first-child');
	/* h2要素直後の.sectionH3に、class="first-child"を付与 */
	$('#article .sectionH2 h2+.sectionH3').addClass('first-child');
	
	/* fist-child 以外にページトップリンクをつける */
	$('#article .sectionH2:not(.first-child)').prepend(toTopP);
	$('#article .sectionH3:not(.first-child)').prepend(toTopP);
	
	/* 画像を含まないリンクにclass="inLineA"を付与。アンダーラインをDottedにします */
	$('a').addClass('inLineA');
	$('a:has(img)').removeClass('inLineA');
	
	/* h2要素を含まない.sectionH2には、class="noName"を付与。本文開始位置をH2要素があるかのように振舞わせる */
	$('#article .sectionH2').addClass('noName');
	$('#article .sectionH2:has(h2)').removeClass('noName');
	
	/* mailaddress メールアドレスを生成。スパム対策 */
	$('#contact p a').attr('href',mailAddress);
	$('a#infoMail').attr('href',mailInfoAddress);
});

/* 画像のロールオーバー関数 class="mouseon"をつけると、発動。画像名＋Hoverにチェンジ。 */
function initRollOverImages() {
	var image_cache = new Object();
	$("img.mouseon").each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_on = this.src.substr(0, dot) + 'Hover' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_on;
		$(this).hover(
		function() { this.src = imgsrc_on; },
		function() { this.src = imgsrc; });
	});
}

$(document).ready(initRollOverImages);

/* スムーズスクロール すべてのページ内リンクに自動で発動 */
$.fn.slideScroll = function(options){
	
	var c = $.extend({
		interval: 20, // 変化はあんまりないかも
		easing: 0.6, // 0.4 ~ 2.0 くらいまで
		comeLink: false
	},options);
	var d = document;
	
	// timerとposのscopeを$.fn.slideScroll内に限定する
	var timer;
	var pos;
	
	// スクロール開始時の始点を得る
	function currentPoint(){
		var current = {
			x: d.body.scrollLeft || d.documentElement.scrollLeft,
			y: d.body.scrollTop || d.documentElement.scrollTop
		}
		return current;
	}
	
	// 現在のウィンドウサイズとターゲット位置から最終到達地点を決める
	function setPoint(){
		
		// 表示部分の高さと幅を得る
		var h = d.documentElement.clientHeight;
		var w = d.documentElement.clientWidth;
		
		// ドキュメントの最大の高さと幅を得る
		var maxH = d.documentElement.scrollHeight;
		var maxW = d.documentElement.scrollWidth;
		
		// ターゲットの位置が maxH(W)-h(w) < target < maxH(W) なら スクロール先をmaxH(W)-h(w)にする
		pos.top = ((maxH-h)<pos.top && pos.top<maxH) ? maxH-h : pos.top;
		pos.left = ((maxW-w)<pos.left && pos.left<maxW) ? maxW-w : pos.left;
	}
	
	// 次のスクロール地点を決める
	function nextPoint(){
		var x = currentPoint().x;
		var y = currentPoint().y;
		var sx = currentPoint().x;
		var sy = Math.ceil((y - pos.top)/(5*c.easing));
		var next = {
			x: x - sx,
			y: y - sy,
			ax: sx,
			ay: sy -1
		}
		return next;
	}
	
	// 到達地点に近づくまでスクロールを繰り返す
	function scroll(href){
		var movedHash = href;
		timer = setInterval(function(){
			nextPoint();
			
			// 到達地点に近づいていたらスクロールをやめる
			if(Math.abs(nextPoint().ax)<1 && Math.abs(nextPoint().ay)<2){
				clearInterval(timer);
				window.scroll(pos.left,pos.top);
				location.href = movedHash;
			}
			window.scroll(nextPoint().x,nextPoint().y);
		},c.interval);
	}
	
	// URIにhashがある場合はスクロールする
	function comeLink(){
		if(location.hash){
			if($(location.hash) && $(location.hash).length>0){
				pos = $(location.hash).offset();
				setPoint();
				window.scroll(0,0);
				if($.browser.msie){
					setTimeout(function(){
						scroll(location.hash);
					},50);
				}else{
					scroll(location.hash);
				}
			}
		}
	}
	if(c.comeLink) comeLink();
	
	// アンカーにclickイベントを定義する
	$(this).each(function(){
		if(this.hash && $(this.hash).length>0 
			&& this.href.match(new RegExp(location.href.split("#")[0]))){
			var hash = this.hash;
			$(this).click(function(){
				// ターゲットのoffsetを得る
				pos = $(hash).offset();
				
				// スクロール中ならスクロールをやめる
				clearInterval(timer);
				
				// 到達地点を決めてスクロールを開始する
				setPoint();
				scroll(this.href);
				return false;
			});
		}
	});
}
