prompting = {
	show: function(_this, id, type, user_id) {
		var $container = $('#' + id + ' div.prompting');
		var $input = $(_this);
		var vals = _this.value.split(";");
		var val = vals[vals.length-1].replace(" ","");
	
		$container.show();

		$(document).click(function(){
			$container.hide();
		});
		
		$(_this).click(function() {
			$container.hide();
		});
		
		$container.click(function(e) {
			e.stopPropagation();
		});
		
		$input.addClass('preloader');
		$.ajax({
			type: "get",
			url: "/promting/?user=" + val + "&type=" + type + "&id=" + user_id,
			success: function(html) {
				$container.html(html);
				$input.removeClass('preloader');
			}
		});
	},
	
	add: function(_this, id) {
		var $input = $('#' + id + ' input[type=text]');
		
		if ($input.length) {
			var val = $input.val();
			var n = _this.innerHTML.replace(' ', '');
			var o = '';
			var a = val.split(';');
			
			n = this.clear(n);
			
			for (i = 0, len = a.length; i < len; i++) {
				a[i] = a[i].replace(' ', '');
				if (i < (len - 1) && a[i] != n && a[i] != '') o = o + a[i] + '; ';
			}
			
			$input.val(o + n + "; ");
		}
		
		$('#' + id + ' div.prompting').hide();
	},
	
	clear: function strip_html(s) {
		var re = /(<([^>]+)>)/gi;
		for (i = 0, len = s.length; i < len; i++) {
			s = s.replace(re, '');
		}
		return s;
	}
};

recipient = {
	show: function(_this, sid, pid) {
	
		var $list = $('#' + sid);
		var input = _$(pid);
		
		if ($list.height() > 214) $list.addClass('recipients-overflow');

		if ($list && input) {
			$(document).click(function(e){
				if ((e.srcElement || e.target) != _this) $list.hide();
			});
			
			if ($list.css('display') == 'none') {
				$list.show();
				var val = input.value.split(';');
				var contact = $('#' + sid + ' a').get();

				for (v = 0, vlen = val.length; v < vlen; v++) {
					val[v] = val[v].replace(' ', '');
					
					for (c = 0, clen = contact.length; c < clen; c++) {
						if (contact[c].childNodes[0].innerHTML.replace(' ', '') == val[v]) {
							$(contact[c]).removeClass('js-plus').addClass('js-minus');
						}
					}
				}
			
			} else {
				$list.hide();
			}
			
			$list.click(function(e) {
				e.stopPropagation();
			});
		}
		
		_this.blur();
	},
	
	add: function(_this, pid) {
		var input = _$(pid);
		
		if (input) {
			var val = input.value.split(';');
			var n = _this.childNodes[0].innerHTML.replace(' ', '');
			var str = '';

			for (i = 0, len = val.length; i < len; i++) {
				val[i] = val[i].replace(' ', '');
				if (val[i] != n && val[i] != '') str = str + val[i] + '; ';
			}
			
			if (_this.className.indexOf('js-plus') != -1) {
				str = str + n + '; ';
				$(_this).removeClass('js-plus').addClass('js-minus');
			} else {
				$(_this).removeClass('js-minus').addClass('js-plus');
			}
			
			input.value = str;
		}
		
		_this.blur();
	}
};


function toggle_message(_this, id) {
	var $body = $('#' + id + ' div.message-body');
	var $title = $('#' + id + ' td.message-title a');
	var $toggle = $('#' + id + ' a.toggle');
	
	if ($body.css('display') == 'none') {
		$toggle.addClass('toggle-active');
		$body.slideDown();
		$title.fadeOut();
	} else {
		$toggle.removeClass('toggle-active');
		$body.slideUp();
		$title.fadeIn();
	}
	
	_this.blur();
}


