/*
	micropost.js - needs JSON.js, ui_common.js, ui_const.js, newsfeed.js(at newsfeed), notice.js(at notice)

	micropost_box_id : id of micropost wrapper division in newsfeed or blog page. newsfeed is same with that of facebook. blog maybe similar with Wall in facebook.

	java_url : base url page implemented with java.

	blog_owner : at first, used for list_type blog. at now, it also used for identifying whether user logged in or not in onnl page.
*/

function micropost(micropost_box_id, list_type, blog_owner, java_url) {
	this.micropost_box = $(micropost_box_id);
	
	/* list_type : -1(dummy - using for setting write block only. there is no micropost_box), 0(newsfeed), 1(blog), 2(mention), 3(dialog), 4(scrap), 5(notice), 6(onnl), 7(direct) in ui_const.js */
	this.list_type = list_type;

	this.blog_owner = blog_owner;
	this.java_url = java_url;
	this.current_page = 0;
	this.is_last_page = false;
	this.while_loading = false;
	this.dialog_root = 0;
};


/*
	code : 
		null(type = "today")
		cocode(type = "talk")
		ifund_id(type = "iFUNd")
		parent_id(type = "append")
	
	mention_to :
		default null parameter
		if user click 'write mention' button, then
		micropost_id which relate with that mention will be value of mention_to 
	
	receiver_id :
		default null parameter
		if user click 'write mention' button, then 
		array of user_id who receive that mention will be value of receiver_id.
*/
micropost.prototype.write = function(type, code, content, input_textarea, mention_to, receiver_ids)
{
	var url = "/micropost/write/";
	var params = $H({
		'type': type,
		'code': code,
		'content': content,
		'mention_to' : mention_to,
		'receiver_ids' : JSON.encode( receiver_ids )
	});
	var micropost_instance = this;

	var ajax = new Ajax.Request( url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			var response = eval("(" + xmlHttp.responseText + ")");
			if(response.resultCode != 'SUCCESS'){
				alert(response.message);
				return;
			} else {
				input_textarea.value = '';
			}
			micropost_instance.after_write();
			
		},
		onFailure: function(request)
		{
			alert("FAIL");
			return;
		}
	});
};

micropost.prototype.after_write = function()
{
	this.init_box();
	this.current_page = 0;

	this.get_microposts();
};

micropost.prototype.remove = function(micropost_id)
{
	var url = "/micropost/delete/";
	var params = $H({
		'micropost_id': micropost_id
	});
	var micropost_instance = this;

	var ajax = new Ajax.Request( url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			var response = eval("(" + xmlHttp.responseText + ")");
			if(response.resultCode != 'SUCCESS'){
				alert(response.message);
				return;
			}
			open_layer_popup('삭제되었습니다.', LAYER_POPUP_TYPE_OK);
			micropost_instance.after_remove(micropost_id);
		},
		onFailure: function(request)
		{
			alert("FAIL");
			return;
		}
	});
};

micropost.prototype.after_remove = function(micropost_id)
{
	var post_box = $$('.micropost_id_' + micropost_id)[0];
	if(this.list_type == LIST_TYPE_BLOG){
		this.get_blog_posts(this.blog_owner);
	} else {
		this.micropost_box.removeChild(post_box);
	}
};


micropost.prototype.scrap = function(micropost_id)
{
	var url = '/micropost/scrap_post/';
	var params = $H({
		'micropost_id': micropost_id
	});
	var micropost_instance = this;

	var ajax = new Ajax.Request( url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			var response = eval("(" + xmlHttp.responseText + ")");
			if(response.resultCode != 'SUCCESS'){
				alert(response.message);
				return;
			}
			open_layer_popup("해당 글을 스크랩했습니다.", LAYER_POPUP_TYPE_OK);
			micropost_instance.after_scrap(micropost_id);
		},
		onFailure: function(request)
		{
			alert("FAIL");
			return;
		}
	});

};

micropost.prototype.after_scrap = function(micropost_id)
{
	var micropost_instance = this;
	var scrap_btn = $$('.micropost_id_' + micropost_id + ' .scrap_btn')[0];
	var scrap_img = $$('.micropost_id_' + micropost_id + ' .scrap_btn img')[0];	
	scrap_img.src = '/assets/images/static/icon01_on.png';
	scrap_btn.onmouseover = function(){
		scrap_img.src = '/assets/images/static/icon01_revert_on.png';
	};
	scrap_btn.onmouseout = function(){
		scrap_img.src = '/assets/images/static/icon01_on.png';
	};
	scrap_btn.onclick = function(){
		open_layer_popup('스크랩을 해제하시겠습니까?', 
		LAYER_POPUP_TYPE_YES_NO,
		function() {
			micropost_instance.unscrap(micropost_id)
		});
	};
	scrap_btn.title = '스크랩해제하기';
};


micropost.prototype.unscrap = function(micropost_id)
{
	var url = '/micropost/unscrap_post/';
	var params = $H({
		'micropost_id': micropost_id
	});
	var micropost_instance = this;

	var ajax = new Ajax.Request( url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			var response = eval("(" + xmlHttp.responseText + ")");
			if(response.resultCode != 'SUCCESS'){
				alert(response.message);
				return;
			}
			open_layer_popup("스크랩이 해제되었습니다.", LAYER_POPUP_TYPE_OK);
			micropost_instance.after_unscrap(micropost_id);
		},
		onFailure: function(request)
		{
			alert("FAIL");
			return;
		}
	});
};

