function overlays(e) {
	var hide_join = false;
	var hide_fav = false;
	$('.overlay_info').stopTime("hideDropdown");
	$('.overlay_info').hide();

	rel = $(this).parent().attr('rel')
	id = rel.split('_')[1]
	type = rel.split('_')[0]
	uid = id+'_'+type

	//if($.browser.msie)
	//{
		if (!$('#upyachka').children('#'+uid).attr('class')) {$('#upyachka').append('<div class="overlay_info" id="'+uid+'"></div>')};

		overlay = $('#upyachka').children('#'+uid)

		overlay.css('position', 'absolute')
		overlay.css('z-index', '999')

		overlay.css('left', $(this).parent().offset().left)
		overlay.css('top', $(this).parent().offset().top)
	//}
	//else
	//{
	//	if (!$(this).parent().children('.overlay_info').attr('class')) {$(this).parent().append('<div class="overlay_info"></div>')};

	//	overlay = $(this).parent().children('.overlay_info');
	//}

	if (overlay.html() == '')
	{
		if (type == 'user')
			json_url = '/accounts/json/' + id + '/';
		else if (type == 'entity')
			json_url = '/entities/json/' + id + '/';
		$.getJSON(json_url,
			function (data) {
				if (type == 'user')
				{
					avatar = 'http://static.kanobu.ru/kanobu/modern/images/48x48_user.jpg'

					online = ''

					if (data.avatar != '')
					{
						avatar = data.avatar
					};

					if (data.is_online)
					{
						online = ' name-i-online'
					};

					html = $('#overlay_temaplate').html()
					html = html.split('<!--separator-->')
					overlay.html(html[0])

					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-write').parent().hide()
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-newpl').parent().hide()
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').parent().hide()
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplvid').parent().hide()
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplpic').parent().hide()
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-editprofile').parent().hide()

					if (data.is_me)
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-write').parent().show()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-newpl').parent().show()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').parent().show()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplvid').parent().show()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplpic').parent().show()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-editprofile').parent().show()

						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-write').click(function () {window.location = '/blog/add/'; return false})
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-newpl').click(function () {window.location = '/playlist/add/'; return false})
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplpic').click(function () {window.location = '/image/add/'; return false})
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-editprofile').click(function () {window.location = '/accounts/profile/edit/'; return false})

						if (data.media_upload)
						{
							overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplvid').parent().show()
						}
						else
						{
							overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplvid').parent().hide()
						};

						if (!data.audio_upload)
						{
							overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').attr('alt', 'msgLowRating')
							overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').addClass('action_check_rating')
						}
						else
						{
							overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').click(function () { window.location = '/audio/add/?interests='+data.pk; return false })
						};

						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-mail').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-removefriend').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-addfriend').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-invite').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-disignore').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-ignore').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-ban').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-unban').parent().hide()
					};

					remove_friend = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-removefriend')
					add_friend = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-addfriend')

					add_friend.parent().attr("action-friend-add", id)
					remove_friend.parent().attr("action-friend-del", id)
					overlay.children('.vcard').children('.friends').attr("action-friend-added", id)

					remove_ignore = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-disignore')
					add_ignore = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-ignore')

					add_ignore.parent().attr("action-ignore-add", id)
					remove_ignore.parent().attr("action-ignore-del", id)
					overlay.children('.vcard').children('.ignore').attr("action-ignore-added", id)

					unban = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-unban')
					ban = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-ban')

					ban.parent().attr("action-ban-add", id)
					unban.parent().attr("action-ban-del", id)

					ban.parent().hide()
					unban.parent().hide()

					if (data.current_super && !data.is_me)
					{
						if (data.is_banned)
						{
							unban.parent().show()
						}
						else
						{
							ban.parent().show()
						};
					};

					if (data.is_friend)
					{
						overlay.children('.vcard').children('.friends').show()
						add_friend.parent().hide()
					}
					else
					{
						overlay.children('.vcard').children('.friends').hide()
						remove_friend.parent().hide()
					};

					if (data.is_ignore)
					{
						overlay.children('.vcard').children('.ignore').show()
						add_ignore.parent().hide()
					}
					else
					{
						overlay.children('.vcard').children('.ignore').hide()
						remove_ignore.parent().hide()
					};

					if (type == 'user')
					{
						overlay.children('.vcard').children('span').html('пользователь')
					};

					if (data.rating >= 0)
					{
						overlay.children('.void').children('strong').attr('class','positive')
					}
					else
					{
						overlay.children('.void').children('strong').attr('class','negative')
					};

					/*if (data.birthday == 'None' || !data.show_birthday)
					{
						overlay.children('.userinfo').children('.bday_display').hide()
						overlay.children('.userinfo').children('.bday').hide()
					}
					else
					{
						overlay.children('.userinfo').children('.bday').html(data.birthday)
					};

					if ((data.country == 'None' && data.city == 'None') || (data.country == '' && data.city == ''))
					{
						overlay.children('.userinfo').children('.adr_display').hide()
						overlay.children('.userinfo').children('.adr').hide()
					}
					else
					{
						if(data.country == 'None' || data.country == '')
						{
							str = overlay.children('.userinfo').children('.adr').html()+' ';
							str = str.replace(',', '')
							overlay.children('.userinfo').children('.adr').html(str)
						}
						else
						{
							overlay.children('.userinfo').children('.adr').children('.country-name').html(data.country)
						}

						if(data.city == 'None' || data.city == '')
						{
							str = overlay.children('.userinfo').children('.adr').html()+' ';
							str = str.replace(',', '')
							overlay.children('.userinfo').children('.adr').html(str)
						}
						else
						{
							overlay.children('.userinfo').children('.adr').children('.locality').html(data.city)
						}
					};
					*/
					if (data.imfriends_count != 0)
					{
						overlay.children('.userinfo').children('.imfriend').html(data.imfriends_count)
					}
					else
					{
						overlay.children('.userinfo').children('.imfriend_display').hide()
						overlay.children('.userinfo').children('.imfriend').hide()
					}

					if (data.myfriends_count != 0)
					{
						overlay.children('.userinfo').children('.myfriend').html(data.myfriends_count)
					}
					else
					{
						overlay.children('.userinfo').children('.myfriend_display').hide()
						overlay.children('.userinfo').children('.myfriend').hide()
					}

					overlay.children('.userinfo').children('.lastvisit_display').html(data.lastvisit_display)
					if (data.is_online)
					{
						overlay.children('.userinfo').children('.lastvisit').html('Сейчас онлайн')
					}
					else
					{
						overlay.children('.userinfo').children('.lastvisit').html(data.last_login)
					};

					//overlay.children('.vcard').click(function () {window.location = '/accounts/'+id+'/'; return false})
					overlay.children('.vcard').attr('href', '/accounts/'+id+'/')
					overlay.children('.vcard').children('.photo').attr('src', avatar)
					overlay.children('.vcard').children('strong').attr('class','fn'+online)
					overlay.children('.vcard').children('strong').html(data.username)
					overlay.children('.void').children('strong').children('em').html(data.rating)
					overlay.children('.userinfo').children('.registered').html(data.rigistration_date)
					overlay.children('.overlay_expand').children('.useractions').children( 'li').children('.icon-mail').click(function () {window.location='/accounts/'+id+'/message_add/'; return false})
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-invite').click(function () { window.location = '/accounts/msgbox/send/invitation/user'+id+'/'; return false })

					if (data.achievements) {
						var $userinfo = overlay.children('.userinfo').prepend('<dd class="achiv" title="achievment"></d>');
						var $achivs = $userinfo.children('dd.achiv');
						for (var i = 0; i < data.achievements.length; i++) {
							$achlink = $('<a></a>').attr('href', data.achievements[i].url);
							$achlink.appendTo($achivs);

							$('<img alt="" width="24"></img>').attr('src', data.achievements[i].image)
								.attr('title', data.achievements[i].name).appendTo($achlink)
						}
					} else {
					}

				}
				else if(type == 'entity')
				{
					html = $('#overlay_temaplate').html()

					overlay.html(html.split('<!--separator-->')[1])

					overlay.children('.vcard').children('.entity_name').html(data.entity_name);

					if (data.rating >= 0)
					{
						overlay.children('.void').children('strong').attr('class','positive')
					}
					else
					{
						overlay.children('.void').children('strong').attr('class','negative')
					};

					creator_avatar = 'http://static.kanobu.ru/kanobu/modern/images/16x16_user.jpg'

					if (data.creator_avatar)
					{
						creator_avatar = data.creator_avatar
					};


					image = 'http://static.kanobu.ru/kanobu/modern/images/'
					user_info = overlay.children('.userinfo')
					var link_type = 'interests'
					if(data.category_slug == 'communities')
					{
						hide_fav = true;
						user_info.children('.moderator_display').css({'display':''});
						user_info.children('.moderator').css({'display':''})
						//user_info.children('.moderator').children('.vcard').click(function () { window.location = '/accounts/'+data.creator_pk+'/'; return false })
						user_info.children('.moderator').children('.vcard').attr('href', '/accounts/'+data.creator_pk+'/')
						user_info.children('.moderator').children('.vcard').children('em').html( data.creator)
						user_info.children('.moderator').children('.vcard').children('.photo').attr( 'src', creator_avatar)

						if(data.members_total>0)
						{
							user_info.children('.member_display').css({'display':''});
							user_info.children('.member').css({'display':''})
							user_info.children('.member').html(data.members_total)
						}
						if(data.posts_total>0)
						{
							user_info.children('.post_display').css({'display':''});
							user_info.children('.post').css({'display':''})
							user_info.children('.post').html(data.posts_total)
						}
						link_type = 'commlinks'
						image = image + '48x48_comm.jpg'
					}
					if(data.category_slug == 'games')
					{
						hide_join = true;
						if(data.favorites_total>0)
						{
							user_info.children('.favorites_display').css({'display':''});
							user_info.children('.favorites').css({'display':''})
							user_info.children('.favorites').html(data.favorites_total)
						}

						if(data.platform)
						{
							user_info.children('.platform_display').css({'display':''});
							user_info.children('.platform').css({'display':''})
							var platform = ''
							if(data.platform.length>1) user_info.children('.platform_display').html('Платформы:')
							for(i=0;i<data.platform.length;i++)
							{
								platform += data.platform[i][1]
								if(data.platform.length-1 != i) platform += ', '
							}
							user_info.children('.platform').html(platform)
						}

						if(data.genre)
						{
							user_info.children('.genre_display').css({'display':''});
							user_info.children('.genre').css({'display':''})
							var genre = ''
							if(data.genre.length>1) user_info.children('.genre_display').html('Жанры:')
							for(i=0;i<data.genre.length;i++)
							{
								genre += data.genre[i][1]
								if(data.genre.length-1 != i) genre += ', '
							}
							user_info.children('.genre').html(genre)
						}
						image = image + '48x64_game.jpg'
					}
					if(data.category_slug == 'films')
					{
						hide_join = true;
						if(data.favorites_total>0)
						{
							user_info.children('.favorites_display').css({'display':''});
							user_info.children('.favorites').css({'display':''})
							user_info.children('.favorites').html(data.favorites_total)
						}

						if(data.genre)
						{
							user_info.children('.genre_display').css({'display':''});
							user_info.children('.genre').css({'display':''})
							var genre = ''
							if(data.genre.length>1) user_info.children('.genre_display').html('Жанры:')
							for(i=0;i<data.genre.length;i++)
							{
								genre += data.genre[i][1]
								if(data.genre.length-1 != i) genre += ', '
							}
							user_info.children('.genre').html(genre)
						}

						if(data.director)
						{
							user_info.children('.director_display').css({'display':''});
							user_info.children('.director').css({'display':''})
							var director = ''
							if(data.director.length>1) user_info.children('.director_display').html('Режиссеры:')
							for(i=0;i<data.director.length;i++)
							{
								director += data.director[i][1]
								if(data.director.length-1 != i) director += ', '
							}
							user_info.children('.director').html(director)
						}
						image = image + '48x64_film.jpg'
					}

					if(data.category_slug == 'books')
					{
						hide_join = true;
						if(data.favorites_total>0)
						{
							user_info.children('.favorites_display').css({'display':''});
							user_info.children('.favorites').css({'display':''})
							user_info.children('.favorites').html(data.favorites_total)
						}

						if(data.genre)
						{
							user_info.children('.genre_display').css({'display':''});
							user_info.children('.genre').css({'display':''})
							var genre = ''
							if(data.genre.length>1) user_info.children('.genre_display').html('Жанры:')
							for(i=0;i<data.genre.length;i++)
							{
								genre += data.genre[i][1]
								if(data.genre.length-1 != i) genre += ', '
							}
							user_info.children('.genre').html(genre)
						}

						if(data.director)
						{
							user_info.children('.author_display').css({'display':''});
							user_info.children('.author').css({'display':''})
							var director = ''
							if(data.director.length>1) user_info.children('.director_display').html('Авторы:')
							for(i=0;i<data.director.length;i++)
							{
								director += data.director[i][1]
								if(data.director.length-1 != i) director += ', '
							}
							user_info.children('.author').html(director)
						}
						image = image + '48x64_book.jpg'
					}

					if(data.category_slug == 'persons' && data.birthday)
					{
						hide_fav = true;
						hide_join = true;
						user_info.children('.birthday_display').css({'display':''});
						user_info.children('.birthday').css({'display':''})
						user_info.children('.birthday').html(data.birthday)
					}
					if(data.category_slug == 'persons'){
						image = image + '48x48_person.jpg'
						hide_fav = true;
						hide_join = true;
					}

					if(data.category_slug == 'events')
					{
						if(data.creator != 'None')
						{
							user_info.children('.organizer_display').css({'display':''});
							user_info.children('.moderator').css({'display':''})
							//user_info.children('.moderator').children('.vcard').click(function () { window.location = '/accounts/'+data.creator_pk+'/'; return false })
							user_info.children('.moderator').children('.vcard').attr('href', '/accounts/'+data.creator_pk+'/')
							user_info.children('.moderator').children('.vcard').children('em').html( data.creator)
							user_info.children('.moderator').children('.vcard').children('.photo').attr( 'src', creator_avatar)
						}
						else
						{
							overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-mail').parent().hide()
							overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-invite').parent().hide()
						};

						user_info.children('.eventdate_display').css({'display':''});
						user_info.children('.eventdate').css({'display':''})
						user_info.children('.eventdate').html(data.event_date)

						if(data.members_total>0)
						{
							user_info.children('.member_display').css({'display':''});
							user_info.children('.member').css({'display':''})
							user_info.children('.member').html(data.members_total)
						}
						image = image + '48x48_event.jpg'
					}

					if(data.category_slug == 'companies')
					{
						hide_fav = true;
						hide_join = true;
						if(data.roles != '')
						{
							user_info.children('.profile_display').css({'display':''});
							user_info.children('.profile').css({'display':''})
							user_info.children('.profile').html(data.roles)
						}
						image = image + '48x48_company.jpg'
					}

					if (data.image)
					{
						image = data.image
					}

					//overlay.children('.vcard').click(function () {window.location = '/' + data.category_slug + '/'+data.slug+'/'; return false})
					overlay.children('.vcard').attr('href', '/' + data.category_slug + '/'+data.slug+'/')
					overlay.children('.vcard').children('.photo').attr('src', image)
					overlay.children('.vcard').children('strong').attr('class','fn')
					overlay.children('.vcard').children('strong').html(data.header)
					overlay.children('.void').children('strong').children('em').html(data.rating)
					overlay.children('.userinfo').children('.registered').html(data.rigistration_date)
					overlay.children('.overlay_expand').children('.useractions').children( 'li').children('.icon-mail').click(function () {window.location='/accounts/'+data.creator_pk+'/message_add/'; return false})
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-invite').click(function () { window.location = '/accounts/msgbox/send/invitation/user'+data.creator_pk+'/'; return false })
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-write').click(function () { window.location = '/blog/add/?'+link_type+'='+data.pk; return false })
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplpic').click(function () { window.location = '/image/add/?'+link_type+'='+data.pk; return false })
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-editprofile').click(function () { window.location = '/entities/communities/'+data.pk+'/edit/'; return false })
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-ignore').click(function () { window.location = '/entities/communities/'+data.pk+'/delete/'; return false })
					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplvid').click(function () { window.location = '/video/add/?'+link_type+'='+data.pk; return false })

					if (data.badges) {
						var $userinfo = overlay.children('.userinfo').prepend('<dd class="achiv" title="achievment"></d>');
						var $achivs = $userinfo.children('dd.achiv');
						for (var i = 0; i < data.badges.length; i++) {
							$achlink = $('<a></a>').attr('href', data.badges[i].url);
							$achlink.appendTo($achivs);

							$('<img alt="" width="24"></img>').attr('src', data.badges[i].image)
								.attr('title', data.badges[i].name).appendTo($achlink)
						}
					} else {
					}

					if(data.category_slug != 'communities' && data.category_slug != 'events')
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-mail').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-invite').parent().hide()
					}

					if (data.has_update_perm)
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-editprofile').parent().show()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-ignore').parent().show()
					}
					else
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-editprofile').parent().hide()
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-ignore').parent().hide()
					};

					if (data.media_upload)
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplvid').parent().show()
					}
					else
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplvid').parent().hide()
					};

					if (!data.audio_upload)
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').attr('alt', 'msgLowRating')
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').addClass('action_check_rating')
					}
					else
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplaud').click(function () { window.location = '/audio/add/?interests='+data.pk; return false })
					};

					overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplfile').click(function  () { window.location = '/file/add/?commlinks='+data.pk; return false })

					if (data.file_upload)
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplfile').parent().show()
					}
					else
					{
						overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-uplfile').parent().hide()
					};

					addfav = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-addfav')
					removefav = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-removefav')

					addfav.parent().attr("action-faventry-add", id)
					removefav.parent().attr("action-faventry-del", id)

					if (hide_fav){
						addfav.parent().hide();
						removefav.parent().hide();
					}else{
						if (data.is_fav)
						{
							addfav.parent().hide();
							removefav.parent().show();
						}
						else
						{
							addfav.parent().show();
							removefav.parent().hide();
						};
					}

					connect = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-addfriend')
					unconnect = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-removefriend')
					invite = overlay.children('.overlay_expand').children( '.useractions').children('li').children('.icon-invite')

					if (hide_join){
						unconnect.parent().hide();
						connect.parent().hide();
					}else{
						if (data.im_in)
						{
							connect.parent().hide();
							unconnect.parent().show();
							invite.parent().show();
						}
						else
						{
							connect.parent().show();
							unconnect.parent().hide();
							invite.parent().hide();
						};
					};
					connect.parent().attr("action-community-add", id);
					unconnect.parent().attr("action-community-del", id);
					invite.parent().attr("action-community-added", id);

				};

				$(".overlay_expand").hover(
					function () {
						// прячем все другие такие же выпадающие списки
						$('.useractions').stopTime("hideDropdown");
						//$('.useractions').hide();

						$(this).children('.useractions').show();
						$(this).children('.useractions').stopTime("hideDropdown");
					},
					function () {
						$(this).children('.useractions').oneTime(500, "hideDropdown", function(){
							$(this).hide();
						});
					}
				);
			}
		);
	};

	//if ($.browser.msie)
	//{
		$('#upyachka').children('#'+uid).show()
		$('#upyachka').children('#'+uid).stopTime("hideDropdown");
	/*}
	else
	{
		$(this).parent().children('.overlay_info').show();
		$(this).parent().children('.overlay_info').stopTime("hideDropdown");
	};*/

	$('.overlay_info').mouseleave(function() {
		$(this).oneTime(200, "hideDropdown", function(){
			$(this).hide();
		});
	});

	return false;
}

$(document).ready(function	() {
	$(".userbox a.vcard").live("click", overlays);
})

