(function($){
	$(document).ready(function(){
		addBackground();
		toggle_contents();
		resize();
	});
	$(window).resize(function(){
		resize();
	});

	function toggle_contents () {
		var $content = $('#content');
		var height = $content.height();
		var $wrap = $('#wrap');

		$content.hide();

		$wrap.hover(function() {
			$content.stop().animate({
				height: height
			}, 200, function() {
			});
		}, function() {
			$content.stop().animate({
				height: 0,
				borderWidth: 0
			}, 400, function() {
			});
		});
	};

	function addBackground () {
		var images = $('#key_visual ul li a');
		var randomImageIndex = Math.round(Math.random()*(images.length-1));

		var img = new Image();

		$(img)
			.load(function () {
				$('#key_visual').append(this);
				resize();
			})
			.error(function () {
				// notify the user that the image could not be loaded
			})
			.attr('src', images[randomImageIndex]);
	}

	function resize(){
		$('#key_visual > img')
			.width($(window).width())
			.height($(window).width() * 0.66);

		if($('#key_visual > img').height() <= $(window).height()){
			$('#key_visual > img')
				.height($(window).height())
				.width($(window).height() * 1.5);
		}
	}
})(jQuery);

