(function($){
	$.fn.animateColor = function(options){
		return this.each(function(){
			$(this).mouseover(function(){
				$(this).stop();
				$(this).animate({color:options.overColor},options.time);
			});
			$(this).mouseout(function(){
				$(this).stop();
				$(this).animate({color:options.outColor},options.time);
			});
		});
	}
})(jQuery)

jQuery(document).ready(function(){
	var wrapperWidth = $('#wrapper').width();
	var headerImageWidth = $('#header-image').width();

	// webkit hack for image dimensions
	if(headerImageWidth === 0){
			headerImageWidth = 1168;
	}
	// vertically center post-info content.
	var height;
	$('.post-info').each(function(){
		height = 0;
		$(this).children('p').each(function(){
			height += $(this).height();
		});
		$(this).children('.title').css('marginTop',parseInt((($('.post-info').height() / 2) - (height / 2)),10).toString() + 'px');
	});

	// complete all white lines to the left, and set up width change on window resize.
	var leftOffset = parseInt($('#wrapper').offset().left,10);
	var whiteSpaceWidth = 154;
	$('.white-space').each(function(){
		$(this).css({'right':leftOffset.toString() + 'px', 'width':(leftOffset + whiteSpaceWidth).toString() + 'px'});
	});
	// horizontally center fixed header logo
	$('#header-image').css('left',(leftOffset + ((wrapperWidth / 2) - (headerImageWidth / 2))).toString() + 'px');
	/*
	console.log('leftOffset: ' + leftOffset);
	console.log('wrapperWidth: ' + wrapperWidth);
	console.log('headerImageWidth: ' + headerImageWidth);
	*/
	$(window).resize(function(){
		leftOffset = parseInt($('#wrapper').offset().left,10);
		$('.white-space').each(function(){
			$(this).css({'right':leftOffset.toString() + 'px', 'width':(leftOffset + whiteSpaceWidth).toString() + 'px'});
		});
		// horizontally center fixed header logo
		$('#header-image').css('left',(leftOffset + ((wrapperWidth / 2) - (headerImageWidth / 2))).toString() + 'px');
	});

	$('a .post-image').mouseover(function(){
		$(this).stop();
		$(this).fadeTo(200, 0.1);
	});
	$('a .post-image').mouseout(function(){
		$(this).stop();
		$(this).fadeTo(200, 1);
	});
	
	$('#home-and-back').children('a').add('#email-link').animateColor({
		overColor:'#FFFFFF',
		outColor:'#A4A6A9',
		time:300
	});
	$('#send-button').animateColor({
		overColor:'#999999',
		outColor:'#5B5C63',
		time:300
	});

	$('.post-info-map').mouseover(function(){
		$(this).prevAll('.post-info-background').fadeOut(1);
	});
	$('.post-info-map').mouseout(function(){
		$(this).prevAll('.post-info-background').fadeIn(1);
	});
	$('.post-info-link').mouseover(function(){
		$(this).prevAll('.post-info-background').stop();
		$(this).prevAll('.post-info-background').css('display','none');
		$(this).prevAll('.post-info-background').css('opacity','1');
	});
	$('.post-info-link').mouseout(function(){
		$(this).prevAll('.post-info-background').stop();
		$(this).prevAll('.post-info-background').fadeIn(1);
	});
	$('.post-info > img, .post-info-link').click(function(){
		window.location = $(this).parent().prevAll('.image-link').attr('href');
	});
});



var onContactFormSuccess = function(data){
	if(data == 'error'){
		onContactFormError();
	}
	else{
		$('#contact-message').children('p').fadeOut('slow', function(){
			$(this).html(
				'your message<br />was sent successfully<br />'
			);
			$(this).css('marginTop','60px');
			$(this).fadeIn('slow');
		});
	}
}

var onContactFormError = function(){
	$('#contact-message').children('p').fadeOut('slow', function(){
		$(this).html(
			'there was an error<br />with your request<br />please try again'
		);
		$(this).css('marginTop','54px');
		$(this).fadeIn('slow');
	});
}

function onContactFormSubmit(){
	var form = $("#contact-form > form");
	jQuery.ajax({
		url: form.attr('action'),
		data: form.serialize() + '&script=true',
		dataType: 'text',
		type: form.attr('method'),
		success: onContactFormSuccess,
		error: onContactFormError
	});
	return false;
}


