$(function() {
	var $loader		= $('#loading');
	var $menu		= $('#menu');
	var $currImage 	= $('#bgimages').children('img:first');
	var maximg = $('.thumb').length;
	var current = 1;
	$.paused = false;

	$(window).bind("load", function(){
		$currImage.css('display', 'none');
		$loader.fadeOut(800);
		$currImage.fadeIn(1000);
		resizeimg();
		$('a.iframe', '#schedule').iframe();
		setTimeout(function(){
			$menu.animate({'left':'0px'},500);
		},
		2000);
	});

	$(document).ready(function() {
		resizeimg();
		$("a#xmasimg").fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});

	});

	$(window).bind("resize", function(){
		resizeimg();
	});

	slideshow_interval = setInterval(nextslide, 8000);

	$menu.find('li').live('click',function(){
		var $this = $(this);
		if ($this.attr('class') == "m_profile"){
			$('#overlay').fadeIn(500);
			$('#profile').fadeIn(1000);
			$menu.stop().animate({'left':'-200px'},200);
			clearInterval(slideshow_interval);
		}
		if ($this.attr('class') == "m_access"){
			$('#overlay').fadeIn(500);
			$('#access').fadeIn(1000);
			$menu.stop().animate({'left':'-200px'},200);
			clearInterval(slideshow_interval);
		}
		if ($this.attr('class') == "m_schedule"){
			$('#overlay').fadeIn(500);
			$('#schedule').fadeIn(1000);
			$menu.stop().animate({'left':'-200px'},200);
			clearInterval(slideshow_interval);
		}
	}).bind('mouseenter',function(){
		$(this).children('span').stop().animate({'left':'0'},200);
	}).bind('mouseleave',function(){
		$(this).children('span').stop().animate({'left':'-200px'},200);
	});

	$('.contentbox').find('.closebox').live('click', function(){
		$menu.stop().animate({'left':'0px'},200);
		$('#overlay').fadeOut(1000);
		$(this).parent().fadeOut(500);
		if(!($.paused)) slideshow_interval = setInterval(nextslide, 8000);
	});

	function resizeimg() {
		var nowimg = $('#bgimages');
	  	return nowimg.each(function() {
			var ratio = 1000 / 1600;
			var imagewidth = nowimg.width();
			var imageheight = nowimg.height();
			var bw = $(window).width();
			var bh = $(window).height();
			var offset;

			if ((bh / bw) > ratio){
			    nowimg.height(bh);
			    nowimg.width(bh / ratio);
			    nowimg.children().height(bh);
			    nowimg.children().width(bh / ratio);
			} else {
			    nowimg.width(bw);
			    nowimg.height(bw * ratio);
			    nowimg.children().width(bw);
			    nowimg.children().height(bw * ratio);
			}
			nowimg.children().css('left', (bw - nowimg.width())/2);
			nowimg.children().css('top', (bh - nowimg.height())/2);
		});
	};

	function nextslide(){
		$loader.show();
		current++;
		if(current > maximg)current = 1;
		var $currImage = $('#bgimages').children('img:first');
		$currImage.removeClass('activeimg');
		$currImage.addClass('oldimg');
		$('<img class="activeimg" />').insertBefore($currImage);
		resizeimg();
		$('.activeimg').attr('src',$('.thumb').get(current - 1).alt);
		$('#bgimages').children('img:first').load(function(){
			$loader.fadeOut(800);
			$('.oldimg').fadeOut(1000,function(){
				$('.oldimg').remove();
			});
		});
	}
});

