$(document).ready( function()
	{
		init_prettyPhoto();
		init_focus_me();
		init_blink_me();
		init_signup_form();
		init_poll_toggle();
		init_flavor_selects();
		init_truth_anim();
		init_overlay_icons();
		init_customlabel_types();
		deobfuscate_emails();
	}
);


function init_prettyPhoto()
{
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
}


function init_focus_me()
{
	if($('input.focus_me'))
	{
		$('input.focus_me').focus();
	}
}

function init_blink_me()
{
	$(".blink_me").fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300);
}


function deobfuscate_emails()
{
	$(".replaceAt").replaceWith("@");
	$(".obfuscate").each(function(i){
		this.href = "mailto:" + this.innerHTML;
	});
}


function init_signup_form()
{
	signup_form = $("div#signup").html();
	$("#signup_form #submit").click(function(){
		$("div#response").html('<p><span class="signup_red">Sending email, please wait...</span></p>');
		$.ajax({
			type: "POST",
			url:  "/signup",
			data: "email=" + $("input#email").val(),
			success: function(result){
				// The "result" variable should contain the email address if successful, "error" or "blank" if not.
				if (result == "error") {
					$("div#response").html('<p><span class="signup_red">There was an error submitting that email address.</span></p>').show();
					$("div#signup").html(signup_form);
				} else if (result == "blank") {
					$("div#response").html('<p><span class="signup_red">Oops! Please enter an email address.</span></p>');
					$("div#signup").html(signup_form);
				} else {
					$("div#response").html('<p><span class="signup_green">Your email has been submitted. Thanks!</span></p>');
					$("div#signup").html('');
				}
				initSignupForm();
			}
		});
		return false;
	});
}


function init_poll_toggle()
{
	$('.poll_toggle').click(function(){
		$('#voting').toggleClass('poll_hidden');
		$('#score').toggleClass('poll_hidden');
		return false;
	});
}


function init_flavor_selects()
{
	$('select#flavor_choices').change(function(){
		if ($('select#flavor_' + this.value).length) {
			// Selected a smaller number, must remove extra selects
			for (i=1; i<=7; i++) {
				if (i > this.value && $('select#flavor_' + i).length) {
					$('select#flavor_'+i).remove();
				}
			}
		} else {
			// Selected a larger number, must add more selects
			for (i=1; i<=this.value; i++) {
				if (!$('select#flavor_' + i).length) {
					$('select#flavor_1').clone().attr('id', 'flavor_'+i).attr('name', 'flavor_'+i).attr('selected','').appendTo('div#flavor_selects');
				}
			}
		}
	});
}


function init_truth_anim()
{
	$('p.truth').cycle('fade');
}


function init_overlay_icons()
{
	$('a.popout_link').append('<img src="/images/icon_popout.png" class="overlay_icon" />');
	$('a.lightbox_link').append('<img src="/images/icon_lightbox.png" class="overlay_icon" />');
}


function init_customlabel_types()
{
	var type = $('input.top_type:checked').val();
	if (type == 'logo') {
		$('div#top_message').hide();
	} else {
		$('div#top_logo').hide();
	}
	
	$('input#radio_logo').click(function(){
		$('div#top_logo').show();
		$('div#top_message').hide();
	});
	
	$('input#radio_message').click(function(){
		$('div#top_logo').hide();
		$('div#top_message').show();
	});
}


function copyAddress()
{
	$("#scn").val($("#bcn").val());
	$("#sfn").val($("#bfn").val());
	$("#sln").val($("#bln").val());
	$("#sa1").val($("#ba1").val());
	$("#sa2").val($("#ba2").val());
	$("#sct").val($("#bct").val());
	$("#sst").val($("#bst").val());
	$("#szp").val($("#bzp").val());
}






