
	function tx_communitymessages_getParam(paramName) {
		var SELF_LOCATION = self.location.href;

		if (arguments[1]) {
			SELF_LOCATION = arguments[1];
		}

		SELF_LOCATION = SELF_LOCATION.replace(/%5B/g,'[');
		SELF_LOCATION = SELF_LOCATION.replace(/%5D/g,']');
		
		var urlSplit = SELF_LOCATION.split('?');
		if (!urlSplit[1]) { // no query
			return '';
		}
		
		var urlQuery = urlSplit[1];
		var paramsSplit = urlSplit[1].split('&');
		for (var i = 0; i < paramsSplit.length; i++) {
			paramSplit = paramsSplit[i].split('=');
			if (paramSplit[0] == paramName) {
				return paramSplit[1] ? paramSplit[1] : '';
			}
		}
		return '';
   	}
   	
   	function startAjaxWait() {
  		
   		$('#tx-communitymessages-mailbox').prepend('<div id="ajax-loader" class="wait"><p></p><p class="vertical-align"><img alt="Bitte warten..." src="typo3conf/ext/tmpl_zen/res/img/ajax-loader.gif"/> <span> Bitte warten...</span></p></div>');
   		
   	}

   	function stopAjaxWait() {
   		$('#ajax-loader').remove();
   	}

	function initMessageCenter() {
		$('#tx-communitymessages-folder li.mailbox a').each(function() {
			$(this).click(function(e) {
				var href = $(this).attr('href');
				var action = tx_communitymessages_getParam('tx_communitymessages[mailboxtype]', href);
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id', href),
					{
						'tx_communitymessages[mailboxtype]': action,
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[ajax_action]': 'loadMailbox'
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
			});
		});
		$('#tx-communitymessages-folder li.sendMessage a').click(function(e) {
				var href = $(this).attr('href');
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id', href),
					{
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[ajax_action]': 'sendMessage'
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
		});
		$('#tx-communitymessages-messagelist .sender a, #tx-communitymessages-messagelist .subject a').each(function() {
			$(this).click(function(e) {
				var href = $(this).attr('href');
				var messageId = tx_communitymessages_getParam('tx_communitymessages[messageId]', href);
				// console.log(messageId);
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id', href),
					{
						'tx_communitymessages[messageId]': messageId,
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[ajax_action]': 'loadMessage'
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
			});
		});
		$('.deleteIcon').click(function(e) {
				var href = $(this).attr('href');
				var messageId = tx_communitymessages_getParam('tx_communitymessages[messageId]', href);
				// console.log(messageId);
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id', href),
					{
						'tx_communitymessages[messageId]': messageId,
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[response]': 'json',
						'tx_communitymessages[ajax_action]': 'deleteMessageRow'
					},
					function(response) {
						stopAjaxWait();
						responseObj = eval('('+response+')');
						// console.log(responseObj);
						if (responseObj.status == 'deleted') {
							$('#messageRow'+responseObj.msgId).fadeOut('slow');
						}
					}
				);
				return false;
		});
		$('.replyIcon').click(function(e) {
				var href = $(this).attr('href');
				var messageId = tx_communitymessages_getParam('tx_communitymessages[messageId]', href);
				// console.log(messageId);
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id', href),
					{
						'tx_communitymessages[messageId]': messageId,
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[ajax_action]': 'replyMessage'
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
		});
		$('#tx-communitymessages-buttons #button_delete').click(function(e) {
				var pClass = $(this).parent().attr('class');
				var messageId = pClass.replace('msg', '');
				// console.log(messageId);
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id'),
					{
						'tx_communitymessages[messageId]': messageId,
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[response]': 'html',
						'tx_communitymessages[ajax_action]': 'deleteMessage'
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
		}).attr('value', '');
		$('#tx-communitymessages-buttons #button_reply').click(function(e) {
				var pClass = $(this).parent().attr('class');
				var messageId = pClass.replace('msg', '');
				// console.log(messageId);
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id'),
					{
						'tx_communitymessages[messageId]': messageId,
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[ajax_action]': 'replyMessage'
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
		}).attr('value', '');
		$('#tx-communitymessages-buttons #button_send').click(function(e) {
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id'),
					{
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[action]': 'send',
						'tx_communitymessages[ajax_action]': 'replyMessage',
						'tx_communitymessages[recipients]': $('input[@name="tx_communitymessages[recipients]"]').val(),
						'tx_communitymessages[subject]': $('input[@name="tx_communitymessages[subject]"]').val(),
						'tx_communitymessages[message]': $('textarea[@name="tx_communitymessages[message]"]').val()
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
		}).attr('value', '');
		$('#tx-communitymessages-buttons #button_cancel').click(function(e) {
				e.stopPropagation();
				startAjaxWait();
				$.post(
					'/index.php?id='+tx_communitymessages_getParam('id'),
					{
						'tx_communitymessages[cmd]': 'ajaxActions',
						'tx_communitymessages[ajax_action]': 'loadMailbox'
					},
					function(response) {
						stopAjaxWait();
						$('#tx-communitymessages-mailbox').html(response);
						initMessageCenter();
					}
				);
				return false;
		}).attr('value', '');
		$('#tx-communitymessages-contacts-selector').change(function() {
			if (this.options[this.selectedIndex].value != '') {
				idList = $('#recipients_hidden').val().split(',');
				for (var i=0; i<idList.length; i++) {
					if (idList[i] == $(this).val()) return; 
				}
				
				idList.push($(this).val());
				$('#recipients_hidden').val(idList.join());
				$('#recipients_list').append('<li id="recipients_list_entry'+this.options[this.selectedIndex].value+'">'+this.options[this.selectedIndex].text+'</li>');
				initRecipientList();
			}
		});
		initRecipientList();
	}
	
	function initRecipientList() {
		$('#recipients_list li').click(function() {
			idStr = $(this).attr('id');
			// console.log(idStr);
			idStr = idStr.replace('recipients_list_entry', '');
			// console.log(idStr);
			$(this).remove();
			
			idList = $('#recipients_hidden').val().split(',');
			for (var i=0; i<idList.length; i++) {
				if (idList[i] == idStr) idList.splice(i,1); 
			}
			$('#recipients_hidden').val(idList.join());
		});
/*		
		if ($('#recipients_hidden').length > 0) {
			var idList = $('#recipients_hidden').val().split(',');
			if (idList.length == 1 && idList[0] == "") {
				$('#tx-communitymessages-buttons').hide();
			} else {
				$('#tx-communitymessages-buttons').show();
			}
		}
*/
	}
	
	$(document).ready(function() {
		initMessageCenter();
	});

