/* Infomation */

function hitTest(o, l) {
    function getOffset(o){
        for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight};
            o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
        return r.r += r.l, r.b += r.t, r;
    }
    var a = arguments, j = a.length;
    j > 2 && (o = {offsetLeft: o, offsetTop: l, offsetWidth: j == 5 ? a[2] : 0,
    offsetHeight: j == 5 ? a[3] : 0, offsetParent: null}, l = a[j - 1]);
    for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j ? l = [l] : l).length; i;
        b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r))
        && (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) && (r[r.length] = l[i]));
    return j ? !!r.length : r;
};


function selectTopic(selectnumber) {
		open_infodetail(selectnumber);
};

	function open_infodetail(selectnumber){
		var infobox = $('info_list');
		var infodetail = $('info_detail');
		var fx = new Fx.Morph(infobox, {
			duration: '400',
			transition: Fx.Transitions.Sine.easeOut,
			unit: 'px'
		}).start({
			'opacity': '0.85',
			'margin-left': '210'
		}).chain(function() {
			infodetail.setStyle('display', 'block');
			$$('#info_detail .text').setStyle('display','none');
			$$('#info_detail .text')[selectnumber].setStyle('display','block');
			$$('#info_detail .text')[selectnumber].setStyle('opacity','0');
			$$('#info_detail .detail_back')[0].setStyle('opacity', '0');
			$$('#info_detail .detail_back')[0].setStyle('height', $$('#info_detail .text')[selectnumber].getSize().y);
			new Fx.Morph($$('#info_detail .detail_back')[0], {
				duration: '500',
				transition: Fx.Transitions.Sine.easeInOut,
				unit: 'px'
			}).start({
				'opacity': ['0','0.8']
			}).chain(function() {
				new Fx.Morph($$('#info_detail .text')[selectnumber], {
				}).start({
					'opacity': ['0','1']
				})
			})
		})

	};

	function close_infodetail(){
		var infobox = $('info_list');
		var infodetail = $('info_detail');

		var fx = new Fx.Morph($$('#info_detail .text')[0], {
			duration: '70',
			transition: Fx.Transitions.Sine.easeOut,
			unit: 'px'
		}).start({
			'opacity': '0'
		}).chain(function() {
			$$('#info_detail .text').setStyle('display','none');
			new Fx.Morph($$('#info_detail .detail_back')[0], {
			duration: '70',
			transition: Fx.Transitions.Sine.easeInOut,
			unit: 'px'
			}).start({
				'opacity': '0'
			}).chain(function() {
				new Fx.Morph($('info_detail'), {
				duration: '0',
				transition: Fx.Transitions.Sine.easeInOut,
				unit: 'px'
				}).start({
					'display': 'none'
				}).chain(function() {
					new Fx.Morph(infobox, {
					duration: '400',
					transition: Fx.Transitions.Sine.easeInOut,
					unit: 'px'
					}).start({
						'margin-left': '604'
					})
				})
			})
		})
	};



/* Schedule */


function selectSchedule(schedulenumber) {
		open_schedetail(schedulenumber);
};

function open_schedetail(schedulenumber){
	window.addEvent('domready', function() {
		var schedetail = $('schedule_detail');

		$('schedule_detail').setStyle('display','block');
		$('schedule_detail').setStyle('opacity','0');
		$$('#schedule_detail .detail_back')[0].setStyle('opacity','0.9');
		$$('#schedule_detail .text').setStyle('display','none');
		$$('#schedule_detail .text')[schedulenumber].setStyle('display','block');
		$$('#schedule_detail .detail_back')[0].setStyle('height', $$('#schedule_detail .text')[schedulenumber].getSize().y);
		$$('#schedule_detail .text')[schedulenumber].setStyle('opacity','0');


		var fx = new Fx.Morph($('schedule_detail'), {
			duration: '200',
			transition: Fx.Transitions.Sine.easeOut
		}).start({
			'opacity': ['0', '1']
		}).chain (function() {
			new Fx.Morph($$('#schedule_detail .text')[schedulenumber], {
			}).start({
				'opacity': ['0','1']
			})
		})
	})
};

function close_schedetail(){
	window.addEvent('domready', function() {
		var schedetail = $('schedule_detail')

		var fx = new Fx.Morph(schedetail, {
			duration: '200',
			transition: Fx.Transitions.Sine.easeOut,
			unit: 'px'
		}).start({
			'opacity': 0
		}).chain(function() {
			new Fx.Morph(schedetail, {
			duration: '200',
			transition: Fx.Transitions.Sine.easeInOut,
			unit: 'px'
			}).start({
			'display': 'none'
			})
		})
	})
};


