$(function() {
	/*
	$('a.fancybox').each(function() {
		$(this).fancybox({
			'imageScale':         true,
			'zoomOpacity':        true,
			'overlayShow':        false,
			'zoomSpeedIn':        500,
			'zoomSpeedOut':       500,
			'hideOnContentClick': true
		});
	});
	*/
	
	if(!$.cookie('clouds') === 'animated' && !$.cookie('clouds') === 'static')
		$.cookie('clouds', null);
	
	if($.cookie('clouds') === null)
		$.cookie('clouds', 'animated', { expires: 365, path: '/' });
	
	$('body').append('<div id="cloud-control"><a href="#">Static clouds please!</a></div>');
	$.data($('#cloud-control a')[0], 'otherLinkText', 'Animated clouds please!');

	if($.cookie('clouds') === 'static') {
		switchCloudLinkText();
	}
	
	$('#cloud-control')
		.css({
			'top': '-21px',
			'opacity': '0.4'
		})
		.bind('mouseenter', function() {
			$(this).stop(true, true).animate({ top: '0px', opacity: '1.0' });
		})
		.bind('mouseleave', function() {
			$(this).animate({ top: '-21px', opacity: '0.4' });
		})
	.children('a')
		.click(function() {
			toggleCloudBehaviour();		
			return false;
		});

	startClouds();
	
	function startClouds() {
		if($('img.cloud').length === 0) {
			var clouds = new Array();
			var i;
			
			for(i = 0; i < 6; i++)
				clouds[i] = $('<img id="cloud' + i + '" class="cloud" src="/img/cloud' + i + '.png" />');
			
			$.each(clouds, function() { $(this).appendTo('body') });
			
			if($.cookie('clouds') === 'animated') {
				$('img.cloud').load(function() {
					var cloud = this;
					if($(cloud).is(':visible')) {
						$(cloud)
							.css('top',  Math.ceil(Math.random() * ($(window).height() - $(cloud).height() - $('#grass').height())))
							.css('left', $(window).width() + $(cloud).width())
							.hide();
						$.data(cloud, 'cloudTimer', setTimeout(function() { animateCloud(cloud) }, Math.random() * $(window).width() * 60));
					}
				});
			} else if($.cookie('clouds') === 'static') {
				$('img.cloud').load(function() {
					var cloud = this;
					$(cloud)
						.hide()
						.css('top',  Math.ceil(Math.random() * ($(window).height() - $(cloud).height() - $('#grass').height())))
						.css('left', Math.ceil(Math.random() * ($(window).width() - $(cloud).width())))
						.fadeIn('slow');
				});
			}
		}
	}
	
	function animateCloud(cloud) {
		$(cloud)
			.fadeIn('slow')
			.animate(
				{ 'left': '-' + $(cloud).width() * 2 + 'px' }, 
				$(window).width() * 60, 
				'linear', 
				function() {
					var cloud = this;
					$(cloud)
						.css('top',  Math.ceil(Math.random() * ($(window).height() - $(cloud).height() - $('#grass').height())))
						.css('left', $(window).width() + $(cloud).width())
						.fadeOut('slow');
					$.data(cloud, 'cloudTimer', setTimeout(function() { animateCloud(cloud) }, Math.random() * $(window).width() * 60));
				}
			);
	}

	function toggleCloudBehaviour() {
		var $clouds = $('img.cloud');
		var wasAnimating = false;

		if($clouds.length !== 0) {
			if($.cookie('clouds') === 'animated') {
				$clouds.each(function() { clearTimeout($.data(this, 'cloudTimer')) }).stop(true);
				wasAnimating = true;
			}
			$.cookie('clouds', wasAnimating ? 'static' : 'animated', { expires: 365, path: '/' });
			$clouds.fadeOut('slow', function() { 
				$(this).remove();
				startClouds();
			});
			switchCloudLinkText();
		}
	}
	
	function switchCloudLinkText() {
		var $cloudLink = $('#cloud-control a');
		var tmpText = $cloudLink.text();
		$cloudLink.text($.data($cloudLink[0], 'otherLinkText'));
		$.data($cloudLink[0], 'otherLinkText', tmpText);
	}
});