$(function() {
	var rangeX = 100;
	var rangeY = 100;
	var gravity = .001;
	
	$(window).load(function() {
		$('.wrapper').fadeIn('slow', function() {});	
		
		setTimeout(start, 500);
		$('img.plug').animate({left: 0}, 1300);
	});
	
	$('.floating_data_communications_line').data('center', -80).hide();
	$('.floating_data_communications_line').data('travel', 300);
	$('.floating_data_communications_line').data('orientation', 'horizontal');
	
	$('.floating_audio_visual_integration_line').data('center', -100).hide();
	$('.floating_audio_visual_integration_line').data('travel', 300);
	$('.floating_audio_visual_integration_line').data('orientation', 'horizontal');

	$('.floating_security_systems_line').data('center', -40).hide();
	$('.floating_security_systems_line').data('travel', 300);
	$('.floating_security_systems_line').data('orientation', 'horizontal');
	
	$('.floating_electric_line').data('center', -100).hide();
	$('.floating_electric_line').data('travel', 300);
	$('.floating_electric_line').data('orientation', 'vertical');


	function start() {
		$('.floating').each(function() {
			$(this).data('startX', $(this).position().left);
			$(this).data('startY', $(this).position().top);
			$(this).data('x', 0);
			$(this).data('y', 0);
			$(this).data('rightBound', 20);
			$(this).data('leftBound', -20);
			$(this).data('topBound', -20);
			$(this).data('bottomBound', 20);
			$(this).data('velocityX', -1 + Math.random()*2);
			$(this).data('velocityY', -1 + Math.random()*2);
			$(this).data('line', $('.' + $(this).attr('class').split(' ')[1] + '_line'));
			$(this).data('line').fadeIn('slow');
		});		
		
		setInterval(function() {
			$('.floating').each(function() {
				var x = $(this).data('x');
				var y = $(this).data('y');
				var rightDistance = $(this).data('rightBound') - x;
				var leftDistance = x - $(this).data('leftBound');
				var topDistance = y - $(this).data('topBound');
				var bottomDistance = $(this).data('bottomBound') - y;
				
				var velocityX = $(this).data('velocityX') + (rightDistance - leftDistance) * .01;
				var velocityY = $(this).data('velocityY') + (bottomDistance - topDistance) * .01;
				
				velocityX *= .98;
				velocityY *= .98;
								
				$(this).data('velocityX', velocityX);
				$(this).data('velocityY', velocityY);
				
				$(this).data('x', x + velocityX);
				$(this).data('y', y + velocityY);
				
				$(this).css('left', $(this).data('startX') + $(this).data('x'));
				$(this).css('top', $(this).data('startY') + $(this).data('y'));
				changeLine($(this));
			});
		}, 50);
		
		setInterval(chooseDestination, 2000);
		
		function chooseDestination() {
			$('.floating').each(function() {
				$(this).data('velocityX', $(this).data('velocityX') - 1 + 2 * Math.random());
				$(this).data('velocityY', $(this).data('velocityY') - 1 + 2 * Math.random());
			});
		}
		
		function changeLine(elem) {
			var line = elem.data('line');
			if (line.size()) {
				var elemPos = Math.round(line.data('orientation') == 'horizontal' ? elem.data('y') : elem.data('x'));
				var bgY = line.data('center') + elemPos * 20;
				
				if (Math.abs(bgY) > line.data('travel')) {
					bgY = -line.data('travel');
				}
				
				if (bgY > 0) {
					bgY = 0;
				}
				if (line.data('orientation') == 'vertical') {
					line.css('backgroundPosition', bgY + 'px ' + 'top');
				} else {
					line.css('backgroundPosition', 'left ' + bgY + 'px');
				}
			}
		}
	
		chooseDestination();
	};
});
