$(function(){
	$("a.vote_up").click(function(){
		//get the id
		the_id = $(this).attr('id');
	
		// show the spinner
		$(this).parent().html("<img src='images/spinner.gif'/>");
		
		//fadeout the vote-count 
		$("span#votes_count"+the_id).fadeOut("fast");
		
		//the main ajax request
		$.ajax({
			type: "POST",
			data: "action=vote_up&id="+$(this).attr("id"),
			url: "ajax/votes.php",
			success: function(msg)
			{
				$("span#votes_count"+the_id).html(msg);
				//fadein the vote count
				$("span#votes_count"+the_id).fadeIn();
				//remove the spinner
				$("span#vote_buttons"+the_id).remove();
			}
		});
	});
	
	$("a.vote_down").click(function(){
		//get the id
		the_id = $(this).attr('id');
		
		// show the spinner
		$(this).parent().html("<img src='images/spinner.gif'/>");
		
		//the main ajax request
		$.ajax({
			type: "POST",
			data: "action=vote_down&id="+$(this).attr("id"),
			url: "ajax/votes.php",
			success: function(msg)
			{
				$("span#votes_count"+the_id).fadeOut();
				$("span#votes_count"+the_id).html(msg);
				$("span#votes_count"+the_id).fadeIn();
				$("span#vote_buttons"+the_id).remove();
			}
		});
	});
	
	$("a.rvote_up").live('click', function(){
		//get the id
		the_id = $(this).attr('id');
	
		// show the spinner
		$(this).parent().html("<img src='images/spinner.gif'/>");
		
		//fadeout the vote-count 
		$("span#rvotes_count"+the_id).fadeOut("fast");
		
		//the main ajax request
		$.ajax({
			type: "POST",
			data: "action=vote_up&id="+$(this).attr("id"),
			url: "ajax/rvotes.php",
			success: function(msg)
			{
				$("span#rvotes_count"+the_id).fadeOut();
				$("span#rvotes_count"+the_id).html(msg);
				$("span#rvotes_count"+the_id).fadeIn();
				$("span#rvote_buttons"+the_id).remove();
			}
		});
	});
	
	$("a.rvote_down").live('click', function(){
		//get the id
		the_id = $(this).attr('id');
		
		// show the spinner
		$(this).parent().html("<img src='images/spinner.gif'/>");
		
		//the main ajax request
		$.ajax({
			type: "POST",
			data: "action=vote_down&id="+$(this).attr("id"),
			url: "ajax/rvotes.php",
			success: function(msg)
			{
				$("span#rvotes_count"+the_id).fadeOut();
				$("span#rvotes_count"+the_id).html(msg);
				$("span#rvotes_count"+the_id).fadeIn();
				$("span#rvote_buttons"+the_id).remove();
			}
		});
	});
	$("#login-submit").click(function(){
		
		var datastring = "user_username=" + $("#login-username").val();
		datastring += "&user_password=" + $("#login-password").val();
		
		$('#sub-logged-out').html('<img src="images/ajax-loader-green.gif" width="220" height="19" />');
				
		if ($('#remember').attr('checked') == true) { 
			datastring += "&remember=yes"
		}
		
		//the main ajax request
		$.ajax({
			type: "POST",
			data: datastring,
			url: "ajax/login.php",
			success: function(msg)
			{
				if (msg == 'username') {
					$('#login-error').html('Your username was not found!');
					$('#sub-logged-out').html('<a>Please login to submit a reply</a>');
				} else if (msg == 'password') {
					$('#login-error').html('Your username and password did not match!');
					$('#sub-logged-out').html('<a>Please login to submit a reply</a>');
				} else if (msg == 'change') {
					$(window.location).attr('href', 'change_password.php');
				} else {
					$('#h3-username').html(msg);
					$('#logged-in').show();
					$('#logged-out').hide();
					if (pagename == 'post.php') {
						$('#sub-logged-in').show();
						$('#sub-logged-out').hide();
					} else if (pagename == 'register.php') {
						$(window.location).attr('href', 'index.php');
					}				
				}
			}
		});
	});
	$("#logout-link").click(function(){
					
		$('#sub-logged-out').html('<img src="images/ajax-loader-green.gif" width="220" height="19" />');			
								
		//the main ajax request
		$.ajax({
			type: "POST",
			data: '',
			url: "ajax/logout.php",
			success: function(msg)
			{
				$('#logged-in').hide();
				$('#logged-out').show();
				$("#login-username").val('');
				$("#login-password").val('');
				if (pagename == 'post.php') {
					$('#sub-logged-in').hide();
					$('#sub-logged-out').html('<a>Please login to submit a reply</a>');
					$('#sub-logged-out').show();
				} else if (pagename == 'submission.php') {
					$(window.location).attr('href', 'index.php');
				} else if (pagename == 'profile.php') {
					$(window.location).attr('href', 'index.php');
				} else if (pagename == 'change_password.php') {
					$(window.location).attr('href', 'index.php');
				}
			}
		});
	});
	$("#flash").hide();
	$("#repsubmit").click(function() {
	
		var reply_text = $("#reply_text").val();
		var post_id = $("#post_id").val();
		var dataString = 'reply_text=' + reply_text + '&post_id=' + post_id;
			
		if(reply_text!=''){
			
			$("#flash").fadeIn(400).html('<img src="images/spinner.gif" align="absmiddle">&nbsp;<span class="loading">Loading Comment...</span>');
			$.ajax({
				type: "POST",
				url: "ajax/replies.php",
				data: dataString,
				cache: false,
				success: function(html){
					
					if (html == 'login') {
						//$(window.location).attr('href', 'login.php?redirect&post_id=' + post_id);
						$(window.location).attr('href', 'index.php');
					} else {
		 
						$("div#update").prepend(html);
						$("div#update div:last").fadeIn("slow");
						document.getElementById('reply_text').value='';
						$("#reply_text").focus();
				 
						$("#flash").hide();
					}
				
			  	}
		 	});
		} else {
			$("#flash").fadeIn(400).html('Please enter a reply');
		}
		return false;
	});
	$("#post-submit").click(function(){
		
		if ($("#post_location").val() == '' && $("#post_text").val() == '') {
			$('#submit-error').html('You must enter a location and your night out!');
		} else if ($("#post_text").val() == '') {
			$('#submit-error').html('You must enter your night out!');
		} else if ($("#post_location").val() == '') {
			$('#submit-error').html('You must enter a location!');
		} else {
		
			var datastring = "post_location=" + $("#post_location").val();
			datastring += "&post_text=" + $("#post_text").val();
		
			$('#submit-post').html('<img src="images/ajax-loader.gif" width="220" height="19" />');
			
			//the main ajax request
			$.ajax({
				type: "POST",
				data: datastring,
				url: "ajax/submit.php",
				success: function(msg)
				{
					$('#submit-post').html(msg);
				}
			});
		}
	});
	$("#profile-submit").click(function(){
		
		var datastring = "user_email=" + $("#user_email").val();
		datastring += "&user_country=" + $("#user_country").val();
		datastring += "&user_password=" + $("#user_password").val();
		datastring += "&conf_password=" + $("#conf_password").val();

		$('#profile-box').show();
		$('#profile-post').html('<img src="images/ajax-loader-grey.gif" width="220" height="19" />');

		//the main ajax request
		$.ajax({
			type: "POST",
			data: datastring,
			url: "ajax/profile.php",
			success: function(msg)
			{
				$('#profile-post').html(msg);
			}
		});
	});
	$("#register-submit").click(function(){
		
		var datastring = "user_username=" + $("#user_username").val();
		datastring += "&user_email=" + $("#user_email").val();
		datastring += "&user_country=" + $("#user_country").val();
		datastring += "&user_password=" + $("#user_password").val();
		datastring += "&conf_password=" + $("#conf_password").val();

		$('#register-box').show();
		$('#register-post').html('<img src="images/ajax-loader-grey.gif" width="220" height="19" />');

		//the main ajax request
		$.ajax({
			type: "POST",
			data: datastring,
			url: "ajax/register.php",
			success: function(msg)
			{
				$('#register-post').html(msg);
				if (msg.IndexOf("The following errors occurred") == -1) {
					$('#register-form').hide();
				}
			}
		});
	});	
	$("#user_username").blur(function() {
		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		//the main ajax request
		$.ajax({
			type: "POST",
			data: "user_username=" + $(this).val(),
			url: "ajax/username.php",
			success: function(msg)
			{
				if(msg=='no') {
					$("#msgbox").fadeTo(200,0.1,function() {
						$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
					});
				} else {
					$("#msgbox").fadeTo(200,0.1,function() {
						$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
					});
				}
			}
		});
	});
	$("#forgot-username-submit").click(function(){

		if ($("#user_email").val() == '') {
			$('#msg').html('Please enter your email address in the text box below');
		} else {
			var datastring = "user_email=" + $("#user_email").val();

			$('#msg').html('<img src="images/ajax-loader-grey.gif" width="220" height="19" />');	

			//the main ajax request
			$.ajax({
				type: "POST",
				data: datastring,
				url: "ajax/fusername.php",
				success: function(msg)
				{
					if (msg == 'found') {
						$('#fusername').html('<h3>Forgot Your Username?</h3>Your username has been sent.');
					} else {
						$('#msg').html('Your email was not found, try again.');
					}
				}
			});
		}
	});
	$("#forgot-password-submit").click(function(){


		if ($("#user_email").val() == '' || $("#user_username").val() == '' ) {
			$('#msg').html('Please enter both fields in the text boxes below');
		} else {
			var datastring = "user_email=" + $("#user_email").val() + "&user_username=" + $("#user_username").val();

			$('#msg').html('<img src="images/ajax-loader-grey.gif" width="220" height="19" />');
	
			//the main ajax request
			$.ajax({
				type: "POST",
				data: datastring,
				url: "ajax/fpassword.php",
				success: function(msg)
				{
					if (msg == 'found') {
						$('#fusername').html('<h3>Forgot Your Password?</h3>Your password has been sent.');
					} else if (msg == 'email') {
						$('#msg').html('Your email did not match your username, please try again!');
					} else {
						$('#msg').html('Your username was not found, please try again!');
					}
				}
			});
		}
	});
	$("#change-password-submit").click(function(){

		if ($("#user_password").val() == '' || $("#confirm_password").val() == '' ) {
			$('#msg').html('Please enter both fields in the text boxes below');
		} else {
			var datastring = "user_password=" + $("#user_password").val() + "&confirm_password=" + $("#confirm_password").val();
			$('#msg').html('<img src="images/ajax-loader-grey.gif" width="220" height="19" />');
	
			//the main ajax request
			$.ajax({
				type: "POST",
				data: datastring,
				url: "ajax/cpassword.php",
				success: function(msg)
				{
					if (msg == 'found') {
						$('#fusername').html('<h3>Change Your Password</h3>Your password has been changed.');
					} else if (msg == 'invalid') {
						$('#msg').html('Password must be between 8 and 25 characters, please try again!');
					} else {
						$('#msg').html("Passwords don't match, please try again!");
					}
				}
			});
		}
	});
});
