var cachebreaker = "124124213";
var debug = false;

var base_offset = new Array();
var offset = new Array();

var logo_selector = "#l0, #l1, #l2"
var lc = 3;
var move_amount = 6000;

function debug(s) {
	if (typeof console != "undefined") {
		console.log(s);
	}
}

$(document).ready(function() {
	if (debug) console.log("Document ready");
	
//	if ($("#l0")) {
//		if (debug) console.log("Setting up logo animation");
//		base_offset = $("#logo").offset();
//		offset = new Array();
//		$(logo_selector).each(function(i) {
//			offset[i] = $(this).offset();
//		});
//	}
	
//	animate();
	
//	setInterval("animate_lights()", 6000);
	
	
	$("a[rel=darken]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'overlayShow'		: true,
		'overlayColor'		: '#FFF',
		'overlayOpacity'	: '0.3',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});
	
	$("a[rel=gallery]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'overlayShow'		: true,
		'overlayColor'		: '#FFF',
		'overlayOpacity'	: '0.3',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});
   
});

function animate_lights() {
	if (debug) console.log("animating lights");
	$("div.light").each(function(i){
		$(this).delay(i * 1000).animate({opacity: 0.0}, 2000, 'linear',
			function(){
				$(this).animate({opacity: 1.0}, 2000, 'linear');
			});
	});
}

function animate() {
	var x = (Math.random() * move_amount) - (move_amount / 2);
	var y = (Math.random() * move_amount) - (move_amount / 2);
	
	var xmod = 0.01;
	var ymod = 0.01;
//	if (Math.random() > 0.9) xmod = 0.1

	$(logo_selector).each(function(i) {
		var ni = i;
		var delay = i * 750;
		if (Math.random() > 0.9) delay += 3000;
		if (Math.random() < 0.1) delay = 0;

		var tx = offset[i].left - x * xmod;
		var ty = offset[i].top - y * ymod;
		
		if (i == 2) {
			tx = offset[i].left;
		} else {
			if (Math.random() > 0.5) {
				tx = offset[i].left;
			} else {
				ty = offset[i].top;
			}
		}
		
		tx = tx - base_offset.left;
		ty = ty - base_offset.top;
//		$(this).css("left", tx);
//		$(this).css("top", ty);
		$(this).delay(delay + 1000).animate(
			{left: tx, top: ty},
			1000,
			'linear',
			function() {
				if (i == lc - 1) {
					animate();
				}
			}
		);
	});
}
