var is_request = 0;

var groups = {

	join:	function(gid) {
		new Request.JSON({
			'url': base_url+'communities/join.json',
			onComplete: function(res) {
				if (res.errors == undefined) {
					$('join-' + gid).setStyle('display', 'none');
					$('unjoin-' + gid).setStyle('display', 'block');
					
					var count = 0;
					if($('groups-count')) {
						count = $('groups-count').get('text');
					} else if($('groups-count-menu')) {
						count = $('groups-count-menu').get('text');
					}
					
					if(count == '') {
						count = 0;
					} else {
						count = parseInt(count);
					}

					if($('groups-count')) {
						$('groups-count').set('text', count+1);
					}
					$('groups-count-menu').set('text', count+1);
				}
			}
		}).get({'group_id':gid});
	},

	unJoin:	function(gid) {
		new Request.JSON({
			'url': base_url+'communities/unjoin.json',
			onComplete: function(res) {
				if (res.errors == undefined) {
					$('join-' + gid).setStyle('display', 'block');
					$('unjoin-' + gid).setStyle('display', 'none');

					var count = 0;
					if($('groups-count')) {
						count = $('groups-count').get('text');
					} else if($('groups-count-menu')) {
						count = $('groups-count-menu').get('text');
					}
					
					if(count == '') {
						count = 0;
					} else {
						count = parseInt(count);
					}

					if($('groups-count')) {
						$('groups-count').set('text', count-1);
					}
					$('groups-count-menu').set('text', count-1);
				}
			}
		}).get({'group_id':gid});
	},
	
	hide:	function(gid) {
		if (window.confirm('Вы действительно хотите спрятать эту группу?')) {
			new Request.JSON({
				'url': base_url + 'communities/hide.json',
				onComplete: function(res){
					if (res.errors == undefined) {
						$('hide-' + gid).setStyle('display', 'none');
						$('restore-' + gid).setStyle('display', 'block');
					}
				}
			}).get({
				'group_id': gid
			});
		}
	},

	restore: function(gid) {
		new Request.JSON({
			'url': base_url+'communities/restore.json',
			onComplete: function(res) {
				if (res.errors == undefined) {
					$('hide-' + gid).setStyle('display', 'block');
					$('restore-' + gid).setStyle('display', 'none');
				}
			}
		}).get({'group_id':gid});
	},
	
	del: function(gid) {
		if (window.confirm('Вы действительно хотите удалить эту группу?')) {
			window.location.href = base_url+'communities/delete_group/id:'+gid+'/';
		}
	},

	hide_topic: function(tid) {
		if(window.confirm('Do you realy want to hide this topic?')) {
			new Request.JSON({
				'url': base_url+'communities/hide_topic.json',
				onComplete: function(res) {
					$('hide-topic-'+tid).setStyle('display', 'none');
					$('restore-topic-'+tid).setStyle('display', 'block');
				}
			}).get({'topic_id':tid});
		}
	},

	restore_topic: function(tid) {
		new Request.JSON({
			'url': base_url+'communities/restore_topic.json',
			onComplete: function(res) {
				$('hide-topic-'+tid).setStyle('display', 'block');
				$('restore-topic-'+tid).setStyle('display', 'none');
			}
		}).get({'topic_id':tid});
	},

	delete_topic: function(tid) {
		if (window.confirm('Вы действительно хотите удалить этот топик?')) {
			window.location.href = base_url+'communities/delete_topic/id:'+tid+'/';
		}
	},

	create_topic: function(edit, id, info) {
		if (edit) {
			/*if (!$('we_content')) {
				var content = $('group_edit_info').getHTML();
				$('group_edit_info').setHTML('');
				var msg = document.mochaUI.newWindow({
					id			:	'we_content',
					title		:	title_new,
					width		:	325,
					height		:	80,
					draggable	:	true,
					content		:	content,
					resizable	:	false,
					scrollbars	:	false,
					modal		:	true,
					y			:	($(window).getScroll().y + (($(window).getSize().y - 80) >> 1)),
					onClose		: 	function() {
										$('group_edit_info').setHTML(content);
									}
				});
			} else {
				$('we_content').fade('show');
			    $('we_content').setStyle('top',($(window).getScroll().y + (($(window).getSize().y - 80) >> 1)));
			}*/

			Modal.show({
				'content':	'group_edit_info',
				'title':	window.translatedLabels['newMessage']
			});

			$('topic-id').set('value', id);
			for (var i = 0; i < info.length; i++) {
				var item = info[i];
				$('topic-title-edit-' + item['alias']).set('value', item['value']);
			}
		} else {
			/*if  (!$('w_content')) {
				var content = $('group_info').getHTML();
				$('group_info').setHTML('');
				var msg = document.mochaUI.newWindow({
					id			:	'w_content',
					title		:	title_new,
					width		:	325,
					height		:	330,
					draggable	:	true,
					content		:	content,
					resizable	:	false,
					scrollbars	:	false,
					modal		:	true,
					y			:	($(window).getScroll().y + (($(window).getSize().y - 330) >> 1)),
					onClose		: 	function() {
										$('group_info').setHTML(content);
									}
				});
				new Tips($$('.tip'));
			} else {
				$('w_content').fade('show');
			    $('w_content').setStyle('top',($(window).getScroll().y + (($(window).getSize().y - 330) >> 1)));
			}*/

			Modal.show({
				'content':	'group_info',
				'title':	window.translatedLabels['newMessage']
			});
			tips.attach('group-info-send-help-title-tip');
		}
	},
	
	send_topic_info: function(group_id, edit) {
		if (is_request == 1) {
			return 0;
		}
		
		var params = {'group_id':group_id, 'send_to_check':$('send-to-check').get('checked')};
		params.topic_title = {};
		var field_id = 'topic-title-';
		if (edit) {
			field_id = 'topic-title-edit-';
			params.topic_id = $('topic-id').get('value');
		} else {
			params.title = {};
			params.content = {};
			$$('span[id^=title-lang-]').each(function(el){
				var id = el.getProperty('id').match('title-lang-(.*)')[1];
				params.title[id] = $('title-' + id).get('value');
			});
			
			$$('span[id^=content-lang-]').each(function(el){
				var id = el.getProperty('id').match('content-lang-(.*)')[1];
				params.content[id] = $('content-' + id).get('value');
			});
		}

		$$('span[id^='+field_id+'lang-]').each(function(el){
			var id = el.getProperty('id').match(field_id+'lang-(.*)')[1];
			params.topic_title[id] = $(field_id + id).get('value');
		});
		
		is_request = 1;
		new Request.JSON({
			'url': base_url+(edit ? 'communities/edit_topic.json' : 'communities/create_topic.json'),

			onComplete: function(res) {
				if (res != null && res.errors == undefined && res.topic_id) {
					if (edit) {
						window.location.href = base_url + 'communities/group' + group_id + '.html';
					} else {
						window.location.href = base_url + 'communities/group' + group_id + '/topic' + res.topic_id + '.html';
					}
				}
				is_request = 0;
			}
		}).post(params);
	},

	edit_topic: function(tid) {
		new Request.JSON({
			'url': base_url+'communities/topic.json',
			onComplete: function(res){
				if (res.errors == undefined) {
					groups.create_topic(true, tid, res);
				}
			}
		}).post({'topic_id':tid});
	},

	showMessageWindow: function() {
		/*if  (!$('msg_gnew')) {
			var content = $('m_content').getHTML();
			$('m_content').setHTML('');
			var msg = document.mochaUI.newWindow({
				id			:	'msg_gnew',
				title		:	title_new,
				width		:	325,
				height		:	250,
				draggable	:	true,
				content		:	content,
				resizable	:	false,
				scrollbars	:	false,
				modal		:	true,
				y			:	($(window).getScroll().y + (($(window).getSize().y - 200) >> 1)),
				onClose		: 	function() {
									$('m_content').setHTML(content);
								}
			});
		} else {
			$('msg_gnew').fade('show');
		    $('msg_gnew').setStyle('top',($(window).getScroll().y + (($(window).getSize().y - 200) >> 1)));
		}*/

		Modal.show({
			'content':	'm_content',
			'title':	window.translatedLabels['newMessage']
		});

		if (arguments.length == 2) {
			$('reply-div').setStyle('display', 'block');
			$('reply-label').setStyle('display', 'block');
			$('txt-reply-to').setStyle('display', 'inline');
			$('txt-reply-to-id').setStyle('display', 'block');
			$('txt-reply-to').set('value', arguments[1]);
			$('txt-reply-to-id').set('value', arguments[0]);
		} else {
			$('reply-div').setStyle('display', 'none');
			$('reply-label').setStyle('display', 'none');
			$('txt-reply-to').setStyle('display', 'none').set('value', '');
			$('txt-reply-to-id').setStyle('display', 'none').set('value', '');
		}

		tips.attach('m-content-send-help-title-tip');
	},

	send_message: function(tid, gid) {
		if (is_request == 1) {
			return 0;
		}
		
		var params = {'topic_id':tid, 'reply_to':$('txt-reply-to-id').get('value'), 'send_to_check':$('send-to-check').get('checked')};
		params.title = {};
		params.content = {};
		
		$$('span[id^=title-lang-]').each(function(el) {
			var id = el.getProperty('id').match('title-lang-(.*)')[1];
			params.title[id] = $('title-'+id).get('value');
		});

		$$('span[id^=content-lang-]').each(function(el) {
			var id = el.getProperty('id').match('content-lang-(.*)')[1];
			params.content[id] = $('content-'+id).get('value');
		});
		
		is_request = 1;
		new Request.JSON({
			'url': base_url+'communities/add_message.json',
			onRequest: function() {
				Modal.showLoadingIcon();
				//document.mochaUI.showLoadingIcon($('msg_gnew_canvasIcon'));
			},
			onComplete: function(res) {
				if (res != null && res.errors == undefined && res.result) {
					window.location.href = base_url+'communities/group'+gid+'/topic'+tid+'.html';
				}

				//document.mochaUI.hideLoadingIcon($('msg_gnew_canvasIcon'));
				Modal.hideLoadingIcon();
				is_request = 0;
			}
		}).post(params);
	},

	hide_message: function(mid) {
		if (window.confirm('Вы действительно хотите спрятать это сообщение?')) {
			window.location.href = base_url+'communities/hide_message/id:'+mid+'/';
		}
	},

	restore_message: function(mid) {
		window.location.href = base_url+'communities/restore_message/id:'+mid+'/';
	},
	
	delete_message: function(mid) {
		if (window.confirm('Вы действительно хотите удалить это сообщение?')) {
			window.location.href = base_url + 'communities/delete_message/id:' + mid + '/';
		}
	},
	
	switchMessageText: function(message_id, lang_alias) {
		$$('span[id^=msg'+message_id+'-lang-]').each(function(el) {
			var id = el.getProperty('id').match('msg'+message_id+'-lang-(.*)')[1];
			if(id == lang_alias) {
				$('msg'+message_id+'-lang-' + id).removeClass('tdu').addClass('tdn b');
			} else {
				$('msg'+message_id+'-lang-' + id).removeClass('tdn b').addClass('tdu');
			}
		});

		$$('div[id^=msg'+message_id+'-]').each(function(el) {
			var id = el.getProperty('id').match('msg'+message_id+'-(.*)')[1];
			if(id == lang_alias) {
				$('msg'+message_id+'-' + id).setStyle('display', 'block');
			} else {
				$('msg'+message_id+'-' + id).setStyle('display', 'none');
			}
		});
	},

	messageNotNormal: function(message_id) {
		/*if  (!$('msg_warning')) {
			var content = $('m_warning_content').getHTML();
			$('m_warning_content').setHTML('');
			var msg = document.mochaUI.newWindow({
				id			:	'msg_warning',
				title		:	title_new,
				width		:	300,
				height		:	150,
				draggable	:	true,
				content		:	content,
				resizable	:	false,
				scrollbars	:	false,
				modal		:	true,
				y			:	($(window).getScroll().y + (($(window).getSize().y - 200) >> 1)),
				onClose		: 	function() {
									$('m_warning_content').setHTML(content);
								}
			});
		} else {
			$('msg_warning').fade('show');
		    $('msg_warning').setStyle('top',($(window).getScroll().y + (($(window).getSize().y - 200) >> 1)));
		}*/

		Modal.show({
			'content':	'm_warning_content',
			'title':	window.translatedLabels['newMessage']
		});

		$('message_id').set('value', message_id);
	},

	send_warning_message: function(tid, gid) {
		if (is_request == 1) {
			return 0;
		}

		is_request = 1;
		new Request.JSON({
			'url': base_url+'communities/warning_message.json',
			onRequest: function(){
				//document.mochaUI.showLoadingIcon($('msg_warning_canvasIcon'));
				Modal.showLoadingIcon();
			},

			onComplete: function(res) {
				if (res != null && res.errors == undefined && res.result) {
					window.location.href = base_url+'communities/group'+gid+'/topic'+tid+'.html';
				}

				//document.mochaUI.hideLoadingIcon($('msg_warning_canvasIcon'));
				Modal.hideLoadingIcon();
				//document.mochaUI.closeWindow('m_warn');
				Modal.hide();
				is_request = 0;
			}
		}).post({'message_id':$('message_id').get('value'), 'message':$('warning-content').get('value')});
	},

	showCommentWindow: function(message_id, lang_alias) {
		$('comment_message_id').set('value', message_id);
		$('lang_alias').set('value', lang_alias);

		/*if (!$('msg_comment')) {
			var content = $('m_comment_content').getHTML();
			$('m_comment_content').setHTML('');
			var msg = document.mochaUI.newWindow({
				id			:	'msg_comment',
				title		:	title_new,
				width		:	470,
				height		:	250,
				draggable	:	true,
				content		:	content,
				resizable	:	false,
				scrollbars	:	false,
				modal		:	true,
				y			:	($(window).getScroll().y + (($(window).getSize().y - 250) >> 1)),
				onClose		: 	function() {
									$('m_comment_content').setHTML(content);
								}
			});
		} else {
			$('msg_comment').fade('show');
		    $('msg_comment').setStyle('top',($(window).getScroll().y + (($(window).getSize().y - 250) >> 1)));
		}*/

		Modal.show({
			'content':	'm_comment_content',
			'title':	window.translatedLabels['newMessage']
		});
	},
	
	add_comment: function() {
		var html = $('comment-content').get('value');
		var message_body = $('message-body-'+$('comment_message_id').get('value')+'-'+$('lang_alias').get('value')).getHTML();
		message_body = wordwrap(message_body, 66, "\n>");
		$('comment-content').set('value', ">"+ message_body+"\n\n"+html);
		document.getElementById('comment-content').focus();
	},

	send_comment_message: function(tid, gid) {
		if (is_request == 1) {
			return 0;
		}

		is_request = 1;
		new Request.JSON({
			'url': base_url+'communities/comment_message.json',
			onRequest: function(){
				//document.mochaUI.showLoadingIcon($('msg_comment_canvasIcon'));
				Modal.showLoadingIcon();
			},

			onComplete: function(res) {
				if (res != null && res.errors == undefined && res.result) {
					window.location.href = base_url+'communities/group'+gid+'/topic'+tid+'.html';
				}
				if (res.errors != undefined) {
					//document.mochaUI.closeWindow('msg_comment');
					Modal.hide();
				}

				//document.mochaUI.hideLoadingIcon($('msg_comment_canvasIcon'));
				Modal.hideLoadingIcon();
				is_request = 0;
			}
		}).post({'message_id':$('comment_message_id').get('value'), 'message':$('comment-content').get('value')});
	},

	showComments: function(message_id) {
		var show = true;
		if($('msg'+message_id+'-show-comments').hasClass('b')) {
			show = false;
			$('msg'+message_id+'-show-comments').removeClass('b');
		} else {
			$('msg'+message_id+'-show-comments').addClass('b');
		}
		
		$$('div[id^=msg'+message_id+'-comment-]').each(function(el) {
			el.setStyle('display', show ? 'block' : 'none');
		});
	},
	
	rejectComment: function(message_id, comment_id) {
		if (window.confirm('Вы действительно хотите удлить этот комментарий?')) {
			new Request.JSON({
				'url': base_url + 'communities/reject_comment.json',
				
				onComplete: function(res){
					if (res != null && res.errors == undefined && res.result) {
						$('msg' + message_id + '-comment-' + comment_id).remove();
						var cnt = $('comment' + message_id + '-count').getHTML();
						$('comment' + message_id + '-count').setHTML(cnt-1);
					}
				}
			}).post({
				'message_id': message_id,
				'comment_id': comment_id
			});
		}
	},
	
	acceptComment: function(group_id, topic_id, message_id, comment_id) {
		if (window.confirm('Вы действительно хотите принять этот комментарий?')) {
			new Request.JSON({
				'url': base_url + 'communities/accept_comment.json',
				
				onComplete: function(res){
					if (res != null && res.errors == undefined && res.result) {
						$('msg' + message_id + '-comment-' + comment_id+'-actions').remove();
						//window.location.href = base_url+'communities/group'+group_id+'/topic'+topic_id+'.html';
					}
				}
			}).post({
				'message_id': message_id,
				'comment_id': comment_id
			});
		}
	},
	
	moveMessage: function(message_id) {
		/*var move_window = document.mochaUI.newWindow({
			id			:	'msg_move',
			title		:	title_new,
			width		:	450,
			height		:	80,
			draggable	:	true,
			loadMethod	:	'xhr',
			contentURL	:	'communities/get_move_template.html',
			resizable	:	false,
			scrollbars	:	false,
			modal		:	true,
			y			:	($(window).getScroll().y + (($(window).getSize().y - 80) >> 1)),
			onContentLoaded :	function() {
									$('move-message-id').set('value', message_id);
								},
			onCloseComplete	: function() {
								delete(move_window);
							}
		});*/
		Modal.addEvent('contentLoaded', groups.onMoveMessageWindowContentLoad);
		Modal.show({
			'contentURL':	'communities/get_move_template.html',
			'title':		window.translatedLabels['newMessage']
		});
	},

	send_move_message: function() {
		var message_id = $('move-message-id').get('value');
		var topic_id = $('topic_id').get('value');
		
		new Request.JSON({
			'url': base_url + 'communities/move_message.json',
			
			onComplete: function(res){
				if (res != null && res.errors == undefined && res.result) {
					window.location.reload();
				}
			}
		}).post({
			'message_id': message_id,
			'topic_id': topic_id
		});
	},

	onMoveMessageWindowContentLoad: function(content) {
		content.getChild('move-message-id').set('value', message_id);
		Modal.removeEvent('contentLoaded', groups.onMoveMessageWindowContentLoad);
	}
}
