var social_action_class = "item_congdong";
var social_action_prefix = "social_action";
var cd_comment_1 = 'cd_comment_1';
var cd_comment_2 = 'cd_comment_2';
var comment_class = 'item_comment';
var comment_prefix = "action_comment";
$(document).ready(function(){
	place = typeof(place) != 'undefined' ? place : 'profile';
	if(place==''){
		cd_comment_1 = 'cd_comment_1_all'; 
		cd_comment_2 = 'cd_comment_2_all'; 
		comment_class = 'item_comment_all';
	}
	socialActionDeletionAttachEvent();
	commentAttachEvent();
	commentDeletionAttachEvent();
})

function socialActionDeletionAttachEvent(element){
	element = typeof(element) != 'undefined' ? element : $('.'+social_action_class);
	// For deleting comment
	element.hover(
		function(){
			var social_action_id = $(this).attr('id');
			social_action_id = social_action_id.substring(social_action_prefix.length);
			$('#social_action_delete'+social_action_id).css('visibility','visible');
		},
		function(){
			var social_action_id = $(this).attr('id');
			social_action_id = social_action_id.substring(social_action_prefix.length);
			$('#social_action_delete'+social_action_id).css('visibility','hidden');
		}
	);
}

function commentAttachEvent(){
	// For posting comment
	$('.cd_comment_active').focus(function(){
        show_comment($(this));
		fixPNG();
    });
    $('.cd_comment_active').blur(function(){
        hide_comment($(this));
		fixPNG();
    });
}
function commentDeletionAttachEvent(element){
	element = typeof(element) != 'undefined' ? element : $('.'+comment_class);
	// For deleting comment
	element.hover(
		function(){
			var comment_id = $(this).attr('id');
			var social_action_id = comment_id.substring(comment_prefix.length);
			$('#comment_delete'+social_action_id).css('visibility','visible');
		},
		function(){
			var comment_id = $(this).attr('id');
			var social_action_id = comment_id.substring(comment_prefix.length);
			$('#comment_delete'+social_action_id).css('visibility','hidden');
		}
	);
}

function fixPNG(){
	if(is_IE6){
		if(place == 'profile'){
			actionListFixPNG();
		} else {
			contentFixPNG();
		}
	}
}

function show_comment(obj){
    var txt = obj.val();
    if(txt == 'Bình luận'){
        obj.val('');
        obj.removeClass(cd_comment_1);
        obj.addClass(cd_comment_2);
        obj.parent('div').children('button').show();
        $('.bg_content_w506').css('background','transparent url('+webroot+'/static/images/bg_content_w506.png) no-repeat scroll left top');
		// Fix PNG IE6
		fixPNG();
    }
}

function hide_comment(obj){
    var txt = obj.val();
    if(txt == ''){
        obj.val('Bình luận');
        obj.removeClass(cd_comment_2);
        obj.addClass(cd_comment_1);
        obj.parent('div').children('button').hide();
		
        // Fix PNG IE6
		fixPNG();
    }
}

function actionListFixPNG(){
	if(is_IE6){
		//Fix png for IE6
		$('.bg_content_w506').css('background','transparent url('+webroot+'static/images/bg_content_w506.png) no-repeat scroll left top');
	}
}
function contentFixPNG(){
	if(is_IE6){
		//Fix png for IE6
		$('.bg_content_w742').css('background','transparent url('+webroot+'static/images/bg_content_w742.png) no-repeat scroll left top');
	}
}

function showNextSocialActionList(user_id){
	$.get(webroot+"social/ajax_showNextSocialActionList/"+user_id+'/'+social_action_start+'/'+is_mine, 		function(data){
		var position = data.indexOf('|');
		social_action_start = parseInt(data.substring(0,position));
		data = data.substring(position+1,data.length);
		social_action_start = parseInt(social_action_start);
		$('.cd_xemthem').before(data);
		if(social_action_start==0){
			$('.cd_xemthem').hide();
		}
		
		actionListFixPNG();
		
        socialActionDeletionAttachEvent();
		//Attach click event handler to open comment form
		commentAttachEvent();
		commentDeletionAttachEvent();
		
		// Fix PNG IE6
		fixPNG();
	});
	
	return false;
}
function postComment(social_action_id, text_box_id){
	var ajax_postComment = function(){
		var comment = $('#'+text_box_id).val();
		$.post(	webroot+"social/ajax_postComment", 
				{ 'comment': comment ,'social_action_id': social_action_id,'place':place} ,	
				function(data){
					var position = data.indexOf('|');
					var comment_count = parseInt(data.substring(0,position));
					data = data.substring(position+1,data.length);
					$('#comment_count'+social_action_id).html(comment_count);
					$('#'+text_box_id).val('');
					$('#'+text_box_id).parent().parent().before(data);
					
					commentDeletionAttachEvent($('#'+text_box_id).parent().parent().prev());
					
					// Fix PNG IE6
					fixPNG();
				}
		);
	};
	var focusTextbox = function(){
		$('#'+text_box_id).focus();
	}
	do_ajaxCheckLogin(ajax_postComment,'Vui lòng đăng nhập để gởi bình luận.',focusTextbox);
	return false;
}

