/*global window, $ */
/*jslint
	white: true, onevar: true, undef: true, nomen: true, eqeqeq: true,
	plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true
*/

$(function () {

	var melt, fancy;
	
	// If you came looking for this code because you noticed
	// the behavior, let me know.  It'll make my day.
	melt = function () {
	
		var random, animate, 
			color, cookie, duration;
		
		cookie = 'backgroundColor';
		duration = 60000;
	
		random = function () {
			var i, c;
			c = [];
			for (i = 0; i < 3; i += 1) {
				c.push(64 + Math.floor(Math.random() * 64));
			}
			return 'rgb(' + c.join(',') + ')';
		};
		
		animate = function () {
			$('body').animate({
				'backgroundColor': random()
			}, duration, 'swing', animate);
		};
		
		color = $.cookie(cookie);
		if (color) {
			$('body').css({
				'backgroundColor': color
			});
		}
	
		$(window).unload(function () {
			var color;
			color = $('body').css('backgroundColor');
			$.cookie(cookie, color, {path: '/'});
		});
	
		animate();

	};
	
	fancy = function () {
	
		// add rel attributes
		$('.gallery').each(function () {
			$('a', this).attr('rel', this.id);
		});
	
		// apply the fancybox
		$('.gallery a').fancybox({
			overlayOpacity: 0.5
		}).click(function () {
			this.blur();
		});
	};

	melt();
	fancy();

});
