var UserMenu = {
	Show: function(){
		if($('.usermenu').css('display') == 'none'){
			$('.usermenu').show();	
		} else  {
			$('.usermenu').hide();	
		}
	}
}

var ProfileFunction = {
	Show: function(){
		if($('.profile_function').css('display') == 'none'){
			$('.profile_function').show();	
		} else  {
			$('.profile_function').hide();	
		}
	}
}

//COVER
var Cover = {
	Init: function(i, hi){
		$('#cover_img').attr('src', i);
		$("#les10_ex2").draggable({
			axis: 'y',
			stop: function(){
				$('#hidden_cancel_button, #hidden_save_button, #hidden_delete_button').show();
			},
			drag: function(event, ui){
				var d = ui.position.top;
				$('#hidden_cancel_button, #hidden_save_button, #hidden_delete_button').hide();
				if(d >= 0){
					$("#les10_ex2").remove();
					$('#cover_restart').html('<div style="width:900px;height:'+hi+'px;position:relative;top:0px;z-index:1" id="les10_ex2"><img src="'+i+'" width="900" id="cover_img" /></div>');
					$('#hidden_cancel_button, #hidden_save_button, #hidden_delete_button').show();
					Cover.Init(i, hi);
				}
				h = parseInt('-'+(hi-320));
				if(d <= h){
					$("#les10_ex2").remove();
					$('#cover_restart').html('<div style="width:900px;height:'+hi+'px;position:relative;top:'+h+'px;z-index:1" id="les10_ex2"><img src="'+i+'" width="900" id="cover_img" /></div>');
					$('#hidden_cancel_button, #hidden_save_button, #hidden_delete_button').show();
					Cover.Init(i, hi);
				}
			}
		});
	},
	Delete: function(){
		$('.cover_description, #hidden_cancel_button, #hidden_save_button, #hidden_delete_button, #hidden_edit_button').hide();
		$('#upload_cover').show();
		$('#cover_img').attr('src', '');
		$('.cover_loaded_bg').css('cursor', 'default').hide();
		$('#cover_restart').html('');
		$("#les10_ex2").draggable('destroy');
		$.post('/index.php?go=editprofile&act=delcover');
	},
	Save: function(){
		Cover.Cancel();
		t = $("#les10_ex2").attr('style').split('top:');
		s = t[1].split('px');
		s[0] = s[0].replace('-', '');
		$.post('/index.php?go=editprofile&act=savecoverpos', {cover_position: s[0]});
	},
	Cancel: function(t){
		$('.cover_description, #hidden_cancel_button, #hidden_save_button, #hidden_delete_button,').hide();
		$('#hidden_edit_button').show();
		$('.cover_loaded_bg').css('cursor', 'default');
		$("#les10_ex2").draggable('destroy');
		if(t) $("#les10_ex2").css('top', '-'+t+'px');
	},
	Change: function(i, h, public_id){
		$('.cover_description, #hidden_cancel_button, #hidden_save_button, #hidden_delete_button,').show();
		$('#hidden_edit_button').hide();
		$('.cover_loaded_bg').css('cursor', 'move');
		Cover.Init(i, h);
	}
}
//ALBUMS
var Albums = {
	CreatAlbum: function(){
		Page.Loading('start');
		$.post('/index.php?go=albums&act=create_page', function(data){
			Box.Show('albums', 450, lang_title_new_album, data, lang_box_canсel, lang_album_create, 'StartCreatAlbum(); return false;', 0, 0, 1, 1);
			$('#name').focus();
			Page.Loading('stop');
		});
	},
	Delete: function(id, hash){
		Box.Show('del_album_'+id, 350, lang_title_del_photo, '<div style="padding:15px;">'+lang_del_album+'</div>', lang_box_canсel, lang_box_yes, 'Albums.StartDelete('+id+', \''+hash+'\'); return false;');
	},
	StartDelete: function(id, hash){
		$('#box_loading').show();
		$.post('/index.php?go=albums&act=del_album', {id: id, hash: hash}, function(d){
			Box.Close('del_album_'+id);
			$('#album_'+id).remove();
			updateNum('#albums_num');
			if($('.albums').size() < 1)
				Page.Go(location.href);
		});
	},
	Drag: function(){
		$("#dragndrop ul").sortable({
			cursor: 'move',
			opacity: 0.9,
			scroll: false,
			update: function(){
				var order = $(this).sortable("serialize"); 
				$.post("/index.php?go=albums&act=save_pos_albums", order, function(){}); 
			}
		});
	},
	EditBox: function(id){
		Page.Loading('start');
		$.post('/index.php?go=albums&act=edit_page', {id: id}, function(d){
			Page.Loading('stop');
			Box.Show('edit_albums_'+id, 450, lang_edit_albums, d, lang_box_canсel, lang_box_save, 'Albums.SaveDescr('+id+'); return false', 0, 0, 1, 1);
		});
	},
	SaveDescr: function(id){
		var name = $("#name_"+id).val();
		var descr = $("#descr_t"+id).val();
		if(name != 0){
			$("#name_"+id).css('background', '#fff');
			$('#box_loading').show();
			$.post('/index.php?go=albums&act=save_album', {id: id, name: name, descr: descr, privacy: $('#privacy').val(), privacy_comm: $('#privacy_comment').val()}, function(data){
				$('#box_loading').hide();
				if(data == 'no_name'){
					$('.err_red').show().text(lang_empty);
					ge('box_but').disabled = false;
				} else if(data == 'no'){
					$('.err_red').show().text(lang_nooo_er);
					ge('box_but').disabled = false;
				} else {
					Box.Close('edit_albums_'+id);
					row = data.split('|#|||#row#|||#|');
					$('#descr_'+id).html('<div style="padding-top:4px;">'+row[1]+'</div>');
					$('#albums_name_'+id).html(row[0]);
				}
			});
		} else {
			$("#name_"+id).css('background', '#ffefef');
			setTimeout("$('#name_"+id+"').css('background', '#fff').focus()", 800);
			$('#box_loading').hide();
		}
	},
	EditCover: function(id, page_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
		
		Box.Page(
			'/index.php?go=albums&act=edit_cover', //URL
			'id='+id+page, //POST данные
			'edit_cover_'+id+page_num, //ID
			627, //Ширина окна
			lang_edit_cover_album, //Заголовок окна
			lang_box_canсel, //Имя кнопки для закртие окна
			0, //Текст кнопки выполняющая функцию
			0, //Сама функция для выполнения
			400, //Высота окна
			'overflow', //Скролл
			'bg_show_top', //Внутреняя тень окна верх
			'bg_show_bottom', //Внутреняя тень окна низ
			'',
			1
		);
	},
	SetCover: function(id, aid, photo){
		$('#box_loading').show();
		$.get('/index.php?go=albums&act=set_cover', {id: id}, function(){
			$('#cover_'+aid).html('<img src="'+photo+'" alt="" />');
			Box.Close('edit_cover_'+aid);
			$('#box_loading').hide();
		});
	}
}

//PHOTOS
var Photo = {
	Show: function(h){
		Distinguish.GeneralClose();
		var id = h.split('_');
		var uid = id[0].split('photo');
		var section = h.split('sec=');
		var fuser = h.split('wall/fuser=');
		var note_id = h.split('notes/id=');
		var msg_id = h.split('msg/id=');
		
		if(fuser[1])
			section[1] = 'wall';
			
		if(note_id[1]){
			section[1] = 'notes';
			fuser[1] = note_id[1];
		}
		
		if(msg_id[1]){
			section[1] = 'msg';
			fuser[1] = msg_id[1];
		}

		$('.photo_view').hide();
		
		if(is_moz && !is_chrome) scrollTopForFirefox = $(window).scrollTop();
		$('html').css('overflow', 'hidden');
		if(is_moz && !is_chrome) $(window).scrollTop(scrollTopForFirefox);

		if(ge('photo_view_'+id[1])){
			$('#photo_view_'+id[1]).show();
			history.pushState({link:h}, null, h);
		} else {
			Photo.Loading('start');
			$.post('/index.php?go=photo', {uid: uid[1], pid: id[1], section: section[1], fuser: fuser[1]}, function(d){
				if(d == 'no_photo'){
					Photo.Loading('stop');
					Box.Info('no_video', lang_dd2f_no, lang_photo_info_text, 300);
					$('html').css('overflow-y', 'auto');
					return false;
				} else if(d == 'err_privacy'){
					Photo.Loading('stop');
					addAllErr(lang_pr_no_title);
					$('html').css('overflow-y', 'auto');
				}
				
				if(section[1] != 'loaded')
					history.pushState({link:h}, null, h);
				
				$('body').append(d);
				$('#photo_view_'+id[1]).show();

				Photo.Loading('stop');
			});
		}
	},
	Profile: function(uid, photo, type){
		Photo.Loading('start');
		$.post('/index.php?go=photo&act=profile', {uid: uid, photo: photo, type: type}, function(d){
			Photo.Loading('stop');
			if(d == 'no_photo'){
				Box.Info('no_video', lang_dd2f_no, lang_photo_info_text, 300);
				$('html').css('overflow-y', 'auto');
			} else {
				$('body').append(d);
				$('#photo_view').show();
				$('html').css('overflow-y', 'hidden');
			}
		});
	},
	Prev: function(h){
		var id = h.split('_');
		$('.video_view').hide();
		$('html').css('overflow', 'hidden');

		$('.pinfo, .photo_prev_but, .photo_next_but').show();
		$('.ladybug_ant').imgAreaSelect({remove: true});
		
		if(ge('photo_view_'+id[1])){
			$('#photo_view_'+id[1]).show();
			return false;
		} else {
			Photo.Show(h);
		}
	},
	Close: function(close_link){
		$('.ladybug_ant').imgAreaSelect({remove: true});
		
		$('.photo_view').remove();
		$('html').css('overflow-y', 'auto');
		
		if(is_moz && !is_chrome) $(window).scrollTop(scrollTopForFirefox);

		if(close_link != false)
			history.pushState({link: close_link}, null, close_link);
	},
	Loading: function(f){
		if(f == 'start'){
			if(is_moz && !is_chrome) scrollTopForFirefox = $(window).scrollTop();
			$('html').css('overflow', 'hidden');
			if(is_moz && !is_chrome) $(window).scrollTop(scrollTopForFirefox);
			var loadcontent = '<div class="photo_view" id="photo_load" style="padding-right:17px" onClick="Photo.setEvent(event, false)">'+
			'<div class="photo_close" onClick="Photo.LoadingClose(); return false" style="right:15px;"></div>'+
			'<div class="photo_bg" style="height:310px;padding-top:290px;">'+
			'<center><img src="/templates/Default/images/progress.gif" alt="" /></center>'+
			'</div>'+
			'</div>';
			$('body').append(loadcontent);
			$('#photo_load').show();
		} 
		if(f == 'stop')
			$('#photo_load').remove();
	},
	LoadingClose: function(){
		$('#photo_load').remove();
		$('html').css('overflow-y', 'auto');
	},
	Init: function(target){
		this.target = $(target);
		var that = this;
		$(window).scroll(function(){
			if ($(document).height() - $(window).height() <= $(window).scrollTop()){
				alert(1);
			}
		});
	},
	Panel: function(id, f){
		if(f == 'show')
			$('#albums_photo_panel_'+id).show();
		else
			$('#albums_photo_panel_'+id).hide();
	},
	MsgDelete: function(id, aid, type){
		Box.Show('del_photo_'+id, '400', lang_title_del_photo, '<div style="padding:15px;">'+lang_del_photo+'</div>', lang_box_canсel, lang_box_yes, 'Photo.Delete('+id+', '+aid+', '+type+'); return false');
	},
	Delete: function(id, aid, type){
		$('#box_loading').show();
		$.get('/index.php?go=albums&act=del_photo', {id: id}, function(){
			Box.Close('del_photo_'+id);
			if(!type){
				$('#a_photo_'+id).remove();
				$('#p_jid'+id).remove();
				
				updateNum('#photo_num');
			} else 
				$('#pinfo_'+id).html(lang_photo_info_delok);
		});
	},
	SetCover: function(id, jid){
		Page.Loading('start');
		$.get('/index.php?go=albums&act=set_cover', {id: id}, function(){
			$('.albums_new_cover').fadeOut();
			$('#albums_new_cover_'+jid).fadeIn();
			Page.Loading('stop');
		});
	},
	EditBox: function(id, r){
		Page.Loading('start');
		$.get('/index.php?go=albums&act=editphoto', {id: id}, function(data){
			Box.Show('edit_photo_'+id, '400', 'Photo editing', '<div style="padding:20px;"><div  style="color:#888;padding-bottom:10px;"><b>Photo description</b></div><textarea class="inpst" id="descr_'+id+'" style="width:335px; resize:none; padding:10px;height:71px;">'+data+'</textarea></div>', 'Cancel', 'Save', 'Photo.SaveDescr('+id+', '+r+'); return false');
			Page.Loading('stop');
		});
	},
	SaveDescr: function(id, r){
		var descr = $('#descr_'+id).val();
		$('#box_loading').show();
		$.post('/index.php?go=albums&act=save_descr', {id: id, descr: descr}, function(d){
			Box.Close('edit_photo_'+id);
			if(r == 1)
				$('.photo_view').remove();
			else
				$('#photo_descr_'+id).html(d);
		});
	},
	setEvent: function(event, close_link){
		var oi = (event.target) ? event.target.id: ((event.srcElement) ? event.srcElement.id : null);
		var el = oi.substring(0, 10);
		if(el == 'photo_view' || el == 'photo_load')
			Photo.Close(close_link);
	},
	Rotation: function(pos, id){
		$('#loading_gradus'+id).show();
		$.post('/index.php?go=photo&act=rotation', {id: id, pos: pos}, function(d){
			var rndval = new Date().getTime(); 
			$('#ladybug_ant'+id).attr('src', d+'?'+rndval);
			$('#loading_gradus'+id).hide();
		});
	}
}

//PHOTOS COMMENTS
var comments = {
	add: function(id){
		var comment = $('#textcom_'+id).val();
		if(comment != 0){
			butloading('add_comm', '56', 'disabled', '');
			$.post('/index.php?go=photo&act=addcomm', {pid: id, comment: comment},  function(data){
				if(data == 'err_privacy'){
					addAllErr(lang_pr_no_title);
				} else {
					$('#comments_'+id).append(data);
					$('#textcom_'+id).val('');
				}
				butloading('add_comm', '56', 'enabled', lang_box_send);
			});
		} else {
			$('#textcom_'+id).val('');
			$('#textcom_'+id).focus();
		}
	},
	delet: function(id, hash){
		textLoad('del_but_'+id);
		$.post('/index.php?go=photo&act=del_comm', {hash: hash}, function(){
			$('#comment_'+id).html('<div style="padding-bottom:5px;color:#777;">'+lang_del_comm+'</div>');
		});
	},
	delet_page_comm: function(id, hash){
		textLoad('full_del_but_'+id);
		$.post('/index.php?go=photo&act=del_comm', {hash: hash}, function(){
			$('#comment_all_'+id).html('<div style="padding-bottom:5px;color:#777;">'+lang_del_comm+'</div>');
		});
	},
	all: function(id, num){
		textLoad('all_lnk_comm_'+id);
		$('#all_href_lnk_comm_'+id).attr('onClick', '').attr('href', '#');
		$.post('/index.php?go=photo&act=all_comm', {pid: id, num: num}, function(d){
			$('#all_href_lnk_comm_'+id).hide();
			$('#all_comments_'+id).html(d);
		});
	},
}

