/// <reference path="jquery-1.4.1.js" />
/// <reference path="validator.js" />

$(function () {
	$('#footer ul li a').stop(true, true).hover(function () {
		$(this).animate({
			bottom: '0px'
		}, 150);
	}, function () {
		$(this).stop(true, true).animate({
			bottom: '-8px'
		}, 150);
	});

	$('.dados-footer .bt-receba-novidades').stop(true, true).click(function () {
		$('#footer .receba-novidades').animate({
			bottom: '0px'
		}, 350);
	});

	$('#txtEmailNews').setLabelValue({
		label: 'Email'
	});

	$('#newsletter_ok').click(function () {
		var email = $('#txtEmailNews').val();
		$('#txtEmailNews').removeClass('erro');
		if (!validations.isEmail(email)) {
			$('#txtEmailNews').addClass('erro');
			return false;
		}
		var jsonText = JSON.stringify({ email: email });

		$.ajax({
			type: "POST",
			url: appPath + "contato.aspx/SaveNewsletter",
			contentType: "application/json; charset=utf-8",
			data: jsonText,
			dataType: "json",
			beforeSend: function () {

			},
			success: function (data, textStatus, jqXHR) {
				tracker('/novidades-dzarm/cadastro');
				$('#footer .receba-novidades').animate({
					bottom: '-35px'
				}, 350, function () {
					$('#txtEmailNews').val('');
					$('#footer .receba-novidades-enviado').animate({
						bottom: '0px'
					}, 350, function () {
						setTimeout(function () {
							$('#footer .receba-novidades-enviado').animate({
								bottom: '-35px'
							}, 350);
						}, 750);
					});
				});
			},
			error: function (jqXHR, textStatus, errorThrown) {
				alert(jqXHR.responseText);
			}
		});
	});
});

jQuery.fn.setLabelValue = function () {
	var args = arguments[0] || {}; // It's your object of arguments
	var o = $(this[0]);

	o.val(args.label).data("init", args.label);
	o.focus(function () {
		if ($(this).val() == $(this).data("init")) {
			$(this).val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).data("init"));
		}
	});
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i = 0; i < string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum, rnum + 1);
	}
	return randomstring;
}
