function makeComment()
{
	if(user_id != null)
	{
		$('#box_comments_post').show()
		$('#comments').hide()
	}
	else
		loginPopup()
		
	return false
}
function addComment()
{
	$.ajax({type:'POST',url:base_dir+'comments/add',data: $('#commentForm').serialize()+'&json=1', dataType:'json',
		success:function(json){if(!json.success) makePopUp("Whoops!", json.error);
		else{$('textarea[name="comment"]').val('');$('#box_comments_post').hide();addCmntCount(1);toggleCmnts()}}});
	return false
}

function getCmnts(offset)
{
	if(offset == null) offset = 0
	$("#comments").empty();
	$("#comments").html("<center><img src='"+base_dir+"img/load.gif' /></center>");
	$.getJSON(base_dir+'comments/getForOS/outshout_id/'+outshout_id+'/offset/'+offset+'/json',
	function(json){
			$("#comments").empty();
			//$('#comments').html('<ul>');
			for(i in json.comments){
				var cmnt = json.comments[i];
				var comment = '<ul><li><a href="'+base_dir+cmnt.username+'">'
							 +'<img alt="Avatar" src="'+base_dir+'users/avatar/user_id/'+cmnt.user_id+'" /></a>'
							 +'<p><a href="'+base_dir+cmnt.username+'">'+cmnt.username+'</a> says:</p>'
							 +'<p>'+cmnt.comment+'</p>'
							 if(user_id != null && user_id == sender_id)
							 	comment += '<p><a href="#" class="delete" onclick="return delCmnt('+cmnt.comment_id+')"><img src="/img/buttons/delete_small.gif" class="delete_small_btn" /></a></p>'
					comment += '</li><li class="bord_bot_dot"></li></ul>'
				
				
				$('#comments').append(comment);
			}
	});
	return false
}

function delCmnt(cmnt_id)
{
	$.ajax({type:'POST',url:base_dir+'comments/delComment',data: 'cmnt_id='+cmnt_id+'&type=2&json=1', dataType:'json',
		success:function(json){if(json.success) {getCmnts();addCmntCount(-1) }else makePopUp("Whoops!", json.error);}});
	return false
}

function addCmntCount(val)
{
	var count = eval($('#cmnt_count').html())+val
	$('#cmnt_count').html(count)
	if($('#cmnt_count').html() == '')$('#cmnt_count').html('0')
	if(count == 0) $('#comments').hide()
}

function toggleCmnts()
{
	$('#box_comments_post').hide()
	$('#comments').toggle()
	if($('#comments').css('display') != 'none') getCmnts()
	return false
}

function inputTags()
{
	if(user_id == null) loginPopup();
	else
		$("#tag_input").val('').addClass("tag_input_focus")
}

function addTags()
{
	$.ajax({type:'POST',url:base_dir+'tags/add',data: $('#addTags').serialize()+'&json=1', dataType:'json',
		success:function(json){if(json.error) makePopUp("Whoops!", json.error);
			else{$('#tag_input').val(''); toggleTagField(); getTags()}}})
	return false
}

function getTags()
{
	var tags = ''
	$.getJSON(base_dir+'tags/getForOS/outshout_id/'+outshout_id+'/json',
		function(json){
			for(i in json.tags) tags += '<a href="'+base_dir+'outshouts/tagged/tag/'+json.tags[i]+'">'+json.tags[i]+"</a>, "
			$('#taglist').html(tags.substr(0,(tags.length-2)))
		})
}

function toggleTagField()
{
	if ($("#tag_input").hasClass("tag_input_focus"))
		$("#tag_input").removeClass("tag_input_focus")
	return false
}

function addFriend(friend_id)
{
	$.ajax({type:'POST',url:base_dir+'friends/add',data: 'friend_id='+friend_id+'&json=1', dataType:'json',
		success:function(json){
			if(json.success)
				$('#addFriendHolder').html('<a><img src="' + base_dir + 'img/buttons/profile_added_friend.png" /></a>');
			else makePopUp("Whoops!", json.error);
		}});
	return false
}

function playbackStarted()
{
	$.post(base_dir+"outshouts/incrementViews",{outshout_id:outshout_id})
}


function saveFavOs(outshout_id)
{
	$.ajax({type:'POST',url:base_dir+'outshouts/save',data: 'outshout_id='+outshout_id+'&json=1', dataType:'json',
		success:function(json){if(json.success) showSaveFavMsg(json.success); else showSaveFavMsg("Oops! " + json.error)}})
}

function showSaveFavMsg(msg)
{
	$('#saveFavMsg .body').html(msg)
	$('#saveFavMsg').fadeIn(1500)
	setTimeout("$('#saveFavMsg').fadeOut(1500)", 3000)
}
	

function flag(os_id)
{
	$.ajax({type:'POST',url:base_dir+'outshouts/flag',data: '&os_id='+os_id+'&json=1', dataType:'json',
				success:function(json){if(json.success){ 
				$('#link_flag').html("<a href=\"#\" onclick='makePopUp(\"Flag Outshout\", \"You have already flagged this outshout!\");return false' id=\"flagged\" title=\"Outshout flagged!\"></a>");
				makePopUp("Flag Outshout", "You've flagged this OUTSHOUT for objectionable content.");}}})
	
	return false;	
}