//FRIENDS
var Friends = {
	Add: function(for_id, user_name){
		if(for_id){
			Page.Loading('start');
			
			if(user_name)
				name = user_name;
			else
				name = $('title').text();
			
			$.get('/friedns/send_demand/'+for_id, function(data){
				if(data == 'yes_demand')
					Box.Info('add_demand_'+for_id, lang_demand_ok, lang_demand_no, 300);
				else if(data == 'yes_demand2')
					Box.Info('add_demand_k_'+for_id, lang_dd2f_no, lang_dd2f22_no, 300);
				else if(data == 'yes_friend')
					Box.Info('add_demand_k_'+for_id, lang_dd2f_no, lang_22dd2f22_no, 300);
				else
					Box.Info('add_demand_ok_'+for_id, lang_demand_ok, '<b><a href="'+location.href+'" onClick="Page.Go(this.href); return false">'+name+'</a></b> '+lang_demand_s_ok, 400);
					
				Page.Loading('stop');
			});
		}
	},
	DemandSended: function(for_id){
		Box.Info('add_sending_demand_'+for_id, lang_demand_sending, lang_demand_sending_t);
	},
	Take: function(take_user_id){
		Page.Loading('start');
		$.get('/friedns/take/'+take_user_id, function(data){
			Page.Loading('stop');
			$('.friends_send_message_button').hide();
			$('#friends_send_message_button').hide();
			$('.friends_delete_friend_button').hide();
			$('#action_'+take_user_id).html(lang_take_ok).css('color', '#777').fadeIn(1500);
		});
	},
	Reject: function(reject_user_id){
		Page.Loading('start');
		$.get('/friedns/reject/'+reject_user_id, function(data){
			Page.Loading('stop');
			$('.friends_send_message_button').hide();
			$('#friends_send_message_button').hide();
			$('.friends_delete_friend_button').hide();
			$('#action_'+reject_user_id).html(lang_take_no).fadeIn(1500);
		});
	},
	Delete: function(user_id, atype){
		if(atype){
			var ava_s1 = $('#avatar_'+user_id).attr('src');
			var ava = ava_s1.replace('/users/'+user_id+'/', '/users/'+user_id+'/');
		} else
			var ava = $('#avatar_'+user_id).attr('src');
		
		Box.Show('del_friend_'+user_id, 410, lang_title_del_photo, '<div style="padding:15px;text-align:center"><img src="'+ava+'" alt="" /><br /><br />Are you sure you want to remove this person from your friends list?</div>', lang_box_canсel, lang_box_yes, 'Friends.goDelete('+user_id+', '+atype+'); return false');
	},
	goDelete: function(user_id, atype){
		$.post('/index.php?go=friends&act=delete', {delet_user_id: user_id}, function(data){
			if(atype > 0){
				Page.Go(location.href);
			} else {
				$('#friend_'+user_id).remove();
				updateNum('#friend_num');
			}
			$('#friend_delete_text').text('Add to friends');
			$('#friend_delete_button').attr('onClick', 'Friends.Add('+user_id+'); return false');
			
			Box.Close('del_friend_'+user_id);
		});
	}
}

//FAVE
var Favourites = {
	Add: function(fave_id){
		$('#add_favourites_load').show();
		$.post('/index.php?go=fave&act=add', {fave_id: fave_id}, function(data){
			if(data == 'no_user')
				Box.Info('add_fave_err_'+fave_id, lang_dd2f_no, lang_no_user_fave, 300);
			else if(data == 'yes_user')
				Box.Info('add_fave_err_'+fave_id, lang_dd2f_no, lang_yes_user_fave, 300);
				
			$('#add_favourites').attr('onClick', 'Favourites.Delete('+fave_id+'); return false').attr('href', '/');
			$('#text_add_favourites').text(lang_del_fave);
			$('#add_favourites_load').hide();
		});
	},
	Delete: function(fave_id){
		$('#add_favourites_load').show();
		$.post('/index.php?go=fave&act=delet', {fave_id: fave_id}, function(data){
			$('#add_favourites').attr('onClick', 'Favourites.Add('+fave_id+'); return false').attr('href', '/');
			$('#text_add_favourites').text(lang_add_fave);
			$('#add_favourites_load').hide();
		});
	},
	DeleteBox: function(fave_id){
		Box.Show('del_fave', 410, lang_title_del_photo, '<div style="padding:15px;">'+lang_fave_info+'</div>', lang_box_canсel, lang_box_yes, 'Favourites.goDelete('+fave_id+'); return false');
	},
	goDelete: function(fave_id){
		$('#box_loading').show();
		$.post('/index.php?go=fave&act=delet', {fave_id: fave_id}, function(data){
			$('#user_'+fave_id).remove();
			Box.Close('del_fave');
			
			fave_num = $('#fave_num').text();
			
			$('#fave_num').text(fave_num-1);
				
		});
	}
}

//MESSAGES
var Messages = {
	Write: function(user_id){
		var content = '<div style="padding:40px"><div style="float:left; padding-bottom:10px;">Enter your message:</div><textarea id="msg" style="width:362px; height:120px; padding:10px; resize:none;"></textarea><div class="clear"></div>'+
		'</div>';
		
		Box.Show('new_msg', 460, lang_new_msg, content, lang_box_canсel, lang_new_msg_send, 'Messages.Send('+user_id+'); return false');
		$('#msg').focus();
	},
	Send: function(for_user_id){
		var msg = $('#msg').val();
		if(msg != 0){
			$('#box_loading').show();
			$.post('/index.php?go=messages&act=send', {for_user_id: for_user_id, msg: msg}, function(data){
				Box.Close('new_msg');
				if(data == 'max_strlen')
					Box.Info('msg_info', lang_dd2f_no, lang_msg_max_strlen, 300);
				else if(data == 'no_user')
					Box.Info('msg_info', lang_dd2f_no, lang_no_user_fave, 300);
				else if(data == 'err_privacy')
					Box.Info('msg_info', lang_pr_no_title, lang_pr_no_msg, 400, 4000);
				else
					Box.Info('msg_info', lang_msg_ok_title, lang_msg_ok_text, 300);
			});
		} else {
			$('#msg').val('');
			$('#msg').focus();
		}
	},
	Search: function(folder){
		var msg_query = $('#msg_query').val();
		if(folder)
			var se_folder = '&act=outbox';
		else
			var se_folder = '';
			
		if(msg_query != 0 && msg_query != 'Search by reports received' && msg_query != 'Search for sent messages'){
			var se_query = '&se_query='+encodeURIComponent(msg_query);
			Page.Go('/index.php?go=messages'+se_folder+se_query);
		} else {
			$('#msg_query').val('');
			$('#msg_query').focus();
			$('#msg_query').css('color', '#000');
		}
	},
	Delete: function(mid, folder){
		$('#del_text_'+mid).remove();
		$('#del_load_'+mid).show();
		$.post('/index.php?go=messages&act=delet', {mid: mid, folder: folder}, function(){
			$('#bmsg_'+mid).remove();
			$('#del_load_'+mid).remove();
			updateNum('#all_msg_num');
			myhtml.title_close(mid);
		});
	},
	Reply: function(for_user_id, type){
		var msg = $('#msg_value').val();
		var attach_files = $('#vaLattach_files').val();
		if(msg != 0 || attach_files != 0){
			if(type == 'reply')
				butloading('msg_sending', 50, 'disabled');
			else
				butloading('msg_sending', 56, 'disabled');
			$.post('/index.php?go=messages&act=send', {for_user_id: for_user_id, msg: msg, attach_files: attach_files}, function(data){
				if(data == 'max_strlen')
					Box.Info('msg_info', lang_dd2f_no, lang_msg_max_strlen, 300);
				else if(data == 'no_user')
					Box.Info('msg_info', lang_dd2f_no, lang_no_user_fave, 300);
				else if(data == 'err_privacy')
					Box.Info('msg_info', lang_pr_no_title, lang_pr_no_msg, 400, 4000);
				else {
					Page.Go('/messages/i');
					$('.green_error').show();
				}

				if(type == 'reply')
					butloading('msg_sending', 50, 'enabled', 'Reply');
				else
					butloading('msg_sending', 56, 'enabled', 'Send');
			});
		} else {
			$('#msg_value').val('');
			$('#msg_value').focus();
		}
	},
	History: function(for_user_id, page){
		textLoad('history_lnk');
		if(page)
			Page.Loading('start');
		$.post('/index.php?go=messages&act=history', {for_user_id: for_user_id, page: page}, function(data){
			$('#history_lnk').hide();
			$('.msg_view_history_title').show();
			$('#msg_historyies').html(data);
			if(page)
				Page.Loading('stop');
		});
	}
}

//Following
var Following = {
	Add: function(for_user_id){
		$('#add_following_load').show();
		$.post('/index.php?go=following&act=add', {for_user_id: for_user_id}, function(d){
			$('#add_following_load').hide();
			$('#text_add_following').text(lang_unsubscribe);
			$('#following_button').attr('onClick', 'Following.Delete('+for_user_id+'); return false');
		});
	},
	Delete: function(del_user_id){
		$('#add_following_load').show();
		$.post('/index.php?go=following&act=delete', {del_user_id: del_user_id}, function(){
			$('#add_following_load').hide();
			$('#text_add_following').text(lang_subscription);
			$('#following_button').attr('onClick', 'Following.Add('+del_user_id+'); return false');
		});
	},
	All: function(for_user_id, page_num, following_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
			
		Box.Page('/index.php?go=following&act=all', 'for_user_id='+for_user_id+'&following_num='+following_num+page, 'followings_'+page_num, 436, 'Your following', lang_msg_close, 0, 0, 345, 1, 1, 1, 0, 1);
		
	}
}

var Followers = {
	All: function(uid, page_num, followers_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
		Box.Page('/index.php?go=following&act=followers', 'uid='+uid+'&followers_num='+followers_num+page, 'followers_'+page_num, 436, 'Your Followers', lang_msg_close, 0, 0, 345, 1, 1, 1, 0);    
	},
}

//VIDEOS
var videos = {
	add: function(videos){
		Box.Page('/index.php?go=videos&act=add', '', 'add_video', 510, lang_video_new, lang_box_canсel, lang_album_create, 'videos.send('+videos+'); return false', 0, 0, 1, 1, 'video_lnk');
	},
	load: function(){
		video_lnk = $('#video_lnk').val();
		good_video_lnk = $('#good_video_lnk').val();
		if(videos.serviece(video_lnk)){
			if(video_lnk != 0){
				if(video_lnk != good_video_lnk){
					$('#box_loading').show();
					$.post('/index.php?go=videos&act=load', {video_lnk: video_lnk}, function(data){
						if(data == 'no_serviece'){
							$('#no_serviece').show();
						} else {
							row = data.split(':|:');
							$('#result_load').show();
							$('#photo').html('<img src="'+row[0]+'" alt="" id="res_photo_ok" />');
							$('#title').val(row[1]);
							$('#descr').val(row[2]);
							$('#good_video_lnk').val(video_lnk);
							$('#no_serviece').hide();
						}
						$('#box_but').show();
						$('#box_loading').hide();
					});
				} else
					$('#no_serviece').hide();
			} else
				$('#result_load').hide();
		} else
			$('#no_serviece').show();
	},
	serviece: function(request){
		var pattern = new RegExp(/http:\/\/www.youtube.com|http:\/\/youtube.com|http:\/\/www.vimeo.com|http:\/\/vimeo.com|http:\/\/smotri.com|http:\/\/www.smotri.com/i);
		return pattern.test(request);
	},
	send: function(videos){
		title = $('#title').val();
		good_video_lnk = $('#good_video_lnk').val();
		descr = $('#descr').val();
		photo = $('#res_photo_ok').attr('src');
		if(good_video_lnk != 0){
			if(title != 0){
				$('#box_loading').show();
				$('#box_but').hide();
				$.post('/index.php?go=videos&act=send', {title: title, good_video_lnk: good_video_lnk, descr: descr, photo: photo, privacy: $('#privacy').val(), videos: videos}, function(d){
					$('#box_loading').hide();
					Box.Close('add_video');
					d = d.split('|');
					if(notes == 1)
						wysiwyg.boxVideo('http://'+location.host+d[0], d[1], d[2]);
					else
						Page.Go('/videos');
				});
			} else
				Box.Info('msg_videos', lang_dd2f_no, lang_videos_no_url, 300);
		} else
			Box.Info('msg_videos', lang_dd2f_no, lang_videos_no_url, 300);
	},
	page: function(){
		name = $('.scroll_page').attr('id');
		get_user_id = $('#user_id').val();
		last_id = $('#'+name+' input:last').attr('value');
		set_last_id = $('#set_last_id').val();
		videos_size = $('#videos_num').val();
		videos_opened_num = $('.onevideo').size();
		
		if(set_last_id != last_id && videos_size > 20 && videos_size != videos_opened_num){
			$('#'+name).append(lang_scroll_loading);
			$.post('/index.php?go=videos&act=page', {get_user_id: get_user_id, last_id: last_id}, function(d){
				$('#'+name).append(d);
				$('#scroll_loading').remove();
			});
			$('#set_last_id').val(last_id);
		}
	},
	scroll: function(){
		$(window).scroll(function(){
			if($(document).height() - $(window).height() <= $(window).scrollTop()+250){
				videos.page();
			}
		});
	},
	delet: function(vid, type){
		Box.Show('del_video_'+vid, 400, lang_title_del_photo, '<div style="padding:30px;" id="text_del_video_'+vid+'">'+lang_videos_del_text+'</div>', lang_box_canсel, lang_box_yes, 'videos.startDel('+vid+', \''+type+'\'); return false');
		$('#video_object').hide(); //скрываем код видео, чтоб модал-окно норм появилось
	},
	startDel: function(vid, type){
		$('#box_but').hide();
		$('#box_loading').show();
		$('#text_del_video_'+vid).text(lang_videos_deletes);
		$.post('/index.php?go=videos&act=delet', {vid: vid}, function(){
			$('#video_'+vid).html('<div style="margin-top:10px;"'+lang_videos_delok+'</div>');
			Box.Close('del_video_'+vid);
			updateNum('#nums');
			
			if(type == 1)
				$('#video_del_info').html(lang_videos_delok_2);
		});
	},
	editbox: function(vid){
		Box.Page('/index.php?go=videos&act=edit', 'vid='+vid, 'edit_video', 510, lang_video_edit, lang_box_canсel, lang_box_save, 'videos.editsave('+vid+'); return false', 285, 0, 1, 1, 0);
		$('#video_object').hide(); //скрываем код видео, чтоб модал-окно норм появилось
	},
	editsave: function(vid){
		var title = $('#title').val();
		var descr = $('#descr').val();
		$('#box_but').hide();
		$('#box_loading').fadeIn();
		$.post('/index.php?go=videos&act=editsave', {vid: vid, title: title, descr: descr, video_privacy: $('#video_privacy').val()}, function(d){
			$('#video_title_'+vid+', #video_full_title_'+vid).text(title);
			$('#video_descr_'+vid+', #video_full_descr_'+vid).html(d);
			Box.Close('edit_video');
			$('#video_object').show(); //показываем код видео, чтоб модал-окно норм появилось
		});
	},
	loading: function(f){
		if(f == 'start'){
			$('html').css('overflow', 'hidden');
			var loadcontent = '<div class="photo_view" id="video_load">'+
			'<div class="photo_close" onClick="videos.loadingClose(); return false" style="right:15px;"></div>'+
			'<div class="video_show_bg">'+
			'<div class="video_show_object" style="padding-top:230px;height:230px"><center><img src="/templates/Default/images/progress_gray.gif" alt="" /></center></div><div class="video_show_panel" style="height:20px"></div>'+
			'</div>'+
			'</div>';
			$('body').append(loadcontent);
			$('#video_load').show();
		} 
		if(f == 'stop')
			$('#video_load').remove();
	},
	loadingClose: function(){
		$('#video_load').remove();
	},
	show: function(vid, h, close_link){
		if(vid){
			videos.loading('start');
			$.post('/index.php?go=videos&act=view', {vid: vid, close_link: close_link}, function(data){
				videos.loading('stop');
				if(data == 'no_video'){
					Box.Info('no_video', lang_dd2f_no, lang_video_info_text, 300);
					$('html').css('overflow-y', 'auto');
				} else if(data == 'err_privacy'){
					addAllErr(lang_pr_no_title);
					$('html').css('overflow-y', 'auto');
				} else {
					$('html').css('overflow', 'auto');
					history.pushState({link:h}, null, h);
					$('body').append(data);
					$('#video_show_'+vid).show();
				}
			});
		}
	},
	prev: function(req_href){
		filter_one = req_href.split('_');
		filter_two = filter_one[0].split('video');
		video_url = '/video'+filter_two[1]+'_'+filter_one[1];
		videos.show(filter_one[1], video_url);
	},
	close: function(owner_id, close_link){
		$('.video_view').remove();
		$('html').css('overflow-y', 'auto');

		if(is_moz && !is_chrome) $(window).scrollTop(scrollTopForFirefox);
		
		if(close_link)
			history.pushState({link: close_link}, null, close_link);
		else
			history.pushState({link: '/videos/'+owner_id}, null, '/videos/'+owner_id);
	},
	addcomment: function(vid){
		comment = $('#comment').val();
		if(comment != 0){
			butloading('add_comm', '56', 'disabled', '');
			$.post('/index.php?go=videos&act=addcomment', {vid: vid, comment: comment}, function(d){
				$('#comments').append(d);
				$('#comment').val('');
				butloading('add_comm', '56', 'enabled', lang_box_send);
			});
		} else {
			$('#comment').val('');
			$('#comment').focus();
		}
	},
	allcomment: function(vid, num, owner_id){
		textLoad('all_lnk_comm');
		$('#all_href_lnk_comm').attr('onClick', '').attr('href', '#');
		$.post('/index.php?go=videos&act=all_comm', {vid: vid, num: num, owner_id: owner_id}, function(d){
			$('#all_href_lnk_comm').hide();
			$('#all_comments').html(d);
		});
	},
	deletcomm: function(comm_id){
		textLoad('video_del_but_'+comm_id);
		$.post('/index.php?go=videos&act=delcomment', {comm_id: comm_id}, function(){
			$('#video_comment_'+comm_id).html(lang_del_comm);
		});
	},
	setEvent: function(event, owner_id, close_link){
		var oi = (event.target) ? event.target.id: ((event.srcElement) ? event.srcElement.id : null);
		var el = oi.substring(0, 10);
		if(el == 'video_show')
			videos.close(owner_id, close_link);
	},
	addmylist: function(vid){
		$('#addok_hide').hide();
		$('#addok').show().html('Добавлено');
		$.post('/index.php?go=videos&act=addmylist', {vid: vid});
	}
}

