$(document).ready(
function(){

	$('a#user_details_link').click(
		function()
		{
			$('div#user_details, div#change_password').slideDown(800);
		
			return false;
		});

	$('select#select_city').change(
		function()
		{
			if($(this).val() == 0)
				$('div#other_city_title, div#other_city').fadeIn();
			else
				$('div#other_city_title:visible, div#other_city:visible').fadeOut();
		});
		
	$('a#cancel_user_details, a#cancel_change_password').click(
		function()
		{
			$('div#user_details, div#change_password').slideUp(800);
			$("div#change_password input:password").val('');
			$("div#user_details form")[0].reset();
			return false;
		});
		
	$('a#send_change_password').click(
		function()
		{
			var password 	 = $("div#change_password input#password").val();
			var newpassword  = $("div#change_password input#newpassword").val();
			var newpassword2 = $("div#change_password input#newpassword2").val();
			
			if(newpassword != newpassword2)
			{
				alert('вы ввели разные пароли');
				return false;
			}
			
			$.ajax(
			{
				type: 'POST',
				url: '/request/changeuserpassword',
				data: {password: password, newpassword: newpassword},
				success:
					function(response)
					{
						var resp = $.toJSON(response);
						if(!resp.result)
						{
							alert(resp.msg);
						}
						else
						{
							$('div#user_details, div#change_password').slideUp(800);
							$("div#change_password input:password").val('');
							$("div#user_details form")[0].reset();
						}
					}
			});
		
			return false;
		});
		
		
	$('a#send_user_details').click(
		function()
		{
			$("div#user_details form")[0].submit();
			return false;
		});
		
	$('p#status').click(
		function()
		{
			var status = '';
			if($(this).css('opacity') == 1)
				status = $(this).text();
				
			$('div#change_status textarea').val(status);
			$(this).hide();
			$('div#change_status').show();
			$('div#change_status textarea').focus();
		});
		
	$('input#apply_status').click(
		function()
		{
			var status = $('div#change_status textarea').val();
			var opacity = 1;
			
			$.ajax(
				{
				type: 'POST',
				url: '/request/changeuserstatus',
				data: {status: status},
				success:
					function(response)
					{
						var resp = $.toJSON(response);
						if(!resp.result)
						{
							alert(resp.msg);
						}
						else
						{
							if(status == '')
							{
								status = '[указать статус]';
								opacity = 0.5;
							}
			
							$('p#status').text(status).css({opacity: opacity});
							$('div#change_status').hide();
							$('p#status').show();
						}
					}
				});
		});
		
	$('input#clear_status').click(
		function()
		{
			$.ajax(
				{
				type: 'POST',
				url: '/request/changeuserstatus',
				data: {status: ''},
				success:
					function(response)
					{
						var resp = $.toJSON(response);
						if(!resp.result)
						{
							alert(resp.msg);
						}
						else
						{
							$('p#status').text('[указать статус]').css({opacity: 0.5});
							$('div#change_status').hide();
							$('p#status').show();
						}
					}
				});
		});
		
	$('div#news').jScrollPane();
});