$(document).ready(function() {
	$('#btn_submit').click(function() {
		if($('#prayer_input').attr('value') == 'What do you want to pray about?' || $('#prayer_input').attr('value') == '' ){
			return false;
		} else {
		$.post('/prayer/up', $('#send_prayer').serialize(), 
			function(data) { 
				var d = JSON.parse(data);
				var p = $('<div class="prayer" id="p_'+d.id+'"><a href="#" class="answer">Answered<\/a><span class="date">'+d.date+'<\/span><div class="request"><div class="confirm" style="display: none;"><span class="msg">Delete this?</span><a href="#" class="yes">Yes</a><a href="#" class="no">No</a></div><a href="http://twitter.com/home?status=Join%20me%20in%20prayer?%20 - '+d.base_url+d.id+'" class="tweet">Tweet this prayer</a><a href="#" class="delete">Delete<\/a><h3>'+d.prayer+'<\/h3><\/div><\/div>').css('display', 'none');
				$('#prayer_input').attr('value', 'What do you want to pray about?');
				$('#prayer_list').prepend(p);
				p.fadeIn('slow');
			});
		}
		return false;
	});
	$('.answer').live('click', function() {
		var $this = $(this);
		var id = $(this).closest('.prayer').attr('id').split('_');
		$.post('/prayer/answered', { prayer_id: id[1] }, 
			function(data) {
				if(data != '') {
					/*var msg = new Array(
						'Hey, alright!  Big ups to God!',
						'Ah yeah! Give Him glory!',
						'Woot!  Glory to God!',
						'Amen! Our God is good!'
					);
					var high = (parseFloat(3) - parseFloat(0)) + 1;
				    var rand = Math.floor(Math.random()*high) + parseFloat(0);

					$('#message').text(msg[rand]);
					$('#message').slideDown('slow');
					var s = function status_msg() {
						$('#message').animate({ opacity: 0 }, 1500).slideUp('slow', function() { $('#message').text('').css('opacity', 1);  });
					}
					setTimeout(s, 3500); */
					$('#p_'+id[1]).fadeOut(500, function() {$(this).remove()});
				}
			});
		return false;
	});
	$('.join').live('click', function() {
		var $this = $(this);
		if($this.hasClass('on')){
			alert("You're already praying for this person");
			return false;
		}
		else {
		var id = $(this).closest('.prayer').attr('id').split('_');
		$.post('/prayer/praying', { prayer_id: id[1] }, 
			function(data) {
				if(data != '') {
					$this.addClass('on');
				}
			});
		}
		return false;
	});
	$('.tweet').live('click', function(e) {
		e.preventDefault();
		var id = $(this).closest('.prayer').attr('id').split('_');
		var url = $(this).attr('rel');
		$.post('/prayer/tweet_prayer/'+id[1],
			function(data) {
				if(data != '') {
					window.location = url;
				}
		});
		return false;
	});
	$('.option').click(function() {
		var id = $(this).attr('id').split('_');
		$(this).toggleClass('on');
		if($(this).hasClass('on')) {
			$('#option_'+id[1]).attr('value', 1);
		}
		else {
			$('#option_'+id[1]).attr('value', 0);
		}
		return false;
	});
	$("input[type=text]").each(function() {
		$(this).attr("default", $(this).attr("value"));
		$(this).focus(function() {
			if ($(this).attr("value") == $(this).attr("default")) {
				$(this).attr("value", "");
			}
		}).blur(function() {
			if ($(this).attr("value") == "") {
				$(this).attr("value", $(this).attr("default"));
			}
		});
	});
	$('#login_pass').focus(function() {
		$(this).attr('value', '');
		$('#login_pass').dPassword();
	}).blur(function() {
		$(this).attr('value', 'Password');
	});
	$('#btn_login_submit').click(function() {
		$('#login_form').submit();
		return false;
	});
	$('#btn_go').click(function() {
		$('#login_form').submit();
		return false;
	});
	$('#btn_change_pw').click(function() {
		$('#change_pw').submit();
		return false;
	});
	$('#btn_invite_submit').click(function() {
		$('#invite_form').submit();
		return false;
	});
	$('#btn_close').click(function() {
		$('#login').fadeOut();
		$('#login_email').attr('value', 'Email Address');
		$('#login_pass').attr('value', 'Password');
	});
	$('#btn_login, #btn_home_login').click(function() {
		$('#login').fadeIn();
		return false;
	});
	$('#btn_invite').click(function() {
		$('#login.invite_form').fadeIn();
		return false;
	});
	//Message Dropdown
  	if($('#message').hasClass('active')) {
  	$('#message').fadeIn('fast');
		var s = function status_msg() {
			$('#message').animate({ opacity: 0 }, 1500).slideUp('slow').removeClass('active').addClass('inactive');
		}
  	setTimeout(s, 3500);
  	}  	
  	$('#profile_options .option').click(function() {
  		var option;
		var id = $(this).attr('id').split('_');
		if($(this).hasClass('on')) { option = 'on'; }
		else { option = 'off'; }
		$.post('/profile/option/'+id[1]+'/'+option);
		return false;
	});
	
	$('.delete, .flag, .remove').live('click', function(e) {
			e.preventDefault();
			var $this = $(this);
			var confirm = $(this).prevAll('.confirm');
			confirm.fadeIn('fast');
			$('.yes').live('click', function(){
				var c = $(this).closest('.confirm');
				if($this.hasClass('delete')) {
					var id = $(this).closest('.prayer').attr('id').split('_');				
					$.post('/prayer/delete_prayer/'+id[1], function(data) {
						if(data == 'success') {
							$('#p_'+id[1]).fadeOut(500, function() {$(this).remove()});
						}
					});
				}
				if($this.hasClass('remove')) {
					var id = $(this).closest('.prayer').attr('id').split('_');
					$.post('/prayer/remove_prayer/'+id[1], function(data) {
						if(data == 'success') {
							$('#p_'+id[1]).fadeOut(500, function() {$(this).remove()});
						}
					});
				}
				if($this.hasClass('flag')) {
					var id = $(this).closest('.prayer').attr('id').split('_');
					$.post('/prayer/flag_prayer/'+id[1], function(data) {
						if(data == 'success') {
							c.fadeOut();
							var msg = $('#message').text('We\'ve flagged this prayer for review.');
							$('#message').slideDown('slow');
							var s = function status_msg() {
								$('#message').animate({ opacity: 0 }, 1500).slideUp('slow', function() { $('#message').text('').css('opacity', 1);  });
							}
							setTimeout(s, 3500); 	
						}
					});
				}
				return false;
			});
			$('.no').live('click', function(){
				$(this).closest('.confirm').fadeOut();
				return false;
			});			
	});
});
