var prayerResponse;

function showUserWebcastUI(device) {
	if (!device) device='';
	
	//check if on horizon campus
	var on_campus = $("#on_campus").val();
	
	centerWebcastUI();
	$("#webcast_ui, #webcast_ui_wrapper").animate({
		'height':'512'						 
	},1000);	
	$("#webcast_selectors").animate({
		'left':'-1000'								
	},1000, function() {
		$("#webcast_content").hide();
		var rand = Math.random();
		$("#webcast_content").load('webcast_ui/includes/individual.asp?rand=' + rand, function() {
			$("#webcast_content").fadeIn(1000, function() {
				if (on_campus=="F") {
					//load video
					$("#player_wrapper").html(loadVideo('ind',device));
					$("#cgi_wrapper").html(loadVideo('cgi'));
					$("#input_name").focus();
				}
				else {
					//load campus video
					$("#player_wrapper").html(loadVideo('campus'));
				}
			});																					   
		});
	});
	
}

function showChurchWebcastUI() {
	
	//check if on horizon campus
	var on_campus = $("#on_campus").val();	
	
	if (on_campus=="F") {
		centerWebcastUI();
		$("#webcast_ui, #webcast_ui_wrapper").animate({
			'height':'512'						 
		},1000);
		$("#webcast_content").fadeOut(1000, function() {
			var rand = Math.random();
			$("#webcast_content").load('webcast_ui/includes/church.asp?rand=' + rand, function() {
				$("#webcast_content").fadeIn("slow");
				//load video
				$("#player_wrapper").html(loadVideo('church'));
			});
		});
	}
	else {
		alert("Sorry, the webcast stream to churches is unavailable to computers on the Horizon campus.");	
	}
}

function showPasswordUI() {
	$("#webcast_selectors").animate({
		'left':'-1000'								
	},1000, function() {
		$("#webcast_content").hide();
		var rand = Math.random();
		$("#webcast_content").load('webcast_ui/includes/password.asp?rand=' + rand, function() {
			$("#webcast_content").fadeIn(1000, function() {
				$("#input_password").focus();										
			});
		});
	});
}

function selectWebcast() {
	var webcast_ui_h = $("#webcast_ui").height();
	if (webcast_ui_h > 329) {
		$("#webcast_ui, #webcast_ui_wrapper").animate({
			'height':'329'											  
		},1000);	
	}
	var rand = Math.random();
	$("#webcast_content").load('webcast_ui/includes/select_webcast.asp?rand=' + rand);	
}

function loadVideo(type,device) {
	
	var h = 0;
	var w = 0;
	var player = '';
	var rand = Math.random();
	var redirect = '';
	if (!device) device='';
	if (device=="iphone") redirect='http://www.horizonsd.org/iphone.php';
	
	switch (type) {
		case "ind":
			h = 267;
			w = 503;
			player = "liveflashwop.asp";
			break;
		case "church":
			h = 375;
			w = 705;
			player = "liveflashwophd.asp";
			break;
		case "cgi":
			h = 176;
			w = 332;
			player = "liveflashwopcg.asp";
			break;
		case "campus":
			h = 267;
			w = 503;
			player = "liveflashwop_campus.asp";
			break;
	}
	var html = '<iframe src="http://prayandfast.org/webcast/' + player + '?rand=' + rand + '" id="webcast_frame" name="webcast_frame" scrolling="no" frameborder="no" align="left" height="' + h + 'px" width="' + w + 'px" style="background-color:#000000;"></iframe>';
	
	if (redirect=='') {
		return html;
	}
	else {
		window.location = redirect;	
	}
}

function submitPrayerRequest(key) {
	
		//hide send button
		$("#input_send_prayer_request").hide();
	
		function verify(data) {
				if (data==1) {
					//success
					clearPrayerForm();
					var msg = "Thank you! We have received your prayer request, and are privileged to join you in prayer.  Keep your browser window open and stay tuned; as time permits, our internet prayer group will lift up your request during our webcast.";
					alert(msg);
					$("#input_send_prayer_request").show();
				}
				else {
					// error
					alert(data);
					$("#input_send_prayer_request").show();
				}
		}
		
		$.post("process/send_prayer_request.asp", $("#frmPrayerRequest").serialize(), function(data){ verify(data) } );		
	
		if (key) {
			prayerResponse = setInterval( "checkPrayerResponse(" + key + ")", 5000 );
		}
}

function clearPrayerForm() {
	$("#input_name").val('');
	$("#input_email").val('');
	$("#input_location").val('');
	$("#input_prayer_request").val('');	
	$("#input_name").focus();
}

function processChurchLogin() {
		function verify(data) {
				if (data==1) {
					//success
					showChurchWebcastUI();
				}
				else {
					// error
					alert(data);
				}
		}
		
		$.post("webcast_ui/process/church_login.asp", $("#frmWebcast").serialize(), function(data){ verify(data) } );		
}

function centerWebcastUI() {
	var screen_h = $(window).height();
	if (screen_h<1055) {
		var pad = Math.floor((screen_h-542)/2);
		var y = (256-pad);
		if (y>=0) {
			$('html,body').animate({scrollTop : y},1000); 
		}
	}
}

function checkPrayerResponse(key) {
		var rand = Math.random();
		$.get("webcast_ui/process/check_prayer_response.php?key=" + key + "&rand=" + rand, function(data) {
			if (data=="1") {
				//response exists
				showPrayerResponse(key);
			}
		} );
}

function showPrayerResponse(key) {
		var rand = Math.random();
		$.get("webcast_ui/process/show_prayer_response.php?key=" + key + "&rand=" + rand, function(data) {
				$("#cgi_wrapper").removeClass("black_background");
				$("#cgi_wrapper").css({
					'height':'267px',
					'padding-right':'15px',
					'width':'317px'
				});
				$("#cgi_wrapper").html(data);
				clearInterval(prayerResponse);
		} );
}



//CHURCH TESTING
function churchTest() {

	$.post("process/check_church_password.asp",$("#frmChurchTesting").serialize(),function(data) {
		if (data=="1") {
			//success
			//window.open('http://prayandfast.org/webcast/flashlivehd.asp','archive','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=854,height=480');
			loadPopup('webcast/liveflashhd_wrapper.asp','705','375');
			$("#input_church_password").val('');
			$("body").focus();
		}
		else {
			alert(data);	
		}
	});

}
