/**
 * Action on focus
 */
function activityCommentInputFocus(actionId, randId) {
	if ($F('activity_comment_' + actionId + '_input_' + randId) == 'Write a comment...') {
		$('activity_comment_' + actionId + '_input_' + randId).clear();
	}
	$('activity_comment_' + actionId + '_button_' + randId).show();
}

/**
 * Action on blur
 */
function activityCommentInputBlur(actionId, randId) {
	if ($F('activity_comment_' + actionId + '_input_' + randId).blank()) {
		$('activity_comment_' + actionId + '_input_' + randId).value = 'Write a comment...';
		$('activity_comment_' + actionId + '_button_' + randId).hide();
		if ($('activity_' + actionId + '_comments_block_' + randId) && $('activity_' + actionId + '_comments_block_' + randId).innerHTML.blank()) {
			$('activity_' + actionId + '_inputs_block_' + randId).hide();
			$('comment_link_' + actionId + '_' + randId).show();
		}
	}
}

/**
 * Sends comment of activity
 */
function sendComment(actionId, randId, urlPart) {
	textarea = 'activity_comment_' + actionId + '_input_' + randId;
	new Ajax.Request(urlPart + 'activity/comments', {
		parameters: { action_id: actionId, text: $F(textarea) },
		onSuccess: function(transport) {
			loadAllComments(actionId, randId, urlPart);
			$('activity_' + actionId + '_inputs_block_' + randId).hide();
			$('add_activity_comment_' + actionId + '_' + randId).show();
			$('activity_comment_' + actionId + '_input_' + randId).clear();
		}
	});
}

function appendActivity(a, beforePulsate, appendAfter) {
	beforePulsate = beforePulsate || Prototype.emptyFunction;
	
	var list = $('activity-list')
	
	if (! list) {
		return false;
	}
	
	var root = list.down('label');

	if (appendAfter) {
		Element.insert(appendAfter, {'after': a});
	}
	else {
		if (! root) {
			root = list.down('h3');
			if (root) {
				root.remove();
				list.appendChild(a);
			}
			else {
				Element.insert(list.down('.wrapper'), {'before': a});
			}
		}
		else {
			Element.insert(root, {after: a});
		}
	}
	
	//a = root.next('div.wrapper');
	
	new Effect.BlindDown(a, {
		duration: 0.5,
		afterFinish: function() {
			new Effect.Pulsate(a, {
				duration: 0.5,
				beforeStart: function() {
					beforePulsate();
				}
			});
		}
	});
	
	return true;
}

/**
 * Sends comment of activity
 */
function rePost(actionId, urlPart, t) {
	var indicator = $('activityIndicator'), oldText = indicator.innerHTML;
	new Ajax.Request(urlPart + 'activity/repost', {
		parameters: { action_id: actionId },
		onCreate: function() {
			new Effect.Appear(indicator.update('reposting ...'));
			t.removeAttribute('onclick');
			t.setAttribute('onclick', 'return false;');
		},
		onSuccess: function(transport) {
			var r = transport.responseText;
			$('activityIndicator').hide();
			indicator.update(oldText);
			var mask_start = 'id="activity_', mask_end = '"';
			var id = parseInt(r.substring(r.indexOf(mask_start)+mask_start.length, r.indexOf(mask_end, r.indexOf(mask_start)+mask_start.length)));
			liveSearch.exclude(id);
			appendActivity(r, function() {
				new Effect.Fade(indicator, {
					afterFinish: function() {
						//indicator.update(oldText)
					}
				});
			});			
		}
	});
}

/**
 * Loads all comments of activity
 */
function loadAllComments(actionId, randId, urlPart) {
	commentBlock = 'activity_' + actionId + '_comments_block_' + randId;
	$(commentBlock).up().className = 'commentsBubble';
	new Ajax.Updater(commentBlock, urlPart + 'activity/comments', {
		parameters: { action_id: actionId }
	});
}

/**
 * Returns number of activity comments
 */
function getActivityCommentsCount(actionId, randId, urlPart) {
	countBlock = 'activity_' + actionId + '_comments_count_' + randId;
	new Ajax.Updater(countBlock, urlPart + 'activity/comments', {
		parameters: { action_id: actionId, get_count: 1 }
	});
}

/**
 * Removes activity comment
 */
