var last_menu_open;

function refreshCaptcha( target, field )
{
	$.post("/captcha/refresh/", '', function(data)
	{
		$('#' + target).attr('src', '/system/captcha/' + data + '.jpg');
		$('#' + field).attr( 'value', data );
	});	
}

function closeCurrentWindow()
{
	window.close();
}

function hideObjectByTargetName(target)
{
	if(target)
	{
		$('#' + target).hide();
	}
}

function showObjectByTargetName(target)
{
	if(target)
	{
		$('#' + target).show();
	}
}

function warningActionMessage(link, text)
{
	var agree = confirm(text);

	if(agree)
	{
		document.location = link;
	}
}

function redirectUrl(link)
{
	if(link)
	{
		window.location.href = link;
	}
}

function blogsPostEdit(target, blog_id, post_id)
{
	$.get("/blogs/post_edit/"+ blog_id +"/"+ post_id +"/", function(data)
	{
		$('#'+target).html(data);
		$('#short_story').markItUp(mySettings);
		$('#full_story').markItUp(mySettings);
	});
}

function blogsPostSave(target, blog_id, post_id)
{
	var form_data = $("#formid_"+post_id).serialize();

	$.post("/blogs/post_save/"+ blog_id +"/"+ post_id +"/", form_data, function(data)
	{
		$('#'+target).html(data);
	});
}

function blogsPostDelete(target, blog_id, post_id)
{
	var agree = confirm('Вы действительно хотите удалить эту запись?');

	if (agree)
	{
		$.post("/blogs/post_delete/"+ blog_id +"/"+ post_id +"/", '', function(data)
		{
			$('#'+target).html(data);
		});
	}
}