//SEARCH
function CheckRequestSearch(request){
	var pattern = new RegExp(/search/i);
 	return pattern.test(request);
}

var gSearch = {
	go: function(){
		var query = $('#query').val();
		
		//Если открыта страница поиска
		if(CheckRequestSearch(location.href)){
			query = $('#query_full').val();
			gender = $('#gender').val();
			day = $('#day').val();
			month = $('#month').val();
			year = $('#year').val();
			country = $('#country').val();
			city = $('#select_city').val();
			type = $('#se_type_full').val();
			online = $('#online').val();
			avatar = $('#avatar').val();
		}
		
		if(query == 'Search..'){
			query = '';
		} 
		
		if(CheckRequestSearch(location.href) && type == 1){
				if(gender != 0) all_queryeis_gender = '&gender='+gender;
				else all_queryeis_gender = '';
				if(day != 0) all_queryeis_day = '&day='+day;
				else all_queryeis_day = '';
				if(month != 0) all_queryeis_month = '&month='+month;
				else all_queryeis_month = '';
				if(year != 0) all_queryeis_year = '&year='+year;
				else all_queryeis_year = '';
				if(country != 0) all_queryeis_country = '&country='+country;
				else all_queryeis_country = '';
				if(city != 0) all_queryeis_city = '&city='+city;
				else all_queryeis_city = '';
				if(online != 0) all_queryeis_online = '&online='+online;
				else all_queryeis_online = '';
				if(avatar != 0) all_queryeis_avatar = '&avatar='+avatar;
				else all_queryeis_avatar = '';
				res_sort_query = all_queryeis_gender+all_queryeis_day+all_queryeis_month+all_queryeis_year+all_queryeis_country+all_queryeis_city+all_queryeis_online+all_queryeis_avatar;
			} else
				res_sort_query = '';
			
			lnk = '/?go=search&query='+encodeURIComponent(query)+'&type='+type+res_sort_query;
			Page.Loading('start');
			$.post(lnk, {ajax: 'yes'}, function(data){
				Page.Loading('stop');
				history.pushState({link:lnk}, null, lnk);
				$('#page').html(data);
				('#query_full').focus();
				//Прокручиваем страницу в самый верх
				$('html').scrollTop(0);
				//Удаляем кеш фоток и видео
				$('.photo_view, .box_pos, .box_info, .video_view').remove();
				//Возвращаем scroll
				$('html').css('overflow-y', 'auto');
			});
	}
}

//CHECKBOX
var myhtml = {
	checkbox: function(id){
		name = '#'+id;
		$(name).addClass('html_checked');
		
		if(ge('checknox_'+id)){
			myhtml.checkbox_off(id);
		} else {
			$(name).append('<div id="checknox_'+id+'"><input type="hidden" id="'+id+'" /></div>');
			$(name).val('1');
		}
	},
	checkbox_off: function(id){
		name = '#'+id;
		$('#checknox_'+id).remove();
		$(name).removeClass('html_checked');
		$(name).val('');
	},
	checked: function(arr){
		$.each(arr, function(){
			myhtml.checkbox(this);
		});
	},
	title: function(id, text, prefix_id, pad_left){
		if(!pad_left)
			pad_left = 5;
			
		$("body").append('<div id="js_title_'+id+'" class="js_titleRemove"><div id="easyTooltip">'+text+'</div><div class="tooltip"></div></div>');	
		xOffset = $('#'+prefix_id+id).offset().left-pad_left;
		yOffset = $('#'+prefix_id+id).offset().top-32;
		
		$('#js_title_'+id)
			.css("position","absolute")
			.css("top", yOffset+"px")
			.css("left", xOffset+"px")						
			.css("display", "none")
			.css("z-index", "5000")
			.fadeIn('fast');
			
		$('#'+prefix_id+id).mouseout(function(){
			$('.js_titleRemove').remove();
		});
	},
	title_close: function(id){
		$('#js_title_'+id).remove();
	},
	updateAjaxNav: function(gc, pref, num, page){
		$.get('/updateAjaxNav', {gcount: gc, pref: pref, num: num, page:page}, function(data){
			$('#nav').html(data);
		});
	},
	scrollTop: function(){
		$('.scroll_fix_bg').hide(); 
		$(window).scrollTop(0);
	}
}

