// namespace
window.LOV = window.LOV || {};
LOV.Sony = LOV.Sony || {};
LOV.Sony.Business = LOV.Sony.Business || {};

// shortcut
var sonyB = LOV.Sony.Business;

sonyB.SentToFriends = sonyB.SentToFriends || {};

sonyB.SentToFriends.isValid = true;

sonyB.SentToFriends.init = function() {
	$('#sendToFriendsClose').click(sonyB.SentToFriends.close);
	$('#sendToFriendsSend').click(sonyB.SentToFriends.submit);
	$('#sendToFriendsMore').click(sonyB.SentToFriends.moreFriends);
	$('#sendToFriendsForm').formValidation({
		required: 'accept',
		err_message: 'Atenção:\nAlguns campos foram preenchidos de maneira incorreta. Por favor, preencha novamente.\nObrigado.',
		err_list: false,
		on_error: sonyB.SentToFriends.invalidField
	});
	$('#sendToFriendsForm').submit(sonyB.SentToFriends.onSubmit);

	$('#footerCompartilhe').click(sonyB.SentToFriends.open);
};
$().ready(sonyB.SentToFriends.init);

sonyB.SentToFriends.onSubmit = function() {
	if (sonyB.SentToFriends.isValid) {
		$.ajax({
			type: 'POST',
			url: '/ajax/sendToFriends.aspx',
			data: '&name=' + $('#name').attr('value') +
				'&email=' + $('#email').attr('value') +
				'&nameFriend=' + $('#nameFriend').attr('value') +
				'&emailFriend=' + $('#emailFriend').attr('value') +
				'&link=' + $('#link').attr('value') +				
				'&message=' + $('#message').attr('value') || '',
			success: sonyB.SentToFriends.success,
			error: sonyB.SentToFriends.error
		});
	}
	return false;
};

sonyB.SentToFriends.submit = function() {
	sonyB.SentToFriends.isValid = true;
	$('#sendToFriendsForm').submit();
};

sonyB.SentToFriends.open = function() {
	$('#sendToFriends').show();
	if (typeof sIFR == 'function') {
		sIFR.replaceElement(named({sSelector: "#sendToFriends h2 span", sFlashSrc: "/swf/h2.swf", sWmode: "transparent", sColor: "#333333"}));
	}
	$('#sendToFriends input[@type=text]')[0].focus();
};

sonyB.SentToFriends.close = function() {
	$('#sendToFriends').hide();
	sonyB.SentToFriends.reset();
};

sonyB.SentToFriends.reset = function() {
	$('#sendToFriends textarea, #sendToFriends input[@type=text]').each(function(i) {
		this.value = '';
	});
};

sonyB.SentToFriends.success = function() {
	$('#sendToFriendsSubmit').hide();
	$('#sendToFriendsSuccess').show();
};

sonyB.SentToFriends.error = function() {
	alert('Problemas ao enviar a mensagem. Tente novamente mais tarde.');
};

sonyB.SentToFriends.moreFriends = function() {
	$('#dataFriend input[@type=text]').each(function(i) {
		this.value = '';
	});
	$('#dataFriend input[@type=text]')[0].focus();
	$('#sendToFriendsSuccess').hide();
	$('#sendToFriendsSubmit').show();
};

sonyB.SentToFriends.invalidField = function(pElm, pAction) {
	if (pAction) {
		sonyB.SentToFriends.isValid = false;
	}
};