micropost.prototype.after_unscrap = function(micropost_id) 
{
	var micropost_instance = this;
	var scrap_btn = $$('.micropost_id_' + micropost_id + ' .scrap_btn')[0];
	var scrap_img = $$('.micropost_id_' + micropost_id + ' .scrap_btn img')[0];
	scrap_img.src = '/assets/images/static/icon01.png';
	scrap_btn.onmouseover = function(){
		scrap_img.src = '/assets/images/static/icon01_on.png';
	};
	scrap_btn.onmouseout = function(){
		scrap_img.src = '/assets/images/static/icon01.png';
	};
	scrap_btn.onclick = function(){
		open_layer_popup('이 글을 스크랩하시겠습니까?', 
		LAYER_POPUP_TYPE_YES_NO,
		function() {
			micropost_instance.scrap(micropost_id);
		});
	};
	scrap_btn.title = '스크랩하기';
};

micropost.prototype.spread = function(micropost_id)
{
	var url = '/micropost/spread_post/';
	var params = $H({
		'micropost_id': micropost_id
	});
	var micropost_instance = this;

	var ajax = new Ajax.Request( url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			var response = eval("(" + xmlHttp.responseText + ")");
			if(response.resultCode != 'SUCCESS'){
				alert(response.message);
				return;
			}
			open_layer_popup("해당 글을 소문냈습니다.", LAYER_POPUP_TYPE_OK);
			micropost_instance.after_spread(micropost_id);
		},
		onFailure: function(request)
		{
			alert("FAIL");
			return;
		}
	});
};

micropost.prototype.after_spread = function(micropost_id)
{
	var micropost_instance = this;
	var spread_btn = $$('.micropost_id_' + micropost_id + ' .spread_btn')[0];
	var spread_img = $$('.micropost_id_' + micropost_id + ' .spread_btn img')[0];
	spread_img.src = '/assets/images/static/icon04_on.png';
	spread_btn.onmouseover = function(){
		spread_img.src = '/assets/images/static/icon04_revert_on.png';
	};
	spread_btn.onmouseout = function(){
		spread_img.src = '/assets/images/static/icon04_on.png';
	};
	spread_btn.title = '소문내기 해제';
	spread_btn.onclick = function(){
		open_layer_popup('소문내기를 해제하시겠습니까?', 
		LAYER_POPUP_TYPE_YES_NO,
		function(){
			micropost_instance.unspread(micropost_id);
		});
	};
};

micropost.prototype.unspread = function(micropost_id)
{
	var url = '/micropost/unspread_post/';
	var params = $H({
		'micropost_id': micropost_id
	});
	var micropost_instance = this;
	var ajax = new Ajax.Request( url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			var response = eval("(" + xmlHttp.responseText + ")");
			if(response.resultCode != 'SUCCESS'){
				alert(response.message);
				return;
			}
			open_layer_popup("해당 글의 소문내기를 해제했습니다.", LAYER_POPUP_TYPE_OK);
			micropost_instance.after_unspread(micropost_id);
		},
		onFailure: function(request)
		{
			alert("FAIL");
			return;
		}
	});
};

micropost.prototype.after_unspread = function(micropost_id)
{
	var micropost_instance = this;
	var spread_btn = $$('.micropost_id_' + micropost_id + ' .spread_btn')[0];
	var spread_img = $$('.micropost_id_' + micropost_id + ' .spread_btn img')[0];
	spread_img.src = '/assets/images/static/icon04.png';
	spread_btn.onmouseover = function(){
		spread_img.src = '/assets/images/static/icon04_on.png';
	};
	spread_btn.onmouseout = function(){
		spread_img.src = '/assets/images/static/icon04.png';
	};
	spread_btn.title = '소문내기';
	spread_btn.onclick = function(){
		open_layer_popup('이 글을 follwer들에게 소문내시겠습니까?',
		LAYER_POPUP_TYPE_YES_NO,
		function(){
			micropost_instance.spread(micropost_id);
		});
	};
};

micropost.prototype.init_box = function()
{
	this.micropost_box.update();
	this.micropost_box.style.textAlign = 'left';
};

micropost.prototype.show_loading = function()
{
	var dummy_div = new Element('div');
	dummy_div.className = 'loading_div';
	var img = new Element('img');
	img.src = "/assets/images/common/progress.gif";
	img.style.textAlign = "center";
	img.style.height = "16px";
	img.style.width = "16px";
	img.style.padding = "10px 0";

	dummy_div.style.textAlign = "center";
	dummy_div.appendChild(img);
	this.micropost_box.appendChild(dummy_div);
};

micropost.prototype.hide_loading = function()
{
	var loading_divs = this.micropost_box.select('div.loading_div');
	for (var i=0; i<loading_divs.length; i++){
		var loading_div = loading_divs[i];
		this.micropost_box.removeChild(loading_div);
	}
};

micropost.prototype.show_no_data = function()
{
	var dummy_div = new Element('div');

	dummy_div.style.textAlign = "center";
	dummy_div.style.height = "50px";
	dummy_div.style.lineHeight = "49px";
	dummy_div.update('등록된 글이 없습니다.');
	this.micropost_box.appendChild(dummy_div);
};

