// countdown slider
function countdownSlider(periods) {
	var totalPixels = 265;
	
	var startDate = new Date($('#countdownTimer').attr('data-startDate'));
	var endDate = new Date($('#countdownTimer').attr('data-endDate'));
	var totalInterval = Math.ceil((endDate.getTime()-startDate.getTime()));
	var currentInterval = ((periods[0] * 31556926) + (periods[1] * 2629744) + (periods[3] * 86400) + (periods[4] * 3600) + (periods[5] * 60) + periods[6]) * 1000;
	
	var waitRatio = Math.ceil(totalInterval / currentInterval);
	var calculatedPosition = Math.ceil(totalPixels / waitRatio);
	
	$('#countdownSlider').css({'width': calculatedPosition});
}

// start the countdown and move slider
$.fn.beginCountdown = function() {
	$(this).each(function() {
		var expirationDate = new Date($(this).attr('data-endDate'));
		$(this).countdown({
			until		: expirationDate,
			format	: 'dHM',
			layout	: '{dn} {dl} {hn} {hl} {mn} {ml}',
			onTick	: countdownSlider
		});
	});
}

// what to do when DOM fully loaded
$(document).ready(function(){
	$('#slider').nivoSlider({
		effect: 'boxRandom', // Specify sets like: 'fold,fade,sliceDown'
		directionNav: false, // Next & Prev navigation
		controlNav: true, // 1,2,3... navigation
		beforeChange: function(){}, // Triggers before a slide transition
		afterChange: function(){}, // Triggers after a slide transition
		slideshowEnd: function(){}, // Triggers after all slides have been shown
		lastSlide: function(){}, // Triggers when last slide is shown
		afterLoad: function(){} // Triggers when slider has loaded
	});
	
	$('#countdownTimer').beginCountdown();
});