//WALL
var Post = {
	ShowMenu: function(rid){
		if($('#wall_function_block_'+rid).css('display') == 'none'){	
			$('#wall_function_block_'+rid).css('display', 'block');
		} else if($('#wall_function_block_'+rid).css('display') == 'block'){	
			$('#wall_function_block_'+rid).css('display', 'none');
		} else {
			$('#wall_function_block_'+rid).css('display') == 'block';
		}
		$.post('/index.php?go=wall', {rid: rid});
	},
	Send: function(){
		wall_text = $('#wall_text').val();
		
		attach_files = $('#vaLattach_files').val();
		for_user_id = location.href.split('http://'+location.host+'/u');
		
		rec_num = parseInt($('#profile_posts_num').text())+1;
		if(!rec_num)
			rec_num = 1;
			
		if(wall_text != 0 || attach_files != 0){
			butloading('wall_send', 56, 'disabled');
			$.post('/index.php?go=wall&act=send', {wall_text: wall_text, for_user_id: for_user_id[1], attach_files: attach_files}, function(data){
				if(data == 'err_privacy'){
					addAllErr(lang_pr_no_title);
				} else {
					$('#wall_records').html(data);
					$('#wall_all_record').html('');
					$('#profile_posts_num').text(rec_num)
					$('#wall_text').val('');
					$('#attach_files').hide();
					$('#attach_files').html('');
					$('#vaLattach_files').val('');
					butloading('wall_send', 56, 'enabled', 'Отправить');
				}
				
			});
		} else {
			$('#wall_text').val('');
			$('#wall_text').focus().css('border-color', '#fe4d4d');
			$('#profile_wall_ava_arrow').css('background-position', '0px -76px');
		}
	},
	Delete: function(rid){
		var rec_num = parseInt($('#profile_posts_num').text())-1;
		if(!rec_num)
			rec_num = '0';
		$('#profile_posts_num').text(rec_num)
		$('#wall_record_'+rid).html("<div style='padding:10px; text-align:center; transition:ease 2s'>"+lang_wall_del_ok+"</div>").fadeTo(500);
		setTimeout($('#wall_record_'+rid).html("<div style='padding:10px; text-align:center'>"+lang_wall_del_ok+"</div>").fadeOut(500), 1000);
		$('#wall_fast_block_'+rid).remove();
		myhtml.title_close(rid);
		if(rec_num == 0){
			$('.profile_records_none').show();
		}
		$.post('/index.php?go=wall&act=delet', {rid: rid});
	},
	AttachPhoto: function(id, page_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
		
		Box.Page('/index.php?go=albums&act=all_photos_box', page, 'all_photos_'+page_num, 627, lang_wall_attatch_photos, lang_box_canсel, 0, 0, 410, 1, 1, 1, 0, 1);
	},
	AttachVideo: function(id, page_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
		
		Box.Page('/index.php?go=videos&act=all_videos', page, 'all_videos_'+page_num, 627, lang_wall_attatch_videos, lang_box_canсel, 0, 0, 400, 1, 1, 1, 0, 1);
	},

	AttachInsert: function(type, data, action_url, uid){
		$('#attach_files').show();
		var attach_id = Math.floor(Math.random()*(1000-1+1))+1;
		var for_user_id = location.href.split('/u');
		if(uid)
			for_user_id[1] = uid;
		
		//Если вставляем фотографию
		if(type == 'photo'){
			Box.Close('all_photos', 1);
			res_attach_id = 'photo_'+attach_id;
			$('#attach_files').append('<span id="attach_file_'+res_attach_id+'" class="attach_photo"><div class="profile_post_attach_photo"><div class="profile_post_attach_del" onMouseOver="myhtml.title(\''+res_attach_id+'\', \''+lang_wall_no_atttach+'\', \'wall_photo_\')" onMouseOut="myhtml.title_close(\''+res_attach_id+'\')" onClick="Post.AttachPhotoDelete(\''+res_attach_id+'\', \'photo_u|'+action_url+'||\')" id="wall_photo_'+res_attach_id+'"></div><div class="profile_post_attach_photo_ico"></div><img src="'+data+'" alt="" /></div></span>');
			$('#vaLattach_files').val($('#vaLattach_files').val()+'photo_u|'+action_url+'||');
		}
		
		//Если вставляем видео
		if(type == 'video'){
			Box.Close('all_videos', 1);
			res_attach_id = 'video_'+attach_id;
			aPslit = action_url.split('|');
			action_url = action_url.replace('http://'+location.host+'/uploads/videos/'+aPslit[2]+'/', '');
			$('#attach_files').append('<span id="attach_file_'+res_attach_id+'" class="attach_video"><div class="profile_post_attach_video"><div class="profile_post_attach_del" onMouseOver="myhtml.title(\''+res_attach_id+'\', \''+lang_wall_no_atttach+'\', \'wall_video_\')" onMouseOut="myhtml.title_close(\''+res_attach_id+'\')" onClick="Post.AttachVideoDelete(\''+res_attach_id+'\', \'video|'+action_url+'||\')" id="wall_video_'+res_attach_id+'"></div><div class="profile_post_attach_video_ico"></div><img src="'+data+'" alt="" /></div></span>');
			$('#vaLattach_files').val($('#vaLattach_files').val()+'video|'+action_url+'||');
		}

		count_photo = $('.attach_photo').size();
		if(count_photo > 3)
			$('#wall_attach_photo').attr('onMouseOver', 'myhtml.title("photo", "Вы можете прикрепить не более 4-ёх фотографий", "wall_attach_")').attr('onClick', '').css('cursor', 'default');
		count_video = $('.attach_video').size();
		if(count_video > 3)
			$('#wall_attach_video').attr('onMouseOver', 'myhtml.title("video", "Вы можете прикрепить не более 4-ёх видеозаписей", "wall_attach_")').attr('onClick', '').css('cursor', 'default');
	},
	AttachPhotoDelete: function(id, realId){
		$('#vaLattach_files').val($('#vaLattach_files').val().replace(realId, ''));
		$('#attach_file_'+id).remove();
		myhtml.title_close(id);
		count_photo = $('.attach_photo').size();
		if(!count_photo)
			$('#attach_files').hide();

		if(count_photo < 4)
			$('#wall_attach_photo').attr('onMouseOver', 'myhtml.title("photo", "Прикрепить к записи фотографию", "wall_attach_")').attr('onClick', 'Post.AttachPhoto()').css('cursor', 'pointer');
	},
	AttachVideoDelete: function(id, realId){
		$('#vaLattach_files').val($('#vaLattach_files').val().replace(realId, ''));
		$('#attach_file_'+id).remove();
		myhtml.title_close(id);
		count_video = $('.attach_video').size();
		if(!count_video)
			$('#attach_files').hide();

		if(count_video < 4)
			$('#wall_attach_video').attr('onMouseOver', 'myhtml.title("video", "Прикрепить к записи видеозапись", "wall_attach_")').attr('onClick', 'Post.AttachVideo()').css('cursor', 'pointer');;
	},
	All: function(for_user_id){
		textLoad('wall_link');
		$('#wall_l_href').attr('onClick', '');
		last_id = $('.wallrecord:last').attr('id').replace('wall_record_', '');
		rec_num = parseInt($('#wall_rec_num').text());
		$.post('/index.php?go=wall&act=page', {last_id: last_id, for_user_id: for_user_id}, function(data){
			$('#wall_all_record').append(data);
			$('#wall_l_href').attr('onClick', 'Post.All('+for_user_id+'); return false');
			$('#wall_link').html(lang_wall_all_lnk);
			count_record = $('.wallrecord').size();
			if(count_record >= rec_num)
				$('#wall_l_href').hide();
		});
	},
	OpenFastCommentFormShow: function(rid){
		val = $('.wall_fast_text').val();
		$('.wall_fast_texatrea').val(''); //Текстовое значение полей Texatrea делаем 0
		$('.wall_post_post_comment_input').hide(); //закрываем окно комментирование и полей textarea комментирования
		$('#fast_form_'+rid).show(); //показываем форум комментирования
		$('#fast_comm_link_'+rid).hide(); //скрываем кнопку комментировать
	},
	OpenFastCommentFormShow2: function(rid){
		val = $('.wall_fast_text2').val();
		$('.wall_fast_texatrea2').val(''); //Текстовое значение полей Texatrea делаем 0
		$('.wall_fast_texatrea2').show(); //Текстовое значение полей Texatrea делаем 0
		$('.wall_post_post_comment_input2').hide(); //закрываем окно комментирование и полей textarea комментирования
		$('#fast_form').show(); //показываем форум комментирования
		$('#fast_comm_link_'+rid).hide(); //скрываем кнопку комментировать
	},
	OpenFastCommentFormHide: function(){
		$('.wall_fast_texatrea').val(''); //Текстовое значение полей Texatrea делаем 0
		$('.wall_post_fast_comment_form, .wall_post_fast_comment_form2').hide();//закрываем окно комментирование и полей textarea комментирования
		$('.wall_post_post_comment_input').show(); //возвращаем input поле со словом "Комментировать..." и кнопку комменатировать
	},
	FastCommentSend: function(rid, for_user_id, type){
		wall_text = $('#fast_text_'+rid).val();
		if(wall_text != 0){
			butloading('fast_buts_'+rid, 56, 'disabled');
			$.post('/index.php?go=wall&act=send', {wall_text: wall_text, for_user_id: for_user_id, rid: rid, type: type}, function(data){
				if(data == 'err_privacy'){
					addAllErr(lang_pr_no_title);
				} else {
					$('#ava_rec_'+rid); //добавляем для авы класс wall_ava_mini
					$('#fast_textarea_'+rid).remove(); //удаляем полей texatra 
					$('#fast_comm_link_'+rid).remove(); //удаляем кнопку комментировать
					$('#wall_fast_comment_'+rid).html(data); //выводим сам результат
					$('.wall_fast_text').val(''); //Текстовое значение полей Texatrea делаем 0
					$('.wall_fast_texatrea').val(''); //Текстовое значение полей Texatrea делаем 0
					$('.wall_post_fast_comment_form').hide();//закрываем окно комментирование и полей textarea комментирования
					$('.wall_post_post_comment_input').hide(); //возвращаем input поле со словом "Комментировать..." и кнопку комменатировать
					$('.wall_post_post_comment_input2').show();
				}
				butloading('fast_buts_'+rid, 56, 'enabled', lang_box_send);
			});
		} else {
			$('#fast_text_'+rid).val('');
			$('#fast_text_'+rid).focus();
		}
	},
	CommentDelete: function(rid){
		$('#wall_fast_comment_'+rid).html('<div style="padding-bottom:10px;">'+lang_wall_del_com_ok+'</div>');
		$.post('/index.php?go=wall&act=delet', {rid: rid});
	},
	AllComments: function(rid, for_user_id, type){
		textLoad('wall_all_comm_but_'+rid);
		$('#wall_all_but_link_'+rid).attr('onClick', '');
		$.post('/index.php?go=wall&act=all_comm', {fast_comm_id: rid, for_user_id: for_user_id, type: type}, function(data){
			if(data == 'err_privacy')
				addAllErr(lang_pr_no_title);
			else
				$('#wall_fast_comment_'+rid).html(data); //выводим сам результат
		});
	},
	AddLike: function(rec_id, user_id, type){
		if($('#wall_like_cnt'+rec_id).text())
			var wall_like_cnt = parseInt($('#wall_like_cnt'+rec_id).text())+1;
		else {
			$('#wall_posts_post_liked_users_'+rec_id).show();
			$('#update_like'+rec_id).val('1');
			var wall_like_cnt = 1;
		}
		
		$('#wall_like_cnt'+rec_id).html(wall_like_cnt);
		$('#wall_like_link'+rec_id).attr('onClick', 'Post.RemoveLike('+rec_id+', '+user_id+', \''+type+'\')');
		$('#like_user'+user_id+'_'+rec_id).show();
		updateNum('#like_text_num'+rec_id, 1);
		
		if(type == 'uPages')
			$.post('/index.php?go=wall&act=like_yes', {rid: rec_id});
	},
	RemoveLike: function(rec_id, user_id, type){
		var wall_like_cnt = parseInt($('#wall_like_cnt'+rec_id).text())-1;
		if(wall_like_cnt <= 0){
			var wall_like_cnt = '';
			$('#wall_posts_post_liked_users_'+rec_id).hide();
		}
		
		$('#wall_like_cnt'+rec_id).html(wall_like_cnt);
		$('#wall_like_link'+rec_id).attr('onClick', 'Post.AddLike('+rec_id+', '+user_id+', \''+type+'\')');
		$('#Xlike_user'+user_id+'_'+rec_id).hide();
		$('#like_user'+user_id+'_'+rec_id).hide();
		updateNum('#like_text_num'+rec_id);

		if(type == 'uPages')
			$.post('/index.php?go=wall&act=like_no', {rid: rec_id});
	},
	LikedFiveUsers: function(rec_id, type){		
		$('.wall_posts_post_liked_users').hide();
		if(!ge('like_cache_block'+rec_id) && $('#wall_like_cnt'+rec_id).text() && $('#update_like'+rec_id).val() == 0){
			if(type == 'uPages'){
				$.post('/index.php?go=wall&act=liked_users', {rid: rec_id}, function(data){
					$('#likes_users'+rec_id).html(data+'<span id="like_cache_block'+rec_id+'"></span>');
					$('#wall_posts_post_liked_users_'+rec_id).show();
				});
			}
		} else
			if($('#wall_like_cnt'+rec_id).text())
				$('#wall_posts_post_liked_users_'+rec_id).show();
	},
	LikedFiveUsersHide: function(){
		$('.wall_posts_post_liked_users').hide();
	},
	AllLikedUsers: function(rid, page_num, liked_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
			
		Box.Page('/index.php?go=wall&act=all_liked_users', 'rid='+rid+'&liked_num='+liked_num+page, 'all_liked_users_'+rid+page_num, 436, lang_wall_liked_users, lang_msg_close, 0, 0, 345, 1, 1, 1, 0, 1);
	},
	PhotoView: function(rec_id, public_id, src, pos){
		var photo = $('#photo_wall_'+rec_id+'_'+pos).attr('src').replace('c_', '');
		var size = $('.page_num'+rec_id).size();
		if(size == 1){
			var topTxt = 'Просмотр фотографии';
			var next = 'Photo.Close(\'\'); return false';
		} else {
			var topTxt = 'Фотография <span id="pTekPost">'+pos+'</span> из '+size;
			var next = 'Post.PhotoViewNext('+rec_id+'); return false';
		}
		
		var content = '<div id="photo_view" class="photo_view" onClick="Post.PhotoViewSetEvent(event)">'+
'<div class="photo_close" onClick="Photo.Close(\'\'); return false;"></div>'+
 '<div class="photo_bg cursor_pointer" onClick="'+next+'" style="min-height:400px">'+
  '<div class="photo_com_title" style="padding-top:0px;">'+topTxt+'<div><a href="/" onClick="Photo.Close(\'\'); return false">Закрыть</a></div></div>'+
  '<div class="photo_img_box"><img src="'+photo+'" id=\"photo_view_src\" style="margin-bottom:7px" /></div><div class="line_height">'+
  '<input type="hidden" id="photo_pos" value="'+pos+'" />'+
  '</div><div class="clear"></div>'+
 '</div>'+
'<div class="clear"></div>'+
'</div>';

		$('body').append(content);
		$('#photo_view').show();

		if(is_moz && !is_chrome) scrollTopForFirefox = $(window).scrollTop();
		
		$('html').css('overflow-y', 'hidden');
		
		if(is_moz && !is_chrome) $(window).scrollTop(scrollTopForFirefox);
		
	},
	PhotoViewNext: function(rec_id){
		var pos = parseInt($('#photo_pos').val())+1;
		if($('#photo_wall_'+rec_id+'_'+pos).attr('src'))
			var next_src = $('#photo_wall_'+rec_id+'_'+pos).attr('src').replace('c_', '');
		else
			var next_src = false;

		$('#photo_pos').val(pos);
		$('#pTekPost').text(pos);
		
		//Если уже последняя фотка, то следующей фоткой делаем первую
		if(pos > $('.page_num'+rec_id).size()){
			$('#photo_pos').val('1');
			$('#pTekPost').text('1');
			var next_src = $('#photo_wall_'+rec_id+'_1').attr('src').replace('c_', '');
		}
		$('#photo_view_src').attr('src', next_src);
	},
	PhotoViewSetEvent: function(event){
		var oi = (event.target) ? event.target.id: ((event.srcElement) ? event.srcElement.id : null);
		if(oi == 'photo_view')
			Photo.Close('');
	},
	FullText: function(rid){
		$('#hide_wall_rec'+rid).css('max-height', 'none');
		$('#hide_wall_rec_lnk'+rid).hide();
	}
}

//BBCODES
var bbcodes = {
	tag: function(ibTag, ibClsTag, source){
		if(!source)
			source = '';
		bbcodes.insert(ibTag+source, ibClsTag);
	},
	insert: function(ibTag, ibClsTag){
		var obj_ta = eval('document.entryform.text');
		var ss = obj_ta.selectionStart;
        var st = obj_ta.scrollTop;
        var es = obj_ta.selectionEnd;
		var start = (obj_ta.value).substring(0, ss);
        var middle = (obj_ta.value).substring(ss, es);
        var end = (obj_ta.value).substring(es, obj_ta.textLength);
		middle = ibTag + middle + ibClsTag;
		obj_ta.value = start + middle + end;
        var cpos = ss + (middle.length);
        obj_ta.selectionStart = cpos;
        obj_ta.selectionEnd = cpos;
		obj_ta.focus();
	}
}

var wysiwyg = {
	boxPhoto: function(img, uid, pid){
		Box.Close('all_photos', 1);
		lang_notes_sett_box_content	= '<div style="padding:15px">'+
		'<div class="texta" style="width:90px">Ширина:</div><input type="text" id="width_'+pid+'" class="inpst" maxlength="3" size="3" value="140" /> &nbsp;px<div class="mgclr"></div>'+
		'<div class="texta" style="width:90px">Высота:</div><input type="text" id="height_'+pid+'" class="inpst" maxlength="3" size="3" value="100" /> &nbsp;px<div class="mgclr"></div>'+ 
		'<div class="texta" style="width:90px">Выравнивание:</div><div class="padstylej"><select class="inpst" id="pos_'+pid+'"><option value="0">стандартно</option><option value="1">по левому краю</option><option value="2">по правому краю</option><option value="3">по центру</option></select></div><div class="mgclr"></div>'+ 
		'<div class="texta" style="width:90px">&nbsp;</div><div class="html_checkbox" id="img_link_'+pid+'" onClick="myhtml.checkbox(this.id)">Добавить ссылку</div><div class="mgclr"></div>'+ 
		'<div class="texta" style="width:90px">&nbsp;</div><div class="html_checkbox" id="img_blank_'+pid+'" onClick="myhtml.checkbox(this.id)" style="margin-top:5px">Открывать в новом окне</div><div class="mgclr"></div>'+ 
		'<div class="texta" style="width:90px">&nbsp;</div><div class="html_checkbox" id="img_border_'+pid+'" onClick="myhtml.checkbox(this.id)" style="margin-top:5px">Показывать рамку</div><div class="mgclr"></div>'+ 
		'</div>';
		Box.Show('note_add_photo_'+pid, 300, lang_notes_setting_addphoto, lang_notes_sett_box_content, lang_box_canсel, lang_box_save, 'wysiwyg.inPhoto(\''+img+'\', '+uid+', '+pid+')', 0, 0, 0, 0, 1);
		myhtml.checked(['img_link_'+pid, '0']);
	},
	inPhoto: function(img, uid, pid){
		Box.Close('note_add_photo_'+pid, 1);
		width = $('#width_'+pid).val();
		height = $('#height_'+pid).val();
		pos = $('#pos_'+pid).val();
		img_link = $('#img_link_'+pid).val();
		img_blank = $('#img_blank_'+pid).val();
		img_border = $('#img_border_'+pid).val();

		if(pos == 3){
			spos = '[center]';
			epos = '[/center]';
		} else {
			spos = '';
			epos = '';
		}
		
		bbcodes.tag(spos+'[photo]', '[/photo]'+epos, uid+'|'+pid+'|'+img+'|'+width+'|'+height+'|'+img_border+'|'+img_blank+'|'+pos+'|'+img_link);
	},
	boxVideo: function(img, uid, vid){
		Box.Close('all_videos', 1);
		lang_notes_sett_box_content	= '<div style="padding:15px">'+
		'<div class="texta" style="width:90px">Ширина:</div><input type="text" id="v_width_'+vid+'" class="inpst" maxlength="3" size="3" value="175" /> &nbsp;px<div class="mgclr"></div>'+
		'<div class="texta" style="width:90px">Высота:</div><input type="text" id="v_height_'+vid+'" class="inpst" maxlength="3" size="3" value="131" /> &nbsp;px<div class="mgclr"></div>'+ 
		'<div class="texta" style="width:90px">Выравнивание:</div><div class="padstylej"><select class="inpst" id="v_pos_'+vid+'"><option value="0">стандартно</option><option value="1">по левому краю</option><option value="2">по правому краю</option><option value="3">по центру</option></select></div><div class="mgclr"></div>'+ 
		'<div class="texta" style="width:90px">&nbsp;</div><div class="html_checkbox" id="v_img_blank_'+vid+'" onClick="myhtml.checkbox(this.id)" style="margin-top:5px">Открывать в новом окне</div><div class="mgclr"></div>'+ 
		'<div class="texta" style="width:90px">&nbsp;</div><div class="html_checkbox" id="v_img_border_'+vid+'" onClick="myhtml.checkbox(this.id)" style="margin-top:5px">Показывать рамку</div><div class="mgclr"></div>'+ 
		'</div>';
		Box.Show('note_add_video_'+vid, 300, lang_notes_setting_addvdeio, lang_notes_sett_box_content, lang_box_canсel, lang_box_save, 'wysiwyg.inVideo(\''+img+'\', '+uid+', '+vid+')', 0, 0, 0, 0, 1);
	},
	inVideo: function(img, uid, vid){
		Box.Close('note_add_video_'+vid, 1);
		width = $('#v_width_'+vid).val();
		height = $('#v_height_'+vid).val();
		pos = $('#v_pos_'+vid).val();
		img_blank = $('#v_img_blank_'+vid).val();
		img_border = $('#v_img_border_'+vid).val();	

		if(pos == 3){
			spos = '[center]';
			epos = '[/center]';
		} else {
			spos = '';
			epos = '';
		}

		bbcodes.tag(spos+'[video]', '[/video]'+epos, uid+'|'+vid+'|'+img+'|'+width+'|'+height+'|'+img_border+'|'+img_blank+'|'+pos);
	},
	linkBox: function(){
		lang_wysiwyg_box_content = '<div style="padding:15px">'+
		'<div class="texta" style="width:90px">Адрес ссылки:</div><input type="text" id="l_http" class="inpst" style="width:300px" /><div class="mgclr"></div>'+
		'<div class="texta" style="width:90px">Текст ссылки:</div><input type="text" id="l_text" class="inpst" style="width:300px" /><div class="mgclr"></div>'+ 
		'</div>';
		Box.Show('w_link', 450, lang_wysiwyg_title, lang_wysiwyg_box_content, lang_box_canсel, lang_box_save, 'wysiwyg.insertLink()');
		$('#l_http').focus();
	},
	insertLink: function(){
		link = $('#l_http').val();
		link_text = '|'+$('#l_text').val();
		if(!$('#l_text').val())
			link_text = '';
		if(link != 0)
			bbcodes.tag('[link]', '[/link]', link+link_text);
		Box.Close('w_link');
	}
}