micropost.prototype.get_newsfeed = function(page)
{
	page = parseInt(page) > 0 ? page : 0;

	var url = "/micropost/get_newsfeed/";
	var params = $H({'page': page});
	var micropost_instance = this;
	this.show_loading();

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval( '(' + xmlHttp.responseText + ')' );
				micropost_instance.wrap_microposts(data);
			} catch (e) {
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
};

micropost.prototype.get_blog_posts = function(page)
{
	page = parseInt(page) > 0 ? page : 0;

	var url = "/micropost/get_blog_posts/";
	var params = $H({'page' : page, 'blog_owner_id' : this.blog_owner});
	var micropost_instance = this;
	this.show_loading();

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval('(' + xmlHttp.responseText + ')' );
				micropost_instance.wrap_microposts(data);
			} catch (e) {
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
};


micropost.prototype.get_mentions = function(page)
{
	page = parseInt(page) > 0 ? page : 0;

	var url = "/micropost/get_mentions/";
	var params = $H({'page' : page});
	var micropost_instance = this;
	this.show_loading();

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval( '(' + xmlHttp.responseText + ')' );
				micropost_instance.wrap_microposts(data);
			} catch (e) {
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
};

micropost.prototype.get_scraps = function(page)
{
	page = parseInt(page) > 0 ? page : 0;

	var url = "/micropost/get_scraps/";
	var params = $H({
		'page':page, 'blog_owner_id' : this.blog_owner
	});
	var micropost_instance = this;
	this.show_loading();

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval( '(' + xmlHttp.responseText + ')' );
				micropost_instance.wrap_microposts(data);
			} catch (e) {
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
}

micropost.prototype.get_dialog = function(micropost_id)
{
	this.init_box();

	var url = "/micropost/get_dialog/";
	var params = $H({'micropost_id' : micropost_id});
	var micropost_instance = this;
	this.show_loading();
	this.dialog_root = micropost_id;

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval('(' + xmlHttp.responseText + ')');
				micropost_instance.wrap_microposts(data);
			} catch (e) {
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
};

micropost.prototype.get_notices = function(page)
{
	page = parseInt(page) > 0 ? page : 0;

	var url = "/micropost/get_notices";
	var params = $H({'page': page});
	
	var micropost_instance = this;
	this.show_loading();

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval( '(' + xmlHttp.responseText + ')' );
				micropost_instance.wrap_microposts(data);
			} catch (e) {
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
};

micropost.prototype.get_onnl = function(page)
{
	page = parseInt(page) > 0 ? page : 0;

	var url = "/micropost/get_onnl";
	var params = $H({'page':page});

	var micropost_instance = this;
	this.show_loading();

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval( '(' + xmlHttp.responseText + ')' );
				micropost_instance.wrap_microposts(data);
			} catch (e) {
				alert("plz retry .. " + e);
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
};

micropost.prototype.get_direct = function(micropost_id)
{
	this.init_box();

	var url = "/micropost/get_direct/";
	var params = $H({'micropost_id' : micropost_id});
	var micropost_instance = this;
	this.show_loading();
	this.dialog_root = micropost_id;

	var ajax = new Ajax.Request(url,
	{
		asynchronous: true,
		method: 'post',
		parameters: params,
		onSuccess: function(xmlHttp)
		{
			try {
				var data = eval('(' + xmlHttp.responseText + ')');
				micropost_instance.wrap_microposts(data);
			} catch (e) {
			}
		},
		onFailure: function(request)
		{
			alert('FAIL');
			return;
		}
	});
};

micropost.prototype.wrap_microposts = function(data)
{
	this.hide_loading();	
	if(data.resultCode == 'NODATA')
	{
		this.show_no_data();
		this.is_last_page = true;
		return;
	}

	var list = data.result.list;

	var loop = list.length;
	var micropost_instance = this;
	
	$R(0, loop, true).each(function(i){
		var background_color = (i % 2) ? '#f4f4f4' : '#fafafa';
		switch(list[i]['type']){
			case SOURCE_TYPE_MESSAGE:
				var message_box = micropost_instance.get_message_box(list[i], background_color);
				micropost_instance.micropost_box.appendChild(message_box);
			break;
			case SOURCE_TYPE_NOTICE:
				var notice_box = micropost_instance.get_notice_box(list[i], background_color);
				micropost_instance.micropost_box.appendChild(notice_box);
			break;
			default:
				var post_box = micropost_instance.get_post_box(list[i], background_color);
				micropost_instance.micropost_box.appendChild(post_box);
			break;
		}
	});
	this.while_loading = false;

};


