// JavaScript Document
$(document).ready(function(){
	rollOver();
	pageTop();
	$("#pageBody").subMenuStoker("#sideBar p");
	pullDown();
	alphaAnchor();
	$(".ar").alphaRo({from:1, to:0.6});
});

/*	ロールオーバー
-------------------------------------------------------------------------- */
function rollOver() {
	$("img[src*='_off.']").hover( function() {
		$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
	}, function() {
		$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
	});
}

/*	プルダウン
-------------------------------------------------------------------------- */
function pullDown() {
	$(".navi li.pull").each( function() {
		var dl = $(this).find("dl");
		var dd = $(this).find("dd");
		dd.hide();
		dl.hover( function() {
			dd.show();
		}, function() {
			dd.hide();
		} );
	} );
}

function alphaAnchor() {
	$(".itemBox table td a img").hover( function() {
		$(this).css("opacity", 0.8);
	}, function() {
		$(this).css("opacity", 1);
	} );
}

/*	スムーズスクロール
-------------------------------------------------------------------------- */
function pageTop() {
	var speed = 700;
	$("a.pn[href^='#']").click(function() {
		var target = $(this).attr('href').substr(1);
		var targetY = 0;
		var h = Math.max( document.body.clientHeight , document.body.scrollHeight );  
		h = Math.max( h , document.documentElement.scrollHeight );  
		h = Math.max( h , document.documentElement.clientHeight );
		var inH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		if(target != '') {
			targetY = $("*[name='"+target+"']").offset().top;
			if(targetY+inH > h) targetY = h-inH;
		}
		$('html').animate({scrollTop:targetY}, speed, "easeInOutSine");
		$('body').animate({scrollTop:targetY}, speed, "easeInOutSine");
		return false;
	});
}
/*	
-------------------------------------------------------------------------- */
jQuery.fn.extend({
	subMenuStoker : function(child) {
		if( !$("body.index").length ) {
			var toPos = 10;
			var cBox = $(this);
			var lBox = $(this).find(child);
			var dTop = lBox.offset().top;
			cBox.css("position", "relative");
			lBox.css( {"position":"absolute", "top":0} );
			var scrInterval = setInterval( function() {
				var nHeight = lBox.outerHeight({margin:true});
				var cHeight = cBox.outerHeight({margin:true});
				var scr = $(window).scrollTop()+toPos;
				var tTop = parseInt( lBox.css('top').replace('px', '') );
				var tgt = scr-dTop;
				tgt = tgt<0 ? 0 : tgt;
				
				if( tgt+nHeight > cHeight ) {
					tgt = cHeight-nHeight;
				}
				
				var sabun = (tgt-tTop)/10;
				var x = sabun<0 ? -0.5 : 0.5;
				var n = tTop + sabun + x;
				
				lBox.css('top', n);
			}, 15);
		}
	},
	
	/*	Alpha RollOver
	-------------------------------------------------------------------------- */
	alphaRo : function(opt) {
		if( !opt ) opt = new Object();
		var _from = opt.from!=null ? opt.from : 1;
		var _to = opt.to!=null ? opt.to : 0.8;
		var _f = opt.fade!=null ? opt.fade : false;
		var _s = opt.speed!=null ? opt.speed : "normal";
		var _g = opt.group!=null ? opt.group : true;
		$(this).css({"opacity":_from, display:"inline-block"}).bind("mouseenter", {toNum:_to}, toOpacity).bind("mouseleave", {toNum:_from}, toOpacity);
		function toOpacity(event) {
			var tgt = $(this);
			var rel = tgt.attr('rel');
			if( rel && _g ) {
				tgt = $("*[rel='"+rel+"']");
			}
			( _f ) ? tgt.queue([]).fadeTo(_s, event.data.toNum) : tgt.css("opacity", event.data.toNum);
		}
		return $(this);
	}
});
