function GetRandom(min, max) {
	if(min > max) {
		return(-1);
	}
	if(min == max) {
		return(min);
	}
	return (min + parseInt(Math.random() * (max - min + 1)));
}

function in_array(a,p){
 for (i=0;i<a.length;i++)
  if (a[i] == p) return true
 return false
}

$(function() {

		jQuery.each(jQuery.browser, function(i, val) {
			if(false) {
				
		// box positioning
		var max_width = $(window).width() - 140; // wegen body padding and jitter
		if (max_width > 750) max_width = 750;

		var max_height = $(window).height() - 400; // wegen body padding and header and box height
		if (max_height > 250) max_height = 250;

		$('#content').css('min-height', max_height + 250);

		var space = 10; // overlapping space
		var jitter = 3; // random amount of variation
		var width = 0;
		var count = 0;
		$('.box').each(function() { // look how many and how wide they are
			width += parseInt($(this).css('width')); //width doesn't work in IE when width is not set in CSS
			count++;
		});
		var gap = (width - max_width) / count;
		var last_left = 0;
		$('.box').each(function() { // first position
			$(this).css('left', (last_left + GetRandom(0,jitter) * space) + 'px');
			last_left += parseInt($(this).css('width')) - gap;
			$(this).css('top', ((GetRandom(0,count) * max_height / count)) + 'px');
			$(this).css('zIndex', GetRandom(0,count));
			$(this).show();
		});
		$('.box').each(function() { // change the order
			if (GetRandom(0, 1))
				$(this).prependTo( $(this).parent() );
		});
		var last_left = 0;
		var dur = 1000; // ms for animation
		var tops = new Array();
		var top = 0;
		$('.box').each(function() { // animate everything
			$(this).animate({left: (last_left + GetRandom(0,jitter) * space) + 'px'}, { queue:false, duration:dur });
			last_left += parseInt($(this).css('width')) - gap;
			while (in_array(tops, top)) //use every top position
				top = GetRandom(0,count - 1);
			tops.push(top);
			$(this).animate({top: ((top * max_height / (count - 1))) + 'px'}, { queue:false, duration:dur });
		});
			}
		});

	var zIndex = 5;
	$('.box').hover(function() {
		$(this).css('zIndex', ++zIndex);
	});

		var old_width;
		var old_height;
		var old_left;
		var old_top;

	$('.box.video').click(function() {
		if (!$(this).hasClass('open')) {
			old_width = parseInt($(this).css('width'));
			old_height = parseInt($(this).css('height'));
			old_left = parseInt($(this).css('left'));
			old_top = parseInt($(this).css('top'));
			var win_width = $(window).width();
			if (win_width > 960) win_width = 960;
			
			// old width = 480
			var new_width;
			if ($(this).hasClass('orange'))
				new_width = 480;
			else
				new_width = 700;
			var new_height = parseInt($(this).find('.big_box_content').attr('id')) + 30;
			$('#fog').css('zIndex', zIndex).fadeIn();
			$(this).css('zIndex', ++zIndex);
			$(this).animate({left: ((win_width - new_width) / 2) + 'px'}, { queue:false, duration:'fast' });
			$(this).animate({top: (0) + 'px'}, { queue:false, duration:'fast' });
			$(this).animate({width: (new_width) + 'px'}, { queue:false, duration:'fast' });
			$(this).animate({height: (new_height) + 'px'}, { queue:false, duration:'fast' });
			$(this).find('.big_box_content').show();
			$(this).find('.box_content').hide();
			$(this).addClass('open');
		}
	});
	
	$('#fog').click(function() {
		var box = $('.box.video.open');
		box.animate({left: (old_left) + 'px'}, { queue:false, duration:'fast' });
		box.animate({top: (old_top) + 'px'}, { queue:false, duration:'fast' });
		box.animate({width: (old_width) + 'px'}, { queue:false, duration:'fast' });
		box.animate({height: (old_height) + 'px'}, { queue:false, duration:'fast' });
		box.find('.big_box_content').hide();
		box.find('.box_content').show();
		box.removeClass('open');
		$('#fog').fadeOut();
	});
	
	$('.box').click(function() {
		var href = $(this).find('a').attr('href');
		if (!$(this).hasClass('video')) window.location = href;
	});

});