micropost.prototype.get_message_box = function(micropost_info, background_color)
{
	var micropost_id = micropost_info['id'];
	
	var message_box = new Element('div');
	message_box.className = 'message_box';
	message_box.className += ' micropost_id_' + micropost_id;

	var content = new Element('div');
	content.className = 'content';

	var title = this.get_message_title(micropost_info);
	content.appendChild(title);

	var timestamp = new Element('span');
	timestamp.className = 'timestamp';
	timestamp.innerHTML = '&nbsp;&nbsp' + micropost_info['AT'];
	title.appendChild(timestamp);

	var detail = new Element('div');
	detail.className = 'detail';
	var detail_text = this.wrapping_content(micropost_info['content']);
	detail.innerHTML = detail_text;
	detail.hide();
	
	var show_detail_btn = new Element('a');
	show_detail_btn.className = 'show_detail_btn';
	show_detail_btn.innerHTML = 'more&nbsp;';
	var show_detail_btn_img = new Element('img');
	show_detail_btn_img.src = "/assets/images/static/icon21.gif";
	show_detail_btn.appendChild(show_detail_btn_img);
	show_detail_btn.onclick = function(){
		detail.show();
		show_detail_btn.hide();
		hide_detail_btn.show();
	}

	var hide_detail_btn = new Element('a');
	hide_detail_btn.className = 'hide_detail_btn';
	hide_detail_btn.innerHTML = 'close&nbsp;';
	var hide_detail_btn_img = new Element('img');
	hide_detail_btn_img.src = "/assets/images/static/icon22.gif";
	hide_detail_btn.appendChild(hide_detail_btn_img);
	hide_detail_btn.onclick = function(){
		detail.hide();
		show_detail_btn.show();
		hide_detail_btn.hide();
	}
	hide_detail_btn.hide();
	content.appendChild(show_detail_btn);
	content.appendChild(hide_detail_btn);

	var hide_btn = this.get_hide_btn(micropost_info);
	title.appendChild(hide_btn);

	message_box.appendChild(content);
	message_box.appendChild(detail);
	return message_box;
};

micropost.prototype.get_notice_box = function(micropost_info, background_color)
{
	var micropost_id = micropost_info['id'];

	var notice_box = new Element('div');
	notice_box.className = 'notice_box';
	notice_box.className += ' micropost_id_' + micropost_id;

	var content = new Element('div');
	content.className = 'content';

	var title = new Element('span');
	title.className = 'title';
	
	var s_begin = new Element('span');
	s_begin.className = 'ib30';
	var s_end = new Element('span');
	s_end.className = 'ib30';
	var title_img = new Element('img');
	title_img.className = 'v_align_middle';
	title_img.src = "/assets/images/static/icon_notice.gif";
	title.appendChild(s_begin);
	title.appendChild(title_img);
	title.appendChild(s_end);

	var content_text = this.wrapping_content(micropost_info['content']);
	title.innerHTML += content_text;
	content.appendChild(title);
	
	var timestamp = new Element('span');
	timestamp.className = 'timestamp';
	timestamp.innerHTML = '&nbsp;&nbsp;' + micropost_info['AT'];
	title.appendChild(timestamp);

	var detail = new Element('div');
	detail.className = 'detail';
	var detail_text = micropost_info['detail'];
	detail.innerHTML = detail_text;
	detail.hide();
	
	var show_detail_btn = new Element('a');
	show_detail_btn.className = 'show_detail_btn';
	show_detail_btn.innerHTML = 'more&nbsp;';
	var show_detail_btn_img = new Element('img');
	show_detail_btn_img.src = "/assets/images/static/icon21.gif";
	show_detail_btn.appendChild(show_detail_btn_img);
	show_detail_btn.onclick = function(){
		detail.show();
		show_detail_btn.hide();
		hide_detail_btn.show();
	}

	var hide_detail_btn = new Element('a');
	hide_detail_btn.className = 'hide_detail_btn';
	hide_detail_btn.innerHTML = 'close&nbsp;';
	var hide_detail_btn_img = new Element('img');
	hide_detail_btn_img.src = "/assets/images/static/icon22.gif";
	hide_detail_btn.appendChild(hide_detail_btn_img);
	hide_detail_btn.onclick = function(){
		detail.hide();
		show_detail_btn.show();
		hide_detail_btn.hide();
	}
	hide_detail_btn.hide();
	content.appendChild(show_detail_btn);
	content.appendChild(hide_detail_btn);

	
	if(this.list_type != LIST_TYPE_NOTICE){
		var hide_btn = this.get_hide_btn(micropost_info);
		title.appendChild(hide_btn);
	} else {
		var notice_delete_btn = this.get_notice_delete_btn(micropost_info);
		title.appendChild(notice_delete_btn);
	}
	
	notice_box.appendChild(content);
	notice_box.appendChild(detail);
	return notice_box;
};

// each micropost box.
micropost.prototype.get_post_box = function(micropost_info, background_color)
{
	var micropost_id = micropost_info['id'];

	var post_box = new Element('div');
	post_box.className = 'post_box';
	post_box.className += ' micropost_id_' + micropost_id;
	post_box.style.backgroundColor = background_color;
	
	if(micropost_info['is_related_mention']){
		post_box.className += ' mention';
		var re_img = new Element('img');
		re_img.className = 're_image png';
		re_img.src = '/assets/images/static/re.png';
		post_box.appendChild(re_img);
		post_box.style.backgroundColor = '#ffffff';
	}


	var profile_image_wrap = new Element('a');
	profile_image_wrap.href = '/blog/microposts/' + micropost_info['writer_id'];

	var profile_image = new Element('img');
	profile_image.className = 'profile_image';
	profile_image.src = micropost_info['profile_image'];

	profile_image_wrap.appendChild(profile_image);

	var nickname = new Element('div');
	var nickname_link = new Element('a');

	nickname.className = 'nickname';
	nickname_link.href = '/blog/microposts/' + micropost_info['writer_id'];
	nickname_link.innerHTML = micropost_info['writer_nick'];
	nickname.appendChild(nickname_link);

	var content = new Element('div');
	content.className = 'content';
	content.innerHTML = this.wrapping_content(micropost_info['content']);

	var related = this.get_related(micropost_info);

	var timestamp = new Element('span');
	timestamp.className = 'timestamp';
	timestamp.innerHTML = micropost_info['AT'];
	
	post_box.appendChild(profile_image_wrap);
	post_box.appendChild(nickname);
	post_box.appendChild(content);
	post_box.appendChild(related);
	post_box.appendChild(timestamp);

	/* different with the other list type, [onnl] and [direct] are accessible in case user is not logged in. so check whether user is logged in or not by blog_owner. in other words, blog_owner means viewer id in list type [onnl] or [direct]. if viewer is not logged in, it is ''. */
	if(!((this.list_type == LIST_TYPE_ONNL || this.list_type == LIST_TYPE_DIRECT)&& this.blog_owner == '')){
		var util_box = this.get_util_box(micropost_info);
		util_box.hide();
		post_box.appendChild(util_box);
	}


	post_box.onmouseover = function(){
		if(util_box) util_box.show();
		post_box.style.backgroundColor = '#e5f2f7';
	}
	post_box.onmouseout = function(){
		if(util_box) util_box.hide();
		post_box.style.backgroundColor = background_color;
		if(micropost_info['is_related_mention']){
			post_box.style.backgroundColor = '#ffffff';
		}
	}
	return post_box;
};

