$(document).ready(function(){

	function centerModal(modal){
		var h = $(document).height();
		var mh = $(modal).height();

		$(modal).css({
			marginTop: "-" + mh/2 + "px"
		});
	};

	$('.modal').corners("7px");
	$('.modal-wrapper').corners("11px");

	$(document).pngFix();

	$('#scripture-toggle, #scripture-toggle-link')
		.click(
			function(){
				show = $(this).attr('rel');
				if (show === '0'){
					$('#scripture-holder').fadeIn();
					$('#scripture-holder-shadow').fadeIn();
					$(this).attr('rel','1');
					$.ajax({url:'includes/forms/update-show-scripture.php?show=1'});
				} else {
					$('#scripture-holder').fadeOut();
					$('#scripture-holder-shadow').fadeOut();
					$(this).attr('rel','0');
					$.ajax({url:'includes/forms/update-show-scripture.php?show=0'});
				}
				return false;
			}
		)

	$('.false')
		.click(
			function(){
				return false;
			}
		);

	$.fn.supersized.options = {
		startwidth: 1024,
		startheight: 768
	};

	$('#fullscreen-background img').supersized();

	$('input#username').focus(
		function(){
			$('#login-form .hidden').fadeIn();
			$(this).css('color','#000');
		}
	);

	$('input.button').hover(
		function(){
			$(this).css({
				background: '#ccc'
			})
		},
		function(){
			$(this).css({
				background: '#E6E6E6'
			})
		}
	);

	$('.new-contact-form .button, #new-group .button, #new-vision .button').hover(
		function(){
			$(this).css({
				background: '',
				color: '#333'
			})
		},
		function(){
			$(this).css({
				background: '',
				color: '#676767'
			})
		}
	);

	$('input#password').blur(
		function(){
			var password = $(this).attr('value');

			if (password === ''){
				$(this).attr('value','password');
			};
		}
	);

	// REGISTRATION FORM

		$('a.sign-up').click(
			function(){
				centerModal('#register-form');
			    $('#register-form').fadeIn();
			}
		);

		$('.close-modal').click(
			function(){
				$('.modal-wrapper').fadeOut();
			}
		);

		terms = 0;

		$('#terms-checkbox')
			.click(
				function(){
					if (terms === 0){
						terms = 1;
					} else {
						terms = 0;
					}
				}
			);

		$('#terms-of-service')
			.click(
				function(){

					if ($(this).attr('rel') !== 'down'){
						$(this).attr('rel','down').text('hide terms');
					} else {
						$(this).removeAttr('rel').text('view terms');
					}
					$('#terms .hidden').slideToggle();

					return false;
				}
			)

		$('form#the-register-form').submit(
			function(){
					var email = $('#reg-email').val();
					var email2 = $('#reg-email-check').val();
					var first = $('#reg-first').val();
					var last = $('#reg-last').val();
					var pass = $('#reg-pass').val();
					var pass2 = $('#reg-pass-check').val();
					// alert('regemail='+ email + '&regfirst=' + first + '&reglast=' + last + '&regpass='+ pass);

				if (email === '' || first === '' || last === '' || pass === ''){
					$('.errors').fadeIn(); return false;
				} else {
					if (email !== email2){
						$('.errors').html('Please make sure your email addresses match.').fadeIn(); return false;
					} else {
						if (pass !== pass2){
							$('.errors').html('Please make sure your passwords match.').fadeIn(); return false;
						} else {
							if (terms === 0){
								$('.errors').html('Please agree to the Terms of Service.').fadeIn(); return false;
							} else {
								$.get('includes/forms/email-check.php', { regemail: email}, function(data){
									$('.errors').html(data);
									$('.errors').fadeIn();

									if (data !== 'The email address ' + email + ' is already in use.'){
									$.ajax({
										url: 'includes/forms/register.php',
										data: 'regemail='+ email + '&regfirst=' + first + '&reglast=' + last + '&regpass='+ pass,
										success: function(data){
											$('form#the-register-form')
												.html(data);

											$('#register-form')
												.animate({opacity: 1}, 3000)
												.fadeOut();
										}
										})
									 return false;

									}
									return false;
								})
							}
						}
					}
				}
				 return false;
			}
		);

		$('.retr').click(
			function(){
				centerModal('#retr-form');
				$('#retr-form').fadeIn();
			}
		);

		$('#retr-submit').click(
			function(){
				var email = $(this).prev().children('#retr-email').val();

				$.ajax({
					type: 'POST',
					url: 'includes/forms/email-password.php',
					data: 'useremail='+email,
					success: function(data){
						$('#retr-form .errors').html(data).fadeIn();
					}
				}); return false;
			}
		);

	// PRAYER FUNCTIONALITY

	if($('body').attr('class') === 'prayer-page'){
		//alert('prayer');
	}

	$('.the-datepicker')
		.live('click',
			  function(){
				$(this).datepicker({
					showOn: 'focus',
					prevText: '',
					nextText: '',
					dateFormat: 'mm-dd-y',
					changeMonth: true,
					changeYear: true
				}).focus()
			  })

	$('.the-datepicker-full')
		.live('click',
			  function(){
				$(this).datepicker({
					yearRange: "-100:+0",
					showOn: 'focus',
					prevText: '',
					nextText: '',
					dateFormat: 'mm-dd-yy',
					changeMonth: true,
					changeYear: true
				}).focus()
			  })

	$('.the-datepicker').datepicker({
		prevText: '',
		nextText: '',
		dateFormat: 'mm-dd-y'
	});

	$('#ui-datepicker-div').hide();

	$("#select-date").change(
		function(){
			var date = $(this).val();
			getPrayers(date);
		}
	);

	function getPrayers(date){
		$.ajax({
			type: 'POST',
			url: 'includes/forms/get-prayer.php',
			data: 'setdate='+ date,
			success: function(data){
				$('#added-prayers').html(data);
				prayerDetails();
			}
		});
	};

	getPrayers($('body').attr('date'));

	function getAnsweredPrayers(){
		$.ajax({
			type: 'POST',
			url: 'includes/forms/answered-prayers.php',
			success: function(data){
				$('#added-answered-prayers').html(data);
			}
		});
	};

	getAnsweredPrayers();

	function prayerDetails(){

			$('#entries ul li .info-display').hover(
				function(){
					$(this).children('span').show();
				},
				function(){
					$(this).children('span').hide();
				}
			);

			$('a.view-details').click(
				function(){
					if ($(this).html() === 'view details'){
						$(this).html('hide details');
						$(this).parent().parent().next('.description').show();
					} else {
						$(this).html('view details');
						$(this).parent().parent().next('.description').hide();
					}
				}
			);

			$('.prayed').click(
				function(){
					var prayer_id = ($(this).parent().parent().attr('id'));

					// alert(prayer_id);

					if ($(this).attr('rel') === 'checked'){
						$(this).attr('rel','unchecked');
						$.ajax({
							type: 'POST',
							url: 'includes/forms/update-prayed-for.php',
							data: 'val=0&prayer_id='+ prayer_id
						});
					} else {
						$(this).attr('rel','checked');
						$.ajax({
							type: 'POST',
							url: 'includes/forms/update-prayed-for.php',
							data: 'val=1&prayer_id='+ prayer_id
						});
					}
				}
			);

			$('a.answered').click(
				function(){
					var prayer_id = ($(this).parent().parent().parent().attr('id'));
					var date = $('#today').text();

					if ($(this).html() === ' answered! '){
							$(this).html(' unanswered ');
							// $(this).parent().prev().attr('checked', true);

							// alert('includes/forms/prayer-answered.php?prayer_id='+ prayer_id+'&date='+date)

							$.ajax({
								type: 'POST',
								url: 'includes/forms/prayer-answered.php',
								data: 'prayer_id='+ prayer_id + '&date=' + date
							}); return false;
					} else {
							$(this).html(' answered! ');
							// $(this).parent().prev().attr('checked', false);

							$.ajax({
								type: 'POST',
								url: 'includes/forms/prayer-unanswered.php',
								data: 'prayer_id='+ prayer_id
							}); return false;
					}
				}
			);

		$('a.email').click(
			function(){
				var currentemail = $(this).attr('rel');
				var message = $('#default-message').val();

				$('#email-message').text(message);
				$('#email-form input#email-to').val(currentemail);

				$('#email_alert').hide();
				$('#email-form').fadeIn();
			}
		);

		$('#email-form form').submit(
			function(){
				var sendto = $('#email-to').val();
				var message = $('#email-message').val();

				if ($('input[name=cc]').is(':checked')){
					var cc = 1;
				} else {
					var cc = 2;
				}

				if ($('input[name=bcc]').is(':checked')){
					var bcc = 1;
				} else {
					var bcc = 2;
				}

				$.ajax({
					type: 'POST',
					url: 'includes/forms/email-prayer.php',
					data: {sendto:sendto, message:message, cc:cc, bcc:bcc},
					success: function(data){
						$('#email-form .alert')
							.fadeIn()
							.animate({
								opacity: 1
							}, 2000,
								function(){
									$('#email-form').fadeOut();
								});

					}
				});

				return false;
			}
		);

			$('a.edit-prayer').click(
				function(){
					var prayer_id = ($(this).parent().parent().parent().attr('id'));

					$('#edit-prayer span').load('includes/forms/edit-prayer.php?prayer_id='+ prayer_id, function(){

						$('.the-datepicker').datepicker({
							prevText: '',
							nextText: '',
							dateFormat: 'mm-dd-y'
						});

						$('input.button').hover(
							function(){
								$(this).css({
									background: '#ccc'
								})
							},
							function(){
								$(this).css({
									background: '#E6E6E6'
								})
							}
						);

						$('#edit-prayer-button').click(
							function(){
								var title = $('#pe-title').val();
								var desc = $('#pe-description').val();
								var startDate = $('#pe-start-date').val();
								var endDate = $('#pe-end-date').val();
								var email = $('#pe-email').val();

								// alert('utitle='+ title +'&udesc='+ desc +'&ustart_date='+ startDate +'&uend_date='+ endDate +'&uemail='+ email);

								if (title === ''){
									alert('Please add a title.'); return false;
								} else {
									if (startDate === '' || endDate === ''){
										alert('Please add a start and end date.'); return false;
									} else {
										$.ajax({
											type: 'POST',
											url: 'includes/forms/update-prayer.php',
											data: 'prayer_id='+ prayer_id +'&utitle='+ title +'&udesc='+ desc +'&ustart_date='+ startDate +'&uend_date='+ endDate +'&uemail='+ email,
											success: function(){
												getPrayers($('#date-selected').attr('rel'));
												$('#edit-prayer').fadeOut();
												$('#pe-title').val('');
												$('#pe-description').val('');
												$('#pe-start-date').val('');
												$('#pe-end-date').val('');
												$('#pe-email').val('');
											},
											error: function(){
												alert('error');
											}
										}); return false;
									}
								}
							}
						);

						$('.the-datepicker').focus(
							function(){
								$('#search').css('z-index','0');
							}
						).blur(
							function(){
								$('#search').css('z-index','10');
							}
						);

						$('#delete-prayer').hover(
							function(){
								if($(this).html() === 'delete prayer'){
									$(this).css({
										color:'#c00',
										background:'#ccc'
									});
								}
							},
							function(){
								if($(this).html() === 'delete prayer'){
									$(this).css({
										color:'#777',
										background:'#dedede'
									});
								}
							}
						).click(
							function(){
								$(this).hide();
								$(this).next().show();
							}
						);

						$('span#yes, span#no').hover(
							function(){
								$(this).css({
									color:'#000',
									cursor:'pointer'
								});
							},
							function(){
								$(this).css({
									color:'#c00'
								});
							}
						);

						$('span#no').click(
							function(){
								$(this).parent().prev().show();
								$(this).parent().hide();
							}
						);

						$('span#yes').click(
							function(){
								$.ajax({
									type: 'POST',
									url: 'includes/forms/delete-prayer.php',
									data: 'prayer_id='+ prayer_id,
									success:function(){
										getPrayers($('#date-selected').attr('rel'));
										$('#edit-prayer').fadeOut();
										$('#edit-prayer #delete-prayer').show();
										$('#edit-prayer #delete-confirm').hide();
									}
								}); return false;
							}
						);

					});

					centerModal('#edit-prayer');
					$('#edit-prayer').fadeIn();
				}
			);
	}

	$('#add-entry a').click(
		function(){
			centerModal('#new-prayer');
			$('h2, #form-left, #form-right').show();
			$('#prayer-success').hide();
			$('#new-prayer').fadeIn();
			$('#p-title').focus();
		}
	);

	$('#new-prayer form').submit(
		function(){
			var title = $('#p-title').val();
			var desc = $('#p-description').val();
			var startDate = $('#p-start-date').val();
			var endDate = $('#p-end-date').val();
			var email = $('#p-email').val();

			// alert('includes/forms/add-prayer.phptitle='+ title +'&desc='+ desc +'&start_date='+ startDate +'&end_date='+ endDate +'&email='+ email);

			if (title === ''){
				alert('Please add a title.'); return false;
			} else {
				if (startDate === '' || endDate === ''){
					alert('Please add a start and end date.'); return false;
				} else {
					$.ajax({
						type: 'POST',
						url: 'includes/forms/add-prayer.php',
						data: 'title='+ title +'&desc='+ desc +'&start_date='+ startDate +'&end_date='+ endDate +'&email='+ email,
						success: function(data){
							$('#new-prayer form h2, #form-left, #form-right').fadeOut();
							getPrayers($('#date-selected').attr('rel'));
							$('h2#prayer-success')
								.fadeIn()
								.animate({opacity: 1}, {queue: false}, 5000);
							$('#new-prayer')
								.animate({opacity: 1}, 3000)
								.fadeOut();
							$('#p-title').val('');
							$('#p-description').val('');
							$('#p-start-date').val('');
							$('#p-end-date').val('');
							$('#p-email').val('');
						},
						error: function(){
							alert('error');
						}
					}); return false;
				}
			}
		}
	);

	$('#p-email')
		.focus(
			function(){
				if (this.value.length >= 3){
					$('#email-search .results').fadeIn();
				};

				$(this).keyup(
					function (e) {
						if(this.value.length >= 3){
							stext = $(this).val();

							$.ajax({
								url: 'includes/forms/search-contacts.php',
								data: {stext:stext},
								success: function(data){
									$('#email-search .results').html(data).fadeIn();

									$('#email-search .results span')
										.click(
											function(){
												$('#p-email').val($(this).text());
												$('#email-search .results').fadeOut().html('');
											}
										);

									$('.results span').hover(
										function(){
											$(this).css('background','#bbb');
										},
										function(){
											$(this).css('background','#aaa');
										}
									);
								}
							})
						} else {
							$('#email-search .results').fadeOut();
						}
					}
				)
			}
		)
		.blur(
			function(){
				$('#email-search .results').fadeOut();
			}
		);

	$('input#close-prayer-button').click(
		function(){
			$('#new-prayer'	).fadeOut();
			$('#p-title').val('');
			$('#p-description').val('');
			$('#p-start-date').val('');
			$('#p-end-date').val('');
			$('#p-email').val('');
		}
	);

	$('input#close-edit-prayer-button').click(
		function(){
			$(this).parent().parent().fadeOut();
			$('#pe-title').val('');
			$('#pe-description').val('');
			$('#pe-start-date').val('');
			$('#pe-end-date').val('');
			$('#pe-email').val('');
		}
	);

	$('#search input').val('SEARCH PRAYERS');

	$('#search input').focus(
		function(){
			$(this).val('');
			$('#search .results').html('<span>Type 3 letters to start search.</span>').fadeIn();
			$(this).css({
				borderColor: '#000',
				color: '#000'
			});

			$(this).keyup(function (e) {
				if(this.value.length >= 3){
					$.ajax({
						url: 'includes/forms/search-prayer.php',
						data: 'stext='+ $(this).val(),
						success: function(data){
							$('#search .results').html(data);
							$('.results span').hover(
								function(){
									$(this).css('background','#bbb');
								},
								function(){
									$(this).css('background','#aaa');
								}
							);
							$('.results span').click(
								function(){
									var prayer_id = $(this).attr('id');

									$('#edit-prayer span').load('includes/forms/edit-prayer.php?prayer_id='+ prayer_id, function(){

										$('.the-datepicker').datepicker({
											prevText: '',
											nextText: '',
											dateFormat: 'mm-dd-y'
										});

										$('.the-datepicker').focus(
											function(){
												$('#search').css('z-index','0');
											}
										).blur(
											function(){
												$('#search').css('z-index','10');
											}
										);

										$('input.button').hover(
											function(){
												$(this).css({
													background: '#ccc'
												})
											},
											function(){
												$(this).css({
													background: '#E6E6E6'
												})
											}
										);

										$('#edit-prayer-button').click(
											function(){
												var title = $('#pe-title').val();
												var desc = $('#pe-description').val();
												var startDate = $('#pe-start-date').val();
												var endDate = $('#pe-end-date').val();
												var email = $('#pe-email').val();

												// alert('utitle='+ title +'&udesc='+ desc +'&ustart_date='+ startDate +'&uend_date='+ endDate +'&uemail='+ email);

												if (title === ''){
													alert('Please add a title.'); return false;
												} else {
													if (startDate === '' || endDate === ''){
														alert('Please add a start and end date.'); return false;
													} else {
														$.ajax({
															type: 'POST',
															url: 'includes/forms/update-prayer.php',
															data: 'prayer_id='+ prayer_id +'&utitle='+ title +'&udesc='+ desc +'&ustart_date='+ startDate +'&uend_date='+ endDate +'&uemail='+ email,
															success: function(){
																getPrayers($('#date-selected').attr('rel'));
																$('#edit-prayer').fadeOut();
																$('#pe-title').val('');
																$('#pe-description').val('');
																$('#pe-start-date').val('');
																$('#pe-end-date').val('');
																$('#pe-email').val('');
															},
															error: function(){
																alert('error');
															}
														}); return false;
													}
												}
											}
										);

										$('#delete-prayer').hover(
											function(){
												if($(this).html() === 'delete prayer'){
													$(this).css({
														color:'#c00',
														background:'#ccc'
													});
												}
											},
											function(){
												if($(this).html() === 'delete prayer'){
													$(this).css({
														color:'#777',
														background:'#dedede'
													});
												}
											}
										).click(
											function(){
												$(this).hide();
												$(this).next().show();
											}
										);

										$('span#yes, span#no').hover(
											function(){
												$(this).css({
													color:'#000',
													cursor:'pointer'
												});
											},
											function(){
												$(this).css({
													color:'#c00'
												});
											}
										);

										$('span#no').click(
											function(){
												$(this).parent().prev().show();
												$(this).parent().hide();
											}
										);

										$('span#yes').click(
											function(){
												$.ajax({
													type: 'POST',
													url: 'includes/forms/delete-prayer.php',
													data: 'prayer_id='+ prayer_id,
													success:function(){
														getPrayers($('#date-selected').attr('rel'));
														$('#edit-prayer').fadeOut();
														$('#edit-prayer #delete-prayer').show();
														$('#edit-prayer #delete-confirm').hide();
													}
												}); return false;
											}
										);

									});

									centerModal('#edit-prayer');
									$('#edit-prayer').fadeIn();
								}
							)
						}
					});
				} else {
					$('#search .results').html('<span>Type 3 letters to start search.</span>');
				}
			});
		}
	).blur(
		function(){
			$(this).val('SEARCH PRAYERS');
			$('#search .results').fadeOut();
			$(this).css({
				borderColor: '#bbb',
				color: '#bbb'
			});
		}
	);

	$('#upcoming ul').hide();

	$('#upcoming h3').click(
		function(){
			var state = $(this).html();
			if (state === 'see upcoming'){
				$('#upcoming ul').show();
				$(this).html('hide upcoming');
			} else {
				$('#upcoming ul').hide();
				$(this).html('see upcoming');
			}
		}
	);

	$('#upcoming ul li:even').css('background','#eee');

	$('#upcoming ul li').hover(
		function(){
			$(this).css({
				border: '#eee 1px solid'
			});
			// $(this).children('h3').css('color','#676767').animate({paddingLeft:'5px'});
		},
		function(){
			$(this).css({
				border: '#fff 1px solid'
			});
			// $(this).children('h3').css('color','').animate({paddingLeft:''});
		}
	);

	$('#upcoming h3').click(
		function(){
			$('#upcoming li#one .titles').load('includes/forms/get-prayer-title.php?thedate=' + $('li#one h4').html());
			$('#upcoming li#two .titles').load('includes/forms/get-prayer-title.php?thedate=' + $('li#two h4').html());
			$('#upcoming li#three .titles').load('includes/forms/get-prayer-title.php?thedate=' + $('li#three h4').html());
			$('#upcoming li#four .titles').load('includes/forms/get-prayer-title.php?thedate=' + $('li#four h4').html());
			$('#upcoming li#five .titles').load('includes/forms/get-prayer-title.php?thedate=' + $('li#five h4').html());
			$('#upcoming li#six .titles').load('includes/forms/get-prayer-title.php?thedate=' + $('li#six h4').html());
		}
	);

	$('.more').click(
		function(){
			date = ($(this).prev().prev().html());
			$('#select-date').val();
			getPrayers(date);
			$('#upcoming ul').slideUp();
			$('#upcoming h3').html('see upcoming');
		}
	);

	/*-------------- FEEDBACK ---------------- */
	$('#feedback-link')
		.click(
			function(){
				$('#feedback .alerts').hide();
				$('#feedback-holder')
					.animate({
						left: 0
					}, 500);

				return false;
			}
		);

	$('#cancel-feedback')
		.click(
			function(){
				$('#feedback #subject').val('1');
				$('#feedback #message').val('');
				$('#feedback-holder')
					.animate({
						left: '-312px'
					}, 500);

				return false;
			}
		);

	details = navigator.userAgent.toLowerCase() + "/"+screen.width+'x'+screen.height;
	sPath = window.location.pathname;
	sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

	$('#feedback')
		.submit(
			function(){
				sub = $('#subject').val();
				mes = $('#message').val();

				$.ajax({
					url: ("includes/forms/feedback.php"),
					data: {details:details,page:sPage,sub:sub,mes:mes},
					success: function(){
						$('#feedback #subject').val('1');
						$('#feedback #message').val('');

						$('#feedback .alerts')
							.text('Your feedback was submitted, thank you.')
							.fadeIn();

						$('#feedback-holder')
							.animate({
								opacity: 1
							},3000)
							.animate({
								left: '-312px'
							}, 500);
					}
				})

				return false;
			}
		)

});