/*rc: received comment
 *pc: post comment
 *1: rc 2: pc
 */
(function($){
	$.commentsInSpace = {
	type:1,
	rcHtml:null,
	pcHtml:null,
	showRCHtml:function() {
		if($.commentsInSpace.type == 1) return;
		$('#commentContainer').html('');
		$('#commentContainer').html($.commentsInSpace.rcHtml);
		$.commentsInSpace.type = 1;
		$('#rcImg').attr('src', 'http://static.mytuya.com/res/img/myspace/myspaceindex_08.jpg');
		$('#pcImg').attr('src', 'http://static.mytuya.com/res/img/myspace/myspaceindex_09.jpg');
	},
	showPCHtml:function() {
		if($.commentsInSpace.type == 2) return;
		$('#commentContainer').html('');
		if($.commentsInSpace.pcHtml != null) {
			$('#commentContainer').html($.commentsInSpace.pcHtml);
		} else {
			getPostComments();
		}
		$.commentsInSpace.type = 2;
		$('#rcImg').attr('src', 'http://static.mytuya.com/res/img/myspace/shoudao2.jpg');
		$('#pcImg').attr('src', 'http://static.mytuya.com/res/img/myspace/toother2.jpg');
	},
	viewAllComments:function() {
		if($.commentsInSpace.type == 1)
			window.location = '/space/rc_' + $.myplace.id + '_1/';
		else
			window.location = '/space/pc_' + $.myplace.id + '_1/';
	}
	};
})(jQuery);
function loadModules() {
	loadSpaceVisitors();
	getReceivedComments();
	getFriendsNewImages();
}
function loadSpaceVisitors() {
	$.post('/getSpaceVisitors.htm', {spaceid:$.myplace.id}, function(data){$('.most_attention').html(data);});
}
function initComponents() {
	if(document.getElementById('bulletin')!=null) {
		$('#bulletin').editInPlace({
		default_text:'(单击添加你的个人公告)',
		bg_over:'#e5d3e2',
		update_value:'bulletin',
		url:'setMyPlaceBulletin.htm',
		field_type: "textarea",
    	textarea_rows: "5",
    	textarea_cols: "16",
    	success:updateBulletinSuccess,
    	save_button:'<br/><input type="button" class="inplace_save input_login" value="保存"/>',
    	cancel_button: '<input type="button" class="inplace_cancel input_login" value="取消"/>'
		});
	}
	
	if(document.getElementById('placeName')!=null) {
		$('#placeName').editInPlace({
		default_text:'(单击设置你的地盘名称)',
		bg_over:'#e5d3e2',
		update_value:'placeName',
		url:'setMyPlaceName.htm',
		field_type: "text",
    	textarea_rows: "5",
    	textarea_cols: "16",
    	save_button:'<input type="button" class="inplace_save input_login" value="保存"/>',
    	cancel_button: '<input type="button" class="inplace_cancel input_login" value="取消"/>'
		});
	}
}

function initEvent() {
	$('#postLink').click(showPostMessageArea);
}

function updateBulletinSuccess(html, oHtml) {
	var today = new Date();
	var year = today.getFullYear();
	var month = today.getMonth() + 1;
	var day = today.getDate();
	$('#bulletinDate').html('[' + year + '-' + month + '-' + day +']');
}

function showPostMessageArea() {
	
	if($.visitor.uid == null) {
		$('#messageInput').html('您还没登陆，不能留言');
		$('#messageInput').show();	
		window.setTimeout(function(){$('#messageInput').fadeOut("slow", function(){$(this).html('');});}, 2000);
		return;
	};
	var areaHtml = '<textarea id="guestMessage" rows="8"></textarea><br /><div class="right"><input id="btn_postmsg" type="button" class="input_login" value="确定" /></div>';
	$('#messageInput').show();
	$('#messageInput').html(areaHtml);
	
	$('#btn_postmsg').click(postGuestMessage);
	$('#guestMessage').focus();
	return false;
}
function postGuestMessage() {
$.post('/postPlaceMessage.htm', {spaceid:$.myplace.id, message:$('#guestMessage').val()}, function(data){postGuestMessageCallBack(data);});
$('#messageInput').html('正在提交留言...');
}
function postGuestMessageCallBack(data) {
	var result = eval('(' + data + ')');
	if(result.status == 'success') {
		$('#messageInput').hide();
		$(generateMessageLi(result.avatar, result.message)).prependTo('#ulMessages').hide().fadeIn('slow');
	} else {
		$('#messageInput').html('错误：发表留言失败');
		window.setTimeout(function(){$('#messageInput').fadeOut("slow");}, 2000);
	}
}

