var inits = inits || {};
inits.friends = {};
inits.newinvite = {};

inits.friends.newinvite = function() {
	$('.actionbuttons button:first').click(function(e) {
		e.stopPropagation();
		e.preventDefault();
		var mailList = $('#txinvitenewfriends').val();
		if (!mailList.match(/\@/gim)) {
			alert('Informe pelo menos um endereço de e-mail válido.');
			return false;
		}
		mailList = mailList.replace(/ /gim,'').replace(/;/gim,'\n').replace(/\,/gim,'\n'); //'
		$(this).attr("disabled","disabled");
		
		var tmpGroupList = [];
		
		$('#groupList input:checked').each(function () {
			if ($(this).attr('id') != 'newGroup') {
				tmpGroupList.push($('#name_'+$(this).attr('id')).html());
			} else {
				tmpGroupList.push($('#newGroupName').val());
			}
		});
		var groupList = '';
		if (tmpGroupList.length > 0) {
			groupList = tmpGroupList.join(',');
		}
		var postParams = {"subject":"UOL Mais - Novo pedido de amizade","emailList":mailList,"group":groupList};
		utils.showThickBox('blank.vm','Enviando convites',530,140);
		
		$.ajax({
			type: "POST",
			url: "/ajax/inviteFriendByEmail.html",
			data: postParams,
			dataType: "json",
			success: function(data){
				if (data.result == true) {
					utils.showTbMessage('Convite enviado com sucesso.','ok');
					$('#txinvitenewfriends').val('');
				} else {
					utils.showTbMessage('Houve um erro ao enviar os convites.','error');
				}
				$('.actionbuttons button:first').attr("disabled",false);
			}
		});
	});
	
	$('#newGroupName').focus(function(e) {
		e.stopPropagation();
		e.preventDefault();
		if ($(this).hasClass('notModified')) {
			$(this).val('').removeClass('notModified');
			$('#newGroup').attr('checked','checked');
		}
	});
	$('#newGroupName').blur(function(e) {
		e.stopPropagation();
		e.preventDefault();
		if ($(this).val() == '') {
			$(this).val('nome do grupo').addClass('notModified');
			$('#newGroup').attr('checked',false);
		}
	});
}