micropost.prototype.wrapping_content = function(content)
{
	// replace url to link
	var result = content;

	result = result.replace(/</g, '&lt;').replace(/>/g, '&gt;');
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	result = result.replace(exp, "<a href='$1' target='_blank'>$1</a>");

	if(Prototype.Browser.IE){
		result = result.replace(/\n/g, '<br />');
	}
	return result;
};

micropost.prototype.get_message_title = function(micropost_info)
{
	var title = new Element('span');
	title.className = 'title';
	var title_img = new Element('img');
	title_img.className = 'v_align_middle';
	var s_begin = new Element('span');
	s_begin.className = 'ib30';
	var s_end = new Element('span');
	s_end.className = 'ib30';

	var content = micropost_info['content'];
	var title_text = null;
	switch(micropost_info['message_div']){
		case MSG_DIV_iFUNd:
			title_img.src = '/assets/images/static/icon_ifund.gif';
			if(content.match('마감주문내역')){
				title_text = "마감주문내역을 알려드립니다.";
			} else {
				title_text = "실시간 주문이 입력되었습니다.";
			}
		break;
		case MSG_DIV_FOLLOWING:
			title_img.src = '/assets/images/static/icon_following.png';
			if(content.match('마감주문내역')){
				title_text = "마감주문내역을 알려드립니다.";
			} else {
				title_text = "실시간 주문이 입력되었습니다.";
			}		break;
		case MSG_DIV_PORTFOLIO:
			title_img.src = '/assets/images/static/icon_portfolio.png';
			title_text = '포트폴리오 종목이 목표가에 도달했습니다.';
		break;
	}
	title.appendChild(s_begin);
	title.appendChild(title_img);
	title.appendChild(s_end);
	title.innerHTML += title_text;

	return title;
};

// micropost status label - shows type of post('today', 'talk', 'ifund', 'append', 'spread')
micropost.prototype.get_related = function(micropost_info)
{
	var micropost_instance = this;
	var related = new Element('div');
	related.className = 'related';
	if(micropost_info['is_spread'] == 1) {
		var s_begin = new Element('span');
		var s_end = new Element('span');
		var icon = new Element('img');
		var related_spreader = new Element('a');

		s_begin.className = 'ib18';
		s_end.className = 'ib18';
		icon.src = "/assets/images/static/icon07.png";


		related_spreader.appendChild(s_begin);
		related_spreader.appendChild(icon);
		related_spreader.appendChild(s_end);
		related_spreader.href = '#';
		related_spreader.className = 'spread';
		related_spreader.target = '_blank';
		related_spreader.innerHTML += '소문내기 ' + '[' + micropost_info['spreader_nick'] + ']';
		related.appendChild(related_spreader);
	} else {
		switch(micropost_info['type']){
			case SOURCE_TYPE_TODAY:
				var link_dialog = this.get_link_dialog(micropost_info);
				if(link_dialog) related.appendChild(link_dialog);
			break;
			case SOURCE_TYPE_TALK:
				// you should wrap img tag with span for image vertial align.(because of IE6 ..)
				var s_begin = new Element('span');
				var s_end = new Element('span');
				var icon = new Element('img');
				var related_company = new Element('a');

				s_begin.className = 'ib18';
				s_end.className = 'ib18';
				icon.src = "/assets/images/static/icon05.png";
				
				related_company.appendChild(s_begin);
				related_company.appendChild(icon);
				related_company.appendChild(s_end);
				related_company.href = this.java_url + "/company.jsp?cocode=" + micropost_info['code'];
				related_company.className = 'talk';
				related_company.target = '_blank';
				related_company.innerHTML += micropost_info['company_name'] + ' (' + micropost_info['code'] + ')';

				related.appendChild(related_company);
			break;
			case SOURCE_TYPE_iFUNd:
				var s_begin = new Element('span');
				var s_end = new Element('span');
				var icon = new Element('img');
				var related_ifund = new Element('a');

				s_begin.className = 'ib18';
				s_end.className = 'ib18';
				icon.src = "/assets/images/static/icon11.png";

				related_ifund.appendChild(s_begin);
				related_ifund.appendChild(icon);
				related_ifund.appendChild(s_end);
				related_ifund.href = this.java_url + "/ifund/ifundDetail.jsp?menuCode=15&iFUNd_div=O&iFUNd_id=" + micropost_info['code'];
				related_ifund.className = 'ifund';
				related_ifund.target = '_blank';
				related_ifund.innerHTML += micropost_info['ifund_name'];
				related.appendChild(related_ifund);
			break;
			case SOURCE_TYPE_APPEND:
				var s_begin = new Element('span');
				var s_end = new Element('span');
				var icon = new Element('img');
				var related_article = new Element('a');

				s_begin.className = 'ib18';
				s_end.className = 'ib18';
				icon.src = "/assets/images/static/icon06.png";

				var title = micropost_info['article_title'];
				if(title.length > 12){
					title = title.substr(0, 10) + "..";
				}

				related_article.appendChild(s_begin);
				related_article.appendChild(icon);
				related_article.appendChild(s_end);
				related_article.href = "/bbs/link/" + micropost_info['article_id'];
				related_article.className = 'append';
				related_article.target = '_blank';
				related_article.innerHTML += title + ' [' + micropost_info['article_writer_nick'] + ']';

				related.appendChild(related_article);

			break;
			default:
			break;
		}
	}
	return related;
};

