$(document).ready(function(){
	// Make the default text in the email popup and form show and hide nicely:
	$('#email_signup input[name=Email Address]').focus(function(){
		if( $('#email_signup input[name=Email Address]').val() == 'Email Address' ){
			$('#email_signup input[name=Email Address]').val('');
		}
	});

	$('#email_signup input[name=Email Address]').blur(function(){
		if( $('#email_signup input[name=Email Address]').val() == '' ){
			$('#email_signup input[name=Email Address]').val('Email Address');
		}
	});

	// Make the default text in the name popup and form show and hide nicely:
	$('#email_signup input[name=Full Name]').focus(function(){
		if( $('#email_signup input[name=Full Name]').val() == 'Full Name' ){
			$('#email_signup input[name=Full Name]').val('');
		}
	});

	$('#email_signup input[name=Full Name]').blur(function(){
		if( $('#email_signup input[name=Full Name]').val() == '' ){
			$('#email_signup input[name=Full Name]').val('Full Name');
		}
	});

	// Make the default text in the email popup and form show and hide nicely:
	$('#module input[name=Email Address]').focus(function(){
		if( $('#module input[name=Email Address]').val() == 'Email Address' ){
			$('#module input[name=Email Address]').val('');
		}
	});

	$('#module input[name=Email Address]').blur(function(){
		if( $('#module input[name=Email Address]').val() == '' ){
			$('#module input[name=Email Address]').val('Email Address');
		}
	});

	// Make the default text in the name popup and form show and hide nicely:
	$('#module input[name=Full Name]').focus(function(){
		if( $('#module input[name=Full Name]').val() == 'Full Name' ){
			$('#module input[name=Full Name]').val('');
		}
	});

	$('#module input[name=Full Name]').blur(function(){
		if( $('#module input[name=Full Name]').val() == '' ){
			$('#module input[name=Full Name]').val('Full Name');
		}
	});


	// Set up the cookies and interstitial:
	if( undefined == $.cookie('views') ){
		// Set the cookie:
		// Initialize views to 0, even though it was viewed once
		$.cookie('views', '0', { expires: 3, path: '/', domain: 'fanimation.com' });
	} else{	
		// Increment views until we reach 16
		if(parseInt($.cookie('views'), 10) < 16){
			// Increment the cookie:
			$.cookie('views', (parseInt($.cookie('views'), 10) + 1), { expires: 3, path: '/', domain: 'fanimation.com' });
		}
		//When we reach 16 views and we haven't clicked nothanks (or signed up, which also sets the cookie to nothanks for some reason)
		//Reset the views to 0
		if(parseInt($.cookie('views'), 10) >= 16 && $.cookie('action') != 'nothanks'){
			$.cookie('views', 0, { expires: 3, path: '/', domain: 'fanimation.com' });
		}

		// Determine if we should show the interstitial:
		// Show after 3 views (0 based)
		if(parseInt($.cookie('views'), 10) === 2){
			// Show the interstitial:
			var interstitial = $('#interstitial').dialog({
				dialogClass: "module",
				draggable: false,
				height: '375',
				modal: true,
				resizable: false,
				width: '348',
				open: function() {
					try {
						pageTracker._trackEvent("Interstitial", "Show");
					} catch(err) {}

					$('.ui-widget-overlay').click(function() {
						$(interstitial).dialog("close");
						//Set views to 3 for some reason
						$.cookie('views', 3, { expires: 3, path: '/', domain: 'fanimation.com' });
						$.cookie('action', 'clickbehind', { expires: 3, path: '/', domain: 'fanimation.com' });
						try {
							pageTracker._trackEvent("Interstitial", "Click Behind");
						} catch(err) {}
						return false;
					});
					$(".module .close_modal").click(function() {
						$(interstitial).dialog("close");
						//Set views to 3 for some reason
						$.cookie('views', 3, { expires: 30, path: '/', domain: 'fanimation.com' });
						$.cookie('action', 'nothanks', { expires: 30, path: '/', domain: 'fanimation.com' });
						try {
							pageTracker._trackEvent("Interstitial", "No Thanks");
						} catch(err) {}
						return false;
					});
					$(".module .dismiss_modal").click(function() {
						$(interstitial).dialog("close");
						//Set views to 3 for some reason
						$.cookie('views', 3, { expires: 3, path: '/', domain: 'fanimation.com' });
						$.cookie('action', 'maybelater', { expires: 3, path: '/', domain: 'fanimation.com' });
						try {
							pageTracker._trackEvent("Interstitial", "Maybe Later");
						} catch(err) {}
						return false;
					});
					$("#interstitial .exit_modal").click(function() {
						$(interstitial).dialog("close");
						//Set views to 3 for some reason
						$.cookie('views', 3, { expires: 3, path: '/', domain: 'fanimation.com' });
						$.cookie('action', 'close', { expires: 3, path: '/', domain: 'fanimation.com' });
						try {
							pageTracker._trackEvent("Interstitial", "Maybe Later");
						} catch(err) {}
						return false;
					});
				}
				
			});
		}

	}
});