/* Diary */
/*
function changeDiary(){

		if ($('blog_area').blogStatus == 'watusi' ){
			var fx = new Fx.Morph($$('#blog_area .image_area .img')[0], {
				duration: '200',
				transition: Fx.Transitions.Sine.easeOut,
				unit: 'px'
			}).start({
				'opacity': '0' 
			}).chain( function() {
				$$('#blog_area .image_area .img img')[0].src='images/img_diary2.jpg';
				new Fx.Morph($$('#blog_area .image_area .img')[0], {
				}).start({
					'opacity':'1'
				})
			})
			$$('#blog_area .image_area .diary_list img')[0].src='images/btn_watusi01.gif';
			$$('#blog_area .image_area .diary_list img')[1].src='images/btn_lori02.gif';
			var fx = new Fx.Morph($$('#blog_area .text_area')[0], {
				duration: '200',
				transition: Fx.Transitions.Sine.easeOut
			}).start({
				'opacity': '0'
			}).chain( function() {
				$('blog_watusi').setStyle('display','none');
				$('blog_lori').setStyle('display','block');
				$$('#blog_area .title img')[0].src='images/title_lori_diary.gif';
				new Fx.Morph($$('#blog_area .text_area')[0], {
				duration: '200',
				transition: Fx.Transitions.Sine.easeOut
				}).start({
					'opacity':'1'
				})
			})
			$('blog_area').blogStatus = 'lori';
		}else if ($('blog_area').blogStatus == 'lori'){
		
			var fx = new Fx.Morph($$('#blog_area .image_area .img')[0], {
				duration: '200',
				transition: Fx.Transitions.Sine.easeOut
			}).start({
				'opacity': '0' 
			}).chain( function() {
				$$('#blog_area .image_area .img img')[0].src='images/img_diary.jpg';
				new Fx.Morph($$('#blog_area .image_area .img')[0], {
				}).start({
					'opacity':'1'
				})
			})
			$$('#blog_area .image_area .diary_list img')[0].src='images/btn_watusi02.gif';
			$$('#blog_area .image_area .diary_list img')[1].src='images/btn_lori01.gif';
			var fx = new Fx.Morph($$('#blog_area .text_area')[0], {
				duration: '200',
				transition: Fx.Transitions.Sine.easeOut,
				unit: 'px'
			}).start({
				'opacity': '0'

			}).chain( function() {
				$('blog_lori').setStyle('display','none');
				$('blog_watusi').setStyle('display','block');
				$$('#blog_area .title img')[0].src='images/title_watusi_diary.gif';
				new Fx.Morph($$('#blog_area .text_area')[0], {
				duration: '200',
				transition: Fx.Transitions.Sine.easeOut,
				unit: 'px'
				}).start({
					'opacity':'1'
				})
			})

			$('blog_area').blogStatus = 'watusi'
		};
};

window.addEvent('domready',function (){
	$('blog_area').blogStatus = 'watusi';//'lori'

	
	$$('#blog_area .watusi .blog_button').addEvent('click',function (){
		if($('blog_area').blogStatus == 'watusi') return;
		changeDiary();
	})

	$$('#blog_area .lori .blog_button').addEvent('click',function (){
		if($('blog_area').blogStatus == 'lori') return;
		changeDiary();
		
	})
})
*/


/* BIOGRAPHY */

function changeLanguage(nextLanguage) {

	new Fx.Morph($$('.bio .text_area .bio_area'), {
		duration: '200',
		transition: Fx.Transitions.Sine.easeOut
	}).start({
	}).chain( function() {
		
		if(nextLanguage == 0){
			$$('.bio .text_area .language ul li.japanese img')[0].src='images/btn_japanese_02.gif';
			$$('.bio .text_area .language ul li.english img')[0].src='images/btn_english_off.gif';
			$$('.bio .text_area .language ul li.korean img')[0].src='images/btn_korean_off.gif';
		}if(nextLanguage == 1){
			$$('.bio .text_area .language ul li.english img')[0].src='images/btn_english_02.gif';
			$$('.bio .text_area .language ul li.japanese img')[0].src='images/btn_japanese_off.gif';
			$$('.bio .text_area .language ul li.korean img')[0].src="images/btn_korean_off.gif";
		}if(nextLanguage == 2){
			$$('.bio .text_area .language ul li.korean img')[0].src="images/btn_korean_02.gif";
			$$('.bio .text_area .language ul li.english img')[0].src='images/btn_english_off.gif';
			$$('.bio .text_area .language ul li.japanese img')[0].src='images/btn_japanese_off.gif';
		};
		
		$$('.bio .text_area .bio_area').setStyle('display','none');
		$$('.bio .text_area .bio_area')[nextLanguage].setStyle('display','block');
		$$('.bio .text_area .bio_area')[nextLanguage].setStyle('opacity','0');
		new Fx.Morph($$('.bio .text_area .bio_area')[nextLanguage], {
		}).start({
			'opacity':['0','1']
		})
	})

};