function blogsUpdateRating(target, blog_fk, rating)
{
	$.post("/blogs/"+ blog_fk +"/rating/"+ rating +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function blogsUpdateRatingPosts(target, blog_id, post_id, rating)
{
	$.post("/blogs/posts_rating/"+ blog_id +"/"+ post_id +"/"+ rating +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function insertSmiley(target, smiley)
{
	var textarea_data = $('#' + target).val();

	$('#' + target).val(textarea_data +' '+ smiley);
}

function smiliesToogle(target, target_toogle, type)
{
	if(target && target_toogle)
	{
		$.post("/comments/smilies/toogle/"+ type +"/", '', function(data)
		{
			if(type > 0)
			{
				$('#' + target).hide("slow");
				$('#' + target_toogle).html("<span id=\""+ target_toogle +"\"><p><a onClick=\"smiliesToogle('"+ target +"', '"+ target_toogle +"', '0')\" class=\"cursor\">Развернуть смайлы</a></p></span>");
			}
			else
			{
				$('#' + target).show("slow");
				$('#' + target_toogle).html("<span id=\""+ target_toogle +"\"><p><a onClick=\"smiliesToogle('"+ target +"', '"+ target_toogle +"', '1')\" class=\"cursor\">Свернуть смайлы</a></p></span>");
			}
		});
	}
}

function smiliesAnswerToogle(type, comment_fk)
{
	$.post("/comments/smilies/toogle/"+ type +"/", '', function(data)
	{
		if(type > 0)
		{
			$('#smilies_' + comment_fk).hide("slow");
			$('#smilies_toogle_' + comment_fk).html("<span id=\"smilies_toogle_"+ comment_fk +"\"><p><a onClick=\"smiliesAnswerToogle('0', '"+ comment_fk +"')\" class=\"cursor\">Развернуть смайлы</a></p></span>");
		}
		else
		{
			$('#smilies_' + comment_fk).show("slow");
			$('#smilies_toogle_' + comment_fk).html("<span id=\"smilies_toogle_"+ comment_fk +"\"><p><a onClick=\"smiliesAnswerToogle('1', '"+ comment_fk +"')\" class=\"cursor\">Свернуть смайлы</a></p></span>");
		}
	});
}

function addComments(target, killalso, parent_fk)
{
	if(parent_fk > 0)
	{
		var form_data = $("#comments_add_" + parent_fk).serialize();
	
		$('#commenttext_'+parent_fk).css("background-color", "#ebebeb");
		$('#commenttext_'+parent_fk).attr("disabled", "disabled");
		$('#comment_reply_'+parent_fk+' .load').show();
	
	}
	else
	{
		var form_data = $("#comments_add").serialize();
		$('#commenttext').css("background-color", "#ebebeb");
		$('#commenttext').attr("disabled", "disabled");
		$('#insAddComment .load').show();
	}

	$.post("/comments/add/", form_data, function(data)
	{
		if(parent_fk > 0)
		{
			//$('#comment_reply_' + parent_fk).empty();
			//$('#comment_reply_' + parent_fk).remove();
			//$('#comment_reply_' + parent_fk).hide();
			$('#commenttext_'+parent_fk).attr("disabled", "");
			$('#commenttext_'+parent_fk).css("background-color", "#ffffff");
			$('#comment_reply_'+parent_fk+' .load').hide();
			$('#comment_reply_'+parent_fk).html(data);
		}
		else{
			$('#'+target).html(data);
			$('#commenttext').attr("disabled", "");
			$('#commenttext').css("background-color", "#ffffff");
			$('#insAddComment .load').hide();
		}
		
	});
	
	

	//if(killalso)
	//{
	//	$('#' + killalso).html('<textarea name="'+ killalso +'" id="'+ killalso +'" style="width: 450px; height: 130px;" /></textarea>');
	//}
}

function showAnswerCommentsForm(target)
{
	$('#' + target).toggle("slow");
}

function editComments(target, comment_id)
{
	var form_data = $("#comments_edit").serialize();

	$.post("/comments/edit/"+ comment_id +"/", form_data, function(data)
	{
		$('#'+target).html(data);
	});
}

function deleteComments(target, comment_id, object_name, object_id)
{
	var agree=confirm('Вы действительно хотите удалить данный комментарий?');

	if (agree)
	{
		$.post("/comments/delete/"+ comment_id +"/"+ object_name +"/"+ object_id +"/", '', function(data)
		{
			$('#'+target).html(data);
		});
	}
}

HTMLElement.prototype.__defineGetter__ 
(
	"innerText",function()
	{
		var textRange = this.ownerDocument.createRange();
		textRange.selectNodeContents(this);
		return textRange.toString();
	}
);

function quoteComments(comment_id, author)
{
	var insert = document.getElementById(comment_id).innerText;

	insert.toString();

	if(insert.length > 0)
	{
		document.getElementById('commenttext').value = document.getElementById('commenttext').value + '[quote=' + author + ']' + insert +'[/quote]';
	}

	$('#commenttext').focus();
}

function reputationViewComments(target, user_fk)
{
	$.post("/comments/reputation/view/"+ user_fk +"/"+ target +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function reputationChangeComments(target, value, user_fk, object_name, object_fk)
{
	var form_data = $("#reputation_change").serialize();

	$.post("/comments/reputation/change/"+ value +"/"+ user_fk +"/"+ target +"/"+ object_name +"/"+ object_fk +"/", form_data, function(data)
	{
		$('#'+target).html(data);
	});
}

function reputationCloseComments(target)
{
	$('#' + target).hide('slow');
	$('#' + target).empty();
	$('#' + target).remove();
	$('#' + target).css({display:"none"});
}

function communitiesPostEdit(target, community_id, post_id)
{
	$.get("/community/post_edit/"+ community_id +"/"+ post_id +"/", function(data)
	{
		$('#'+target).html(data);
		$('#short_story').markItUp(mySettings);
		$('#full_story').markItUp(mySettings);
	});
}

function communitiesPostSave(target, community_id, post_id)
{
	var form_data = $("#formid_" + post_id).serialize();

	$.post("/community/post_save/"+ community_id +"/"+ post_id +"/", form_data, function(data)
	{
		$('#'+target).html(data);
	});
}

function communitiesPostDelete(target, community_id, post_id)
{
	var agree = confirm('Вы действительно хотите удалить эту запись?');

	if(agree)
	{
		$.post("/community/post_delete/"+ community_id +"/"+ post_id +"/", '', function(data)
		{
			$('#'+target).html(data);
		});
	}
}

function communitiesPostFix(post_id, pos, fix)
{
	var target = post_id + "_fix"; 
	$.post("/communities/postfix/"+ fix +"/"+ post_id +"/"+ pos +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function communitiesUpdateRating(target, community_fk, rating)
{
	$.post("/community/"+ community_fk +"/rating/"+ rating +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function communitiesUpdateRatingPosts(target, post_fk, rating)
{
	$.post("/community/posts/rating/"+ post_fk +"/"+ rating +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function communitiesMemberExit(target, community_fk)
{
	$.post("/community/member/leave/"+ community_fk +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function communitiesMemberJoin(target, community_fk)
{
	$.post("/community/member/join/"+ community_fk +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function photosDeletePhoto(target, owner_name, photo_fk)
{
	$.post("/user/"+ owner_name +"/photos/delete/"+ photo_fk +"/", '', function(data)
	{
		$('#'+target).html(data);
	});
}

function photosAddImagesInput()
{
	var tbl = document.getElementById('tblSample');
	var lastRow = tbl.rows.length;

	if(lastRow >= 9)
	{
		alert('Лимит одновременных закачек: 10 фото!');
	}
	else
	{
		var iteration = lastRow+1;
		var row = tbl.insertRow(lastRow);
		var cellRight = row.insertCell(0);
		var el = document.createElement('input');

		el.setAttribute('type', 'file');
		el.setAttribute('name', 'userfile_' + iteration);
		el.setAttribute('size', '40');
		el.setAttribute('value', iteration);
		cellRight.appendChild(el);

		document.getElementById('images_number').value = iteration;
	}
}

function photosDeleteImagesInput()
{
	var tbl = document.getElementById('tblSample');
	var lastRow = tbl.rows.length;

	if (lastRow > 1)
	{
		tbl.deleteRow(lastRow - 1);
		document.getElementById('images_number').value =  document.getElementById('images_number').value - 1;
	}
}

function userProfileEditShow(target)
{
	if(target)
	{
		$('#' + target).focus();
		$('#' + target).toggle('slow');
	}
}

function wallRecordShowForm(target, textarea_name)
{
	if(target)
	{
		$('#' + textarea_name).markItUpRemove();
		$('#' + textarea_name).markItUp(mySettings);
		$('#' + target).toggle('slow');
	}
}

function wallRecordAdd(target, wall_fk, form_name, textarea_name, hide_target)
{
	if(target)
	{
		var form_data = $('#' + form_name).serialize();

		$.post("/wall/record/add/", form_data, function(data)
		{
			$('#' + hide_target).hide();
			$('#' + target).html(data);
		});
	}
}

function wallRecordDelete(target, record_fk)
{
	var agree = confirm('Вы действительно хотите удалить эту запись со стены?');

	if (agree)
	{
		$.post("/wall/record/delete/"+ record_fk +"/", '', function(data)
		{
			$('#'+target).html(data);
		});
	}
}

function friendsUserAdd(target, friend_fk)
{
	if(friend_fk > 0)
	{
		$.post("/friends/add/"+ friend_fk +"/1/", '', function(data)
		{
			$('#' + target).html(data);
		});
	}
}

function friendsUserDelete(target, friend_fk)
{
	if(friend_fk > 0)
	{
		$.post("/friends/delete/"+ friend_fk +"/1/", '', function(data)
		{
			$('#' + target).html(data);
		});
	}
}

function usersRegisterCheckAvailibleName(target, form_name)
{
	if(target)
	{
		var form_data = $('#' + form_name).serialize();

		$.post("/users/register/checkname/", form_data, function(data)
		{
			$('#' + target).html(data);
		});
	}
}

function changeUserProfileKarma(target, user_fk, type, voted_num)
{
	if(target && user_fk > 0 && (type == 'up' || type == 'down'))
	{
		if(type == 'up')
		{
			var type = 1;
			var voted_num = voted_num + 1;
		}
		else
		{
			var type = 0;
			var voted_num = voted_num - 1;
		}

		$.post("/user/change/karma/"+ user_fk +"/"+ type +"/", '', function(data)
		{
			$('#' + target).html(data);
			$('#karma_user_voted_num').html(voted_num);
		});
	}
}

function changeSelectedMenu(menu_name)
{
	if(last_menu_open && last_menu_open != menu_name)
	{
		$('#' + last_menu_open + '_items').hide();
	}

	last_menu_open = menu_name;

	if(menu_name)
	{
		$('#' + menu_name + '_items').toggle();
	}
}

function updateCommentsRating(target, comment_fk, type)
{
	if(target && comment_fk > 0 && type)
	{
		if(type == 'up')
		{
			var type = 1;
		}
		else if(type == 'down')
		{
			var type = 0;
		}

		$.post("/comments/rating/update/"+ comment_fk +"/"+ type +"/", '', function(data)
		{
			$('#' + target).html(data);
		});
	}
}

function addUserFriendFromAjaxPopup(friend_fk)
{
	if(friend_fk > 0)
	{
		$.post("/friends/add/"+ friend_fk +"/", '', function(data)
		{
			hideObjectByTargetName('popup_msg');
		});
	}
}

function rejectUserFriendFromAjaxPopup(friend_fk)
{
	if(friend_fk > 0)
	{
		$.post("/friends/reject/"+ friend_fk +"/", '', function(data)
		{
			hideObjectByTargetName('popup_msg');
		});
	}
}

function findUsersForNewMailSend(target, form_name)
{
	if(target)
	{
		var form_data = $('#' + form_name).serialize();

		$.post("/mail/find/user", form_data, function(data)
		{
			$('#' + target).html(data);
		});
	}
}

function bannedUser(username, ban)
{
	$.post("/user/"+ username +"/ban", '', function(data)
	{
		if(ban == true){
			$('#useLinkBan').html(':: <b><a href="javascript:bannedUser(\'' + username + '\', false)">Разбанить</a></b>');
		}
		else{
			$('#useLinkBan').html(':: <b><a href="javascript:bannedUser(\'' + username + '\', true)">Забанить</a></b>');
		}
	});
}

function clearCommentUser(username)
{
	$.post("/user/"+ username +"/clear", '', function(data)
	{
		alert('Комментарии удалены!');
	});
}

function votePhoto(vote, photo_id)
{
	$.post("/user_photo/" + photo_id + "/" + vote + "/", '', function(data)
	{
		$('#vote_num').html(data);
	});	
}

function upImgInPost()
{
	window.open("/communities/upimage/", "Upload", "width=700, height=400, scrollbars=yes");
	//$.post("", '', function(data)
	//{
	//	$('#adminupload').html(data);
	//});	
}

function selectUpButton(id, inp){
	inp = document.getElementById(inp);
	id  = document.getElementById(id);
	if(id.value == 0){
		inp.style.backgroundColor = '#009900';
		inp.value = 'Отмечено';
		id.value = 1;
	}
	else{
		inp.style.backgroundColor = '#990000';
		inp.value = 'Отметить';
		id.value = 0;
	}
} 


function chekComPost(){
	if(document.getElementById('title_value').value != '' && document.getElementById('short_story').value != ''){
		document.getElementById('addPost').submit();
	}
	else{
		alert('Не все поля заполнены!');
	}
}