function showAllComments(social_action_id){
	var comment_count = $('#comment_count'+social_action_id);
	$.get(webroot+"social/ajax_showAllComments/"+social_action_id+'/'+place, function(data){
		$('.comment_item'+social_action_id).hide();
		comment_count.parent().parent().after(data);
		commentDeletionAttachEvent(comment_count.parent().parent().siblings());
		comment_count.parent().parent().hide();
		
		// Fix PNG IE6
		fixPNG();
	});
	
	return false;
}

function follow(category_id, content_id){
	return createSocialAction('follow',category_id, content_id);
}
function like(category_id, content_id){
	return createSocialAction('like',category_id, content_id);
}
function createSocialAction(action,category_id,content_id){
	var postSocialAction = function(){
		$.post(webroot+'social/'+action+'/'+category_id+'/'+content_id ,	
			function(data){
				if(data == 'Done'){
					$('#'+action+content_id).hide();
				}
			}
		);
	}
	do_ajaxCheckLogin(postSocialAction);
	return false;
}

/*Begin: BLOG-LIKE*/
function postBlogLikeComment(social_action_id, text_box_id, place){
	var ajax_postComment = function(){
		var comment = $('#'+text_box_id).val();
		$.post(	webroot+"social/ajax_postComment", 
				{ 'comment': comment ,'social_action_id': social_action_id,'place':'profile'} ,	
				function(data){
					var position = data.indexOf('|');
					var comment_count = parseInt(data.substring(0,position));
					data = data.substring(position+1,data.length);
					$('#comment_count'+social_action_id).html(comment_count);
					$('#'+text_box_id).val('');
					$('#'+text_box_id).parent().parent().before(data);
					
					// Fix PNG IE6
					fixPNG();
				}
		);
	};
	var focusTextbox = function(){
		$('#'+text_box_id).focus();
	}
	do_ajaxCheckLogin(ajax_postComment,'Vui lòng đăng nhập để gởi bình luận.',focusTextbox);
	return false;
}
function showAllBlogLikeComments(social_action_id){
	var comment_count = $('#comment_count'+social_action_id);
	$.get(webroot+"social/ajax_showAllComments/"+social_action_id+'/'+place, function(data){
		$('.comment_item'+social_action_id).hide();
		$('#view_all_comments'+social_action_id).after(data);
		commentDeletionAttachEvent($('#view_all_comments'+social_action_id).siblings());
		$('#view_all_comments'+social_action_id).hide();
		
		// Fix PNG IE6
		fixPNG();
	});
	
	return false;
}
/*End: BLOG-LIKE*/

function deleteSocialAction(social_action_id,action_id){
	var ajax_deleteSocialAction = function(){
		if (confirm("Bạn có muốn xóa hoạt động này?")) {
			$.post(	webroot+"social/ajax_deleteSocialAction", 
					{'social_action_id':social_action_id,'action_id':action_id} ,	
					function(data){
						if(data == 'Done'){
							$('#'+social_action_prefix+social_action_id).hide();
							
							// Fix PNG IE6
							//fixPNG();
						}
					}
			);
		}
	};
	do_ajaxCheckLogin(ajax_deleteSocialAction,'Vui lòng đăng nhập để xóa hoạt động này.',ajax_deleteSocialAction);
	return false;
}

function deleteComment(comment_id,social_action_id){
	var ajax_deleteComment = function(){
		if (confirm("Bạn có muốn xóa bình luận này?")) {
			$.post(	webroot+"social/ajax_deleteComment", 
					{ 'comment_id': comment_id, 'social_action_id':social_action_id} ,	
					function(data){
						var comment_count = parseInt(data);
						if(data == 'Done' || comment_count >= 0){
							$('#'+comment_prefix+comment_id).hide();
							if(comment_count >= 0){
								$('#comment_count'+social_action_id).html(comment_count);
							}
							
							// Fix PNG IE6
							fixPNG();
						}
					}
			);
		}
	};
	do_ajaxCheckLogin(ajax_deleteComment,'Vui lòng đăng nhập để xóa bình luận.',ajax_deleteComment);
	return false;
}
