var windowHeight = 0;
var menuHeight = 0;
var footerHeight = 0;
var documentHeight = 0;
var name = "#sideContent";
var menuYloc = null;

$(document).ready(function(){
	menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
	menuHeight = $("#sideContent").height();
	footerHeight = $("#footer").height();
	documentHeight = $(document).height();
	windowHeight = ($.browser.opera ? document.documentElement.clientHeight : $(window).height());
});

$(window).resize(function () {
	windowHeight = ($.browser.opera ? document.documentElement.clientHeight : $(window).height());
});

$(window).scroll(function () {
	//フッタにかぶらない
	if( ( documentHeight - footerHeight - menuHeight ) > $(document).scrollTop() ){
		//ウインドウ高さよりナビの高さが高い場合
		if( ( $(document).scrollTop() + windowHeight ) > ( $("#sideContent").position().top + menuHeight + 250 + 100 ) ){
			if( $(document).scrollTop() >= 250 )
				offset = menuYloc + $(document).scrollTop() - 250 +"px";
			else
				offset = menuYloc + 0 +"px";
		}
		else if( $(document).scrollTop() - 250 < $("#sideContent").position().top ){
			if( $(document).scrollTop() >= 250 )
				offset = menuYloc + $(document).scrollTop() - 250 +"px";
			else
				offset = menuYloc + 0 +"px";
		}
		//else
			//offset = 0 +"px";
	}
	else
		offset = menuYloc + documentHeight - footerHeight - menuHeight - 250 +"px";

	$(name).animate({top:offset},{duration:750,queue:false});
	
});


$(function(){  
	$("a[href*='#']").easingScroll({
		easing: "easeInOutSine",
		duration: 1000
	});
});

function addRule( selector, property ) {
　if(navigator.userAgent.indexOf("Safari") >= 0){ //Safari
	var str = document.createTextNode(selector + "{" + property + "}");
	document.getElementsByTagName("style").item(0).appendChild(str);
　}
　if( document.styleSheets[0].addRule ) //IE
　　document.styleSheets[0].addRule(selector,property);
　else if( document.styleSheets[0].insertRule ) //Mozilla
　　document.styleSheets[0].insertRule( selector + "{" + property + "}", document.styleSheets[0].cssRules.length );
　else
	return false;
}
