function log_in() {
l = document.getElementById('id_login').value;
p = document.getElementById('id_password').value;
if (l=='' || p=='') {
str_talker = 'Поля E-mail и пароль не должны быть пустыми.'; talker();
} else {
agent.call('' ,'login', 'otvet_login', l, p);
}
}

function otvet_login(str) {
if (str=='false') {
document.getElementById('id_login').value = '';
document.getElementById('id_password').value= '';
str_talker = 'Неверный E-mail или пароль, попробуйте авторизоваться снова, или воспользуйтесь ссылкой восстановления пароля.'; talker();
} else {
location = '/';
}
}


function talker() {
if (document.getElementsByClassName('talker')[0].style.display!=='block') {
document.getElementsByClassName('talker_message')[0].innerHTML = str_talker;
document.getElementsByClassName('talker')[0].style.display = 'block';
setTimeout(talker_close, 5000);
}
}

function talker_close() {
document.getElementsByClassName('talker')[0].style.display = 'none';
}

function exit() {
document.cookie = "l=";
document.cookie = "p=";
location = '/';
}

function send_wall() {
if (arr_photos_wall.length>0) {
comment_wall = '';
str_wall_photos = '';
if (arr_photos_wall.length>0) {
for (i=0; i<arr_photos_wall.length; i++) {
if (i==0) { str_wall_photos = str_wall_photos+arr_photos_wall[i]; } else { str_wall_photos = str_wall_photos+','+arr_photos_wall[i]; };
}
}
page_id = uid;
token = tokens[0];
agent.call('' ,'send_wall', 'otvet_send_wall', comment_wall, page_id, str_wall_photos, token);
} else {
str_talker = 'Материал не должен быть пустым.'; talker();
}
}

function photo_show() {
$('#id_modal_photos').modal('show');
if (count_photos<2) {
photo_id = photo_ids[photo_id];
start_photo = false;
page = 1;
more = false;
document.getElementById('id_p1').innerHTML = 1;
document.getElementById('id_p2').innerHTML = 1;
loadcomments();
$('#id_open_smiles').popover('destroy');
arr_photos_wall = new Array();
}
if (count_photos>1) {
if (show_photo) { $('#photo_slider').slick({initialSlide:1, adaptiveHeight:true, speed:0}); show_photo = false; }
$('#photo_slider').slick('slickGoTo', photo_id);
}
}

$('#photo_slider').on('beforeChange', function(event, slick, currentSlide, nextSlide){
if (count_photos>1) {
if (currentSlide!==nextSlide || start_photo) {
start_photo = false;
photo_id = photo_ids[nextSlide];
page = 1;
more = false;
loadcomments();
$('#id_open_smiles').popover('destroy');
arr_photos_wall = new Array();
document.getElementById('id_p1').innerHTML = nextSlide+1;
document.getElementById('id_p2').innerHTML = photo_ids.length;
}
}
});

function loadcomments() {
if (document.getElementById('id_search_more_comments')) { 
document.getElementById('id_search_more_comments').innerHTML = '<img src="/img/preloader.gif" />'; 
} else {
document.getElementById('id_photo_comments').innerHTML = '<div style="text-align:center; padding:20px;"><img src="/img/preloader.gif" /></div>';
}
agent.call('' ,'load_comments', 'otvet_load_comments', photo_id, page);
}

function upload_photo_error() {
if (document.getElementById('uploadifive-file_photos-queue')) {
document.getElementById('uploadifive-file_photos-queue').parentNode.removeChild(document.getElementById('uploadifive-file_photos-queue'));
}
if (document.getElementById('uploadifive-file_comment-queue')) {
document.getElementById('uploadifive-file_comment-queue').parentNode.removeChild(document.getElementById('uploadifive-file_comment-queue'));
}
if (document.getElementById('uploadifive-file_wall-queue')) {
document.getElementById('uploadifive-file_wall-queue').parentNode.removeChild(document.getElementById('uploadifive-file_wall-queue'));
}
str_talker = 'Превышен лимит количества загружаемых изображений. Можно загружать по 5 картинок за один раз.'; talker();
init_upload_photo();
}