micropost.prototype.get_link_dialog = function(micropost_info)
{
	var link_dialog = null;
	switch(this.list_type){
		case LIST_TYPE_NEWSFEED:
		case LIST_TYPE_MENTION:
		case LIST_TYPE_SCRAP:
			if(micropost_info['num_dialog'] > 0){
				link_dialog = new Element('a');
				link_dialog.href = "/blog/dialog/" + micropost_info['id'];
				link_dialog.className = "link_dialog";
				var l = new Element('img');
				l.src = '/assets/images/static/comm01.png';
				var r = new Element('img');
				r.src = '/assets/images/static/comm03.png';
				var message = new Element('span');
				message.innerHTML = "댓글 " + micropost_info['num_dialog'];
				link_dialog.appendChild(l);
				link_dialog.appendChild(message);
				link_dialog.appendChild(r);
			}
			if(micropost_info['code'] != 0){
				link_dialog = new Element('a');
				link_dialog.href = "/blog/dialog/" + micropost_info['code'];
				link_dialog.className = "link_dialog";
				var l = new Element('img');
				l.src = '/assets/images/static/comm01.png';
				var r = new Element('img');
				r.src = '/assets/images/static/comm03.png';
				var message = new Element('span');
				message.innerHTML = "관련글";
				link_dialog.appendChild(l);
				link_dialog.appendChild(message);
				link_dialog.appendChild(r);
			}
		break;
		case LIST_TYPE_BLOG:
			if(!micropost_info['is_related_mention'] && micropost_info['code'] != 0){
				link_dialog = new Element('a');
				link_dialog.href = "/blog/dialog/" + micropost_info['code'];
				link_dialog.className = "link_dialog";
				var l = new Element('img');
				l.src = '/assets/images/static/comm01.png';
				var r = new Element('img');
				r.src = '/assets/images/static/comm03.png';
				var message = new Element('span');
				message.innerHTML = "관련글";
				link_dialog.appendChild(l);
				link_dialog.appendChild(message);
				link_dialog.appendChild(r);
			}
		break;
		default:
		break;
	}
	return link_dialog;
};