//STATUS
var gStatus = {
	open: function(){
		$('#set_status_bg').fadeIn(100);
		$('#status_text').focus();
		$('#status_text').select();
		if($('#status_text').val()){
			$('.yes_status_text').show();
			$('.no_status_text').hide();
		} else {
			$('.yes_status_text').hide();
			$('.no_status_text').show();
		}
		$('.status_tell_friends').hide();
	},
	close: function(){
		$('#set_status_bg').hide();
		$('#status_text').val($('#status_text').val());
	},
	set: function(clear){
		text = $('#status_text').val();
		if(clear){
			text = '';
			$('#status_text').val('');
		}
		if(text != $('#new_status').text()){
			butloading('status_but', 55, 'disabled');
			$.post('/index.php?go=status', {text: text}, function(data){
				if(data){
					$('#status_link').hide();
					gStatus.tell();
					
					$('#new_status').attr('onMouseOver', 'gStatus.tell()');
					
				} else
					$('#status_link').show();
					
				$('#new_status').html(data);
				
				gStatus.close();
				butloading('status_but', 55, 'enabled', lang_box_save);
			});
		} else
			gStatus.close();
	},
	tell: function(){
		$('.status_tell_friends').hide();
		pos = $('#tellBlockPos').position().top;

		$('.status_tell_friends').fadeIn('fast');

		setTimeout(function(){
			$('.status_tell_friends').fadeOut('fast');
		}, 2500);
	},
	startTell: function(){
		for_user_id = location.href.split('http://'+location.host+'/u');
		text = $('#status_text').val();
		tell_friends = $('#tell_friends').val();
		if(tell_friends){
			if(text != 0){
				$.post('/index.php?go=wall&act=send', {wall_text: text, for_user_id: for_user_id[1]}, function(data){
					$('#wall_records').html(data);
					$('#wall_all_record').html('');
					updateNum('#wall_rec_num', 1);
				});
			}
		} else {
			insert_id = $('.wallrecord:first').attr('id').replace('wall_record_', '');
			wall.delet(insert_id);
		}
	}
}

//NEWS
var news = {
	page: function(){
		var type = $('#type').val();
		$('#wall_l_href_news').attr('onClick', '');
		if($('#loading_news').text() == 'Показать предыдущие новости'){
			textLoad('loading_news');
			$.post('/index.php?go=news&type='+type, {page: 1, page_cnt: page_cnt}, function(d){
				if(d != 'no_news'){
					$('#news').append(d);
					$('#wall_l_href_news').attr('onClick', 'news.page(\''+type+'\')');
					$('#loading_news').html('Показать предыдущие новости');
					page_cnt++;
				} else
					$('#wall_l_href_news').hide();
			});
		}
	},
	showWallText: function(id){
		var wh2 = $('#2href_text_'+id).width();
		var wh = $('#href_text_'+id).width()-wh2-40;
		$('.news_wall_msg_bg').hide();
		$('#wall_text_'+id).fadeIn('fast').css('margin-left', wh);
		$('#wall_text_'+id).mouseover(function(){
			$('#wall_text_'+id).fadeOut('fast');
		});
	},
	hideWallText: function(id){
		$('#wall_text_'+id).fadeOut('fast');
	}
}

//SETTINGS
var settings = {
	savenewmail: function(){
		var email = $('#email').val();
		if(settings.isValidEmailAddress(email)){
			butloading('saveNewEmail', '88', 'disabled', '');
			$.post('/index.php?go=settings&act=change_mail', {email: email}, function(d){
				if(d == 1){
					$('#err_email_yes').show();
				} else {
					$('#err_email').hide();
					$('#err_email_yes').hide();
					$('#ok_email').show();
				}
				butloading('saveNewEmail', '88', 'enabled', 'Сохранить адрес');
			});
		} else {
			$('#err_email').show();
			$('#err_email_yes').hide();
		}
	},
	isValidEmailAddress: function(emailAddress){
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	},
	saveNewPwd: function(){
		var old_password = $('#old_password').val();
		var password1 = $('#password1').val();
		var password2 = $('#password2').val();
		if(old_password != 0 && old_password.length >=6 && password1 != 0 && password2 != 0){
					butloading('saveNewPwd', 87, 'disabled');
					$.post('/index.php?go=settings&act=newpass', {old_password: old_password, password1: password1, password2: password2}, function(data){
						$('.pass_errors').hide();
						if(data == 1)
							$('#err_pass_1').show();
						else if(data == 2) {
							$('#err_pass_1').hide();
							$('#err_pass_2').show();
						} else {
							$('#err_pass_1').hide();
							$('#err_pass_2').hide();
							$('#ok_pass').show();
						} butloading('saveNewPwd', 87, 'enabled', 'Изменить пароль');
					});
		} else
			Box.Info('boxerr', 'Ошибка', 'Все поля <b>обязательны</b> для заполнения', 300);
			Box.Close('sec_code');
	},
	saveNewName: function(){
		var name = $('#name').val();
		var lastname = $('#lastname').val();
		if(name.length >= 2 && name != 0 && settings.isValidName(name)){
			if(lastname.length >= 2 && lastname != 0 && settings.isValidName(lastname)){
				butloading('saveNewName', 69, 'disabled');
				$.post('/index.php?go=settings&act=newname', {name: name, lastname: lastname}, function(data){
					$('#err_name_1').hide();
					$('#ok_name').show();
					butloading('saveNewName', 69, 'enabled', 'Изменить имя');
				});
			} else {
				$('#err_name_1').show();
				$('#ok_name').hide();
			}
		} else {
			$('#err_name_1').show();
			$('#ok_name').hide();
		}
	},
	isValidName: function(xname){
		var pattern = new RegExp(/^[a-zA-Zа-яА-Я]+$/);
		return pattern.test(xname);
	},
	privacyOpen: function(id){
		$('.sett_openmenu').hide();
		$('#privacyMenu_'+id).show();
	},
	privacyClose: function(id){
		$('#privacyMenu_'+id).fadeOut(120);
	},
	setPrivacy: function(val_id, mtext, opt, text_id){
		$('#'+val_id).val(opt);
		$('#'+text_id).text(mtext);
		$('#selected_p_'+text_id).text(mtext);
		settings.privacyClose(val_id);
	},
	event: function(event){
		var oi = (event.target) ? event.target.id: ((event.srcElement) ? event.srcElement.id : null);
		var fast_oi = oi.substring(0, 9);

		if(oi != 'privacyMenu_msg' && oi != 'privacy_lnk_msg' && oi != 'privacyMenu_wall1' && oi != 'privacy_lnk_wall1' && oi != 'privacyMenu_wall2' && oi != 'privacy_lnk_wall2' && oi != 'privacyMenu_wall3' && oi != 'privacy_lnk_wall3' && oi != 'privacyMenu_info' && oi != 'privacy_lnk_info')
			$('#privacyMenu_msg, #privacyMenu_wall1, #privacyMenu_wall2, #privacyMenu_wall3, #privacyMenu_info').fadeOut(120);
	},
	savePrivacy: function(){
		var val_msg = $('#val_msg').val();
		var val_wall1 = $('#val_wall1').val();
		var val_wall2 = $('#val_wall2').val();
		var val_wall3 = $('#val_wall3').val();
		var val_info = $('#val_info').val();
		butloading('savePrivacy', 55, 'disabled');
		$.post('/index.php?go=settings&act=saveprivacy', {val_msg: val_msg, val_wall1: val_wall1, val_wall2: val_wall2, val_wall3: val_wall3, val_info: val_info}, function(){
			$('#ok_update').show();
			butloading('savePrivacy', 55, 'enabled', 'Сохранить');
		});
	}
}

//BLACKLIST 
var Blacklist = {
	Add: function(bad_user_id){
		$('#add_blacklist_load').show();
		$.post('/index.php?go=settings&act=addblacklist', {bad_user_id: bad_user_id}, function(){
			$('#add_blacklist_button').attr('onClick', 'Blacklist.Delete('+bad_user_id+', 1); return false');
			$('#text_add_blacklist').text('Разблокировать');
			$('#add_blacklist_load').hide();
		});
	},
	Delete: function(bad_user_id, type){
		$('#add_blacklist_load').show();
		textLoad('del_'+bad_user_id);
		$.post('/index.php?go=settings&act=delblacklist', {bad_user_id: bad_user_id}, function(){
			if(type){
				$('#add_blacklist_button').attr('onClick', 'Blacklist.Add('+bad_user_id+'); return false');
				$('#text_add_blacklist').text('Заблокировать');
				$('#add_blacklist_load').hide();
			} else {
				$('#u'+bad_user_id).remove();
				updateNum('#badlistnum');
			}
		});
	}
}

//CROP
var crop = {
	start: function(id){
		$('.pinfo, .photo_prev_but, .photo_next_but').hide();
		$('#save_crop_text'+id).show();
		var x1w = $('#ladybug_ant'+id).width()-50;
		var y1h = $('#ladybug_ant'+id).height()-50;
		$('#i_left'+id).val('50');
		$('#i_top'+id).val('50');
		$('#i_width'+id).val(x1w);
		$('#i_height'+id).val(y1h);
		$('#ladybug_ant'+id).imgAreaSelect({
			minWidth: 100, 
			minHeight: 100, 
			handles: true, 
			x1: 50, 
			y1: 50, 
			x2: x1w, 
			y2: y1h,
			onSelectEnd: function(img, selection){
				$('#i_left'+id).val(selection.x1);
				$('#i_top'+id).val(selection.y1);
				$('#i_width'+id).val(selection.width);
				$('#i_height'+id).val(selection.height);
			}
			
		});
	},
	close: function(id){
		$('.pinfo, .photo_prev_but, .photo_next_but').show();
		$('#save_crop_text'+id).hide();
		$('#ladybug_ant'+id).imgAreaSelect({
			remove: true
		});
	},
	save: function(pid, uid){
		var i_left = $('#i_left'+pid).val();
		var i_top = $('#i_top'+pid).val();
		var i_width = $('#i_width'+pid).val();
		var i_height = $('#i_height'+pid).val();
		Page.Loading('start');
		$.post('/index.php?go=photo&act=crop', {i_left: i_left, i_top: i_top, i_width: i_width, i_height: i_height, pid: pid}, function(data){Page.Go('/u'+uid);});
	}
}

//SUPPORT
var support = {
	send: function(){
		var title = $('#title').val();
		var question = $('#question').val();
		if(title != 0 && title != 'Пожалуйста, добавьте заголовок к Вашему вопросу..'){
			if(question != 0 && question != 'Пожалуйста, расскажите о Вашей проблеме чуть подробнее..'){
				$('#cancel').hide();
				butloading('send', '56', 'disabled', '');
				$.post('/index.php?go=support&act=send', {title: title, question: question}, function(data){
					if(data == 'limit'){
						Box.Info('err', lang_support_ltitle, lang_support_ltext, 280, 2000);
					} else {
						var qid = data.split('r|x');
						$('#data').html(qid[0]);
						history.pushState({link:'/support?act=show&qid='+qid[1]}, null, '/support?act=show&qid='+qid[1]);
					}
					butloading('send', '56', 'enabled', 'Отправить');
				});
			} else
				setErrorInputMsg('question');
		} else
			setErrorInputMsg('title');
	},
	delquest: function(qid){
		Box.Show('del_quest', 400, lang_title_del_photo, '<div style="padding:15px;" id="text_del_quest">'+lang_support_text+'</div>', lang_box_canсel, lang_box_yes, 'support.startDel('+qid+'); return false');
	},
	startDel: function(qid){
		$('#box_loading').show();
		$.post('/index.php?go=support&act=delet', {qid: qid}, function(){
			Page.Go('/support');
		});
	},
	answer: function(qid, uid){
		var answer = $('#answer').val();
		if(answer != 0 && answer != 'Комментировать..'){
			butloading('send', '56', 'disabled', '');
			$.post('/index.php?go=support&act=answer', {answer: answer, qid: qid}, function(data){
				if(uid == 0)
					$('#status').text('Есть ответ.');
				else
					$('#status').text('Вопрос ожидает обработки.');
				$('#answers').append(data);
				$('#answer').val('');
				butloading('send', '56', 'enabled', lang_box_send);
			});
		} else
			setErrorInputMsg('answer');
	},
	delanswe: function(id){
		$('#asnwe_'+id).html(lang_del_comm);
		$.post('/index.php?go=support&act=delet_answer', {id: id});
	},
	close: function(qid){
		butloading('close', '30', 'disabled', '');
		$.post('/index.php?go=support&act=close', {qid: qid}, function(){
			$('#status').text('Есть ответ.');
			$('#close_but').hide();
		});
	}
}

