window.onload = function(){
	add_chimg("gmenu_btn01");
	add_chimg("gmenu_btn02");
	add_chimg("gmenu_btn03");
	add_chimg("gmenu_btn04");
	add_chimg("gmenu_btn05");
	add_chimg("gmenu_btn06");
	add_chimg("gmenu_btn07");
	add_showdlist("dlist-parent");
	add_esscrll("totop");
	
	if(document.getElementById("access-map")!=null){
		mapLoader();
	}
};
window.onscroll = function(){
	move_btn("totop");
};

function add_chimg(id){
	if(document.getElementById(id)!=null){
		new RollOverImage(document.getElementById(id));
	}
}
function add_esscrll(id){
	if(document.getElementById(id)!=null){
		new esscrller(document.getElementById(id));
	}
}
function add_showdlist(id){
	if(document.getElementById(id)!=null){
		new showdlist(document.getElementById(id));
	}
}

function RollOverImage(img){
	this.img = img;
	this.originalPath = this.img.src;
	this.setMouseOverImage();
}
RollOverImage.prototype.setMouseOverImage = function(){
	var originalPath = this.originalPath;
	/* 第2の画像に置き換える場合 */
	this.mouseoverImg = new Image();
	this.mouseoverImg.src = originalPath.replace(".gif","_on.gif");
	this.img.onmouseover = RollOverImage.createHandler(this.img, this.mouseoverImg.src);
	/* 元の画像に戻す場合 */
	this.img.onmouseout = RollOverImage.createHandler(this.img, this.originalPath);
}
RollOverImage.createHandler = function(img, path){
	return function(){
		img.src = path;
	}
}


function showdlist(object){
	this.obj = object;
	for(i=0; (a = document.getElementsByTagName("ul")[i]); i++) {
		if(a.className=="second-submenu") {
			this.obj.subobj=a;
		}
	}
	this.setshowdlist();
}
showdlist.prototype.setshowdlist = function(){
	this.obj.onmouseover = showdlist.createHandler(this.obj, "visible", "hidden");
	this.obj.onmouseout = showdlist.createHandler(this.obj, "hidden", "visible");
}
showdlist.createHandler = function(obj, state,state2){
	return function(){
		document.getElementById("dlist-container").style.visibility = state;
		if(obj.subobj!=null){
			obj.subobj.style.visibility = state2;
		}
	}
}



function ease_scroll(){
	ease_scrolling=1;
	y = document.body.scrollTop || document.documentElement.scrollTop;
	window.scroll(0,y*9/10-1);
	if(y>0){
			setTimeout("ease_scroll()", 5);
	}else{
		ease_scrolling=0;
		move_btn("totop");
	}
}

var timerID;
var ease_scrolling=0;

function move_btn(id){
	if(ease_scrolling==0){
		btn=document.getElementById(id);
		if(btn!=null){
			set_btn(btn);
			clearInterval(timerID);
			timerID=setInterval("set_btn(btn)", 5);
		}
	}
}



function set_btn(btn){
	y = document.body.scrollTop || document.documentElement.scrollTop;
	dheight=window.innerHeight||document.documentElement.clientHeight;
	//700ってのはボタンのある位置、150ってのはフッタの高さ。
	if(dheight<700){
		y=y+dheight-700;
	}
	y=Math.max(y-150,0);
	btnorg=btn.style.top;
	btn.style.top=(y-parseInt(btn.style.top))/15+parseInt(btn.style.top)+"px";
	if(btnorg==btn.style.top){
			clearInterval(timerID);
	}
}
function esscrller(obj){
	this.obj = obj;
	this.obj.style.top=0;
	this.setOnClickScroll();
}
esscrller.prototype.setOnClickScroll = function(){
	this.obj.onclick = esscrller.createHandler();
}
esscrller.createHandler = function(){
	return function(){
		ease_scroll();
		return false;
	}
}


function mapLoader() {
	var map = new GMap(document.getElementById("access-map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.centerAndZoom(new GLatLng(35.778123,139.997326), 3);
	addMarker(map, 139.997326, 35.778123, "鎌ケ谷総合病院<br />千葉県鎌ヶ谷市初富929番6");
}
function addMarker(map, lng, lat, html) {
	var point = new GPoint(lng, lat);
	var marker = new GMarker(point);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(html);
}