var default_default = '* required';
var default_spam = 'this is for spam reasons';
var error_name = 'please enter a name';
var error_email = 'please enter a valid email';
var error_spam = 'wrong answer';

$(document).ready(function(){
	extend();
	cufon();
	topLinks();
	newWindow();
	projects();
	contactForm();
});

this.extend = function(){
	$.extend($.expr[':'],{
		external: function(a,i){
			if(!a.href) {return false;}
			return a.hostname && a.hostname !== window.location.hostname;
		}
	});
}

this.cufon = function(){
	Cufon.replace('h2', {
		textShadow: '0 1px 0 #fff',
		hover: true
	});
}

this.topLinks = function(){
	$('a#top_link').click(function(){
		$('html, body').animate({scrollTop: 0}, 'normal');
		return false;
	});
}

this.newWindow = function(){
	$('a:external').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
}

this.projects = function(){
	var count = 0;
	
	$('#project .container_12 > .grid_9 img, #project .container_12 > .grid_4 img').each(function(i){
		count = i;
	});
	
	if(count > 0)
		projectPageSetup(count);
}

this.projectPageSetup = function(numImgs){
	$('#project .container_12 > .grid_9 img, #project .container_12 > .grid_4 img').hide();
	$('#project .container_12 > .grid_9 img:first-child, #project .container_12 > .grid_4 img:first-child').show();
	$('#project #project_info').append('<strong><a href="#prev">Previous Image</a></strong> || <strong><a href="#next">Next Image</a></strong><br /><em>(image <span id="on_img">1</span> of ' + (numImgs + 1) + ')</em>');
	
	projectNav(numImgs + 1);
}

this.projectNav = function(imgs){
	$('a[href="#prev"], a[href="#next"]').click(function(){
		var hash = $(this).attr('href').replace('#', '');
		var on = parseInt($('#on_img').text());
		
		switch(hash)
		{
			case 'prev':
				if(on === 1)
					on = imgs - 1;
				else
					on = on - 2;
				
				break;
			case 'next':
				if(on === imgs)
					on = 0;
				
				break;
		}

		$('.grid_9 img:visible, .grid_4 img:visible').hide();
		$('img:eq(' + on + ')').show();
		$('#on_img').text(on + 1);
		
		return false;
	});
}

this.contactForm = function(){
	$('form #name').focus();
	
	$('form #name').keyup(function(){
		validateName();
	});
	
	$('form #email').keyup(function(){
		validateEmail();
	});
	
	$('form #nospam').keyup(function(){
		validateSpam();
	});
	
	$('form input[type="submit"]').click(function(){
		resetFormLabels();
		validateName();
		validateEmail();
		validateSpam();
		
		if(!$('form #name').hasClass('not_valid') && !$('form #email').hasClass('not_valid') && !$('form #nospam').hasClass('not_valid'))
		{
			$('form').submit();
		}
		else
		{
			return false;
		}
	});
}

this.resetFormLabels = function(){
	$('#name_error_label, #email_error_label').removeClass('red').text(default_default);
	$('#spam_error_label').removeClass('red').text(default_spam);
}

this.validateName = function(){
	$('#name_error_label').removeClass('red').text(default_default);

	$('form #name').removeClass('not_valid').filter(function(){
		return this.value === '';
	}).addClass('not_valid');
	
	if($('form #name').hasClass('not_valid'))
	{
		$('#name_error_label').addClass('red').text(error_name);
	}
}

this.validateEmail = function(){
	$('#email_error_label').removeClass('red').text(default_default);

	$('form #email').removeClass('not_valid').filter(function(){
		return !this.value.match(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/);
	}).addClass('not_valid');
	
	if($('form #email').hasClass('not_valid'))
	{
		$('#email_error_label').addClass('red').text(error_email);
	}
}

this.validateSpam = function(){
	$('#spam_error_label').removeClass('red').text(default_spam);

	$('form #nospam').removeClass('not_valid').filter(function(){
		return this.value !== '7';
	}).addClass('not_valid');
	
	if($('form #nospam').hasClass('not_valid'))
	{
		$('#spam_error_label').addClass('red').text(error_spam);
	}
}
