function rand( min, max ) {
	var argc = arguments.length;
	if (argc == 0) {
		min = 0;
		max = 2147483647;
	} else if (argc == 1) {
		throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
	}
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

$(document).ready(function(){
	$(function(){ $(".textblock").hide(); }); //Hide all blocks on load
	$('.textblock a.minbox').click(function() {
		$(this).parents('.textblock').children('.blockbody').toggle(150);
	});
	$(".closebox").click(function(){
		$(this).parents('.textblock').toggle(150);
		$(this).parents('.textblock').children('.blockbody').show();
		return false;
	});
	$("#flashsquare").click(function(){ $("#block1").show(150); return false; });
	$(".showblock2").click(function () { $("#block2").show(150); return false; });
	$(".showblock3").click(function () { $("#block3").show(150); return false; });
	$(".showblock4").click(function () { $("#block4").show(150); return false; });
	$("#partytime").click(function(){
		$('td').each(function() {
		     $(this).css({backgroundColor:'rgb('+rand(1,255)+','+rand(1,255)+','+rand(1,255)+')'});
		});
	});

	$(".textblock").draggable({ handle:'h2', grid: [15,15], stack: '.textblock', containment: 'table#blocks' });
	

	$("#reset").click(function(){
		$('td').css({backgroundColor:'#000'});
	});
	var isDown = false;   // Tracks status of mouse button
	$(document).mousedown(function() {
		isDown = true;      // When mouse goes down, set isDown to true
	})
	.mouseup(function() {
		isDown = false;    // When mouse goes up, set isDown to false
	});	
	$('td').mouseover(function(){
		if(isDown) { $(this).css({backgroundColor:'#000'}); } 
		else { $(this).css({backgroundColor:'rgb('+rand(1,255)+','+rand(1,255)+','+rand(1,255)+')'}); }		
	});
});