//BLOG
var blog = {
	add: function(){
		var title = $('#title').val();
		var text = $('#text').val();
		if(title != 0){
			if(text != 0){
				butloading('notes_sending', 74, 'disabled');
				$.post('/index.php?go=blog&act=send', {title: title, text: text}, function(){
					Page.Go('/blog');
				});
			} else
				setErrorInputMsg('text');
		} else
			setErrorInputMsg('title');
	},
	del: function(id){
		Box.Show('del_quest', 400, lang_title_del_photo, '<div style="padding:15px;" id="text_del_quest">'+lang_news_text+'</div>', lang_box_canсel, lang_box_yes, 'blog.startDel('+id+'); return false');
	},
	startDel: function(id){
		$('#box_loading').show();
		$.post('/index.php?go=blog&act=del', {id: id}, function(){
			Page.Go('/blog');
		});
	},
	save: function(id){
		var title = $('#title').val();
		var text = $('#text').val();
		if(title != 0){
			if(text != 0){
				butloading('notes_sending', 55, 'disabled');
				$.post('/index.php?go=blog&act=save', {id: id, title: title, text: text}, function(){
					Page.Go('/blog?id='+id);
				});
			} else
				setErrorInputMsg('text');
		} else
			setErrorInputMsg('title');
	}
}

//GROUPS
var groups = {
	createbox: function(){
		Box.Show('create', 490, lang_groups_new, '<div style="padding:20px"><div class="videos_text">Название</div><input type="text" class="videos_input" id="title" maxlength="65" /></div>', lang_box_canсel, lang_groups_cretate, 'groups.creat()', 100, 0, 0, 0, 0, 'title');
		$('#title').focus();
	},
	creat: function(){
		var title = $('#title').val();
		if(title != 0){
			$('#box_loading').show();
			ge('box_butt_create').disabled = true;
			$.post('/index.php?go=groups&act=send', {title: title}, function(id){
				Box.Close();
				Page.Go('/public'+id);
			});
		} else
			setErrorInputMsg('title');
	},
	exit: function(id){
		$('#exitlink'+id).html('<div class="color777" style="margin-top:6px;margin-right:7px">Вы вышли из сообщества.</div>');
		$.post('/index.php?go=groups&act=exit', {id: id});
	},
	exit2: function(id, user_id){
		$('#no').hide();
		$('#yes').fadeIn('fast');
		updateNum('#traf');
		updateNum('#traf2');
		if($('#traf').text() == 0){
			$('#users_block').hide();
			$('#num2').html('<span class="color777">Вы будете первым.</span>');
		}
		
		$('#subUser'+user_id).remove();
		
		$.post('/index.php?go=groups&act=exit', {id: id});
	},
	login: function(id){
		$('#yes').hide();
		$('#no').fadeIn('fast');
		if($('#traf').text() == 0) $('#users_block').show();
		updateNum('#traf', 1);
		updateNum('#traf2', 1);
		$.post('/index.php?go=groups&act=login', {id: id});
	},
	loadphoto: function(id){
		Box.Page('/index.php?go=groups&act=loadphoto_page', 'id='+id, 'loadphoto', 400, lang_title_load_photo, lang_box_canсel, 0, 0, 0, 0, 0, 0, 0, 1);
	},
	delphoto: function(id){
		Box.Show('del_photo', 400, lang_title_del_photo, '<div style="padding:15px;">'+lang_del_photo+'</div>', lang_box_canсel, lang_box_yes, 'groups.startdelete('+id+')');
	},
	startdelete: function(id){
		$('#box_loading').show();
		ge('box_butt_create').disabled = true;
		$.post('/index.php?go=groups&act=delphoto', {id: id}, function(){
			$('#ava').attr('src', template_dir+'/images/no_ava.gif');
			$('#del_pho_but').hide();
			Box.Close();
		});
	},
	addcontact: function(id){
		Box.Page('/index.php?go=groups&act=addfeedback_pg', 'id='+id, 'addfeedback', 400, 'Добавление контактного лица', lang_box_canсel, 'Сохранить', 'groups.savefeedback('+id+')', 0, 0, 0, 0, 'upage', 0);
	},
	savefeedback: function(id){
		var upage = $('#upage').val();
		var office = $('#office').val();
		var phone = $('#phone').val();
		var email = $('#email').val();
		if($('#feedimg').attr('src') != template_dir+'/images/contact_info.png'){
			$('#box_loading').show();
			ge('box_butt_create').disabled = true;
			$.post('/index.php?go=groups&act=addfeedback_db', {id: id, upage: upage, office: office, phone: phone, email: email}, function(d){
				if(d == 1){
					Box.Info('err', 'Информация', 'Этот пользователь уже есть в списке контактов.', 300, 2000);
					ge('box_butt_create').disabled = false;
					$('#box_loading').hide();
				} else {
					Box.Close();
					Page.Go('/public'+id);
				}
			});
		} else
			setErrorInputMsg('upage');
	},
	allfeedbacklist: function(id){
		Box.Page('/index.php?go=groups&act=allfeedbacklist', 'id='+id, 'allfeedbacklist', 450, 'Контакты', 'Закрыть', 0, 0, 300, 1, 1, 1, 0, 0);
	},
	delfeedback: function(id, uid){
		$('#f'+uid+', #fb'+uid).remove();
		var si = $('.public_obefeed').size();
		updateNum('#fnumu');
		if(si <= 0){
			$('#feddbackusers').html('<div class="line_height color777" align="center">Страницы представителей, номера телефонов, e-mail<br /><a href="/public'+id+'" onClick="groups.addcontact('+id+'); return false">Добавить контакты</a></div>');
			$('.box_conetnt').html('<div align="center" style="padding-top:10px;color:#777;font-size:13px;">Список контактов пуст.</div><style>#box_bottom_left_text{padding-top:6px}</style>');
		}
		$.post('/index.php?go=groups&act=delfeedback', {id: id, uid: uid});
	},
	editfeedback: function(uid){
		$('#close_editf'+uid).hide();
		$('#editf'+uid).show();
		$('#email'+uid).val($('#email'+uid).val().replace(', ', ''));
	},
	editfeeddave: function(id, uid){
		var office = $('#office'+uid).val();
		var phone = $('#phone'+uid).val();
		var email = $('#email'+uid).val();
		$('#close_editf'+uid).show();
		$('#editf'+uid).hide();
		$('#okoffice'+uid).text(office);
		$('#okphone'+uid).text(phone);
		if(phone != 0 && email != 0)
			$('#okemail'+uid).text(', '+email);
		else
			$('#okemail'+uid).text(email);
			
		$.post('/index.php?go=groups&act=editfeeddave', {id: id, uid: uid, office: office, phone: phone, email: email});
	},
	checkFeedUser: function(){
		var upage = $('#upage').val();
		var pattern = new RegExp(/^[0-9]+$/);
		if(pattern.test(upage)){
			$.post('/index.php?go=groups&act=checkFeedUser', {id: upage}, function(d){
				d = d.split('|');
				if(d[0]){
					if(d[1])
						$('#feedimg').attr('src', '/uploads/users/'+upage+'/100_'+d[1]);
					else
						$('#feedimg').attr('src', template_dir+'/images/100_no_ava.png');
						
					$('#office').focus();
				} else {
					setErrorInputMsg('upage');
					$('#feedimg').attr('src', template_dir+'/images/contact_info.png');
				}
			});
		} else
			$('#feedimg').attr('src', template_dir+'/images/contact_info.png');
	},
	saveinfo: function(id){
		var title = $('#title').val();
		var descr = $('#descr').val();
		var adres_page = $('#adres_page').val();
		var comments = $('#comments').val();
		$('#e_public_title').text(title);
		if(descr != 0){
			$('#descr_display').show();
			$('#e_descr').html(descr);
		}
		if(!adres_page)	var adres_page = 'public'+id;
		var pattern = new RegExp(/^[a-zA-Z0-9_-]+$/);
		if(pattern.test(adres_page)){
			butloading('pubInfoSave', 55, 'disabled');
			$.post('/index.php?go=groups&act=saveinfo', {id: id, title: title, descr: descr, comments: comments, adres_page: adres_page}, function(d){
				if(d == 'err_adres')
					Box.Info('err', 'Ошибка', 'Такой адрес уже занят', 130, 1500);
				else
					if($('#prev_adres_page').val() == adres_page)
						groups.editformClose();
					else if(d == 'no_new')
						Page.Go('/public'+id);
					else
						Page.Go('/'+adres_page);
				
				butloading('pubInfoSave', 55, 'enabled', 'Сохранить');
			});
		} else {
			setErrorInputMsg('adres_page');
			Box.Info('err', 'Ошибка', 'Вы можете изменить короткий адрес Вашей страницы на более удобный и запоминающийся. Для этого введите имя страницы, состоящее из латинских букв, цифр или знаков «_» .', 300, 5500);
		}
	},
	editform: function(){
		$('#edittab1').slideDown('fast');
		$('#public_editbg_container').animate({scrollLeft: "+560"});
	},
	editformClose: function(){
		$('#public_editbg_container').animate({scrollLeft: "-560"}, 1000);
		setTimeout("$('#edittab1').slideUp('fast')", 200);
		$('#edittab2').hide();
	},
	edittab_admin: function(id){
		$('#edittab2').show();
		$('#public_editbg_container').animate({scrollLeft: "+1120"});
	},
	addadmin: function(id){
		var new_admin_id = $('#new_admin_id').val().replace('http://site.ru/u', '');
		var check_adm = $('#admin'+new_admin_id).text();
		if(new_admin_id && !check_adm){
			Box.Page('/index.php?go=groups&act=new_admin', 'new_admin_id='+new_admin_id, 'new_admin_id', 400, 'Назначение руководителя', 'Закрыть', 'Назначить руководителем', 'groups.send_new_admin('+id+', '+new_admin_id+')', 130, 0, 0, 0, 0, 0);
		} else
			addAllErr('Этот пользователь уже есть в списке руководителей.');
	},
	send_new_admin: function(id, new_admin_id){
		var ava = $('#adm_ava').attr('src');
		var adm_name = $('#adm_name').text();
		var data = '<div class="public_oneadmin" id="admin'+new_admin_id+'"><a href="/u'+new_admin_id+'" onClick="Page.Go(this.href); return false"><img src="'+ava+'" align="left" width="32" /></a><a href="/u'+new_admin_id+'" onClick="Page.Go(this.href); return false">'+adm_name+'</a><br /><a href="/" onClick="groups.deladmin(\''+id+'\', \''+new_admin_id+'\'); return false"><small>Удалить</small></a></div>';
		$('#admins_tab').append(data);
		Box.Close();
		$('#new_admin_id').val('');
		$.post('/index.php?go=groups&act=send_new_admin', {id: id, new_admin_id: new_admin_id});
	},
	deladmin: function(id, uid){
		$('#admin'+uid).remove();
		$.post('/index.php?go=groups&act=deladmin', {id: id, uid: uid});
	},
	wall_send: function(id){
		var wall_text = $('#wall_text').val();
		var attach_files = $('#vaLattach_files').val();

		if(wall_text != 0 || attach_files != 0){
			butloading('wall_send', 56, 'disabled');
			$.post('/index.php?go=groups&act=wall_send', {id: id, wall_text: wall_text, attach_files: attach_files, vote_title: $('#vote_title').val(), vote_answer_1: $('#vote_answer_1').val(), vote_answer_2: $('#vote_answer_2').val(), vote_answer_3: $('#vote_answer_3').val(), vote_answer_4: $('#vote_answer_4').val(), vote_answer_5: $('#vote_answer_5').val(), vote_answer_6: $('#vote_answer_6').val(), vote_answer_7: $('#vote_answer_7').val(), vote_answer_8: $('#vote_answer_8').val(), vote_answer_9: $('#vote_answer_9').val(), vote_answer_10: $('#vote_answer_10').val()}, function(data){
				if($('#rec_num').text() == 'Нет записей')
					$('.albtitle').html('<b id="rec_num">1</b> запись');
				else
					updateNum('#rec_num', 1);
				
				$('#wall_text').val('');
				$('#attach_files').hide();
				$('#attach_files').html('');
				$('#vaLattach_files').val('');
				wall.form_close();
				wall.RemoveAttachLnk();
				butloading('wall_send', 56, 'enabled', lang_box_send);
				$('#public_wall_records').html(data);
				
				if($('#rec_num').text() > 10){
					$('#page_cnt').val('1');
					$('#wall_all_records').show();
					$('#load_wall_all_records').html('к предыдущим записям');
				}
			});
		} else
			setErrorInputMsg('wall_text');
	},
	wall_send_comm: function(rec_id, public_id){
		var wall_text = $('#fast_text_'+rec_id).val();

		if(wall_text != 0){
			butloading('fast_buts_'+rec_id, 56, 'disabled');
			$.post('/index.php?go=groups&act=wall_send_comm', {rec_id: rec_id, wall_text: wall_text, public_id: public_id}, function(data){
				$('#fast_form_'+rec_id+', #fast_comm_link_'+rec_id).remove();
				$('#wall_fast_block_'+rec_id).html(data);
				var pattern = new RegExp(/news/i);
				if(pattern.test(location.href)) $('#fast_text_'+rec_id+', #fast_inpt_'+rec_id).css('width', '688px');
			});
		} else
			setErrorInputMsg('fast_text_'+rec_id);
	},
	wall_delet: function(rec_id){
		$('#wall_record_'+rec_id).html('<span class="color777">Запись удалена.</span>');
		$('#wall_fast_block_'+rec_id+', .wall_fast_opened_form').remove();
		$('#wall_record_'+rec_id).css('padding-bottom', '5px');
		myhtml.title_close(rec_id);
		updateNum('#rec_num');
		$.post('/index.php?go=groups&act=wall_del', {rec_id: rec_id});
	},
	comm_wall_delet: function(rec_id, public_id){
		$('#wall_fast_comment_'+rec_id).html('<div class="color777" style="margin-bottom:7px">Комментарий удалён.</div>');
		$.post('/index.php?go=groups&act=wall_del', {rec_id: rec_id, public_id: public_id});
	},
	wall_all_comments: function(rec_id, public_id){
		textLoad('wall_all_comm_but_'+rec_id);
		$('#wall_all_but_link_'+rec_id).attr('onClick', '');
		$.post('/index.php?go=groups&act=all_comm', {rec_id: rec_id, public_id: public_id}, function(data){
			$('#wall_fast_block_'+rec_id).html(data); //выводим сам результат
			var pattern = new RegExp(/news/i);
			if(pattern.test(location.href)) $('#fast_text_'+rec_id+', #fast_inpt_'+rec_id).css('width', '688px');
		});
	},
	wall_page: function(){
		var page_cnt = $('#page_cnt').val();
		var public_id = $('#public_id').val();
		$('#wall_all_records').attr('onClick', '');
		if($('#load_wall_all_records').text() == 'к предыдущим записям' && $('#rec_num').text() > 10){
			textLoad('load_wall_all_records');
			$.post('/index.php?go=public&pid='+public_id, {page_cnt: page_cnt}, function(data){
				$('#public_wall_records').append(data);
				$('#page_cnt').val((parseInt($('#page_cnt').val())+1));
				if($('.wallrecord').size() == $('#rec_num').text()){
					$('#wall_all_records').hide();
				} else {
					$('#wall_all_records').attr('onClick', 'groups.wall_page(\''+public_id+'\')');
					$('#load_wall_all_records').html('к предыдущим записям');
				}
			});
		}
	},
	wall_attach_addphoto: function(id, page_num, public_id){
		wall.attach_menu('close', 'wall_attach', 'wall_attach_menu');
		
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
		
		Box.Page('/index.php?go=groups&act=photos', 'public_id='+public_id+page, 'c_all_photos_'+page_num, 627, lang_wall_attatch_photos, lang_box_canсel, 0, 0, 400, 1, 0, 1, 0, 1);
	},
	wall_attach_insert: function(type, data, action_url){
		if(!$('#wall_text').val())
			wall.form_open();
		
		$('#attach_files').show();
		var attach_id = Math.floor(Math.random()*(1000-1+1))+1;

		//Если вставляем фотографию
		if(type == 'photo'){
			Box.Close('all_photos', 1);
			res_attach_id = 'photo_'+attach_id;
			$('#attach_files').append('<span id="attach_file_'+res_attach_id+'" class="attach_file"><div class="wall_attach_photo float_l"><div class="wall_attach_del" onMouseOver="myhtml.title(\''+res_attach_id+'\', \''+lang_wall_no_atttach+'\', \'wall_photo_\')" onMouseOut="myhtml.title_close(\''+res_attach_id+'\')" onClick="wall.attach_delete(\''+res_attach_id+'\', \'photo|'+action_url+'||\')" id="wall_photo_'+res_attach_id+'"></div><img src="'+data+'" alt="" /></div></span>');
			$('#vaLattach_files').val($('#vaLattach_files').val()+'photo|'+action_url+'||');
		}
		
		//Если вставляем видео
		if(type == 'video'){
			Box.Close('attach_videos');
			res_attach_id = 'video_'+attach_id;
			$('#attach_files').append('<span id="attach_file_'+res_attach_id+'" class="attach_file"><div class="wall_attach_photo float_l"><div class="wall_attach_del" onMouseOver="myhtml.title(\''+res_attach_id+'\', \''+lang_wall_no_atttach+'\', \'wall_photo_\')" onMouseOut="myhtml.title_close(\''+res_attach_id+'\')" onClick="wall.attach_delete(\''+res_attach_id+'\', \'video|'+action_url+'||\')" id="wall_photo_'+res_attach_id+'"></div><img src="'+data+'" alt="" /></div></span>');
			$('#vaLattach_files').val($('#vaLattach_files').val()+'video|'+action_url+'||');
		}

		var count = $('.attach_file').size();
		if(count > 9)
			$('#wall_attach').hide();
	},
	wall_video_add_box: function(){
		wall.attach_menu('close', 'wall_attach', 'wall_attach_menu');
		Box.Show('attach_videos', 400, 'Ссылка видеозаписи на УдинБала', '<div style="padding:15px;"><input  type="text"  placeholder="Введите ссылку видеозаписи на УдинБала.."  class="videos_input" id="video_attach_lnk" style="width:355px;margin-top:10px" /></div>', lang_box_canсel, 'Прикрпепить', 'groups.wall_video_add_select()');
		$('#video_attach_lnk').focus();
	},
	wall_video_add_select: function(){
		var video_attach_lnk = $('#video_attach_lnk').val().replace('http://'+location.host+'/video', '');
		var data = video_attach_lnk.split('_');
		if(video_attach_lnk != 0){
			$('#box_loading').show();
			ge('box_butt_create').disabled = true;
			$.post('/index.php?go=groups&act=select_video_info', {video_id: data[1]}, function(row){
				if(row == 1){
					addAllErr('Неверный адрес видеозаписи');
					$('#box_loading').hide();
					ge('box_butt_create').disabled = false;
				} else {
					groups.wall_attach_insert('video', '/uploads/videos/'+data[0]+'/'+row, row+'|'+data[1]+'|'+data[0]);
					$('#video_attach_lnk').val('');
				}
			});
		} else
			setErrorInputMsg('video_attach_lnk');
	},
	
	wall_tell: function(rec_id){
		$('#wall_tell_'+rec_id).hide();
		myhtml.title_close(rec_id);
		$('#wall_ok_tell_'+rec_id).fadeIn(150);
		$.post('/index.php?go=groups&act=wall_tell', {rec_id: rec_id}, function(data){
			if(data == 1)
				addAllErr(lang_wall_tell_tes);
		});
	},
	all_people: function(public_id, page_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
		
		var num = $('#traf').text();
			
		Box.Page('/index.php?go=groups&act=all_people', 'public_id='+public_id+'&num='+num+page, 'all_peoples_users_'+public_id+page_num, 525, 'Подписчики', lang_msg_close, 0, 0, 345, 1, 1, 1, 0, 1);
	},
	all_groups_user: function(for_user_id, page_num){
		if(page_num)
			page = '&page='+page_num;
		else {
			page = '';
			page_num = 1;
		}
		
		var num = $('#groups_num').text();
			
		Box.Page('/index.php?go=groups&act=all_groups_user', 'for_user_id='+for_user_id+'&num='+num+page, 'all_groups_users_'+for_user_id+page_num, 525, 'Интересные страницы', lang_msg_close, 0, 0, 345, 1, 1, 1, 0, 1);
	}
}

