$(document).ready(init);

var canVideo;
var hash;
var winWidth, winHeight;

function init() {
	hash = window.location.hash;

	hideAll();
	initVideo();
	initView();
	initEvent();

	dispatch();
}

function initVideo() {
	canVideo = 1;
	
	if (navigator.userAgent.indexOf('iPhone') > 0) {
		canVideo = 0;
	} else if (navigator.userAgent.indexOf('iPad') > 0) {
		canVideo = 0;
	} else if (navigator.userAgent.indexOf('Android') > 0) {
		canVideo = 0;
	} else if (navigator.userAgent.indexOf("MSIE 6.") > 0) {
		canVideo = 0;
	} else if (navigator.userAgent.indexOf("MSIE 7.") > 0) {
		canVideo = 0;
	} else if (navigator.userAgent.indexOf("MSIE 8.") > 0) {
		canVideo = 0;
	}

	if (canVideo) {
		$('#poster').remove();
	} else {
		$('#video').remove();
		$('#navigation-bg').remove();
	}

	fitToScreen();
}

function initView() {
	$('#upcoming').find('.upcoming-article').each(function(i, e) {
		$('#upcoming-select-title').append('<option>' + $(e).find('.date').get(0).innerHTML + ' 「' + $(e).find('h3').get(0).innerHTML + '」</option>');
	});
}

function fitToScreen() {
	winWidth = $(window).width();
	winHeight = $(window).height();

	if (canVideo) {
		if ((winWidth / 4) > (winHeight / 3)) {
			$('#video').css({ width: winWidth });
		} else {
			$('#video').css({ height: winHeight });
		}
	} else {
		if ((winWidth / 4) > (winHeight / 3)) {
			$('#poster').css({ width: winWidth });
		} else {
			$('#poster').css({ height: winHeight });
		}
	}

	var cWidth = 800;
	var sideMargin = (winWidth - cWidth);
	var leftMargin = sideMargin > 360 ? sideMargin / 2 : 180;

	$('#navigation').css({ height: winHeight });
	$('#content-bg').css({ height: winHeight, left: leftMargin + 'px' });
	$('#content').css({  height: winHeight, width: winWidth + 'px' });
	$('.c-body').css({ left: leftMargin + 'px' });
	$('#close').css({ left: leftMargin + cWidth - 32 + 'px' });
}

function initEvent() {
	$(window).resize(fitToScreen);

	$('h1').click(hideAll);
	$('#close').click(hideAll);
	$('#nav-upcoming').click(function() { hideAll(); show('#upcoming'); });
	$('#nav-discography').click(function() { hideAll(); show('#discography'); });
	$('#nav-lyrics').click(function() { hideAll(); show('#lyrics'); });
	$('#nav-about').click(function() { hideAll(); show('#about'); });
	$('#nav-contact').click(function() { hideAll(); show('#contact'); });
	$('#nav-past').click(function() { hideAll(); show('#past'); });
	
	$('.back-to-top').click(function() { $('#content').scrollTo({ top: '0px', left: '0px' }, 500); });

	if ($('#navigation-bg')) {
		$('#navigation').mouseenter(function() { $('#navigation-bg').show(0).animate({ width: 180, opacity: 0.4 }, 200); });
		$('#navigation').mouseleave(function() { $('#navigation-bg').animate({ width: 0, opacity: 0 }, 100).hide(0); });
	}

	// discography	
	$('.link-discography').each(function(i, e) {
		$(e).click(function() {
			var a = $('#discography').find('.discography-article').get(i);
			if (a) {
				$('#content').scrollTo(a, 500);
			}
		});
	});
	// lyrics

	$('.link-lyrics').each(function(i, e) {
		$(e).click(function() {
			var a = $('#lyrics').find('.lyrics-article').get(i);
			if (a) {
				$('#content').scrollTo(a, 500);
			}
		});
	});

	
}

function dispatch() {
	if (hash) {
		if ($('#content').find(hash).length > 0) {
			show(hash);
		}
	} else {
		show('#top');
	}
}

function hideAll() {
	$('#content-bg').animate({ height: 0, opacity: 0 }, 200).hide(0);
	$('.c-body').animate({ height: 0, opacity: 0, top: '-' + winHeight + 'px' }, 200).hide(0);
	$('#close').animate({ opacity: 0, top: '-' + winHeight + 'px' }, 200).hide(0);

	window.location.hash = '';
}

function show(selector) {
	$('#content-bg').show(0).animate({ height: winHeight, opacity: 0.4 }, 300);
	$(selector).show(0).animate({ top: 0, height: winHeight, opacity: 1 }, 300);
	$('#close').show(0).animate({ top: '16px', opacity: 1 }, 300);

	window.location.hash = selector;
}
