window.addEvent('domready', function() {
	setStyleSheetSelecter();
	setMenuSlide();
	setSwitchImgLoop();
});


/*フォントサイズスタイルシート選択*/

function setStyleSheetSelecter(title) {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	$$(".header-font a").each(function(obj){
		obj.addEvent('click', function(e){
			setActiveStyleSheet(obj.getProperty("rel"));
			return false;
		});
	});
	setActiveStyleSheet(title);
}

function setActiveStyleSheet(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("rel").indexOf("alt") == -1
			&& a.getAttribute("title")
		) return a.getAttribute("title");
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

window.onunload = function(e) {
	var title = getActiveStyleSheet();
	createCookie("style", title, 0);
}


/*トップページイメージ変更*/
var imglist;
var imgID;
function setSwitchImgLoop(){
	if($$(".switchimg")!=""){
		imglist=$$(".switchimg").shuffle();
		imgID=0;
		imglist.each(function(obj){
			obj.fade("hide");
			obj.style.display="none";
		});
		switchImgLoop();
	}
}
function switchImgLoop(){
	imglist[imgID].style.display="none";
	imglist[imgID].fade("hide");
	imgID=(imgID+1)%imglist.length;
	imglist[imgID].style.display="block";
	imglist[imgID].set('tween', {duration: '1200'});
	imglist[imgID].fade("in");
	setTimeout("switchImgLoop(imglist,imgID)", 4500);
}
Array.prototype.shuffle = function() {
    var i = this.length;
    while(i){
        var j = Math.floor(Math.random()*i);
        var t = this[--i];
        this[i] = this[j];
        this[j] = t;
    }
    return this;
}


/*Hot topics移動*/

var branchtop=0;
var timer=false;

window.addEvent('scroll', function(){
	if(!timer){
		timer=true;
		moveBlanch();
	}
});

function moveBlanch(){
    if(
	   	(window.getScrollHeight()>window.getScrollTop()+$("branch-float").getSize().x+200)
		&&
		(window.getHeight()>$("branch-float").getSize().x+200)
	){
		$("branch-float").style.top=Math.ceil((window.getScrollTop()-parseInt($("branch-float").getStyle("top")))/10)+parseInt($("branch-float").getStyle("top"))+"px";
		if(Math.abs($("branch-float").style.top-window.getScrollTop())<10){
			clearTimeout();
			timer=false;
		}else{
			setTimeout("moveBlanch()", 30);
		}
	}
}


/*サブメニュー表示*/

var currentSubmenu=null;
var currentLnkmenu=null;

function setMenuSlide() {
	$$(".gmenu-main a").each(function(obj){
		slideMenu(obj,obj.getProperty("rel"));
	});
}

function slideMenu(trigger,target) {

	var gmenu = $(trigger);
	var gmenus = new Fx.Slide(target,{
		onComplete: function(){
			if(currentSubmenu!=gmenus&&gmenus.isOpen){
				gmenus.slideOut();
				gmenus.isOpen=false;
			}
			gmenus.isEnable=true;
		}
	});
	gmenus.isEnable=true;
	if($(target).getStyle("display")!="block"){
		$(target).style.display="block";
		gmenus.hide();
	}else{
		gmenus.show();
		gmenu.setStyle("background-position", "0 -110px");
		currentSubmenu=gmenus;
		currentLnkmenu=gmenu;
		gmenus.isEnable=true;
		gmenus.isOpen=true;
	}
	gmenu.addEvent('click', function(e){
		if(gmenus.isEnable){
			gmenu.setStyle("background-position", "0 -110px");
			if(currentSubmenu==null){
				gmenus.slideIn();
				currentSubmenu=gmenus;
				currentLnkmenu=gmenu;
				gmenus.isEnable=false;
				gmenus.isOpen=true;
			}else if(currentSubmenu==gmenus){
				gmenus.slideOut();
				gmenu.setStyle("background-position", "0 0");
				currentSubmenu=null;
				currentLnkmenu=null;
				gmenus.isEnable=false;
				gmenus.isOpen=false;
			}else{
				currentSubmenu.slideOut().chain(function(){
					gmenus.slideIn();
				});	
				currentLnkmenu.setStyle("background-position", "0 0");
				currentSubmenu=gmenus;
				currentLnkmenu=gmenu;
				gmenus.isEnable=false;
				gmenus.isOpen=true;
			}
		}
		(function(){if(currentSubmenu!=gmenus){gmenus.slideOut();}}).delay(500);
		return false;
	});
	gmenu.addEvent('mouseover', function(e){
		if(currentSubmenu!=gmenus)
		gmenu.setStyle("background-position", "0 -55px");
	});
	gmenu.addEvent('mouseout', function(e){
		if(currentSubmenu!=gmenus)
		gmenu.setStyle("background-position", "0 0");
	});
}

/*ムービー*/
function playRotablator(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 550, objectWidth: 550, objectHeight: 460, swfOptions: {version: '8', flashvars: { titleImage:'rotablator',repeat:'0',cid:'1',seriesTitle:'ロータブレーター',series:'rotablator',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}

function playKoalaMaeda(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 650, objectWidth: 650, objectHeight: 460, swfOptions: {version: '8', flashvars: { titleImage:'koala_maeda',repeat:'0',cid:'10',seriesTitle:'ガンとしょくせいかつ',series:'koala_maeda',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}

function playDietMaeda(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 650, objectWidth: 650, objectHeight: 460, swfOptions: {version: '8', flashvars: { titleImage:'diet_maeda',repeat:'0',cid:'30',seriesTitle:'ただしいダイエット',series:'diet_maeda',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}

function playMicroSurgery(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 550, objectWidth: 550, objectHeight: 460, swfOptions: {version: '8', flashvars: { titleImage:'micro_surgery',repeat:'0',cid:'2',seriesTitle:'マイクロサージャリー',series:'micro_surgery',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}

function playPharmacy(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 550, objectWidth: 550, objectHeight: 460, swfOptions: {version: '8', flashvars: { titleImage:'pharmacy',repeat:'0',cid:'26',seriesTitle:'おくすりののみかた',series:'pharmacy',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}

function playMisumiLecture1(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 550, objectWidth: 550, objectHeight: 460, swfOptions: {version: '8', flashvars: { titleImage:'nurse_education',repeat:'0',cid:'7',seriesTitle:'レクチャー１',series:'kango_misumi',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}

function playCasDemo(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 320, objectWidth: 320, objectHeight: 250, swfOptions: {version: '8', params: {quality: 'high'} } } );
}

function playCasBoston(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 550, objectWidth: 550, objectHeight: 490, swfOptions: {version: '8', flashvars: { titleImage:'bostoncas',repeat:'0',cid:'49',seriesTitle:'bostoncas',series:'bostoncas',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}

function playCardiology(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 320, objectWidth: 320, objectHeight: 240, swfOptions: {version: '8', params: {quality: 'high'} } } );
}

function playLaser(doc,hsobj) {
	return hsobj.htmlExpand(doc, { objectType:'swf', width: 550, objectWidth: 550, objectHeight: 460, swfOptions: {version: '8', flashvars: { titleImage:'aso_laser',repeat:'0',cid:'12',seriesTitle:'ASOレーザー',series:'aso_laser',dhost:'www',mhost:'www',domain:'chibanishi-hp.or.jp',extension:'flv' }, params: {quality: 'high'} } } );
}