Messages = {
	MESSAGES_ROOT : '/messages/',
	MESSAGES_ITEM : 'rq/message.php',
	MESSAGES_FORM : 'rq/form.php',
	MESSAGES_FORM_CONTACTS : 'rq/_form_contacts.php',
	MESSAGES_FORM_FILES : 'rq/_form_files.php',
	MESSAGES_FORM_SKINS : 'rq/_form_skins.php',
	MESSAGES_LIST : 'rq/list.php',
	MESSAGES_CONTAINER : '#ct',
	CHARS_PER_MESSAGE: 4000,
	
	elHover: 0,
	ctrlEnter: 0,
	autoComplete: {},

	_init: function() {
		$('#MsgTable tr.msgh').css('cursor', 'pointer');
			
		$('body').append('<div id="MsgLoadingText" style="display:none;"></div>');
			
		Messages._initForm();
	},
	_initForm: function() {
        Attach.appendUpload();
		
		$('#MsgForm button.msg_btnSave').show();
		
		var options = {
			data: {
				'redirect': 'false'
			},
			beforeSubmit: Messages._checkForm,
			success: function(data) {
				var parts = data.split(':');
				
				var action = parts[0];
				var params = parts[1];
				
				if (action == 'errors') {
					Messages._hideLoading();
					
					var errors = params.split(',');
					$(errors).each(function(i, err) {
						Messages._showError(err);
					});
				}
				else {
					var redirect = params;
					var parts = params.split('&');
					var params = {};

					$(parts).each(function(i, a) {
						x = a.split('=');
						params[x[0]] = x[1];
					});
					
					if (action == 'save') {
						var msg_input = $('#MsgForm input[name=msg]');
						
						if (msg_input.val() == 0) {
							msg_input.val(params['msg']);
							Messages._increaseCount('drafts');
						}
						
						Messages._hideLoading();
					}
					else {
						Messages._increaseCount('outbox');
						
						if (params['p'] == 'drafts') {
							Messages._decreaseCount('drafts');
						}

						Messages._redirect(redirect);
					}
				}
			}
		};
		$('#MsgForm').ajaxForm(options);
		
		$('#MsgFormBody').bind('change keyup keydown', Messages._updateCharsLeft);
		
		$('#MsgFormTo, #MsgFormBtnContacts, #MsgFormButtonsLine').show();
		
		Messages.acInit();

		if ($('body.iframe #MsgForm.msg_write').length == 1) {
			setTimeout(function() {
				$('#MsgFormSubject').focus();
			}, 300);
		}
		else if ($('#MsgForm.msg_write').length == 1) {
			$('#MsgFormSubject').focus();
		}
		else {
			$('#MsgUserSelector table input').focus();
		}
		
		$('#MsgFormBtnAttach').bind('click', function() {
			Messages._formWindowToggle();
		});
		
		if ($('#MsgLoadingBg').length == 0) {
			if ($('#MsgListContainer').length > 0) {
				$('#MsgListContainer').append('<div id="MsgLoadingBg" style="display:none;"></div>');
			}
			else {
				$(Messages.MESSAGES_CONTAINER).append('<div id="MsgLoadingBg" style="display:none;"></div>');
			}
		}
		
		Messages._initSkin();
	},
	_checkForm: function(data) {
		var errors = false;
		Messages._removeErrors();
	
		var is_send = false;
		var is_save = false;
		Messages._setErrorText('send');
		
		$(data).each(function(i, ob) {
			if (ob.name == 'send') {
				is_send = true;
			}
			else if (ob.name == 'save') {
				is_save = true;
				Messages._setErrorText('save');
			}
		});

		if (Messages.ctrlEnter == 1) {
			is_send = true;
			Messages.ctrlEnter = 0;
		}
		
		// file upload
		if (is_send == false && is_save == false) {
			return false;
		}
		else {
			if (is_save == false) {
				// receiver(-s)
				if ($('#MsgForm input[name=fid]').val() == 0 && ($('#MsgUserSelector input[name=contacts_ids]').length == 1 && $.trim($('#MsgUserSelector input[name=contacts_ids]').val()).length == 0)) {
					errors = true;
					Messages._showError('err_no_receiver');
				}
			}
				
			// subject & body
			if ($('#MsgFormUploadContent input[type=checkbox]:checked').length == 0) {
				if (is_save == false) {
					if ($.trim($('#MsgFormSubject').val()) == '' && $.trim($('#MsgFormBody').val()) == '') {
						errors = true;
						Messages._showError('err_no_subject');
					}
				}
			}

			if (errors == false) {
				if (is_save == true) {
					Messages._showLoading('saving');
				}
				else {
					Messages._showLoading('sending');
				}

                if ($('#MsgForm').find('form').length) {
                    $('#MsgForm').find('form').remove();
                }
                else if ($('#MsgForm')[0].tagName.toLowerCase() == 'div') {
                    $('#MsgForm').parents('form').submit();
                }

				return true;
			}
			
			return false;
		}
	},
	_setErrorText: function(action) {
		var text = '';
		if (action == 'save') {
			text = Messages._translate('err_not_saved_because');
		}
		else if (action == 'send') {
			text = Messages._translate('err_not_sent_because');
		}
		
		$('#MsgFormError span.reason').text(text);
	},
	_showError: function(text) {
		$('#MsgFormError').show();
		if ($('#MsgFormError ol li.' + text).length == 0) {
			$('#MsgFormError ol').append('<li class="' + text + '">' + Messages._translate(text) + '</li>');
		}
	},
	_removeErrors: function() {
		$('#MsgFormError').hide();
		$('#MsgFormError ol').empty();
	},
	_updateCharsLeft: function(e) {
		if (typeof(e) != 'undefined' && e.type == 'keydown' && e.metaKey == true && (e.keyCode == 13 || e.keyCode == 10)) {
			Messages.ctrlEnter = 1;
			$('#MsgForm').submit();
		}
		
		var chars_left = Messages.CHARS_PER_MESSAGE - $('#MsgFormBody').val().length;
		if (chars_left < 0) {
			$('#MsgFormBody').val($('#MsgFormBody').val().substr(0, Messages.CHARS_PER_MESSAGE)).scrollTop($('#MsgFormBody')[0].scrollHeight);
			chars_left = 0;
		}

		$('#MsgFormCharsLeft').val(chars_left);
	},
	_formWindowToggle: function(window) {
		if ($('#MsgForm' + window).css('display') != 'none') {
			$('#MsgForm' + window).hide();
			$('#MsgFormBtn' + window).removeClass('formBtnActive');
		}
		else {
			if (window == 'Skins') {
				if ($('#MsgFormSkins').html().length == 0) {
					$('#MsgFormSkins').html('<div class="msgb_loading"><img src="http://ifrype.com/img/load.gif" /></div>');
					Messages.reloadSkins();
				}
			}

			if (window != 'Contacts') {
				$('.msg_formWindow:not(#MsgFormContacts)').hide();
				$('.formBtnActive:not(#MsgFormBtnContacts)').removeClass('formBtnActive');
			}
	
			$('#MsgForm' + window).show();
			$('#MsgFormBtn' + window + '').addClass('formBtnActive');
			
	        if (window == 'Smileys') {
	            $('#MsgFormBody').focus();
	        }			
		}
	},
	_fillNames: function(names) {
		$('#MsgFormTo').val(names);
	},
	_redirect: function(data) {
		if (data.length > 0) {
			var parts = data.split('&');
			var params = {};
			
			$(parts).each(function(i, a) {
				x = a.split('=');
				params[x[0]] = x[1];
			});

			if (params['p'] == 'write' && $('body.iframe #MsgForm.msg_write').length == 1) {
				parent.InfoBox3.close();
			}
			else if (params['p'] == 'new' || params['p'] == 'write') {
				document.location = Messages.MESSAGES_ROOT;
			}
			else {
				Messages.page(params['p'], params['pg'], params['uid'], true);
			}
		}
	},
	_showLoading: function(text) {
		var iframe = false;
		
		if ($('body.iframe').length > 0) {
			iframe = true;
		}
		else if ($('#MsgListContainer').length > 0) {
			$('#MsgLoadingBg').css({
				'top': 39,
				'height': $('#MsgListContainer').height() - 39
			});
		}
		else {
			$('#MsgLoadingBg').css({
				'height': $(Messages.MESSAGES_CONTAINER).height()
			});
		}
		
		if (iframe == true) {
			$('#MsgLoadingText').css({
				'top': $('#MsgForm').height() / 2,
				'left': $('#MsgForm').width() / 2 - 100
			}).text(Messages._translate(text) + '...');
		}
		else {
			$('#MsgLoadingText').css({
				'top': $(window).height() / 2,
				'left': $(Messages.MESSAGES_CONTAINER).offset().left + $(Messages.MESSAGES_CONTAINER).width() / 2 - 100
			}).text(Messages._translate(text) + '...');
		}
		
		$('#MsgLoadingBg, #MsgLoadingText').show();
	},
	_hideLoading: function() {
		$('#MsgLoadingBg, #MsgLoadingText').hide();
	},
	_increaseCount: function(page) {
		$('#MsgIconList li.msgl_' + page + ' span').text(parseInt($('#MsgIconList li.msgl_' + page + ' span').text()) + 1);
	},
	_decreaseCount: function(page) {
		$('#MsgIconList li.msgl_' + page + ' span').text(parseInt($('#MsgIconList li.msgl_' + page + ' span').text()) - 1);
	},
	_restoreUpload: function(need_to_empty) {
		$('#MsgFormBtnAttach iframe, #MsgFormBtnAttach form').remove();
		if (need_to_empty != false) {
			$('#MsgFormUploadContent').empty();
		}
		
		Upload.addUploadItem(true);
	},
	_translate: function(text) {
		if (typeof(xMss[text]) == 'undefined') {
			return text;
		}
		else {
			return xMss[text];
		}
	},
	_replySubject: function(subject) {
		if (subject.substr(0, 3) != 'Re:' && subject.substr(0, 3) != 'Re[') {
			subject = 'Re[1]: ' + subject;
		}
		else if (subject.substr(0, 3) == 'Re[') {
			var exp = new RegExp(/^Re\[([\d]+)\]:/);
			count = parseInt(subject.match(exp)[1]);
			subject = subject.replace(exp, 'Re[' + (count + 1)	+ ']:');
		}
		else if (subject.substr(0, 3) == 'Re:') {
			subject = subject.replace(/Re:/, 'Re[2]:');
		}
		
		return subject;
	},
	_forwardSubject: function(subject) {
		return 'Fwd: ' + subject;
	},
	_forwardHeader: function(msg) {
		var head = '\n\n\n---------- ' + Messages._translate('forwarded_message') + ' ----------\n';
		head += Messages._translate('from') + ': ' + $('#MsgInfoFrom_' + msg).val() + '\n';
		head += Messages._translate('subject') + ': ' + $('#MsgInfoSubject_' + msg).val() + '\n';
		head += Messages._translate('date') + ': ' + $('#MsgInfoTime_' + msg).val() + '\n\n';
		
		return head;
	},
	_initSkin: function(ret) {
		if ($('#MsgForm').length > 0) {
			var skin_id = 0;
			var skin_cookie = 'msgSkin' + id + '=';
			var skin_cookie_length = skin_cookie.length;
		
			var parts = document.cookie.split(';');
			$(parts).each(function(i, a) {
				if ($.trim(a).substr(0, skin_cookie_length) == skin_cookie) {
					var x = a.split('=');
					skin_id = parseInt(x[1]);
				}
			});
		
			if (ret == '<h2>') {
				Messages.selectSkin(0);
				
				var date = new Date();
				date.setTime(date.getTime() - 3600);
				document.cookie = skin_cookie + '; expires=' + date.toGMTString() + '; domain=.' + dom + '; path=/messages/';
			}
			else {
				$('#MsgFormSkin').val(skin_id);
				$('#MsgFormBodyLine').addClass('pskin' + skin_id);
				$('#MsgFormSkins .skin' + skin_id).addClass('active');
			}
		}
	},

	
	read: function(page, msg, box, pg, uid) {
		var with_skin = $('#m_' + msg).hasClass('skin');
		var skin_id = $('#m_' + msg).attr('rel');
		
		if (Messages.elHover == 0) {
			var is_visible = 0;
			if ($('#mc_' + msg).css('display') != 'none') {
				is_visible = 1;
			}
			
			if (is_visible == 0) {
				$('#mc_' + msg).show();
				$('#m_' + msg).addClass('msghOpen').addClass('msghOpenSkin' + skin_id);
				
				if ($('#MsgTable').hasClass('msg_inbox') || ($('#MsgTable').hasClass('msg_conversation') && $('#m_' + msg + ' .msgt_convbox').html().indexOf('inbox') != -1)) {
					if ($('#m_' + msg).hasClass('unread')) {
						$('#m_' + msg).removeClass('unread');
						
						var mailcount = $('#header #head_hmail #hmail');
						if (mailcount.length > 0) {
							var current = parseInt(mailcount.text());
							if (current == 1) {
								$('#header #head_hmail').empty();
							}
							else {
								mailcount.text(current - 1);
							}
						}
					}
				}
				
				$('#m_' + msg).next().next().addClass('next');
			}
			else {
				$('#mc_' + msg).hide();
				$('#m_' + msg).removeClass('msghOpen').removeClass('msghOpenSkin' + skin_id);
				
				$('#m_' + msg).next().next().removeClass('next');
			}
			
			if (is_visible == 0 && $.trim($('#mc_body_' + msg).html()).length == 0) {
				$('#mc_body_' + msg).html('<div class="msgb_loading"><img src="http://ifrype.com/img/load.gif" /></div>');
				$('#mc_body_' + msg).load(Messages.MESSAGES_ROOT + Messages.MESSAGES_ITEM + '?p=' + page + '&msg=' + msg + '&uid=' + uid + '&box=' + box + '&pg=' + pg);
			}
			
			return false;
		}
		else if (Messages.elHover == 1) {
			return true;
		}

		return false;
	},
	write: function(page, msg, box, pg, uid) {
		if (Messages.elHover == 0) {
			var is_visible = 0;
			if ($('#mc_' + msg).css('display') != 'none') {
				is_visible = 1;
			}

			$('#MsgTable tr').removeClass('msghOpen next');
			$('#MsgTable tr.msgb').hide();
			$('#MsgTable td.msgb').empty();
			
			if (is_visible == 0) {
				$('#mc_' + msg).show();
				$('#m_' + msg).addClass('msghOpen');

				$('#m_' + msg).next().next().addClass('next');
			}
			else {
				$('#mc_' + msg).hide();
				$('#m_' + msg).removeClass('msghOpen');
				
				$('#m_' + msg).next().next().removeClass('next');
			}
			
			if (is_visible == 0) {
				$('#mc_body_' + msg).html('<div class="msgb_loading"><img src="http://ifrype.com/img/load.gif" /></div>');
				$('#mc_body_' + msg).load(Messages.MESSAGES_ROOT + Messages.MESSAGES_FORM + '?p=' + page + '&msg=' + msg + '&uid=' + uid + '&box=' + box + '&pg=' + pg, function() {
					Messages._initForm();
					
					var fids = $('#MsgFromIds').val();
					if (fids.length > 0) {
						$.post(Messages.MESSAGES_ROOT + Messages.MESSAGES_FORM_CONTACTS, {
								'fids': fids
							},
							function(d) {
								var prefill = eval(d);
								$(prefill).each(function(i, a) {
									Messages.autoComplete.addValue({
										'value': parseInt(a.i),
										'caption': a.n
									});
								});
							}
						);
					}
				});
			}
		
			return false;
		}
		return true;
	},
	reply: function(page, msg, box, pg, uid, fid, link, bug) {
		$('#mc_' + msg + ' .msgb_container').append($('#MsgForm'));
		$('#MsgForm').addClass('reply').removeClass('forward').show();

		$('#MsgForm h1').text(Messages._translate('your_reply'));
		$('#MsgForm input[name=fid]').val(fid);
		$('#MsgFormSubject').val(Messages._replySubject($('#mc_' + msg + ' .msgb_container .msgb_head .msgb_subject').text()));
		$('#MsgFormIsReply').val(1);
		$('#MsgFormIsForward').val(0);
		$('#MsgFormBody').val('').focus();
		$('#MsgFormToLine').hide();
		$('#MsgFormUploadLine').hide();
		
		if (link && link == 2) {
			$('#MsgFormBtnAttachPlaceholder').show();
			$('#MsgFormBtnVideo').show();

    		Attach.appendUpload();
		}
		else {
			$('#MsgFormBtnAttachPlaceholder').hide();
			$('#MsgFormBtnVideo').hide();
		}
		
		if(typeof(bug) == 'undefined'){
			$('#bug_rep').attr('value', 0);
		}else{
			$('#bug_rep').attr('value', bug);
		}
		
		Messages._removeErrors();
	},
	forward: function(page, msg, box, pg, uid, has_attach) {
		$('#mc_' + msg + ' .msgb_container').append($('#MsgForm'));
		$('#MsgForm').addClass('forward').removeClass('reply').show();

		$('#MsgForm h1').text(Messages._translate('forward_message'));
		$('#MsgForm input[name=fid]').val('');
		$('#MsgFormIds').val('');
		$('#MsgFormSubject').val(Messages._forwardSubject($('#mc_' + msg + ' .msgb_container .msgb_subject').text()));
		$('#MsgFormIsReply').val(0);
		$('#MsgFormIsForward').val(1);
		$('#MsgFormBody').val(Messages._forwardHeader(msg) + $('#mc_' + msg + ' .msgb_container .msgb_body_original').val());
	
		$('#MsgFormToLine').show();
		$('#MsgFormUploadLine').hide();
		
		$('#MsgFormBtnAttachPlaceholder').show();
		$('#MsgFormBtnVideo').show();
		
		Messages._removeErrors();
		
		$('#MsgUserSelector table input').focus();
		
		if (has_attach == 1 && $('#mc_' + msg + ' .msgb_container .msgb_foot h2').hasClass('msgf_header_old') === false) {
			$('#MsgFormUploadContent').load(Messages.MESSAGES_ROOT + Messages.MESSAGES_FORM_FILES +'?p=' + page + '&msg=' + msg + '&uid=' + uid + '&box=' + box + '&pg=' + pg, function() {
				Attach.appendUpload();
				$('#MsgFormUploadLine').show();
			});
		}
		else {
			Attach.appendUpload();
		}
	},
	close: function() {
		if ($('#MsgForm.msg_drafts').length == 1) {
			var msg = $('#MsgTable tr.msgb:visible input[name=msg]').val();
			$('#m_' + msg).click();
		}
		else {
			$('#MsgForm').hide();
		}
	},
	page: function(page, pg, uid, no_msg) {
		if (!no_msg) {
			Messages._showLoading('loading');
		}
		$(Messages.MESSAGES_CONTAINER).load(Messages.MESSAGES_ROOT + Messages.MESSAGES_LIST +'?p=' + page + '&pg=' + pg + '&uid=' + uid, function() {
			Messages._initForm();
			Messages._hideLoading();
		});
	},
	toggleContacts: function() {
		Messages._formWindowToggle('Contacts');
	},
	toggleSmileys: function() {
		Messages._formWindowToggle('Smileys');
	},
	toggleSkins: function() {
		Messages._formWindowToggle('Skins');
	},
	selectSkin: function(id) {
		$('#MsgFormSkins .skin.active').removeClass('active');
		$('#MsgFormBodyLine').attr('class', 'line  with_border');

		if ($('#MsgFormSkin').val() == id) {
			$('#MsgFormSkins .skin0').addClass('active');
			$('#MsgFormSkin').val(0);
		}
		else {
			$('#MsgFormSkins .skin' + id).addClass('active');
			$('#MsgFormSkin').val(id);
			$('#MsgFormBodyLine').addClass('pskin' + id);
		}
		
		$('#MsgFormBody').focus();
	},
	previewSkin: function(id) {
		var prefix = '';
		if (typeof(slng) != 'undefined' && slng != 'lv') {
			prefix = slng + '_';
		}
		
		InfoLite.open({
			text: '<img src="http://ifrype.com/img/messages/skins/' + id + '/' + prefix + 'screen.jpg" alt="" />'
		});
	},
	reloadSkins: function() {
		$('#MsgFormSkins').load(Messages.MESSAGES_ROOT + Messages.MESSAGES_FORM_SKINS, function(d) {
			Messages._initSkin(d.substring(0, 4));
		});
	},
	setElHover: function(v) {
		Messages.elHover = v;
	},
	
	acInit: function() {
		if ($('#MsgUserSelector').length > 0) {
			try {
				if( D.DEV ){
					var node = document.getElementById( 'MsgFormToLine' );
					clearNode( node );
					var selector = new D.UserSelector( {
						inputName: 'contacts_ids'
					} );
					selector.append( node );


					return;
				}
				var ac = new MultiAc({
					inputName: 'contacts_ids',
					onGetList: Draugiem.getACFriends,
					groups: true,
					value: {}
				});
				if (Messages.acPrefill){
					ac.value = Messages.acPrefill;
				}
				Messages.autoComplete = ac;
				Messages.autoComplete.append('MsgUserSelector');

				var fl = new FriendList( {
					multiAc: Messages.autoComplete,
					groups: true
				} );
				fl.append("MsgFormContacts");
				Messages.autoComplete.friendList = fl;
			}
			catch(e){}
		}
	},
	
	confirmDelete: function(el, page) {
		var go_on = true;
		var btn_id = $(el).parents('.newButton').attr('id');
		
		if (btn_id == 'MsgDeleteAllBtnHead' || btn_id == 'MsgDeleteAllBtnFoot') {
			if ($('#MsgListContainer input[name=msg_to_delete[]]:checked').length == 0) {
				go_on = false;
			}
		}
		
		if (go_on == true) {
			InfoLite.open({
				width: '600px',
				height: '160px',
				text: '<p class="icon"></p><p class="description">' + Messages._translate('confirm_delete_text') + '</p><div class="buttons"><table class="newButton" cellspacing="0"><tr><td width="13"><div class="el l"></div></td><td class="el m"><a href="javascript:Messages.confirmDeleteYes()"id="MsgConfirmDeleteYes">' + Messages._translate('delete') + '</a></td><td width="13"><div class="el r"></div></td></tr></table><table class="newButton newButtonLink" cellspacing="0"><tr><td width="13"><div class="el l"></div></td><td class="el m"><a href="javascript:Messages.confirmDeleteNo()">' + Messages._translate('cancel') + '</a></td><td width="13"><div class="el r"></div></td></tr></table></div><input type="hidden" id="ConfirmLink" value="' + el.href + '" /><input type="hidden" id="ConfirmPage" value="' + page + '" />'
			});

			$('#infobox3_tm').html('<h2>' + Messages._translate('confirm_delete_title') + '</h2>');
			
			$('#MsgConfirmDeleteYes').focus();
			$('#infobox3_closeButton').hide();
		}
		
		return false;
	},
	confirmDeleteYes: function() {
		var page = $('#infobox3 input#ConfirmPage').val();
		var link = $('#infobox3 input#ConfirmLink').val();
		
		if (link == 'undefined') {
		    $('#MsgListContainer form').remove();
		    $('#MsgListContainer').submit();
		}
		else {
			InfoBox.close();
			Messages._showLoading('deleting');

			$.get(link, function() {
				Messages._decreaseCount(page);
				Messages._redirect(link);
			});
		}
	},
	confirmDeleteNo: function() {
		InfoBox.close();
	},
	del: function(el, page) {
		Messages._showLoading('deleting');

		var link = el.href;
		$.get(link, function() {
			Messages._decreaseCount(page);
			Messages._redirect(link + '&redirect=false');
		});
		
		return false;
	},
	doFavbox: function(el, page) {
		var link = el.href;
		
		if ($(el).hasClass('msg_button')) {
			var button = $(el);
			var a = $(el).parents('tr').prev().find('td.msgt_fav a');
		}
		else {
			var button = $(el).parents('tr').next().find('a.favbox_btn');
			var a = $(el);
		}

		var fav_class = $(a).attr('class');
		
		if (fav_class == 'icn_off') {
			if ($(button).find('span').length > 0) {
				$(button).find('span').text(Messages._translate('remove_from_favourites'));
				$(button).attr('href', $(button).attr('href').replace(/add_favbox/, 'remove_favbox'));
			}
			$(a).attr('href', $(a).attr('href').replace(/add_favbox/, 'remove_favbox'));
			$(a).removeClass('icn_off').addClass('icn_on');

			link = link + '&redirect=false';
			$.get(link, function() {
				Messages._increaseCount('favbox');
			});
		}
		else {
			if ($(button).find('span').length > 0) {
				$(button).find('span').text(Messages._translate('add_to_favourites'));
				$(button).attr('href', $(button).attr('href').replace(/remove_favbox/, 'add_favbox'));
			}
			$(a).attr('href', $(a).attr('href').replace(/remove_favbox/, 'add_favbox'));
			$(a).removeClass('icn_on').addClass('icn_off');

			link = link + '&redirect=false';
			$.get(link, function() {
				Messages._decreaseCount('favbox');
			});
		}

		return false;
	},
	selectAll: function(sender) {
		if (typeof(sender) == 'undefined') {
			$('#MsgSelectAllHead').click();
			return false;
		}
		else {
			if ($(sender).attr('id') == 'MsgSelectAllHead') {
				var checked = $('#MsgSelectAllHead').attr('checked');
				$('#MsgSelectAllFoot').attr('checked', checked);
			}
			else {
				var checked = $('#MsgSelectAllFoot').attr('checked');
				$('#MsgSelectAllHead').attr('checked', checked);
			}
			
			$('#MsgTable .msgt_chb input[type=checkbox]').each(function(i, el) {
				$(el).attr('checked', checked);
			});
		}
	},
	showAll: function() {
	    $('#MsgTable .msgh').each(function(i, tr) {
	        $(tr).click();
	    });
	}
};

$(function() {
	Messages._init();
});