/* Javascript Document for 1944. Please do not steal and use for personal use without giving credit first. */

// Fire when ready
$(document).ready(function() {
	var keys = [];
	var konami = '38,38,40,40,37,39,37,39,66,65';
	
	$(document).keydown(
		function(e) {
			keys.push( e.keyCode );
			if ( keys.toString().indexOf( konami ) >= 0 ) {
				setTimeout(function() { $('div.all').css("visibility", "visible"); }, 0);
				setTimeout(function() { $('div.pipe').animate({marginBottom: '128px'}, 400); }, 0);
				setTimeout(function() { $('div.mario').animate({height: '171px', width: '130px', marginTop: '-400px'}, 600); }, 400);
				setTimeout(function() { $('div.mario').fadeOut(500); }, 800);
				setTimeout(function() { $('div.pipe').fadeOut(500); }, 1700);
				setTimeout(function() { $('div.all').css("visibility", "hidden"); }, 2200);
				setTimeout(function() { keys = []; }, 2000);
			}
		}
	);

	$("#backtotop").fadeOut(300);
	$('.email_fix').map(email_fix);
    
    $('#backtotop').bind('click', function(e){
        $('html, body').animate({ scrollTop: 0}, 500, 'easeInOutQuad');
    });
	
	window.onscroll = function(){
	var activate = $("#header").position().top
		if (activate >= 266) {
			$("#backtotop").fadeIn(400);
		} else {
			$("#backtotop").fadeOut(300);
		}
	}
	return false;
});

function email_fix () {
	$(this).html('<a href="mailto:'+$(this).html().replace(' [ at ] ','@').replace('/','').replace(':','.')+'" title="Email '+$(this).html().replace(' [ at ] ','@').replace('/','')+'">'+$(this).html().replace(' [ at ] ','@').replace('/','')+'</a>');	
}

function check_comment() {
	// Form data
	var name = $('#author').val();
	var email = $('#email').val();
	var web = $('#url').val();
	var text = $('#comment').val();
	var logged_in = $('#logged_in_status').val();
	var ret = '';
	
	if (logged_in !='true') {
		if (name.length < 2 || name=='Enter your name') {
			ret += '<p>- You forgot to enter your name.</p>';
		}
		
		if (email.length < 2 || email=='Enter your email') {
				ret += '<p>- You\'ll need to enter your email address.</p>';
		} else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(email)) {
			ret += '<p>- You\'ll need to enter a valid email address.</p>';
		}
	}
	
	if(text.length < 2 || text=='Enter your comment') {
		ret += '<p>- You forgot to enter your comment.</p>';
	}
	
	if(ret != '') {
		$('#blog_comment_response').html('<p>Oops, we found some problems with your comment.</p>' + ret);
        $('#blog_comment_response_wrapper').animate({height: $('#blog_comment_response').get(0).offsetHeight + 'px'}, 500, 'easeInOutQuad', function(){});
		return false;
	} else {
		if (web=='Enter your website') {
			$('#url').val("");
		}
		return true;
	}
	
}