var DiscoManager = {
	element: '',
	request: '',
	menuClass: '.disco_menu',
	initialContents: 'discography/COLDFEET/CD/?page=1',
	currentCategory: 'COLDFEET',
	currentContent: 'CD',
	currentPage: 1,
	effect: '',
	detailContainer: '',
	detailRequest: '',
	detailEffect: '',
	initialize: function() {
		this.element = $('disco_contents');
		this.request = new Request.HTML({
			update: this.element,
			onSuccess: function(responceXML, responceHTML) {
				this.appear();
			}.bind(this)
		});
		this.effect = new Fx.Morph(this.element, {
			duration: '400',
			transition: Fx.Transitions.Sine.easeInOut
		});
		this.detailContainer = $('disco_detail');
		this.detailRequest = new Request.HTML({
			update: this.detailContainer,
			onSuccess: function(responceXML, responceHTML) {
				this.appearDetail();
			}.bind(this)
		})
		this.detailEffect = new Fx.Morph(this.detailContainer, {
			duration: '200',
			transition: Fx.Transitions.Sine.easeInOut
		})
		this.loadAndInject(this.initialContents);
		$$(this.menuClass).each(function(item) {
			item.addEvent('click', function() {
				this.currentCategory = item.className.split(' ')[1];
				this.currentContent = this.parseMenuID(item.id);
				this.currentPage = 1;
				// this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + this.currentPage + '.html');
				this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + '/?page=' + this.currentPage);
			}.bind(this));
		}.bind(this));
	},
	loadAndInject: function(contents) {
		this.hideDetail();
		this.hide(function() {
			this.request.get(contents);
		}.bind(this))
	},
	parseMenuID: function(id) {
		var ids = id.split('_');
		return ids[ids.length - 1];
	},
	appear: function(callback) {
		this.effect.start({
			'opacity': [0, 1]
		}).chain(callback);
	},
	hide: function(callback) {
		this.effect.start({
			'opacity': [1, 0]
		}).chain(callback);
	},
	next: function() {
		new Fx.Scroll(window).toElement('disco_area')
		// this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + ++this.currentPage + '.html');
		this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + '/?page=' + ++this.currentPage);
	},
	prev: function() {
		new Fx.Scroll(window).toElement('disco_area')
		// this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + --this.currentPage + '.html');
		this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + '/?page=' + --this.currentPage);
	},
	pageTo: function(page) {
		new Fx.Scroll(window).toElement('disco_area')
		this.currentPage = page;
		// this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + this.currentPage + '.html');
		this.loadAndInject('discography/' + this.currentCategory + '/' + this.currentContent + '/?page=' + this.currentPage);
	},
	loadDetail: function(index) {
		if (this.detailContainer.getStyle('opacity') != 0)
			this.hideDetail(function() {
				// this.detailRequest.get('discography/' + this.currentCategory + '/' + this.currentContent + this.currentPage + '/' + index + '.html');
				this.detailRequest.get('discography/' + this.currentCategory + '/' + this.currentContent + '/' + index + '/');
			}.bind(this))
		else
			// this.detailRequest.get('discography/' + this.currentCategory + '/' + this.currentContent + this.currentPage + '/' + index + '.html');
				this.detailRequest.get('discography/' + this.currentCategory + '/' + this.currentContent + '/' + index + '/');
	},
	appearDetail: function(callback) {
		this.detailEffect.start({
			'opacity': [0, 0.86],
			'display': 'block'
		}).chain(callback);
	},
	hideDetail: function(callback) {
		this.detailEffect.start({
			'opacity': [0.86, 0]
		}).chain(function() {
			this.detailContainer.setStyle('display', 'none');
			if (callback != undefined)
				callback();
		}.bind(this));
	}
}
window.addEvent('domready', function() {
	DiscoManager.initialize();
});



