
$(document).ready(function() {
	initNeon();
	initCount();
	slideshow();
});

function initNeon() {
	if ( typeof initNeon.objects == 'undefined' ) {
		initNeon.objects = new Array($('#chat'), $('#back'), $('#pricepoint'));
	}
	
	var intObject = Math.floor(Math.random() * 3);
	var intTime = Math.floor(Math.random() * 10000) + 2000;
	flicker(initNeon.objects[intObject], intTime);
};

function flicker($object, time) {
	$object.fadeTo(200, 0.2)
		.fadeTo(50, 0.5)
		.fadeTo(50, 0.2)
		.fadeTo(50, 0.6)
		.fadeTo(50, 0.2)
		.fadeTo(50, 0.7)
		.fadeTo(50, 0.3)
		.fadeTo(50, 0.7)
		.fadeTo(200, 0.6)
		.fadeTo(200, 0.9)
		.fadeTo(300, 1);
	setTimeout(initNeon, time);
}

function initCount() {
	var arrVolumes = new Array(120, 120, 80, 80, 40, 40, 20, 20, 40, 40, 50, 50, 60, 60, 60, 60, 60, 60, 50, 50, 80, 80, 110, 110);
	var time = new Date();
	var hours = time.getHours();
	
	var callers = Math.round((1 + ((Math.random() * 0.2) - 0.1)) * arrVolumes[ hours ]);
	$('#stats span').text(callers + ' ');
	setTimeout(initCount, 20000);
}

function slideshow() {
	$images = $('#carousel img');
	slideshow.numberOfPhotos = $images.length;
	$('section').css('z-index', 99)
	$images.each(function(i) {
		$(this).css('z-index', ((slideshow.numberOfPhotos - i) + 2) % slideshow.numberOfPhotos)
	});
	setTimeout(function() { rotateSlideshow(0); }, 2500)
}

function rotateSlideshow(currentPhoto) {
	currentPhoto = currentPhoto % slideshow.numberOfPhotos;
	$images = $('#carousel img');
	$images.eq(currentPhoto).fadeOut(1000, function() {
		$('#carousel img').each(function(i) {
			$(this).css('z-index',((slideshow.numberOfPhotos - i) + currentPhoto) % slideshow.numberOfPhotos);
		});
		setTimeout(function() {rotateSlideshow(++currentPhoto);}, 2500);
	});
	$images.eq((currentPhoto + 1) % slideshow.numberOfPhotos).fadeIn(1000);
}


