// JavaScript Document
$(document).ready(function(){
	rollOver();
	pageTop();
	$("#pageBody").subMenuStoker("#rightBar");
	$("a[rel='jumpEvent']").anotherWin({width:820, name:'eventWindow'});
	$(".lang a").hover( function() {
		$(".langBln").show().css("top", 30).animate({top:25},{duration:"fast"});
	}, function() {
		$(".langBln").hide();
	} );
});

/*	ロールオーバー
-------------------------------------------------------------------------- */
function rollOver() {
	$("img[src*='_off.']").filter(function() { return $(this).parent().hasClass("non") ? false : true; }).hover( 
		setRollOver, setRollOut
	);
}

function setRollOver() {
	$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
}

function setRollOut() {
	$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
}


/*	スムーズスクロール
-------------------------------------------------------------------------- */
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;
	});
}

function setHeight(parent, child) {
	parent.each( function() {
		var sHeight = 0;
		$(this).find( child ).each( function() {
			$(this).css('height', 'auto');
			if( sHeight<$(this).height() )
				sHeight = $(this).height();
		} ).height(sHeight);
	} );
}


jQuery.fn.extend({
	subMenuStoker : function(child) {
		var toPos = 20;
		var cBox = $(this);
		var lBox = $(this).find(child);
		var dTop = lBox.offset().top;
		var nowP = dTop;
		var scrInterval, chkInterval;
		if( !$("body.index").length ) {
			cBox.css("position", "relative");
			lBox.css( {"position":"absolute", "top":0} );
			goMoving();
		}
		function chkMoving() {
			chkInterval = setInterval( function() {
				var sabun = getTargetPos()-nowP;
				if( Math.abs(sabun) > 5 ) {
					clearInterval(chkInterval);
					goMoving();
				}
			}, 700 );
		}
		function goMoving() {
			scrInterval = setInterval( function() {
				var sabun = ( getTargetPos()-nowP )/5;
				nowP = nowP + sabun;
				
				if( Math.abs(sabun) < 0.5 ) {
					clearInterval(scrInterval);
					nowP = getTargetPos();
					lBox.css('top', nowP );
					chkMoving();
				} else {
					lBox.css('top', nowP);
				}
			}, 10);
		}
		function getTargetPos() {
			var nHeight = lBox.outerHeight({margin:true});
			var cHeight = cBox.outerHeight({margin:true});
			var scr = $(window).scrollTop()+toPos;
			var tgt = scr-dTop;
			tgt = tgt<0 ? 0 : tgt;
			if( tgt+nHeight > cHeight )
				tgt = cHeight-nHeight;
			return tgt;
		}
	},
	/*	別画面
	-------------------------------------------------------------------------- */
	anotherWin : function(opt) {
		var w = opt.width ? opt.width : 500;
		var h = opt.height ? opt.height : false;
		var n = opt.name ? opt.name : 'subWindow';
		$(this).click( function() {
			var url = $(this).attr('href');
			var rule = 'width='+w+',scrollbars=yes,resizable=yes,menubar=yes';
			if( h ) rule += ',height='+h;
			window.open(url, 'subWindow', rule);
			return false;
		});
	}
});
