pageTracker = null;

function awesome(textboxes)  
{
	textboxes.focus(function(){
		if (!this.value) {
			$(this).prev().addClass('focus');
		}
	});
	textboxes.blur(function(){
		if (!this.value) {
			$(this).prev().removeClass('focus');
			$(this).prev().removeClass('hastext');
		}
	});
	textboxes.keypress(function(){
		$(this).prev().addClass('hastext');
	});
}


function makeRequest(url, type, callback, method, data)
{
	var params = {url: url, success: callback};
	if (method) {
		params['type'] = method;
	}
	if (type) {
		params['dataType'] = type;
	}
	if (data) {
		params['data'] = data;
	}
	$.ajax(params);
}


function beforeSend(formData, form, options)
{
	var data = {
		email: form[0].email.value
	};			
	makeRequest("/index/notify", 'json', afterSend, 'POST', data);
	return false;
}


function afterSend(response)
{
	if (response.data.html) {
		$("#notifier").html(response.data.html);
		awesome($("#notify-form input[type=text]"));
		$("#notify-form").ajaxForm({
    		beforeSubmit: beforeSend,
    		dataType: 'json'
    	});
		if (pageTracker && response.data.saved) {
			try {
				pageTracker._trackPageview("index/notify/saved");
			} catch(err) {}
		}
	}
}


$(document).ready(function() {
	$.ajaxSetup({
		cache: false
	});

	$("#notify").click(function () {
		makeRequest('/index/notify', 'json', function (response) {
			if (response.data.html) {
				$("#notifier").html(response.data.html);
				awesome($("#notify-form input[type=text]"));
				$("#notify-form").ajaxForm({
	        		beforeSubmit: beforeSend,
	        		dataType: 'json'
	        	});
			}
		});
		return false;
	});
});
