/**
 * @author Yakubov Andrey
 */
function userSortComment(user_id) {
	JsHttpRequest.query("/user?request_mode=ajax", {
				'user_id' : user_id,
				'ajax_action' : 'sort_comm'
			}, function(result, errors) {

				if (errors) {
					if (!production_flag) {
						alert(errors);
					}
				}

				if (result) {
					$('#items_on_main').html(result);
					$('#toplinks')
							.html("<span id='show_all'>\
                                    (<a href='javascript:void(0);' onclick='userSortAll("
									+ user_id
									+ ");'>показывать все просмотры</a>)</span>\
                                    <span id='show_comm'>\
                                    с комментарием</span>\
                                    <span id='show_rec'>\
                                    (<a href='javascript:void(0);' onclick='userSortRec("
									+ user_id
									+ ");'>рекомендованные</a>)</span>");
				}
			}, true);
}

function userSortRec(user_id) {
	JsHttpRequest.query("/user?request_mode=ajax", {
				'user_id' : user_id,
				'ajax_action' : 'sort_rec'
			}, function(result, errors) {

				if (errors) {
					if (!production_flag) {
						alert(errors);
					}
				}

				if (result) {
					$('#items_on_main').html(result);
					$('#toplinks')
							.html("<span id='show_all'>\
                                    (<a href='javascript:void(0);' onclick='userSortAll("
									+ user_id
									+ ");'>показывать все просмотры</a>)</span>\
                                    <span id='show_comm'>\
                                    (<a href='javascript:void(0);' onclick='userSortComment("
									+ user_id
									+ ");'>с комментарием</a>)</span>\
                                    <span id='show_rec'>\
                                    рекомендованные</span>");
				}
			}, true);
}

function userSortAll(user_id) {
	JsHttpRequest.query("/user?request_mode=ajax", {
				'user_id' : user_id,
				'ajax_action' : 'sort_all'
			}, function(result, errors) {

				if (errors) {
					if (!production_flag) {
						alert(errors);
					}
				}

				if (result) {
					$('#items_on_main').html(result);
					$('#toplinks')
							.html("<span id='show_all'>\
                                    показывать все просмотры</span>\
                                    <span id='show_comm'>\
                                    (<a href='javascript:void(0);' onclick='userSortComment("
									+ user_id
									+ ");'>с комментарием</a>)</span>\
                                    <span id='show_rec'>\
                                    (<a href='javascript:void(0);' onclick='userSortRec("
									+ user_id
									+ ");'>рекомендованные</a>)</span>");
				}
			}, true);
}

function profileDelete(id)
{
	if (!confirm('Точно хотите удалить?'))
	{
		return;
	}
	
    JsHttpRequest.query("/user?request_mode=ajax", {
                'user_id' : id,
                'ajax_action' : 'profile_delete'
            }, function(result, errors) {

                if (errors) {
                    if (!production_flag) {
                        alert(errors);
                    }
                }

                if (result) {
                    document.location = "/";
                }
            }, true);
}

/**
 * @author kkopachev
 */


function profileEdit() {

	$("#edit_avatar").show();
	$("#edit_username").show();
	$("#edit_about").show();
	$("#edit_save").show();
	$("#edit_add_link").show();
	//$("#add_link").show();
	$(".edit_links").show();
	$("#edit_params").show();
	$("#show_username").hide();
	$("#show_about").hide();
	$("#show_params").hide();
	
	$('#edit_profile_li').hide();
	$('#delete_profile_li').show();

}

function profileSave() {

	$("#profile_error").hide();
	
	var curlinks = new Array();

	$('#links_block > li > input').each(function() {
				curlinks[this.id] = this.value;
		});

	JsHttpRequest.query("/user?request_mode=ajax", {
				'ajax_action' : 'profile_edit',
				'avatar' : document.getElementById('avatarfile'),
				'username' : $('#edit_username').val(),
				'about' : $('#edit_about').val(),
				'links' : curlinks

			}, function(result, errors) {

				if (errors) {
					if (production_flag) {
						// sendUnexpectedErrorReport(errors)
					} else {
						alert(errors);
					}
				} else if (result) {
					if (result.error_status) {
						$("#profile_error").html(result.error).show();
					} else {

						/*
						 * $("#edit_avatar").hide();
						 * 
						 * $("#edit_username").hide(); $("#edit_about").hide();
						 * 
						 * $("#edit_save").hide(); $("#add_link").hide();
						 * $(".edit_links").hide(); $("#edit_params").hide();
						 * $("#show_username").show(); $("#show_about").show();
						 * $("#show_params").show();
						 * 
						 * $("#profile_error").hide();
						 * 
						 * $("")
						 */

						var loc = document.location.href;
						loc = loc.replace('edit_profile', '');
						
						document.location = loc;

					}
				}
			}, true);

}

function profileAddLink(url) {
	
	var curlinks = new Array();
	
	$('#link_add_error').hide();

	$('#links_block > li > input').each(function() {
				curlinks[this.id] = this.value;
		});

	JsHttpRequest.query("/user?request_mode=ajax", {
				'url' : url,
				'cur_links' : curlinks,
				'ajax_action' : 'get_link'
			}, function(result, errors) {
				if (errors) {
					if (!production_flag) {
						alert(errors);
					}
				}

				if (result) {
					if (result.error_status) {
						$('#link_add_error').html(result.error_msg);
	                    $('#link_add_error').show();
					} else {
						$('#links_block').html(result);
						$("#link_field").val('');
					}
				}
			}, true);
}

function updateAvatar()
{
	
	JsHttpRequest.query("/user?request_mode=ajax", {
		'ajax_action' : 'update_avatar',
		'avatar' : document.getElementById('avatarfile')
	}, function(result, errors) {

		if (errors) {
			if (production_flag) {
				// sendUnexpectedErrorReport(errors)
			} else {
				alert(errors);
			}
		} else if (result) {
			if (result.error_status) {
				$("#profile_error").html(result.error).show();
			} else if (result.avatar) {

				$('#user_avatar').attr('src', result.avatar);
				$('#header_avatar').attr('src', result.avatar);

			}
		}
	}, true);

	
}
