$.extend($.expr[':'],{
    inView: function(a) {
        var st = (document.documentElement.scrollTop || document.body.scrollTop),
            ot = $(a).offset().top,
            wh = (window.innerHeight && window.innerHeight < $(window).height()) ? window.innerHeight : $(window).height();
        return ot > st && ($(a).height() + ot) < (st + wh);
    }
});
$('#servicesList li').hover(function() {
	$(this).stop(true, false).animate({
		opacity: 1
	}, 200);
}, function() {
	$(this).stop(true, false).animate({
		opacity: 0.7
	}, 200);
});
$('.colorScreenshotHover').hide();
$('.singleItem').hover(function() {
	$(this).find('.colorScreenshotHover').stop(true, true).fadeIn('slow');
}, function() {
	$(this).find('.colorScreenshotHover').stop(true, true).fadeOut('slow');
});
$('.siteScreenshot, .siteTitleCont').click(function() {
	if($(this).parent().find('.itemDescription').is(":visible")) {
		$(this).parent().find('.itemDescription').slideUp('slow');
		$(this).parent().find('.moreButton').removeClass('closeButton');
		$(this).parent().find('.siteTitleCont').removeClass('siteTitleContActive');
		$(this).parent().find('.siteTitle').removeClass('siteTitleActive');
		$(this).parent().find('.colorScreenshot')
			.addClass('colorScreenshotHover')
			.stop(true, true).fadeOut('slow');
	} else if($(this).parent().find('.itemDescription').is(":hidden")) {
		$(this).parent().nextAll().find('.itemDescription').slideUp('slow');
		$(this).parent().nextAll().find('.moreButton').removeClass('closeButton');
		$(this).parent().nextAll().find('.siteTitleCont').removeClass('siteTitleContActive');
		$(this).parent().nextAll().find('.siteTitle').removeClass('siteTitleActive');
		$(this).parent().nextAll().find('.greyScreenshot').addClass('greyScreenshotHover').stop(true, true).fadeIn('slow');
		$(this).parent().nextAll().find('.colorScreenshot').addClass('colorScreenshotHover').stop(true, true).fadeOut('slow');
		$(this).parent().prevAll().find('.itemDescription').slideUp('slow');
		$(this).parent().prevAll().find('.moreButton').removeClass('closeButton');
		$(this).parent().prevAll().find('.siteTitleCont').removeClass('siteTitleContActive');
		$(this).parent().prevAll().find('.siteTitle').removeClass('siteTitleActive');
		$(this).parent().prevAll().find('.greyScreenshot').addClass('greyScreenshotHover').stop(true, true).fadeIn('slow');
		$(this).parent().prevAll().find('.colorScreenshot').addClass('colorScreenshotHover').stop(true, true).fadeOut('slow');
		$(this).parent().find('.itemDescription').slideDown('slow');
		$(this).parent().find('.moreButton').addClass('closeButton');
		$(this).parent().find('.siteTitleCont').addClass('siteTitleContActive');
		$(this).parent().find('.siteTitle').addClass('siteTitleActive');
		if ($(this).parent().find('.screenShots').is(':not(:inView)')) {
			$('html,body').animate({ 
				 scrollTop: $(this).parent().parent().offset().top 
			}, 'slow');
		}
		$(this).parent().find('.colorScreenshot')
			.removeClass('colorScreenshotHover')
			.stop(true, true).fadeIn('slow');
	}
	return false;
});

$('#submit').click(function() {
	$('.success, .error').remove();
	var formData = {
		'name': $('[name=name]').val(),
		'email': $('[name=email]').val(),
		'subject': $('[name=subject]').val(),
		'message': $('[name=message]').val()
	};
	$.post('contact.php', formData, function(data) {
		if (data && data.success) {
			alert('Thank you for filling out this form, your submission was received.');
			$('#contact')[0].reset();
			$('#name, #email, #subject, #message').removeClass('fieldError');
		}
		else if (data && data.error) {
			if(data.error == 'validation') {
				alert('Please fill in the the required fields with valid information.');
				$.each(data.errors, function(key, error) {
					$('#'+error).addClass('fieldError');
				});
			} else {
				alert(data.error);
				$('#name, #email, #subject, #message').removeClass('fieldError');
			}
		}
	}, 'json');
	return false;
});
$('nav ul li a, #landingButtons li a, .introLink').click(function() {
   var elementClicked = $(this).attr("href");
   var destination = $(elementClicked).offset().top;
   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 500 );
   return false;
});
if($(document).scrollTop() < 700) {
	$('#welcomeLink').addClass('currentPage').prevAll().removeClass('currentPage');
	$('#welcomeLink').nextAll().removeClass('currentPage');
} else if($(document).scrollTop() > 700 && $(document).scrollTop() < 1400) {
	$('#servicesLink').addClass('currentPage').prevAll().removeClass('currentPage');
	$('#servicesLink').nextAll().removeClass('currentPage');
} else if($(document).scrollTop() > 1400 && $(document).scrollTop() < 2100) {
	$('#workLink').addClass('currentPage').prevAll().removeClass('currentPage');
	$('#workLink').nextAll().removeClass('currentPage');
} else if($(document).scrollTop() > 2100 && $(document).scrollTop() < 2900) {
	$('#contactLink').addClass('currentPage').prevAll().removeClass('currentPage');
	$('#contactLink').nextAll().removeClass('currentPage');
} else {
	$('nav ul li').removeClass('currentPage');
}
$(window).scroll(function() {
	if($(document).scrollTop() < 700) {
		if (!$('#welcomeLink').hasClass('currentPage')) {
			$('#welcomeLink').addClass('currentPage').prevAll().removeClass('currentPage');
			$('#welcomeLink').nextAll().removeClass('currentPage');
		}
	} else if($(document).scrollTop() > 700 && $(document).scrollTop() < 1400) {
		if (!$('#servicesLink').hasClass('currentPage')) {
			$('#servicesLink').addClass('currentPage').prevAll().removeClass('currentPage');
			$('#servicesLink').nextAll().removeClass('currentPage');
		}
	} else if($(document).scrollTop() > 1400 && $(document).scrollTop() < 2100) {
		if (!$('#workLink').hasClass('currentPage')) {
			$('#workLink').addClass('currentPage').prevAll().removeClass('currentPage');
			$('#workLink').nextAll().removeClass('currentPage');
		}
	} else if($(document).scrollTop() > 2100 && $(document).scrollTop() < 2900) {
		if (!$('#contactLink').hasClass('currentPage')) {
			$('#contactLink').addClass('currentPage').prevAll().removeClass('currentPage');
			$('#contactLink').nextAll().removeClass('currentPage');
		}
	} else {
		$('nav ul li').removeClass('currentPage');
	}
});
$('.singleItem').hover(function() {
	$(this).find('.siteTitle').addClass('siteTitleHover');
}, function() {
	$(this).find('.siteTitle').removeClass('siteTitleHover');
});