function init_upload_photo() {
arr_photos_wall = new Array();
document.getElementById('id_upload_foto_status').innerHTML = '<form style="text-align:left;"><span style="position:absolute; margin-top:2px; margin-left:30px;">Добавить фото</span><input id="file_photos" name="file_photos" type="file" multiple="true" style="display:none;" onChange="$(\'#file_photos\').uploadifive(\'upload\');"></form>';
upload_photo_id = 0;
$('#file_photos').uploadifive({
'buttonText'    : '',
'buttonClass'   : 'glyphicon icon-attachment',
'fileSizeLimit' : '10240KB',
'uploadLimit'   : 5,
'fileDesc'      : 'jpg',
'fileExt'       : '*.jpg',
'auto'             : true,
'checkScript'      : '/uploadify/check-exists.php',
'formData'         : {'token':tokens[1]},
'uploadScript'     : '/uploadify/upload_photos.php',
'onUploadComplete' : function(file, data) {
if (data!=='0') {
document.getElementById('uploadifive-file_photos-file-'+upload_photo_id).parentNode.removeChild(document.getElementById('uploadifive-file_photos-file-'+upload_photo_id)); 
arr_photos_wall[upload_photo_id] = data;
upload_photo_id++; 
if (document.getElementById('uploadifive-file_photos-queue').innerHTML=='') {
str_wall_photos = '';
if (arr_photos_wall.length>0) {
for (i=0; i<arr_photos_wall.length; i++) {
if (i==0) { str_wall_photos = str_wall_photos+arr_photos_wall[i]; } else { str_wall_photos = str_wall_photos+','+arr_photos_wall[i]; };
}
}
send_wall();
str_talker = 'Фотографии загружены.'; talker(); init_upload_photo(); 
}
} else {
global_error();
arr_photos_wall = new Array();
init_upload_photo();
}
}
});
document.getElementById('upload_photoslide').style.display = 'block';
}

function closesearch() {
document.getElementById('topsearch').style.display='none';
}

function top_search() {
top_query = document.getElementById('id_top_query').value;
if (top_query!=='') {
agent.call('' ,'topsearch', 'otvet_topsearch', top_query);
} else {
document.getElementById('top_result').innerHTML = '';
document.getElementById('topsearch').style.display='none';
}
}

function otvet_topsearch(str) {
if (str!=='') {
document.getElementById('topsearch').style.display='block';
document.getElementById('top_result').innerHTML = str;
} else {
document.getElementById('topsearch').style.display='none';
}
}

function otvet_load_comments(str) {
if (document.getElementById('id_search_more_comments')) { document.getElementById('id_search_more_comments').parentNode.removeChild(document.getElementById('id_search_more_comments')); }
if (str=='') {
document.getElementById('id_photo_comments').innerHTML = '<p style="padding:15px; background:#f5f5f5; border-radius:4px; text-align:center;">Комментарии отсутствуют. Будьте первым!</p>';
} else {
if (more) {
document.getElementById('id_photo_comments').innerHTML = document.getElementById('id_photo_comments').innerHTML + str;
} else {
document.getElementById('id_photo_comments').innerHTML = str;
}
}
$('#id_open_smiles').popover();
init_upload_comment();
arr_photos_comment = new Array();
}

function send_comment() {
if (document.getElementById('id_comment').value!=='' || arr_photos_comment.length>0) {
comment = document.getElementById('id_comment').value;
str_photos_comment = '';
if (arr_photos_comment.length>0) {
for (i=0; i<arr_photos_comment.length; i++) {
if (i==0) { str_photos_comment = str_photos_comment+arr_photos_comment[i]; } else { str_photos_comment = str_photos_comment+','+arr_photos_comment[i]; };
}
}
agent.call('' ,'send_comment', 'otvet_send_comment', comment, photo_id, str_photos_comment, uid, token);
} else {
str_talker = 'Комментарий не должен быть пустым.'; talker();
}
}

function otvet_send_comment() {
str_talker = 'Комментарий добавлен к фото.'; talker(); 
document.getElementById('id_comment').value = '';
document.getElementById('id_photo_comments').innerHTML = '<div style="text-align:center; padding:20px;"><img src="/img/preloader.gif" /></div>';
page = 1;
more = false;
loadcomments();
init_upload_comment();
document.getElementById('upload_comment_results').style.height = '0px';
document.getElementById('upload_comment_results').innerHTML = '';
}

function del_comment() {
agent.call('' ,'delcomment', 'otvet_delcomment', comment_id, token);
}

function otvet_delcomment() {
if (document.getElementById('comment_'+comment_id)) { document.getElementById('comment_'+comment_id).parentNode.removeChild(document.getElementById('comment_'+comment_id)); }
str_talker = 'Комментарий удален.'; talker();
}

function like() {
agent.call('' ,'like', 'otvet_like', object_id, object_type, token);
}