function generateMessageLi(avatar, message) {
	return '<li><div class="myspace_index_message_portrait"><img src="' + avatar + '" /></div><div class="myspace_index_message_content">' + message + '</div><div class="clear"></div></li>';
}
function getMessages() {
	$.post('/viewMyPlaceIndexMessages.htm', {spaceid:$.myplace.id}, function(data){getMessagesCallBack(data);});
}
function getMessagesCallBack(data) {
	$('.myspace_index_message').html(data);	
}
function getReceivedComments() {
	$.post('getReceivedComments.htm', {uid:$.myplace.id}, function(data){getReceivedCommentsCallback(data);});
}
function getReceivedCommentsCallback(data) {
	var result = eval('(' + data + ')');
	if(result.status == 'success') {
		for(var i=0; i < result.comments.length;i++) {
			$('#commentContainer').append(createRComment(result.comments[i]));
		}
		$.commentsInSpace.rcHtml = $('#commentContainer').html();
		$('#rcImg').wrap('<a href="javascript:void 0" onclick="$.commentsInSpace.showRCHtml();"></a>');
		$('#pcImg').wrap('<a href="javascript:void 0" onclick="$.commentsInSpace.showPCHtml();"></a>');
	} else {
		$('#commentContainer').html('加载数据失败');
	}
}
function getPostComments() {
	$.post('getPostComments.htm', {uid:$.myplace.id}, function(data){getPostCommentsCallback(data);});
}
function getPostCommentsCallback(data) {
	var result = eval('(' + data + ')');
	if(result.status == 'success') {
	for(var i=0; i < result.comments.length;i++) {
		$('#commentContainer').append(createPComment(result.comments[i]));
		}
		$.commentsInSpace.pcHtml = $('#commentContainer').html();
	} else {
		$('#commentContainer').html('加载数据失败');
	}
}
function createRComment(comment) {
	var item = '<li><span class="fontblue">[{replies}回复] </span><a href="/tuya/{imageid}/">{content}</a><span class="mytuya_newtopics_time">[ {postdate} by {poster} ]</span></li>';
	return item.replace(/{replies}/, comment.replies).replace(/{imageid}/, comment.imageid).replace(/{content}/, comment.content).replace(/{postdate}/, comment.postdate).replace(/{poster}/, comment.poster);
	
}
function createPComment(comment) {
	var item = '<li><span class="fontblue">[{replies}回复] </span><a href="/tuya/{imageid}/">{content}</a><span class="mytuya_newtopics_time">[ {postdate} ]</span></li>';
	return item.replace(/{replies}/, comment.replies).replace(/{imageid}/, comment.imageid).replace(/{content}/, comment.content).replace(/{postdate}/, comment.postdate);
}

function getFriendsNewImages() {
	$.post('/getFriendNewImages.htm', {uid:$.myplace.id}, function(data){getFriendsNewImagesCallback(data);});
}

function getFriendsNewImagesCallback(data) {
	var result = eval('(' + data + ')');
	if(result.status == 'success') {
		for(var i=0; i < result.images.length;i++) {
			$('#friendImages').append(createImageItem(result.images[i]));
		}
	} else {
		$('#friendImages').html('加载数据失败');
	}
}

function createImageItem(image) {
	var item = '<li><a href="/tuya/{imageid}/"><img src="{imageurl}" /></a></li> \
			<li class="myspace_index_friends_name1"> \
  				<p>by<a href="/space_{authorid}/" class="author"> {authorname}</a></p> \
			</li>';
	return item.replace(/{imageid}/, image.imageid).replace(/{imageurl}/, image.imageurl).replace(/{authorid}/, image.authorid).replace(/{authorname}/, image.authorname);
}
