$(function() {
	// this is dumb but I'm sneaking the contact form submission in here for now.
	$("#submit").click(function() {
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var emailVal = $("#field-email").val();
		if(emailVal == '') {
			$("#field-email").after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("#field-email").after('<span class="error">Please enter a valid email address to send.</span>');
			hasError = true;
		}
		
		var subjectVal = $("#field-subject").val();
		if(subjectVal == '') {
			$("#subject").after('<span class="error">Please enter a subject.</span>');
			hasError = true;
		}
		
		var messageVal = $("#field-message").val();
		if(messageVal == '') {
			$("#message").after('<span class="error">Please enter a message.</span>');
			hasError = true;
		}
		
		if(hasError == false) {
			$(this).hide();
			$("form fieldset").append('<img src="http://caseybritt.com/cms/site/themes/caseybritt/images/loading.gif" alt="Loading" id="loading" />');
			
			$.post("http://caseybritt.com/sendemail.php",
   				{ emailTo: 'contact@caseybritt.com', emailFrom: emailVal, subject: subjectVal, message: messageVal },
   					function(data){
						$("form fieldset").animate({height: 1}, 1000, function() {
							$("form").html('<h3 class="top-20">Success</h3><p>Your email was sent.</p>');
						});
   					}
				 );
		}
		
		
		
		
		
		return false;
	});
	
	
	heightper = 40;
	// set to position absolute
	$('.itemstack').css('position', 'absolute');
	
	// create array of stackitems
	stackArray = new Array();
	inArray = new Array();
	outArray = new Array();
	$('.itemstack').each(function() {
		stackArray.push($(this).attr('id'));
		// $(this).css('top', i*50);
		// i++;
	});
	
	// position each of the items
	for (var i in stackArray) {
		$('#'+stackArray[i]).css('top', i*heightper);
	}
	
	// set the height of the container
	$('#work-list').height($('.itemstack').size() * heightper);
	
	$('.linkstack').click(function(e) {
		var meh = $('.' + $(this).text().toLowerCase()).size();
		
		inArray.length = 0;
		outArray.length = 0;
		
		for (var i in stackArray) {
			// if current has clicked class add to inArray
			// else add to outArray
			if ($('#'+stackArray[i]).hasClass($(this).text().toLowerCase())) {
				inArray.push(stackArray[i]);
				$('#'+stackArray[i]).css('opacity', 1);
			} else {
				outArray.push(stackArray[i]);
				$('#'+stackArray[i]).css('opacity', .7);
			}
		}
		
		stackArray = inArray.concat(outArray);
		
		for (var i in stackArray) {
			$('#'+stackArray[i]).animate({top:+i*heightper});
		}
		
		// alert(meh);
		e.preventDefault();
	});
});