$(document).ready(function() {
	$('nav li.dropdown').hoverIntent(
	function() {
		$(this).find('ul').slideDown();
	},
	function() {
		$(this).find('ul').slideUp();
	});
	
	var margin = 100;
	var margin_top = 30;
	var works = $('a div.item').not('.secret');
	//var height = $('div.main').height() - $('header').outerHeight();
	var height = 300;
	var width = $('#content').width() - (2*margin);
	var count = works.length;
	var box_width = width/count;
	var boxes = new Array();
	for (i=0;i<count;i++) {
		boxes.push(i);
	}	
	boxes.sort(function() {return 0.5 - Math.random()});
	works.each(function(index) {
		//var bottom = margin + margin_top;
		//bottom = bottom.toString() + "px";
		//$(this).css({bottom: 0});
		$(this).fadeIn('fast');
		var item_height = $(this).height();
		var item_width = $(this).width();
		var top = Math.floor(Math.random()*(height-item_height));
		var left = Math.floor(Math.random()*(box_width-item_width));
		var secret_left = Math.floor(Math.random()*(item_width-20));
		left = margin + (boxes[index]*box_width)+left;
		top = margin_top + top;
		var secret = $('a div.secret:eq(' + index + ')');
		if (secret.length) {
			var secret_top = top + 200;
			if (secret_top>height) {
				secret_top = secret_top-height;
			}
			secret_left = left + secret_left;
			secret_top = secret_top.toString() + "px";
			secret_left = secret_left.toString() + "px";
			secret.parent('a').css({left:secret_left, top:secret_top});
			secret.show();
		}
		top = top.toString() + "px";
		left = left.toString() + "px";
		var link = $(this).parent('a');
		link.animate({left: left, top: top});
	});
	$('div.item').not('#ruler').hover(
	function () {
		$(this).find('span').show();
	},
	function () {
		$(this).find('span').hide();
	});
	
	$('div#ruler').hover(
	function() {
		$(this).children('div').slideDown();
	},
	function () {
		$(this).children('div').slideUp();
	});
	
	$('div.lightbox').overlay({mask: {
		color: '#ebecff',
		loadSpeed: 200,
		opacity: 0.9
	}});
}); 