function deleteActivityComment(el, commentId, actionId, urlPart) {
	if (confirm('Are you sure you want to delete this comment?')) {
		new Ajax.Request(urlPart + 'activity/comments', {
			parameters: {
				action_id: actionId,
				delete_comment_id: commentId
			}
		});
		/*elx = el.up(1).down()
		while (elx != null && elx.tagName.toUpperCase() != 'A') {
			elx = elx.next()
		}
		if (elx != null) {
			elx.down().innerHTML--
		}
		el.up(1).remove(1);*/
		try {
			el.up('.comm_item_').remove();
		}
		catch(e) {}
	}
}

/**
 * Checks may activity be deleted by viewer, if yes - image appears 
 */
function checkDeleteActivity(ownerId, actId, randId, toUserId, repostUserId) {

	if ($('owenrIsViewingPage')) {
		var delId = 'deleteActivityN' + actId + 'Of' + ownerId + '_' + randId;
		$(delId).show();
		return true;
	}

	if ($('idOfUserWhoViewThisPage')) {
		var uid = $F('idOfUserWhoViewThisPage');
		if (uid && (ownerId == uid || uid == toUserId || uid == repostUserId)) {
			var delId = 'deleteActivityN' + actId + 'Of' + ownerId + '_' + randId;
			if ($(delId)) {
				$(delId).show();
			}
		}
	}
	return true;
}

/**
 * Hides delete link when mouse is out
 */
function hideDeleteActivity(ownerId, actId, randId) {
	var delId = 'deleteActivityN' + actId + 'Of' + ownerId + '_' + randId;
	if ($(delId)) {
		$(delId).hide();
	}
}

/**
 * Checks may activity comments be deleted by viewer, if yes - image appears 
 */
function checkDeleteActivityComment(commentOwnerId, activityOwnerId, commentId, randId) {
	if ($('owenrIsViewingPage')) {
			if ($('actDelComm' + commentOwnerId + 'u' + activityOwnerId + 'a' + commentId + 'r' + randId)) {
				$('actDelComm' + commentOwnerId + 'u' + activityOwnerId + 'a' + commentId + 'r' + randId).show();
			}
		return true;
	}
	
	if ($('idOfUserWhoViewThisPage')) {
		var uid = $F('idOfUserWhoViewThisPage');		
		if (commentOwnerId == uid || activityOwnerId == uid) {

			if ($('actDelComm' + commentOwnerId + 'u' + activityOwnerId + 'a' + commentId + 'r' + randId)) {				
				$('actDelComm' + commentOwnerId + 'u' + activityOwnerId + 'a' + commentId + 'r' + randId).show();
			}
		}
	}
}

/**
 * Hides delete link for comment when mouse is out
 */
function hideDeleteActivityComment(commentOwnerId, activityOwnerId, commentId, randId) {
	if ($('actDelComm' + commentOwnerId + 'u' + activityOwnerId + 'a' + commentId + 'r' + randId)) {
		$('actDelComm' + commentOwnerId + 'u' + activityOwnerId + 'a' + commentId + 'r' + randId).hide();
	}
}

/**
 * Checks may user comment activity, if yes - textarea or link appears
 */
function checkCanActivityComment(actId, randId, commentsExists) {
	if ($('idOfUserWhoViewThisPage') && parseInt($F('idOfUserWhoViewThisPage')) > 0) {
		if (!commentsExists && $('comment_link_' + actId + '_' + randId)) {
			$('comment_link_' + actId + '_' + randId).show();
		}
		if (commentsExists && $('activity_' + actId + '_inputs_block_' + randId)) {
			$('activity_' + actId + '_inputs_block_' + randId).show();
		} else {
		    $('activity_' + actId + '_inputs_block_' + randId).up().removeClassName('commentsBubble');
		}
	} else {
		if (!commentsExists && $('activity_' + actId + '_inputs_block_' + randId)) {
			$('activity_' + actId + '_inputs_block_' + randId).up().removeClassName('commentsBubble');
		}
	}
}

/**
 * Removes activity
 */
function removeThisActivity(id, url) {
	if (confirm('Are you sure?')) {
		new Ajax.Request(url, {
			asynchronous:true,
			evalScripts:false,
			method:'post',
			onSuccess: function(request, json) {
				if (typeof window.shareQH != 'undefined') {
					var idMedia = window.shareQH.get(id);
					if (! Object.isUndefined(window.uQ[idMedia])) {
						window.uQ[idMedia].stop();
					}
				}
				if ($('activity_'+id)) {
					new Effect.Fade('activity_'+id, {
						afterFinish: function() {
							$('activity_'+id).remove();
							Element.hide('activityIndicator');
						}
					});
				}
			},
			onCreate: function() {
				new Effect.Appear('activityIndicator', {});
			}
		});
	};
	return false;
}