// micropost btn group. 'favorite', 'mention', 'spread' or 'delete'
micropost.prototype.get_util_box = function(micropost_info)
{
	var micropost_instance = this;
	var util_box = new Element('div');
	util_box.className = 'util_box';

	var tweet_btn = new Element('a');
	var tweet_img = new Element('img');

	tweet_img.src = '/assets/images/static/icon17.png';
	tweet_btn.onmouseover = function(){
		tweet_img.src = '/assets/images/static/icon17_on.png';
	};
	tweet_btn.onmouseout = function (){
		tweet_img.src = '/assets/images/static/icon17.png';
	};
	tweet_btn.title = '트위터로 내보내기';
	tweet_btn.className = 'util_btn tweet_btn popup_btn';

	new tweet_util(micropost_info['id'], micropost_info['content'], tweet_btn);
	
	tweet_btn.appendChild(tweet_img);
	util_box.appendChild(tweet_btn);

	var scrap_btn = new Element('a');
	var scrap_img = new Element('img');
	if(micropost_info['is_scraped_by_me'] == '1'){
		scrap_img.src = '/assets/images/static/icon01_on.png';
		scrap_btn.onmouseover = function(){
			scrap_img.src = '/assets/images/static/icon01_revert_on.png';
		};
		scrap_btn.onmouseout = function(){
			scrap_img.src = '/assets/images/static/icon01_on.png';
		};
		scrap_btn.onclick = function(){
			open_layer_popup('스크랩을 해제하시겠습니까?', 
			LAYER_POPUP_TYPE_YES_NO,
			function() {
				micropost_instance.unscrap(micropost_info['id'])
			});
		};
		scrap_btn.title = '스크랩해제하기';
	} else {
		scrap_img.src = '/assets/images/static/icon01.png';
		scrap_btn.onmouseover = function(){
			scrap_img.src = '/assets/images/static/icon01_on.png';
		};
		scrap_btn.onmouseout = function(){
			scrap_img.src = '/assets/images/static/icon01.png';
		};
		scrap_btn.onclick = function(){
			open_layer_popup('이 글을 스크랩하시겠습니까?', 
			LAYER_POPUP_TYPE_YES_NO,
			function() {
				micropost_instance.scrap(micropost_info['id']);
			});
		};
		scrap_btn.title = '스크랩하기';
	}

	scrap_btn.className = 'util_btn scrap_btn popup_btn';

	scrap_btn.appendChild(scrap_img);
	util_box.appendChild(scrap_btn);

	var mention_btn = new Element('a');
	var mention_img = new Element('img');
	mention_img.src = '/assets/images/static/icon02.png';
	mention_btn.onmouseover = function(){
		mention_img.src = '/assets/images/static/icon02_on.png';
	};
	mention_btn.onmouseout = function(){
		mention_img.src = '/assets/images/static/icon02.png';
	};
		
	mention_btn.className = 'util_btn mention_btn popup_btn';
	mention_btn.title = '댓글달기';
	mention_btn.onclick = function(){
		open_mention_popup(micropost_info['writer_nick'],
		function(textarea){
			var content = textarea.value;
			if(content){
			} else {
				open_layer_popup('내용을 입력하세요.', LAYER_POPUP_TYPE_OK);
				return;
			}
			if(content.length > MICROPOST_MAX_LENGTH){
				//open_layer_popup('최대 ' + MICROPOST_MAX_LENGTH + '자 만큼 쓸 수 있습니다.', LAYER_POPUP_TYPE_OK);
				return;
			}

			if(micropost_info['type'] == 'append'){
				var code = micropost_info['article_id'];
			} else {
				var code = micropost_info['code'];
			}
			micropost_instance.write(micropost_info['type'], code, content, textarea, micropost_info['id'], [micropost_info['writer_id']]);

			close_mention_popup();
		});
	};
	mention_btn.appendChild(mention_img);
	util_box.appendChild(mention_btn);

	if(micropost_info['is_mine'] == '1'){
		var delete_btn = new Element('a');
		var delete_img = new Element('img');
		delete_img.src = '/assets/images/static/icon03.png';
		delete_btn.onmouseover = function(){
			delete_img.src = '/assets/images/static/icon03_on.png';
		};
		delete_btn.onmouseout = function(){
			delete_img.src = '/assets/images/static/icon03.png';
		};
		delete_btn.className = 'util_btn delete_btn popup_btn';
		delete_btn.title = '삭제하기';
		delete_btn.onclick = function(){
			open_layer_popup('이 글을 삭제하시겠습니까?', 
			LAYER_POPUP_TYPE_YES_NO,
			function() {
				micropost_instance.remove(micropost_info['id']);
			});
		};
		delete_btn.appendChild(delete_img);
		util_box.appendChild(delete_btn);
	} else {
		var spread_btn = new Element('a');
		var spread_img = new Element('img');
		spread_btn.className = 'util_btn spread_btn popup_btn';
		if(micropost_info['is_spreaded_by_me'] == '1'){
			spread_img.src = '/assets/images/static/icon04_on.png';
			spread_btn.onmouseover = function(){
				spread_img.src = '/assets/images/static/icon04_revert_on.png';
			};
			spread_btn.onmouseout = function(){
				spread_img.src = '/assets/images/static/icon04_on.png';
			};
			spread_btn.title = '소문내기 해제';
			spread_btn.onclick = function(){
				open_layer_popup('소문내기를 해제하시겠습니까?', 
				LAYER_POPUP_TYPE_YES_NO,
				function(){
					micropost_instance.unspread(micropost_info['id']);
				});
			};
		} else {
			spread_img.src = '/assets/images/static/icon04.png';
			spread_btn.onmouseover = function(){
				spread_img.src = '/assets/images/static/icon04_on.png';
			};
			spread_btn.onmouseout = function(){
				spread_img.src = '/assets/images/static/icon04.png';
			};
			spread_btn.title = '소문내기';
			spread_btn.onclick = function(){
				open_layer_popup('이 글을 follwer들에게 소문내시겠습니까?',
				LAYER_POPUP_TYPE_YES_NO,
				function(){
					micropost_instance.spread(micropost_info['id']);
				});
			};
		}
		spread_btn.appendChild(spread_img);
		util_box.appendChild(spread_btn);
	}

	/* if view is admin(valuestar), delete button would appear.*/
	if(micropost_info['is_admin'] == '1' && micropost_info['is_mine'] != '1'){
		var delete_btn = new Element('a');
		var delete_img = new Element('img');
		delete_img.src = '/assets/images/static/icon03.png';
		delete_btn.onmouseover = function(){
			delete_img.src = '/assets/images/static/icon03_on.png';
		};
		delete_btn.onmouseout = function(){
			delete_img.src = '/assets/images/static/icon03.png';
		};
		delete_btn.className = 'util_btn delete_btn popup_btn';
		delete_btn.title = '삭제하기';
		delete_btn.onclick = function(){
			open_layer_popup('이 글을 삭제하시겠습니까?', 
			LAYER_POPUP_TYPE_YES_NO,
			function() {
				micropost_instance.remove(micropost_info['id']);
			});
		};
		delete_btn.appendChild(delete_img);
		util_box.appendChild(delete_btn);
	}

	return util_box;
};