//IM
var i = 0;
var vii_typograf_delay = false;
var vii_msg_te_val = '';
var vii_typograf = true;
var im = {
	typograf: function(){
		var for_user_id = $('#for_user_id').val();
		var a = $('#msg_text').val();
		if(vii_typograf){
			$.post('/index.php?go=im&act=typograf', {for_user_id: for_user_id});
			vii_typograf = false;
		}
		if(!vii_typograf){
			0 == vii_msg_te_val != a && a != 0 < a.length && (clearInterval(vii_typograf_delay), vii_typograf_delay = setInterval(function(){
				$.post('/index.php?go=im&act=typograf&stop=1', {for_user_id: for_user_id});
				vii_typograf = true;
			}, 3000));
		}
	},
	settTypeMsg: function(){
		Page.Loading('start');
		$.post('/index.php?go=messages&act=settTypeMsg', function(d){
			Page.Go('/messages');
		});
	},
	open: function(uid){
		$('.im_oneusr').removeClass('im_usactive');
		$('#dialog'+uid).addClass('im_usactive');
		$('#imViewMsg').html('<img src="'+template_dir+'/images/loading_im.gif" style="margin-left:225px;margin-top:220px" />');
		$.post('/index.php?go=im&act=history', {for_user_id: uid}, function(d){
			$('#imViewMsg').html(d);
			
			$('.im_scroll').append('<div class="im_typograf"></div>').scrollTop(99999);
			
			var aco = $('.im_usactive').text().split(' ');
			$('.im_typograf').html('<div class="no_display" id="im_typograf"><img src="'+template_dir+'/images/typing.gif" /> '+aco[0]+' набирает сообщение..</div>');
	
			$('#msg_text').focus();
		});
	},
	read: function(msg_id, auth_id, my_id){
		if(auth_id != my_id){
			var msg_num = parseInt($('#new_msg').text().replace(')', '').replace('(', ''))-1;
			$.post('/index.php?go=im&act=read', {msg_id: msg_id}, function(){
				if(msg_num > 0)
					$('#new_msg').html("+"+msg_num);
				else
					$('#new_msg').html('');
				
				updateNum('#msg_num'+auth_id);
				if($('#msg_num'+auth_id).text() <= 0)
					$('#msg_num'+auth_id).hide();
			
				$('#imMsg'+msg_id).css('background', '#fff').attr('onMouseOver', '');
			});
		}
	},
	send: function(for_user_id, my_name, my_ava){
		var msg_text = $('#msg_text').val();
		var attach_files = $('#vaLattach_files').val();
		if(msg_text != 0 && $('#status_sending').val() == 1 || attach_files != 0){
			butloading('sending', 56, 'disabled');
			$('#status_sending').val('0');
			$.post('/index.php?go=im&act=send', {for_user_id: for_user_id, my_name: my_name, my_ava: my_ava, msg: msg_text, attach_files: attach_files}, function(data){
				if(data == 'err_privacy')
					Box.Info('msg_info', lang_pr_no_title, lang_pr_no_msg, 400, 4000);
				else {
					$('#im_scroll').append(data);
					$('.im_scroll').scrollTop(99999);
					$('#msg_text, #vaLattach_files').val('');
					$('#attach_files').html('');
					$('#msg_text').focus();
					$('#status_sending').val('1');
					butloading('sending', 56, 'enabled', 'Отправить');
				}
			});
		} else
			setErrorInputMsg('msg_text');
	},
	delet: function(mid, folder){
		$('.js_titleRemove, #imMsg'+mid).remove();
		$.post('/index.php?go=messages&act=delet', {mid: mid, folder: folder});
	},
	update: function(){
		var for_user_id = $('#for_user_id').val();
		var last_id = $('.im_msg:last').attr('id').replace('imMsg', '');
		$.post('/index.php?go=im&act=update', {for_user_id: for_user_id, last_id: last_id}, function(d){
			if(d.length != '49' && d != 'no_new'){
				$('#im_scroll').html(d);
				$('.im_scroll').scrollTop(99999);
			}
			
			if(d.length == 49) $('#im_typograf').fadeIn();
			else $('#im_typograf').fadeOut()
			
		});
	},
	page: function(for_user_id){
		var first_id = $('.im_msg:first').attr('id').replace('imMsg', '');
		$('#wall_all_records').attr('onClick', '');
		if($('#load_wall_all_records').text() == 'Показать предыдущие сообщения'){
			textLoad('load_wall_all_records');
			$.post('/index.php?go=im&act=history', {first_id: first_id, for_user_id: for_user_id}, function(data){
				i++;
				var imHeiah = $('.im_scroll').height();
				$('#prevMsg').html('<div id="appMsgFScroll'+i+'" class="no_display">'+data+'</div>'+$('#prevMsg').html());
				$('.im_scroll').scrollTop($('#appMsgFScroll'+i).show().height()+imHeiah);
				if(!data){
					$('#wall_all_records').hide();
				} else {
					$('#wall_all_records').attr('onClick', 'im.page('+for_user_id+')');
					$('#load_wall_all_records').html('Показать предыдущие сообщения');
				}
			});
		}
	},
	box_del: function(u){
		Box.Show('im_del'+u, 350, 'Удалить все сообщения', '<div style="padding:15px;" id="del_status_text_im">Вы действительно хотите удалить всю переписку с данным пользователем?<br /><br />Отменить это действие будет невозможно.</div>', lang_box_canсel, lang_box_yes, 'im.del('+u+')');
	},
	del: function(u){
		$('#box_loading').show();
		ge('box_butt_create').disabled = true;
		$('#del_status_text_im').text('Переписка удаляется..');
		$.post('/index.php?go=im&act=del', {im_user_id: u}, function(d){
			Box.Close('im_del'+u);
			Box.Info('ok_im', 'История переписки удалена', 'Все сообщения диалога были успешно удалены.', 300, 3000);
			$('#okim'+u).remove();
		});
	},
	updateDialogs: function(){
		$.post('/index.php?go=im&act=upDialogs', function(d){
			$('#updateDialogs').html(d);
		});
	}
}