/********** UPDATED VERSION **********/

/**
 * Click on Comment link
 */
function activityBoxCommentClick(actionId, actRandom) {
	$('bubbleComment_' + actionId + '_' + actRandom).show();
	var acTextarea = $('ac_ta_'+actionId+'_'+actRandom);
	var acTextbox = $('ac_tb_'+actionId+'_'+actRandom);
	if (acTextbox.visible()) {
		commentTextboxClick(actionId, actRandom);
	}
}

/**
 * Click in textbox comment field
 * @param actionId
 * @param actRandom
 * @return
 */
function commentTextboxClick(actionId, actRandom) {
	var acTextarea = $('ac_ta_'+actionId+'_'+actRandom);
	var acCommentButton = $('ac_cb_'+actionId+'_'+actRandom);
	var acTextbox = $('ac_tb_'+actionId+'_'+actRandom);
	acTextbox.hide();
	acTextarea.show();
	acCommentButton.show();
	acTextarea.focus();
}

/**
 * Comment textarea blur
 * @param actionId
 * @param actRandom
 * @return
 */
function commentTextareaBlur(actionId, actRandom) {
	var acTextarea = $('ac_ta_'+actionId+'_'+actRandom);
	var acCommentButton = $('ac_cb_'+actionId+'_'+actRandom);
	if (acTextarea.value.blank()) {
		acTextarea.hide();
		acCommentButton.hide();
		setTimeout("$('ac_tb_"+actionId+"_"+actRandom+"').show()", 200);
	}
}

/**
 * Click on like/unlike link
 * @param el
 * @param actionId
 * @param actRandom
 * @return
 */
function activityBoxLikeClick(el, actionId, actRandom, itemTypeValue, itemIdValue) {
	//quick fix
	//если в шаблоне(_filterBy.tpl) заданы тексты сообщений с учетом интернационализации, то будем выводить их, ежели нет - то как и раньше
	var like = (typeof(likeMessages) != 'undefined' && likeMessages['like']) ? likeMessages['like'] : 'Like';
	var unlike = (typeof(likeMessages) != 'undefined' && likeMessages['unlike']) ? likeMessages['unlike'] : 'Unlike';
	
	if (el.innerHTML == like) {
		el.innerHTML = unlike;
		el.className = 'unlike_link';
		$('bubbleComment_' + actionId + '_' + actRandom).show();
	} else {
		el.innerHTML = like;
		el.className = 'like_link';
		
	}
	new Ajax.Request(updateLikeUrl, {
		parameters: { id: itemIdValue, type: itemTypeValue, template: 'activity' },
		onSuccess: function(transport) {
			$('like_list_'+actionId+'_'+actRandom).innerHTML = transport.responseText;
		}
	});
}

/**
 * Sends comment of activity
 */
function abSendComment(actionId, randId) {
	textarea = 'ac_ta_' + actionId + '_' + randId;
	button = 'ac_cb_' + actionId + '_' + randId;
	commentText = $F(textarea);
	$(textarea).disable();
	$(button).disable();
	new Ajax.Request(urlPart + 'activity/comments', {
		parameters: { action_id: actionId, text: commentText },
		onSuccess: function(transport) {
			$(textarea).enable();
			$(button).enable();
			$(textarea).clear();
			commentTextareaBlur(actionId, randId);
			abLoadAllComments(actionId, randId);
		}
	});
}

/**
 * Loads all activity comments
 * @param actionId
 * @param randId
 * @return
 */
function abLoadAllComments(actionId, randId) {
	commentBlock = 'a_com_bl_' + actionId + '_' + randId;
	new Ajax.Updater(commentBlock, urlPart + 'activity/comments', {
		parameters: { action_id: actionId }
	});
}

/**
 * Checks may user comment activity, if yes - textarea or link appears
 */
function abCheckCanActivityComment(actId, randId, showBubble) {
	if ($('bubbleComment_' + actId + '_' + randId) && showBubble) {;
		$('bubbleComment_' + actId + '_' + randId).show();
	}
}

/**
 * Removes activity comment
 */
function abDeleteActivityComment(commentId, actionId, urlPart) {
	if (confirm('Are you sure you want to delete this comment?')) {
		new Ajax.Request(urlPart + 'activity/comments', {
			parameters: { action_id: actionId, delete_comment_id: commentId }
		});
		$('ac_' + actionId + '_' + commentId).remove();
	}
}
