	$.ajaxSetup ({
	    // Disable caching of AJAX responses
	    cache: false
	});

	var startTimes = [];
	var currentTime;
	var eventsTotal;
	var countdownTimer;
	
	$(document).ready(function(){

		if ( $("div#countdown").length ){

			$.getJSON(
				"/countdown/initiate.php",
				{},
				function(data){
					if ( data.eventsTotal ){
						eventsTotal = data.eventsTotal;
						currentTime = data.currentTime;
						
						i=0;
						$.each(data.events, function(k, v){
							$("div#countdown").append(v.eventLink);
							$("div#countdown").append( 
								"<div id='countdown-event-"+i+"' class='countdown-event'>"+
								
									"<div class='countdown-days-title cwords'>DAYS:</div>"+
									"<div id='countdown-days-"+i+"' class='countdown-days cnumbers'></div>"+
									
									"<div class='countdown-hrs-title cwords'>HRS:</div>"+
									"<div id='countdown-hrs-"+i+"' class='countdown-hrs cnumbers'></div>"+
									
									"<div class='countdown-mins-title cwords'>MINS:</div>"+
									"<div id='countdown-mins-"+i+"' class='countdown-mins cnumbers'></div>"+
									
									"<div class='countdown-secs-title cwords'>SECS:</div>"+
									"<div id='countdown-secs-"+i+"' class='countdown-secs cnumbers'></div>"+
								"</div>"
							);
							startTimes[i] = v.startTime;
							i++;
						});
						updateCountdown();
						countdownTimer = setInterval( updateCountdown, 1000 );
					}
				}
			);
		}
	});
	
	function updateCountdown(){
		
		for ( i=0; i <= startTimes.length - 1; i++ ){
			var secondsBetween = (startTimes[i] - currentTime);
			
			if ( secondsBetween < 0 ){
				$("#countdown-days-"+i).html("00");
				$("#countdown-hrs-"+i).html("00");
				$("#countdown-mins-"+i).html("00");
				$("#countdown-secs-"+i).html("00");
				clearTimeout( countdownTimer );
				return 0;
			}
			var days = Math.floor(secondsBetween/86400);
			var remainder = secondsBetween % 86400;
			var hours = Math.floor(remainder/3600);
				remainder = remainder % 3600;
			var minutes = Math.floor(remainder/60);
				remainder = remainder % 60;
			var seconds = remainder;
			
			if ( days < 10 ){
				if ( days.toString.length < 2 ){
					days = "0"+days.toString();
				}
			}
			if ( hours < 10 ){
				if ( hours.toString.length < 2 ){
					hours = "0"+hours.toString();
				}
			}
			if ( minutes < 10 ){
				if ( minutes.toString.length < 2 ){
					minutes = "0"+minutes.toString();
				}
			}
			if ( seconds < 10 ){
				if ( seconds.toString.length < 2 ){
					seconds = "0"+seconds.toString();
				}
			}
			
			//alert(currentTime);
			$("#countdown-days-"+i).html(days);
			$("#countdown-hrs-"+i).html(hours);
			$("#countdown-mins-"+i).html(minutes);
			$("#countdown-secs-"+i).html(seconds);
		}
		currentTime++;
	}