/* 外部PHP読み込み */
/*
window.addEvent('domready', function() {
	var discocontents = $('blog_watusi')
	var myHTMLRequest = new Request.HTML({update: discocontents}).get('diary_feed.php?member=watusi');
})
window.addEvent('domready', function() {
	var discocontents = $('blog_lori')
	var myHTMLRequest = new Request.HTML({update: discocontents}).get('diary_feed.php?member=lori');
})
*/



/* ページ遷移 */

function toInfo (){
	new Fx.Scroll(window).toElement($('info_area'));
};

window.addEvent('domready', function() {
	$$('.info_btn').addEvent('click', function() {
		toInfo();
	});
});


function toSche (){
	new Fx.Scroll(window).toElement('sche_area');
};

window.addEvent('domready', function() {
	$$('.sche_btn').addEvent('click', function() {
		toSche();
	});
});


function toBio (){
	new Fx.Scroll(window).toElement('bio_area');
};

window.addEvent('domready', function() {
	$$('.bio_btn').addEvent('click', function() {
		toBio();
	});
});

function toDisco (){
	new Fx.Scroll(window).toElement('disco_area');
};

window.addEvent('domready', function() {
	$$('.disco_btn').addEvent('click', function() {
		toDisco();
	});
});

function toContact (){
	new Fx.Scroll(window).toElement('contact_area');
};

window.addEvent('domready', function() {
	$$('.contact_btn').addEvent('click', function() {
		toContact();
	});
});


function toTop (){
	new Fx.Scroll(window).toElement($('top'));
};

window.addEvent('domready', function() {
	$$('.totop').addEvent('click', function() {
		toTop();
		menubar_stop()
	});
});




/* follow_menu */
document.addEvent('mousemove', function(mouseEvent) {
	var x = mouseEvent.page.x;
	var y = mouseEvent.page.y;
	if ($('menubar').close == undefined) return;
	if (hitTest(x, y, $('menubar'))) {
		if ($('menubar').openStatus != 'open') {
			$('menubar').openStatus = 'open';
			$('menubar').open();
		}
	} else {
		if ($('menubar').openStatus != 'close') {
			$('menubar').openStatus = 'close';
			$('menubar').close();
		}
	}
})
window.addEvent('domready', function() {
	$('menubar').openStatus = 'close';

	$('menubar').effects = {
		open:  new Fx.Morph($('menubar'), {duration: 400, transition: Fx.Transitions.Sine.easeOut}),
 		close: new Fx.Morph($('menubar'), {duration: 400, transition: Fx.Transitions.Sine.easeOut})
	}
	// $('menubar').addEvent('mouseover', function() {
	//  this.open();
	// })
	// $('menubar').addEvent('mouseout', function() {
	// 	this.close();
	// })
/*	$('menubar').open = function() {
		this.effects.close.cancel();
		this.effects.open.start({
			width: window.getScrollSize().x + 'px'
		}).chain(function() {
			new Fx.Morph($('open_button'), {
				duration: '300',
				transition: Fx.Transitions.Sine.easeOut,
				unit: 'px'
			}).start({
				'opacity': ['1','0']
			}).chain(function() {
				$('open_button').setStyle('display', 'none');
				$('buttons').setStyle('display', 'block');
				$('buttons').setStyle('opacity', '0');
				new Fx.Morph($('buttons'), {
					duration: '300',
					transition: Fx.Transitions.Sine.easeInOut,
					unit: 'px'

				}).start({

					'opacity': ['0','1'] 
				})
			})
		})
	}
	$('menubar').close = function() {
		this.effects.open.cancel();
		this.effects.close.start({
			
		}).chain(function() {
			new Fx.Morph($('buttons'), {
				duration: '300'
			}).start({
				opacity: ['1','0']
			}).chain(function() {
				$('buttons').setStyle('display', 'none');
				new Fx.Morph($('menubar'), {
				duration: '300'
				}).start({
				'width': '73px'
				}).chain(function() {
					$('open_button').setStyle('display', 'block');
					$('buttons').setStyle('display', 'none');
					$('open_button').setStyle('opacity', '0');
					new Fx.Morph($('open_button'), {
						duration: '500',
						transition: Fx.Transitions.Sine.easeInOut
					}).start({
						'opacity': ['0','1']
					})
				})
			})
		})
	};
*/
})


