var showCountdown = true;

jQuery.noConflict();     
jQuery(document).ready(function($){

if (showCountdown) {

	// [9, 0, 10, 15] = [startHour, startMinute, endHour, endMinute]

	var weeklyServices = [];
	weeklyServices[0] = [ //sunday
		[9, 0, 10, 15],
		[10, 45, 12, 0],
		[17, 0, 18, 0],
		[19, 0, 20, 0]
		];
	weeklyServices[1] = [ //monday
		[12, 0, 13, 0],
		[19, 0, 20, 0]
		];
	weeklyServices[2] = [ //tuesday
		[12, 0, 13, 0],
		[19, 0, 20, 0]
		];
	weeklyServices[3] = [ //wednesday
		[12, 0, 13, 0],
		[19, 0, 20, 0]
		];
	weeklyServices[4] = [ //thursday
		[12, 0, 13, 0],
		[19, 0, 20, 0]
		];
	weeklyServices[5] = [ //friday
		[12, 0, 13, 0],
		[19, 0, 20, 0]
		];
	weeklyServices[6] = [ //saturday
		];

	// get some date data
	// MM/dd/yyyy hh:mm:ss tt
	var rightNow = new Date();
	var targetDate = new Date();
	var rightNowDay = rightNow.getDay();

	// loop through today's services
	function findNextService(day, plusDays) {

		if(day+plusDays > 6) { day = day - 7; }

		var todayServices = weeklyServices[day+plusDays];
		var plusDaysTime = 86400000 * plusDays;

		// if this day has no services, go to the next day
		if(todayServices.length < 1) {
			findNextService(rightNowDay, plusDays+1);
		}

		for (var i=0, len=todayServices.length; i<len; ++i) {
			// set this service' start time
			serviceStart = new Date();
			serviceStart.setHours(todayServices[i][0]);
			serviceStart.setMinutes(todayServices[i][1]);
			serviceStart.setSeconds(0);
			serviceStart.setMilliseconds(0);
			// add time for plus days
			serviceStart.setTime(serviceStart.getTime() + plusDaysTime);

			// set this service' end time
			serviceEnd = new Date(serviceStart.valueOf());
			serviceEnd.setHours(todayServices[i][2]);
			serviceEnd.setMinutes(todayServices[i][3]);

			// if this service has not begun yet, start the countdown
			if(rightNow < serviceStart) {
				startTheClock(serviceStart);
				break;
			}
			// if this service is in progress, itsLive()
			else if (rightNow >= serviceStart && rightNow < serviceEnd) {
				itsLive();
				break;
			}
			// if this service is over, continue to the next service
			else if (rightNow > serviceEnd) {
				// if there are more services today, continue to the next service
				if(i < todayServices.length-1) {
					continue;
				}
				// if there are no more services today, loop through next day's services
				else {
					findNextService(rightNowDay, 1);
				}
			}

		} // end today's services loop
	}

	function startTheClock (targetTime) {
		$('#clock #ticktock').countdown({
		until: targetTime, 
		compact: false,
		onExpiry: itsLive, 
	    layout: '<span class="c-days">{dn}</span>' + 
	        '<span class="c-days-lbl c-lbl">{dl}</span>' + 
	        '<span class="c-hours">{hn}</span>' + 
	        '<span class="c-hours-lbl c-lbl">{hl}</span>' + 
	        '<span class="c-mins">{mn}</span>' + 
	        '<span class="c-mins-lbl c-lbl">{ml}</span>' +
	        '<span class="c-secs">{sn}</span>' + 
	        '<span class="c-secs-lbl c-lbl">{sl}</span>'
	        });
	}

	function itsLive() {
		$('#clock').contents().remove();
		$('#clock').append('<h1 id="live-msg"><a href="http://live.mediasocial.tv/celebrationchurch">Live! Join Now.</a></h1>')
	}

	// and they're off!
	findNextService(rightNowDay, 0);

} // end if showCountdown

});//end doc ready