function otvet_like(str) {
if (str[0]=='1') {
document.getElementById('like_heart_'+object_type+'_'+object_id).style.color = '#5085ad';
if (document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML=='') {
document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML = 1;
} else {
document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML = parseInt(document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML)+1;
}
document.getElementById('popover_'+object_type+'_'+object_id).style.display = 'block';
}
if (str[0]=='0') {
document.getElementById('like_heart_'+object_type+'_'+object_id).style.color = '#b4cee2';
if (document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML=='1') {
document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML = '';
document.getElementById('popover_'+object_type+'_'+object_id).style.display = 'none';
} else {
document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML = parseInt(document.getElementById('like_count_'+object_type+'_'+object_id).innerHTML)-1;
}
}
document.getElementById('popover_'+object_type+'_'+object_id).title = str[1];
$('#popover_'+object_type+'_'+object_id).tooltip('destroy');
}

function like_peoples() {
$('#id_modal_photos').modal('hide');
$('#id_modal_likes').modal('show');
if (document.getElementById('id_likes_more_all')) {
document.getElementById('id_likes_more_all').innerHTML = '<img src="/img/preloader.gif" />';
}
agent.call('' ,'like_peoples', 'otvet_like_peoples', object_id, object_type, more, page);
}

function otvet_like_peoples(str) {
if (document.getElementById('id_likes_more_all')) {
document.getElementById('id_likes_more_all').parentNode.removeChild(document.getElementById('id_likes_more_all'));
}
if (more) {
document.getElementById('id_like_peoples').innerHTML = document.getElementById('id_like_peoples').innerHTML + str[0];
} else {
document.getElementById('id_like_peoples').innerHTML = str[0];
}
document.getElementById('id_like_count').innerHTML = str[1];
}

function add_smile() {
comment = '<smile'+smile_id+' />';
token = tokens[2];
str_photos_comment = '';
agent.call('' ,'send_comment', 'otvet_send_comment', comment, photo_id, str_photos_comment, uid, token);
$('#id_open_smiles').popover('destroy');
}

function init_upload_comment() {
document.getElementById('upload_comment_results').style.height = '0px';
document.getElementById('upload_comment_results').innerHTML = '';
document.getElementById('id_upload_comment_status').innerHTML = '<form style="text-align:left;"><input id="file_comment" name="file_comment" type="file" multiple="true" style="display:none;" onChange="$(\'#file_comment\').uploadifive(\'upload\');"></form>';
upload_photo_id = 0;
$('#file_comment').uploadifive({
'buttonText'    : '',
'buttonClass'   : 'glyphicon icon-attachment',
'fileSizeLimit' : '10240KB',
'uploadLimit'   : 5,
'fileDesc'      : 'jpg',
'fileExt'       : '*.jpg',
'auto'             : true,
'checkScript'      : '/uploadify/check-exists.php',
'formData'         : {'token':tokens[3]},
'uploadScript'     : '/uploadify/upload_comments.php',
'onUploadComplete' : function(file, data) {
if (data) {
if (data!=='0') {
document.getElementById('upload_comment_results').style.height = '50px';
document.getElementById('upload_comment_results').innerHTML = document.getElementById('upload_comment_results').innerHTML + '<div style="position:absolute; margin-left:'+(upload_photo_id*40)+'px; margin-top:10px; width:30px; height:30px; background-image: url(\'/photo/m_'+data+'.jpg\'); background-size:cover; background-position:center top; border-radius:50px; float:left;"></div>';
arr_photos_comment[upload_photo_id] = data;
} else {
global_error();
arr_photos_comment = new Array();
init_upload_comment();
}
}
document.getElementById('uploadifive-file_comment-file-'+upload_photo_id).parentNode.removeChild(document.getElementById('uploadifive-file_comment-file-'+upload_photo_id)); 
upload_photo_id++;
},
'onUpload' : function() {
document.getElementById('uploadifive-file_comment').parentNode.removeChild(document.getElementById('uploadifive-file_comment'));
}
});
}

$(document).on('ready', function() {
$(function() {
if (live_uploads) {
init_upload_photo();
}
});
});

function comment_photo_show() {
if (photo_type==1) {
scroll_comments = $('#id_modal_photos').scrollTop();
}
document.getElementById('id_comment_photo_body').innerHTML = '<div style="text-align:center; padding:20px;"><img src="/img/preloader.gif" /></div>';
$('#id_modal_photos').modal('hide');
$('#id_comment_photo').modal('show');
agent.call('' ,'load_photo', 'otvet_load_photo', photo_name);
}

function otvet_load_photo(str) {
document.getElementById('id_comment_photo_body').innerHTML = str;
page = 1;
more = false;
loadcomments();
$('#id_open_smiles').popover('destroy');
}