/*
window.addEvent('domready', function() {
	$('follow_menu').openStatus = 'close';// 'open'
	$('follow_menu').addEvent('mouseover', function() {
		if (this.openStatus == 'close') {
			open_menu();
			this.openStatus = 'open';
		}
	});
	$('follow_menu').addEvent('mouseout', function() {
		if (this.openStatus == 'open') {
			close_menu();
		}
	});
	
});
function open_menu(){
		var menu_tab = $('follow_menu');
		var opener = $('open_button');
		var contents = $('buttons');

		var fx = new Fx.Morph(opener, {
			duration: '400',
			transition: Fx.Transitions.Sine.easeOut,
			unit: 'px'

		}).start({
			'opacity': [1,0]


		}).chain(function() {
			new Fx.Morph(menu_tab, {
			duration: '1000',
			transition: Fx.Transitions.Sine.easeInOut,
			unit: '%'

			}).start({

			'width': ['7','90']


				}).chain(function() {
					opener.setStyle('display', 'none');
					contents.setStyle('display', 'block');
					contents.setStyle('opacity', '0');
					new Fx.Morph(contents, {
					duration: '500',
					transition: Fx.Transitions.Sine.easeInOut,
						unit: 'px'

					}).start({

						'opacity': [0,1] 

				})
		})
	})
};
function close_menu(){
		var menu_tab = $('follow_menu');
		var opener = $('open_button');
		var contents = $('buttons');

		new Fx.Morph(contents, {
			duration: '1000',
			transition: Fx.Transitions.Sine.easeOut,
			unit: 'px'

		}).start({
			'opacity': [1,0]


		}).chain(function() {

			contents.setStyle('display', 'none');
			opener.setStyle('display', 'block');
			opener.setStyle('opacity', '0');
			new Fx.Morph(menu_tab, {
			duration: '500',
			transition: Fx.Transitions.Sine.easeInOut,
			unit: '%'

			}).start({

					'width': 7


				}).chain(function() {
					
					$('follow_menu').openStatus = 'close';
					new Fx.Morph(opener, {
					duration: '1000',
					transition: Fx.Transitions.Sine.easeInOut,
					unit: '%'

					}).start({

				'opacity': [0,1]

				})
				
		})
	})
};
*/





/* menuber_move */
/*
var menubarStatus = true;
function fixMenuPosition(force) {
	if (force == false && !menubarStatus) return;
	var ikkoshita;
	var ikkoshitaIndex;
	$$('.subtitle').each(function(item, index) {
		if (ikkoshita == undefined) {
			if ( window.getScroll().y - 100 < item.getCoordinates().top) {
				ikkoshita = item;
				ikkoshitaIndex = index;
			} else {
				ikkoshitaIndex = 5;
			}
		}
	});

	new Fx.Morph($('menubar'), {
		transition: Fx.Transitions.Sine.easeInOut,
		duration: '500',
		onStart: function() {
			menubarStatus = false;
		},
		onComplete: function() {
			menubarStatus = true;
		}
	}).start({
		'top': $$('.subtitle')[ikkoshitaIndex].getCoordinates().top + 47 + 'px'
	});
}
window.addEvent('scroll', function() {
	fixMenuPosition(false);
});
*/

var followStatus = true;
var followID;
window.menuFx;
function menubar_follow(){
	var followTargetY = window.getScroll().y +25;
	if (followID != undefined) clearTimeout(followID);
	followID = setTimeout(function() {
		if (!menubarStatus) return;
		window.menuFx = new Fx.Morph($('menubar'), {
			transition: Fx.Transitions.Sine.easeInOut,
			deration:'0',
			unit: 'px'
		}).start({
			'top': followTargetY
		});
		
		
	}.bind(this), 500);

/*
	
	if( followStatus == false ) return ;

	new Fx.Morph($('menubar'), {
		transition: Fx.Transitions.Sine.easeInOut,
		deration:'100',
		unit: 'px',
		onStart: function() {
			followStatus = false;
		},
		onComplete: function() {
			followStatus = true;
		}
	}).start({
		'top': window.getScroll().y 
	});
*/
};

function menubar_stop(){
	if(window.menuFx != undefined)
	window.menuFx.pause()
	$('menubar').setStyle('top','700px');
};

var menubarStatus = true;

window.addEvent('scroll', function() {
	if ( window.getScroll().y > $$('.subtitle')[0].getCoordinates().top) {
		menubarStatus = true;
		menubar_follow();
	} else {
		menubarStatus = false;
		menubar_stop();
	};
	
});
