function beta_email_submit(type)
{
	var email = $('#beta-email-'+type+' input#email').val();
	if ((email == '') || (email == undefined))
	{
		alert('Please enter your email address.');
	}
	else
	{
		$('#beta-email-submit-'+type).attr('disabled', 'disabled');
		$('#ajax_loader_bar_'+type).html(get_ajax_loader_bar());
		$('#ajax_loader_bar_'+type).fadeIn('fast');
		
		var data = 'email='+email;
		
		if (type == 'teacher')
			data+= '&is_teacher=1';
		
		$.ajax({
			url: '/api/post/beta_emails/submit',
			type: 'POST',
			data: data,		
			dataType: 'json',
			cache: false,
			success: function (json) {
				$('#ajax_loader_bar_'+type).fadeOut('fast', function() {
					if (json.success)
					{
						alert('Thanks! Your email address has been recorded.');
					}
					else
					{
						var errors = '';
						for (var i = 0; i < json.errors.length; i++)
							errors+= json.errors[i]+"\n"; 
						alert(errors);
					}
				});
			},
			// typically only one of textStatus or errorThrown will have info
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$('#ajax_loader_bar_'+type).fadeOut('fast');
				$('#beta-email-submit-'+type).removeAttr('disabled');
				if (textStatus)
					alert(textStatus);
				if (errorThrown)
					alert(errorThrown);
			},
			complete: function (XMLHttpRequest, textStatus) {
				$('#ajax_loader_bar_'+type).fadeOut('fast');
				$('#beta-email-submit-'+type).removeAttr('disabled');
			}
		});
	}
}

function rotate_tabs()
{
	if (do_rotate === true)
	{
		rotate_tab('tab-link-'+tab_num);
		if (tab_num < 6)
			tab_num++;
		else
			tab_num = 1;
	}
}

function rotate_tab(id)
{
	if (id)
	{
		var id_parts = id.split('-');
		if (typeof(id_parts[2]) != 'undefined')
		{
			// set all splash panels to 100
			for (var i = 1; i <= 6; i++)
				$('#splash-'+i).css('z-index', 100);
			// set the clicked splash panel to 101
			$('#splash-'+id_parts[2]).css('z-index', 101);
			// update the tab_num var to start the auto-rotation here
			tab_num = id_parts[2];
		}
	}
}

var tab_num = 1;
var do_rotate = true;

$(document).ready(function() {

	$('.tab').click(function(event) {
		var id = event.target.id;
		rotate_tab(id);
	});
	
	$('#home-tabs-row').mouseover(function() {
		do_rotate = false;
	});
	$('#home-tabs-row').mouseout(function() {
		do_rotate = true;
	});
	
	setInterval('rotate_tabs()', 5000);
	
	$('#beta-email-player').submit(function() {
		beta_email_submit('player');
		return false;
	});  

	$('#beta-email-teacher').submit(function() {
		beta_email_submit('teacher');
		return false;
	});  

});