micropost.prototype.write_block_setting = function(write_btn, textarea, cocode_input, ifund_input, count_field)
{
	var micropost_instance = this;
	var enable_func = function(){
		write_btn.src = "/assets/images/static/btn_write.gif";
	};
	var disable_func = function(){
		write_btn.src = "/assets/images/static/btn_write_off.gif";
	};
	textarea.onkeydown = function(){
		text_counter(textarea, MICROPOST_MAX_LENGTH, count_field, enable_func, disable_func);
	};
	textarea.onkeyup = function(){
		text_counter(textarea, MICROPOST_MAX_LENGTH, count_field, enable_func, disable_func);
	};
	write_btn.onclick = function(){
		var content = textarea.value;
		if(content){
		} else {
			open_layer_popup('내용을 입력하세요.', LAYER_POPUP_TYPE_OK);
			return;
		}
		if(content.length > MICROPOST_MAX_LENGTH){
			//open_layer_popup('최대 ' + MICROPOST_MAX_LENGTH + '자 만큼 쓸 수 있습니다.', LAYER_POPUP_TYPE_OK);
			return;
		}
		switch(selected_tab){
			case TAB_TODAY:
				var type = SOURCE_TYPE_TODAY;
				var code = null;
			break;
			case TAB_TALK:
				var type = SOURCE_TYPE_TALK;
				var code = cocode_input.value;
				if(code){
				} else {
					open_layer_popup('기업을 선택하세요.', LAYER_POPUP_TYPE_OK);
					return;
				}
			break;
			case TAB_iFUNd:
				var type = SOURCE_TYPE_iFUNd;
				var code = ifund_input.value;
				if(code){
				} else {
					open_layer_popup('iFUNd를 선택하세요.', LAYER_POPUP_TYPE_OK);
					return;
				}
			break;
			default:
				open_layer_popup('잘못된 타입입니다.', LAYER_POPUP_TYPE_OK);
			return;
		}		
		micropost_instance.write(type, code, content, textarea);
	}	
};

micropost.prototype.get_hide_btn = function(micropost_info)
{
	var hide_btn = new Element('a');
	var hide_img = new Element('img');
	var micropost_instance = this;
	var micropost_id = micropost_info['id'];
	
	hide_img.className = 'v_align_middle';
	hide_img.src = '/assets/images/static/btn_close.gif';
	var success_handler = function(){
		var post_box = $$('.micropost_id_' + micropost_id)[0];
		micropost_instance.micropost_box.removeChild(post_box);
	}
	hide_btn.className = 'hide_btn popup_btn';
	hide_btn.title = '삭제하기';
	hide_btn.onclick = function(){
		open_layer_popup('이 글을 삭제하시겠습니까?', 
		LAYER_POPUP_TYPE_YES_NO,
		function() {
			hide_from_newsfeed(micropost_id, success_handler);
		});
	};
	hide_btn.innerHTML = '&nbsp;&nbsp';
	hide_btn.appendChild(hide_img);
	return hide_btn;
};

micropost.prototype.get_notice_delete_btn = function(micropost_info)
{
	var notice_delete_btn = new Element('a');
	var notice_delete_img = new Element('img');
	var micropost_instance = this;
	var micropost_id = micropost_info['id'];
	var notice_id = micropost_info['article_id'];

	notice_delete_img.className = 'v_align_middle';
	notice_delete_img.src = '/assets/images/static/btn_close.gif';
	var success_handler = function(){
		var post_box = $$('.micropost_id_' + micropost_id)[0];
		micropost_instance.micropost_box.removeChild(post_box);
	}
	notice_delete_btn.className = 'notice_delete_btn popup_btn';
	notice_delete_btn.title = '공지 삭제하기';
	notice_delete_btn.onclick = function(){
		open_layer_popup('보낸 공지가 각 뉴스피드에서 모두 삭제됩니다.<br /> 정말로 삭제하시겠습니까?', 
		LAYER_POPUP_TYPE_YES_NO,
		function() {
			delete_notice(notice_id, success_handler);
		});
	};
	notice_delete_btn.innerHTML = '&nbsp;&nbsp;';
	notice_delete_btn.appendChild(notice_delete_img);
	return notice_delete_btn;
};

micropost.prototype.scroll_bottom_handler = function(e){
	var evt = e || window.event;
	var user_view_bottom = (document.documentElement.scrollTop || document.body.scrollTop) + document.viewport.getHeight();
	var content_bottom = document.body.scrollHeight;
	if(user_view_bottom >= content_bottom && !this.is_last_page && !this.while_loading) {
		this.get_microposts();	
	}
}

micropost.prototype.get_microposts = function(){
	this.while_loading = true;
	switch(this.list_type){
		case LIST_TYPE_NEWSFEED:
			this.get_newsfeed(this.current_page);
		break;
		case LIST_TYPE_BLOG:
			this.get_blog_posts(this.current_page);
		break;
		case LIST_TYPE_MENTION:
			this.get_mentions(this.current_page);
		break;
		case LIST_TYPE_DIALOG:
			this.get_dialog(this.dialog_root);
		break;
		case LIST_TYPE_SCRAP:
			this.get_scraps(this.current_page);
		break;
		case LIST_TYPE_NOTICE:
			this.get_notices(this.current_page);
		break;
		case LIST_TYPE_ONNL:
			this.get_onnl(this.current_page);
		break;
		case LIST_TYPE_DIRECT:
			this.get_direct(this.dialog_root);
		break;
	}
	this.current_page ++;
}

