function wall_add_handlers(prfx) {
	$(prfx+'.day h5').click(function() {
		var day = $(this).parent('.day');
		if (day.is('.collapsed')) {
			day.removeClass('collapsed');
		} else {
			day.addClass('collapsed');
		}
	});
	$(prfx+'.day .post-text').hover(function() {
			$(this).addClass('hover') },
			function() {
			$(this).removeClass('hover') });
	$(prfx+'.day .comment .text').hover(function() {
			$(this).addClass('hover') },
			function() {
			$(this).removeClass('hover') });
			
	$(prfx+'.post-actions>a:first-child').click(function() {
		var frm = $(this).siblings('.comment_form');
		if (frm.is(':visible')) {
			frm.hide();
		} else {
			frm.show();
			frm.find('textarea').focus();
		}		
		return false;
	});
	
	$(prfx+'a.report_button').click(function() {
		var rep = $(this).parent().siblings('.report_form');
		if (rep.is(':visible')) {
			rep.hide();
		} else {
			rep.show();
			rep.find('textarea').focus();
		}
		return false;
	});
	
	$(prfx+'.post-actions>a.translate_button').click(function() {
		var lnk = $(this);
		var txts = lnk.parents('.post').find('.post-text, .comment .text, .post-image-details p');
		GC.translate(txts);
		return false;
	});
	
	/*
	$('.post-actions input.submit').click(function() {
		var frm = $(this).parents('form');
		$.post(frm.attr('action'), frm.serialize(), function(res) {
			console.debug(res);
		}, 'json');
		
		return false;
	});
	*/
}

$(function() {
	var defVal = 'Write a comment...';
	var repDefVal = 'Report this item...';
	
	if (LANG == 'es') {
		defVal = 'Escribe tu comentario...';
	}

	var checkLen = function(ta) {
		if (ta.val().length > 1000) {
			ta.val(ta.val().substring(0, 1000));
		}
	};

	$(".comment_box").val(defVal);
	$(".report_box").val(repDefVal);
	$(".comment_box").focus(function(event) {
		if($(this).val() == defVal)
			$(this).val("");
		checkLen($(this));
	});	
	$(".report_box").focus(function(event) {
		if($(this).val() == repDefVal)
			$(this).val("");
		checkLen($(this));
	});
	
	$(".comment_box").blur(function(event) {
		if($(this).val() == "")
			$(this).val(defVal);
		checkLen($(this));
	});	
	$(".report_box").blur(function(event) {
		if($(this).val() == "")
			$(this).val(repDefVal);
		checkLen($(this));
	});
	$(".comment_box").keydown(function(event) {
		checkLen($(this));
	});	
	$(".report_box").keydown(function(event) {
		checkLen($(this));
	});
	$(".comment_box").parents('form').submit(function() {
		var val = $(this).find('.comment_box').val();
		if (val == '' || val == defVal) {
			return false;
		}
		return true;
	});
	$(".report_box").parents('form').submit(function() {
		var val = $(this).find('.report_box').val();
		if (val == '' || val == repDefVal) {
			return false;
		}
		return true;
	});
	
	if (typeof $(".comment_box").autogrow != 'undefined') {
		$(".comment_box").autogrow();
	}	
	if (typeof $(".report_box").autogrow != 'undefined') {
		$(".report_box").autogrow();
	}
	$("#add-image>a").click(function() {
		$("#add-image .swfUploadCont").show();
		$(this).hide();
		return false;
	});

	wall_add_handlers('');
});
function activateLi(lnk) {
	if (!lnk) return false;
	lnk = $(lnk);
	lnk.parents('li').addClass('active');
	lnk.parents('li').siblings().removeClass('active');
	return lnk;
}
function updateRecentActivities(cnt) {
	var el = $('#recent_activities');
	el.html(cnt);
	recent_activities_add_handlers(el);
	user_tag_add_handlers(el);
}
function show_older_recent_activity() {
	var filt = $('#recent_activity_filters li').index($('#recent_activity_filters li.active'));
	switch (filt) {
		case 1:
			return show_my_recent_activity(null, true);
		case 2:
			return show_friends_recent_activity(null, true);
	}
	return show_all_recent_activity(null, true);
}
function show_all_recent_activity(lnk, isLong) {
	activateLi(lnk);
	$.get(
		WEB_ROOT+
		'recent_activities/show_all_recent_activity'+(isLong ? '/1' : ''), updateRecentActivities
	);
	return false;
}
function show_my_recent_activity(lnk, isLong) {
	activateLi(lnk);
	$.get(
		WEB_ROOT+
		'recent_activities/show_my_recent_activity'+(isLong ? '/1' : ''), updateRecentActivities
	);
	return false;
}
function show_friends_recent_activity(lnk, isLong) {
	activateLi(lnk);
	$.get(
		WEB_ROOT+
		'recent_activities/show_friends_recent_activity'+(isLong ? '/1' : ''), updateRecentActivities
	);
	return false;
}
function updatePosts(filt) {
	var cont = $('#wall-main');
	var loaderAnim = $('<img src="'+WEB_ROOT+'img/loader2.gif" style="margin:15px"/>');
	cont.html(loaderAnim);
	$.get(WEB_ROOT+'wall_posts/older_posts/0/0/'+filt, function(res){
		cont.html(res);
		wall_add_handlers('#wall-main ');
	});
}
function show_all_posts(lnk) {
	activateLi(lnk);
	$('.post').show();
	updatePosts(0);
	return false;
}
function show_my_posts(lnk) {
	activateLi(lnk);
	$('.post.mypost').show();
	$('.post').not('.mypost').hide();
	updatePosts(1);
	return false;
}
function show_friends_posts(lnk) {
	activateLi(lnk);
	$('.post').not('.mypost').show();
	$('.post.mypost').hide();
	updatePosts(2);
	return false;
}

function show_all_comments(lnk) {
	lnk = $(lnk);
	var cont = lnk.parents('.post-comments');
	cont.find('.comment').show();
	lnk.hide();
	return false;
}
function showOlderPosts(lnk, uid, lastId){
	lnk = $(lnk);
	if ($('#postsAjaxCont'+lastId).size()){
		return true;
	}
	var loaderAnim = $('<img src="'+WEB_ROOT+'img/loader2.gif" style="margin:15px"/>');
	lnk.siblings('div').append(loaderAnim);
	
	var filt = $('#posts_filters li').index($('#posts_filters li.active'));
	
	$.get(WEB_ROOT+'wall_posts/older_posts/'+uid+'/'+lastId+'/'+filt, function(res){
		lnk.siblings('div').append('<div id="postsAjaxCont'+lastId+'"></div>');
		$('#postsAjaxCont'+lastId).html(res);
		wall_add_handlers('#postsAjaxCont'+lastId+' ');
		loaderAnim.hide();
	});
	return true;
}