$('#id_comment_photo').on('hidden.bs.modal', function (e) {
$('#id_comment_photo').modal('hide');
if (photo_type==1) {
$('#id_modal_photos').modal('show');
document.getElementById('id_modal_photos').scrollTop = scroll_comments;
}
})

function del_photo() {
agent.call('' ,'del_photo', 'otvet_del_photo', photo_id, uid, token);
}

function otvet_del_photo(str) {
str_talker = 'Фотография удалена.'; talker();
}

function save_photo() {
agent.call('' ,'save_photo', 'otvet_save_photo', photo_id, uid, token);
}

function otvet_save_photo(str) {
str_talker = 'Фотография сохранена на Вашей стене.'; talker();
}

function otvety() {
if (!more) {
document.getElementById('id_body_otvety').innerHTML = '<div style="text-align:center; padding:20px;"><img src="/img/preloader.gif" /></div>';
}
$('#id_modal_otvety').modal('show');
if (document.getElementById('id_otvety_more_all')) {
document.getElementById('id_otvety_more_all').innerHTML = '<img src="/img/preloader.gif" />';
}
agent.call('' ,'otvety', 'otvet_otvety', more, page, token);
}

function otvet_otvety(str) {
if (document.getElementById('id_otvety_more_all')) {
document.getElementById('id_otvety_more_all').parentNode.removeChild(document.getElementById('id_otvety_more_all'));
}
if (str[0]!=='') {
if (more) {
document.getElementById('id_body_otvety').innerHTML = document.getElementById('id_body_otvety').innerHTML + str[0];
} else {
document.getElementById('id_body_otvety').innerHTML = str[0];
}
} else {
document.getElementById('id_body_otvety').innerHTML = '<p style="padding:15px; margin-bottom:0px; background:#f5f5f5; border-radius:4px; text-align:center;">Ответы отсутствуют.</p>';
}
document.getElementById('id_otvety_count').innerHTML = str[1];
}

function ping_online() {
agent.call('' ,'ping_online', 'otvet_ping_online', otvety_count);
}

function otvet_ping_online(str) {
if (str[0]!==0) {
document.getElementById('id_u_visits').innerHTML = '+'+str[0];
document.getElementById('id_u_visits').style.display = 'block';
} else {
document.getElementById('id_u_visits').innerHTML = '';
document.getElementById('id_u_visits').style.display = 'none';
}

if (str[1]!==0) {
document.getElementById('id_u_news').innerHTML = '+'+str[1];
document.getElementById('id_u_news').style.display = 'block';
} else {
document.getElementById('id_u_news').innerHTML = '';
document.getElementById('id_u_news').style.display = 'none';
}

if (str[2]!==0) {
document.getElementById('id_u_friends').innerHTML = '+'+str[2];
document.getElementById('id_u_friends').style.display = 'block';
} else {
document.getElementById('id_u_friends').innerHTML = '';
document.getElementById('id_u_friends').style.display = 'none';
}

if (str[3]!==0) {
document.getElementById('id_u_otvety').innerHTML = '+'+str[3];
document.getElementById('id_u_otvety').style.display = 'block';
} else {
document.getElementById('id_u_otvety').innerHTML = '';
document.getElementById('id_u_otvety').style.display = 'none';
}

if (otvety_count!==str[5]) {
otvety_count = str[5];
str_talker = str[6]; talker();
}

if (str[7]!==0) {
document.getElementById('id_u_dialogs').innerHTML = '+'+str[7];
document.getElementById('id_u_dialogs').style.display = 'block';
} else {
document.getElementById('id_u_dialogs').innerHTML = '';
document.getElementById('id_u_dialogs').style.display = 'none';
}

setTimeout(ping_online, 3000);
}

function visits() {
$('#id_modal_visits').modal('show');
if (document.getElementById('id_visits_more_all')) {
document.getElementById('id_visits_more_all').innerHTML = '<img src="/img/preloader.gif" />';
}
agent.call('' ,'visits', 'otvet_visits', more, page, token);
}

function otvet_visits(str) {
if (document.getElementById('id_visits_more_all')) {
document.getElementById('id_visits_more_all').parentNode.removeChild(document.getElementById('id_visits_more_all'));
}
if (str[0]!=='') {
if (more) {
document.getElementById('id_visits_peoples').innerHTML = document.getElementById('id_visits_peoples').innerHTML + str[0];
} else {
document.getElementById('id_visits_peoples').innerHTML = str[0];
}
} else {
document.getElementById('id_visits_peoples').innerHTML = '<p style="padding:15px; margin-bottom:0px; background:#f5f5f5; border-radius:4px; text-align:center;">К Вам еще никто не заходил.</p>';
}
document.getElementById('id_visits_count').innerHTML = str[1];
}