//Distinguish
var Distinguish = {
	Start: function(id){
		var x1w = $('#ladybug_ant'+id).width();
		var y1h = $('#ladybug_ant'+id).height();
		var scH = $(window).height();
		var scW = $(window).width();
		$('#i_left'+id).val('30');
		$('#i_top'+id).val('30');
		$('#i_width'+id).val(x1w);
		$('#i_height'+id).val(y1h);
		$('#ladybug_ant'+id).css('cursor', 'crosshair');
		if(!$('.distin_friends_list').text()){
			$('#friends_block').remove();
			$('html, body').append('<div id="friends_block"><div class="box_title">Введите имя<div class="box_close" onClick="Distinguish.Close('+id+')"></div></div><div class="distin_inpbg"><input type="text" id="filter" class="inpst" maxlength="50" value="" style="width:160px;" /></div><div class="distin_friends_list"><center><img src="/templates/Default/images/loading_mini.gif" style="margin-top:10px;margin-bottom:10px" /></center></div><div class="distin_inpbg"><div class="button_div float_l"><button onClick="Distinguish.SelectUser(0, 0, '+id+', 0); return false">Добавить</button></div><div class="button_div_gray float_l margin_left"><button onClick="Distinguish.Close('+id+'); return false;" >Отмена</button></div><div class="clear"></div></div></div>');
		}
		$('#ladybug_ant'+id).imgAreaSelect({
			handles: true,
			onSelectEnd: function(img, selection){
				var pvW = $('#ladybug_ant'+id).position().left+selection.x1+selection.width+20;
				var pvH = $('#ladybug_ant'+id).position().top+selection.y1;
				$('#i_left'+id).val(selection.x1);
				$('#i_top'+id).val(selection.y1);
				$('#i_width'+id).val(selection.width);
				$('#i_height'+id).val(selection.height);
				$('#friends_block').css('margin-left', pvW+'px').css('top', '0px').css('margin-top', pvH+'px').fadeIn(400);
				$('#filter').focus();
				if(!$('.distin_friends_list').text()){
					$.post('/index.php?go=distinguish&act=load_friends', {photo_id: id}, function(d){
						$('.distin_friends_list').html(d).css('padding-bottom', '3px').css('padding-top', '3px');
					});
				}
			},
			onSelectChange: function(){
				$('#friends_block').hide();
			}
		});
	},
	ShowTag: function(left, top, width, height, id){
		Distinguish.HideTag();
		var imgHeight = $('#ladybug_ant'+id).height();
		var imgWidth = $('#ladybug_ant'+id).width();
		var aTop = $('#ladybug_ant'+id).position().top;
		var aLeft = $('#ladybug_ant'+id).position().left;
		if(aTop < 56)
			if($('#mark_userid_bg'+id).text()) var aTop = 114;
			else var aTop = 55;
		if(aLeft < 0) var aLeft = 0;
		$('#distinguishSettings_left'+id).css('width', left+'px').css('height', imgHeight+'px').css('left', aLeft+'px');
		$('#distinguishSettings_top'+id).css('height', top+'px').css('width', (imgWidth-left)+'px').css('left', (aLeft+left)+'px');
		$('#distinguishSettings_right'+id).css('left', (width+aLeft+left)+'px').css('height', (imgHeight-top)+'px').css('width', (imgWidth-left-width)+'px').css('top', (aTop+top)+'px');
		$('#distinguishSettings_bottom'+id).css('top', (aTop+height+top)+'px').css('width', width+'px').css('height', (imgHeight-height-top)+'px').css('left', (aLeft+left)+'px');
		$('#distinguishSettingsBorder_left'+id).css('width', left+'px').css('height', height+'px').css('top', (aTop+top)+'px').css('left', aLeft+'px');
		$('#distinguishSettingsBorder_top'+id).css('width', width+'px').css('height', top+'px').css('left', (aLeft+left)+'px');
		$('#distinguishSettingsBorder_right'+id).css('left', (width+aLeft+left-3)+'px').css('height', height+'px').css('width', (imgWidth-left-width)+'px').css('top', (aTop+top)+'px');
		$('#distinguishSettingsBorder_bottom'+id).css('top', (aTop+height+top-3)+'px').css('width', width+'px').css('height', (imgHeight-height-top)+'px').css('left', (aLeft+left)+'px');
		$('#distinguishSettings'+id).show();
	},
	HideTag: function(id){
		$('#distinguishSettings'+id).hide();
	},
	Close: function(id){
		$('#ladybug_ant'+id).css('cursor', 'pointer');
		$('#friends_block').hide();
		$('#ladybug_ant'+id).imgAreaSelect({
			remove: true
		});
	},
	GeneralClose: function(){
		$('#friends_block, .distin_friends_list').remove();
		$('.distinguishSettings').hide();
		$('.ladybug_ant').css('cursor', 'pointer');
		$('.ladybug_ant').imgAreaSelect({remove: true});
	},
	FriendPage: function(page, photo_id){
		$.post('/index.php?go=distinguish&act=load_friends', {page: page, photo_id: photo_id}, function(d){
			$('.distin_friends_list').append(d);
		});
	},
	SelectUser: function(user_id, user_name, photo_id, no_user){
		if(!user_name) var user_name = $('#filter').val();
		var i_left = $('#i_left'+photo_id).val();
		var i_top = $('#i_top'+photo_id).val();
		var i_width = $('#i_width'+photo_id).val();
		var i_height = $('#i_height'+photo_id).val();
		var size = $('.one_dis_user'+photo_id).size();
		if(size >= 1){
			var comma = '<div class="float_l" style="margin-right:4px">, </div>';
			var comma2 = '';
		} else {
			var comma = '';
			var comma2 = '<div class="float_l" id="peopleOnPhotoText'+photo_id+'" style="margin-right:5px">На этой фотографии:</div>';
		}
		Distinguish.Close(photo_id);
		Distinguish.Start(photo_id);
		if(no_user != 0){
			var lnk = '<a href="/u'+user_id+'" id="selected_us_'+user_id+photo_id+'" onClick="Page.Go(this.href); return false" onMouseOver="Distinguish.ShowTag('+i_left+', '+i_top+', '+i_width+', '+i_height+', '+photo_id+')" onMouseOut="Distinguish.HideTag('+photo_id+')" class="one_dis_user'+photo_id+'">';
			var lnk_end = '</a>';
			var user_ok = 'yes';
		} else {
			var lnk = '<span style="color:#000" onMouseOver="Distinguish.ShowTag('+i_left+', '+i_top+', '+i_width+', '+i_height+', '+photo_id+')" onMouseOut="Distinguish.HideTag('+photo_id+')" class="one_dis_user'+photo_id+'">';
			var lnk_end = '</span>';
			var user_id = 0;
			var user_ok = 'no';
		}
		if($('#selected_us_'+user_id+photo_id).text())
			$('#selected_us_'+user_id+photo_id).attr('onMouseOver', 'Distinguish.ShowTag('+i_left+', '+i_top+', '+i_width+', '+i_height+', '+photo_id+')');
		else
			$('#peoples_on_this_photos'+photo_id).append(comma2+'<span id="selectedDivIser'+user_id+photo_id+'">'+comma+'<div class="float_l">'+lnk+user_name+lnk_end+'</div><div class="float_l"><img src="/templates/Default/images/hide_lef.gif" class="distin_del_user" title="Удалить отметку" onClick="Distinguish.DeletUser('+user_id+', '+photo_id+')" /></div></span>');
		
		$('#filter').val('');
		$('.echoUsersList').show();
		if(user_ok == 'yes') var user_name = '';
		$.post('/index.php?go=distinguish&act=mark', {i_left: i_left, i_top: i_top, i_width: i_width, i_height: i_height, photo_id: photo_id, user_id: user_id, user_name: user_name, user_ok: user_ok});
	},
	DeletUser: function(user_id, photo_id, user_name){
		$('#mark_userid_bg'+photo_id).remove().text('');
		$('#selectedDivIser'+user_id+photo_id).remove();
		var size = $('.one_dis_user'+photo_id).size();
		if(size <= 0) $('#peopleOnPhotoText'+photo_id).remove();
		if(user_name) var user_id = 0;
		$.post('/index.php?go=distinguish&act=mark_del', {photo_id: photo_id, user_id: user_id, user_name: user_name});
	},
	OkUser: function(photo_id){
		$('#mark_userid_bg'+photo_id).remove().text('');
		$.post('/index.php?go=distinguish&act=mark_ok', {photo_id: photo_id});
	}
}

//HAPPY FRIENDS
var HappyFr = {
	Show: function(){
		$('.profile_block_happy_friends').css('max-height', (($('.profile_onefriend_happy').size()-4)/2)*190+190+'px');
		$('#happyAllLnk').attr('onClick', 'HappyFr.Close()');
		$('.profile_block_happy_friends_lnk').text('Скрыть');
	},
	Close: function(){
		$('.profile_block_happy_friends').css('max-height', '190px');
		$('#happyAllLnk').attr('onClick', 'HappyFr.Show()');
		$('.profile_block_happy_friends_lnk').text('Показать все');
	},
	HideSess: function(){
		$('.js_titleRemove').remove();
		$('#happyBLockSess').hide();
		$.post('/index.php?go=happy_friends_block_hide');
	}
}

//FAST SEARCH
var vii_search_delay = false;
var vii_search_val = '';
var FSE = {
	Txt: function(){
		var a = $('#query').val();
		if(a.length > 43){
			tch = '..';
			nVal = a.substring(0, 43);
		} else {
			tch = '';
			nVal = a;
		}
		$('#fast_search_txt').text(nVal+tch);
		0 == a.length ? $(".fast_search_bg").hide() : vii_search_val != a && a != 0 < a.length && (clearInterval(vii_search_delay), vii_search_delay = setInterval(function(){
			FSE.GoSe(a);
		}, 600));
		if(a != 0)
			$(".fast_search_bg").show();
	},
	GoSe: function(val){
		clearInterval(vii_search_delay);
		if(val != 0){
			if($('#se_type').val() == 1 || $('#se_type').val() == 2 || $('#se_type').val() == 4){
				$.post('/index.php?go=fast_search', {query: val, se_type: $('#se_type').val()}, function(d){
					$('#reFastSearch').html(d);
				});
			} else
				$('#reFastSearch').html('');
		} else {
			$(".fast_search_bg").hide();
			$('#reFastSearch').html('');
		}

		vii_search_val = val;
	},
	ClrHovered: function(id){
		for(i = 0; i <= 8; i++){
			$('#all_fast_res_clr'+i).css('background', '#fff');
		}
		$('#'+id).css('background', '#eef3f5');
	}
}

//COMPLAIT / REPORT
var Report = {
	Box: function(act, id){
		Box.Close();
		if(act == 'photo') lang_report = 'Жалоба на фотографию';
		else if(act == 'video') lang_report = 'Жалоба на видеозапись';
		else if(act == 'wall') lang_report = 'Жалоба на запись';
		else lang_report = '';
		Box.Show('report', 400, lang_report, '<div style="padding:30px;">Пожалуйста, выберите причину, по которой Вы хотите сообщить администрации сайта об этом материале.<div class="clear"></div><br /><select id="type_report" style="width:300px" onChange="if(this.value > 1) {$(\'#report_comm_block\').show();$(\'#text_report\').focus()} else {$(\'#report_comm_block\').hide();$(\'#text_report\').val(\'\')}"><option value="1">Материал для взрослых</opyion><option value="2">Детская порнография</opyion><option value="3">Эктремизм</opyion><option value="4">Насилие</opyion><option value="5">Пропаганда наркотиков</opyion></select><div class="clear"></div><div id="report_comm_block" class="no_display"><br />Комментарий:<br /><br /><textarea id="text_report" style="width:300px;height:80px; padding:10px;"></textarea></div></div>', lang_msg_close, lang_box_send, 'Report.Send(\''+act+'\', '+id+')');
		$('#audio_lnk').focus();
		$('#video_object').hide();
	},
	Send: function(act, id){
		$('#box_loading').show();
		ge('box_butt_create').disabled = true;
		$.post('/index.php?go=report', {act: act, id: id, type_report: $('#type_report').val(), text_report: $('#text_report').val()}, function(d){
			Box.Close();
			Box.Info('yes_report', 'Спасибо', 'Ваша жалоба отправлена администрации сайта и будет рассмотрена в ближайшее время.', 300, 3000);
			$('#video_object').show();
		});
	},
	WallSend: function(act, id){
		$('#wall_record_'+id).html('<div class="color777">Сообщение помечено как спам.</div>');
		$('#wall_fast_block_'+id).remove();
		$('.js_titleRemove').remove();
		$.post('/index.php?go=report', {act: act, id: id});
	}
}

//REPOST
var Repost = {
	Box: function(rec_id, g_tell){
		Box.Page('/index.php?go=repost&act=all', 'rec_id='+rec_id, 'repost', 430, 'Отправка записи', lang_box_canсel, 'Поделиться записью', 'Repost.Send('+rec_id+', '+g_tell+')', 0, 0, 0, 0, 'comment_repost');
	},
	Send: function(rec_id, g_tell){
		comm = $('#comment_repost').val();
		type = $('#type_repost').val();
		if(type == 1) cas = 'for_wall';
		else if(type == 2)
			if(g_tell) cas = 'groups_2';
			else cas = 'groups';
		else if(type == 3) cas = 'message';
		else cas = '';
		$('#box_loading').show();
		ge('box_butt_create').disabled = true;
		$.post('/index.php?go=repost&act='+cas, {rec_id: rec_id, comm: comm, sel_group: $('#sel_group').val(), g_tell: g_tell, for_user_id: $('#for_user_id').val()}, function(d){
			if(d == 1){
				$('#box_loading').hide();
				ge('box_butt_create').disabled = false;
				addAllErr(lang_wall_tell_tes);
			} else {
				if(type == 1) Box.Info('yes_report', 'Запись отправлена.', 'Теперь эта запись появится в новостях у Ваших друзей.', 300, 2500);
				if(type == 2) Box.Info('yes_report', 'Запись отправлена.', 'Теперь эта запись появится на странице сообщества.', 300, 2500);
				if(type == 3) Box.Info('yes_report', 'Сообщение отправлено.', 'Ваше сообщение отправлено.', 300, 2500);
				Box.Close();
			}
		});
	}
}

//DOCUMENTS
var Doc = {
	AddAttach: function(name, id){
		if(!$('#wall_text').val()) wall.form_open();
		
		$('#attach_files').show();
		attach_id = Math.floor(Math.random()*(1000-1+1))+1;

		Box.Close();
	
		ln = name.length;
		if(ln > 50) name = name.substring(0, 12)+'..'+name.substring(ln-4, ln);
		
		res_attach_id = 'doc_'+attach_id;
		$('#attach_files').append('<div style="padding-bottom:6px;padding-top:6px;display:block;width:100%" id="attach_file_'+res_attach_id+'" class="attach_file" ><div class="doc_attach_ic float_l"></div><div class="doc_attach_text"><div class="float_l">'+name+'</div><img class="float_l cursor_pointer" style="margin-top:2px;margin-left:5px" src="'+template_dir+'/images/close_a.png" onMouseOver="myhtml.title(\''+res_attach_id+'\', \''+lang_wall_no_atttach+'\', \'wall_doc_\')" id="wall_doc_'+res_attach_id+'" onClick="wall.attach_delete(\''+res_attach_id+'\', \'doc|'+id+'||\')" /></div><div class="clear"></div></div><div class="clear"></div>');
		$('#vaLattach_files').val($('#vaLattach_files').val()+'doc|'+id+'||');
		
		if($('.attach_file').size() > 9) $('#wall_attach').hide();
	},
	Del: function(did){
		$('.js_titleRemove').remove();
		$('#doc_block'+did).html('Документ был удалён.');
		updateNum('#upDocNum');
		langNumric('langNumric', $('#upDocNum').text(), 'документ', 'документа', 'документов', 'документ', 'документов');
		$.post('/index.php?go=doc&act=del', {did: did});
	},
	ShowEdit: function(did, id){
		$('#'+id+', #data_doc'+did).hide();
		$('#edit_doc_tab'+did).show();
	},
	CloseEdit: function(did, id){
		$('#'+id+', #data_doc'+did).show();
		$('#edit_doc_tab'+did).hide();
	},
	SaveEdit: function(did, id){
		if($('#edit_val'+did).val() != 0){
			$('#edit_doc_name'+did).text($('#edit_val'+did).val());
			$('#'+id+', #data_doc'+did).show();
			$('#edit_doc_tab'+did).hide();
			$.post('/index.php?go=doc&act=editsave', {did: did, name: $('#edit_val'+did).val()});
		} else
			setErrorInputMsg('edit_val'+did);
	}
}

//VOTES
var Votes = {
	AddInp: function(){
		$('#answerNum').val(parseInt($('#answerNum').val())+1);
		$('#addAnswerInp').append('<div id="div_inp_answr_'+$('#answerNum').val()+'"><div class="texta">&nbsp;</div><input type="text" id="vote_answer_'+$('#answerNum').val()+'" class="inpst vote_answer" maxlength="80" value="" style="width:355px;margin-left:5px" /><div class="mgclr"></div></div>');
		if($('#answerNum').val() == 10) $('#addNewAnswer').html('добавить');
		if($('#answerNum').val() > 2) $('#addDelAnswer').html('<a class="cursor_pointer" onClick="Votes.DelInp()">удалить</a>');
		$('#vote_answer_'+$('#answerNum').val()).focus();
	},
	DelInp: function(id){
		if($('#answerNum').val() > 2){
			$('#answerNum').val(parseInt($('#answerNum').val())-1);
			$('#div_inp_answr_'+$('.vote_answer:last').attr('id').replace('vote_answer_', '')).remove();
			$('#addNewAnswer').html('<a class="cursor_pointer" onClick="Votes.AddInp()">добавить</a>');
		}
		if($('#answerNum').val() == 2) $('#addDelAnswer').html('удалить');
	},
	RemoveForAttach: function(){
		$('#vaLattach_files').val($('#vaLattach_files').val().replace('vote|start||', ''));
		$('.js_titleRemove').remove();
		$('#attach_block_vote').hide();
		$('#vote_title, #vote_answer_1, #vote_answer_2').val('');
		$('#addNewAnswer').html('<a class="cursor_pointer" onClick="Votes.AddInp()">добавить</a>');
		$('#addDelAnswer').html('удалить');
		$('#attatch_vote_title').text('');
		$('#answerNum').val('2');
		for(i = 2; i <= 10; i++)
			$('#div_inp_answr_'+i).remove();
	},
	Send: function(answer_id, vote_id){
		$('#answer_load'+answer_id).append('<img src="'+template_dir+'/images/loading_mini.gif" style="margin-left:5px" />');
		for(i = 0; i <= 10; i++)
			$('#wall_vote_oneanswe'+i).attr('onClick', '');
		$.post('/index.php?go=votes', {vote_id: vote_id, answer_id: answer_id}, function(d){
			$('#result_vote_block'+vote_id).html(d);
		});
	}
}