$(document).ready(function() {
	$(".col.ticker > ul").newsticker();
	if ($("#countdown").length ) {
		var today = new Date();
		var nextService = new Date();
		var dif;
		var scenario;
		//Sunday after evening service to Wed before evening service - countdown to Wed evening (7:00pm)
		if ((today.getDay()==0&&today.getHours()>=20)||(today.getDay()>0&&today.getDay()<3)||(today.getDay()==3&&today.getHours()<19)){
			dif = 3-today.getDay();
			scenario = 1;
		//Wednesday after evening service to Sun before morning service - countdown to Sun morning
		} else if ((today.getDay()==3&&today.getHours()>=20)||(today.getDay()>=4&&today.getDay()<=6)||(today.getDay()==0&&today.getHours()<10)||(today.getDay()==0&&today.getHours()==10&&today.getMinutes()<45)){
			dif = 7-today.getDay();
			if (today.getDay()==0){dif = 0;}
			scenario = 2;
		//Sunday after morning service to Sun before evening service - countdown to Sun evening
		} else if (today.getDay()==0&&((today.getHours()==12&&today.getMinutes()>29)||today.getHours()>12)&&today.getHours()<18){
			dif = 0;
			scenario = 3;
		} else {
			scenario = 4;
		}
		nextService.setDate(today.getDate()+dif);
		switch(scenario){
			case 1: var nextService = new Date(nextService.getFullYear(),nextService.getMonth(),nextService.getDate(),19,0,0); break;
			case 2: var nextService = new Date(nextService.getFullYear(),nextService.getMonth(),nextService.getDate(),10,45,0); break;
			case 3: var nextService = new Date(nextService.getFullYear(),nextService.getMonth(),nextService.getDate(),18,0,0); break;
			case 4: goLive(); break;
		}
		
		if (scenario!=4){
			$('#countdown').countdown(
				{until: nextService, compact: true, timezone: -5,
				 //layout: '<span class="section"><span class="num">{d10}{d1}</span> day</span><span class="section"><span class="num">{h10}{h1}</span> hr</span><span class="section"><span class="num">{m10}{m1}</span> min</span><span class="section"><span class="num">{s10}{s1}</span> sec</span>',
				 layout: '<span class="section"><span class="num{d10}"></span><span class="num{d1}"></span> day</span><span class="section"><span class="num{h10}"></span><span class="num{h1}"></span> hr</span><span class="section"><span class="num{m10}"></span><span class="num{m1}"></span> min</span><span class="section"><span class="num{s10}"></span><span class="num{s1}"></span> sec</span>',
				 expiryText: '<script>goLive();</script>'
			}); 
		}
	}
	
	/* Prayer Alert Subscribe Form */
	$("form#prayer-alert").submit(function(){
		subscribe_form();
		return false;
	});
	
	$("form#prayer-alert input[name=email]").focus(function(){
		if ($(this).val()=="Enter your email address"){
			$(this).val("");
		}
	}).blur(function(){
		if ($(this).val()==""){
			$(this).val("Enter your email address");
		}
	});
});

function subscribe_form(){
	var formData=$("form#prayer-alert").serialize();
	$.ajax({
		type: "POST",
		url: "/emails/",
		data: formData+"&method=AJAX",
		cache: false,
		success: function(resp){
			var json=$.parseJSON(resp);
			$("form#prayer-alert").html(json["return"]);
		}
	});
	return false;
}

function goLive(){
	$("#counter").html("<span class=\"go\"><a href=\"/live\"><b>LIVE</b> &ndash; WATCH NOW!</a></span>");
}
