var NB_COLS = 3;
var COL_WIDTH = 220;
var SPACE = 9;
var PADDING = 9;

var selected = null;
var move = false;

$(document).ready(function() {
	$('.work_content').hide();
	$('.big_visu').css('cursor','pointer');
	$('.big_visu').click(function() {
		if (!move) {
			move = true;
			if (!selected || $(this).parent().attr('id') != selected.parent().attr('id')) {
				if (selected) {
					selected.next().slideToggle();
				}
				selected = $(this);
				$(this).next().slideToggle(500, function() {
					$.scrollTo(selected, 500);
					move = false;
				});
			}
			else {
				$(this).next().slideToggle(500, function() {
					$.scrollTo(selected, 500);
					selected = null;
					move = false;
				});
			}
		}
	});
	$('body').append('<div id="dust"></div>');
	$(window).resize(function() {
		if ($('body').width() <= $('#container').width()) {
			$('#dust').hide();
		}
		else {
			$('#dust').show();
		}
	});
	$('#more_less_link').click(function() {
		if (!move) {
			if ($('#more').css('display') == 'none') {
				$(this).css('background-image', 'url(/wp-content/themes/1md/images/less.gif)');
				move = true;
				$('#more').slideDown(500, function() {
					move = false;
				});
			}
			else {
				$(this).css('background-image', 'url(/wp-content/themes/1md/images/more.gif)');
				move = true;
				$('#more').slideUp(500, function() {
					move = false;
				});
			}
		}
		return false;
	});
	if ($('#choices').length > 0) {
		$.get('/results.php', function(data) {
			var votes = data.split('|');
			var i = 0;
			$('#choices li').each(function() {
				var word = "vote";
				if (parseInt(votes[i]) > 1) word += "s";
				$(this).append(' <span>'+votes[i]+' '+word+'</span>');
				i++;
			});
		});
		if (window.location.search === '?already') {
			alert('You have already voted! What a cheater!');
		}
	}
	if ($('#contact_info').length > 0) {
		$('#contact_info').mouseover(function() {
			$(this).css('background-position','0 -267px');
		});
		$('#contact_info').mouseout(function() {
			$(this).css('background-position','0 0');
		});
	}
	if ($('.post').length > 0) {
		if ($('#comments').length > 0) {
			$('.post').css({
				float: 'left',
				margin: '0 9px 9px 0'
			});
		}
		else {
			var i = 0, left, colsh = new Array(), max = 0;
			for (var j = 0; j < NB_COLS; j++) {
				colsh[j] = 0;
			}
			$('.post').each(function() {
				left = (i%NB_COLS)*COL_WIDTH + (i%NB_COLS)*SPACE;
				$(this).css({position: 'absolute', top: colsh[i%NB_COLS], left: left});
				colsh[i%NB_COLS] += $(this).height() + (2*PADDING) + SPACE;
				if (i == 0) {
					colsh[1] = colsh[0];
					i++;
				}
				i++;
			});
			for (var k = 0; k < colsh.length; k++) {
				if (colsh[k] > max)
					max = colsh[k];
			}
			$('#content').css('height', max);
		}
		
		$('.post').jFade({
			trigger: 'mouseover',
			property: 'background',
			start: '181818',
			end: '242424',
			steps: 12,
			duration: 12
		}).jFade({
			trigger: 'mouseout',
			property: 'background',
			start: '242424',
			end: '181818',
			steps: 12,
			duration: 12
		});
		
	}
});
