jQuery(document).ready(function(){

	form_focus();
	external_links();
	leading_images();
	adjust_images();

});

/**
 * Clears the input fields on focus
 */
function form_focus(){
	jQuery('input.text').each(function(){
		var default_value = jQuery(this).val();
		jQuery(this).focus(function(){
			if(jQuery(this).val() == default_value) jQuery(this).val('');
			jQuery(this).blur(function(){
				if((jQuery(this).val() == default_value) || (jQuery(this).val().length == 0)) jQuery(this).val(default_value);
			});
		});
	});
}

/**
 * External links
 * Open external links in a new window
 */
function external_links(){
	jQuery("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");
}
/**
 * For posts with leading images, adjusts top margin for image
 * Looks for a <p> at the top with an image in it
 */
function leading_images(){

	jQuery('.entry').each(function(){
	
		if(!jQuery(this).hasClass('home')){
		
			firstChild = jQuery(this).find(':first');
		
			if(firstChild.is('p') && firstChild.find(':first').is('img')){
	
				firstChild.css({
					'margin-top': '-24px',
					'width': '538px',
					'border-top': '8px solid #FFF'
				});
			}
		}
	});
}

/**
 * All post images are shifted to the left via CSS
 * This shifts any that aren't full width back.
 * Also removes border from linked images
 */
function adjust_images(){
	
	var fullWidth = 538;
	
	jQuery('.entry').find('img').each(function(){
		
		if(jQuery(this).width() < 538){
			jQuery(this).css('left', 0);
		}
		
		if(jQuery(this).parent().is('a')){
			jQuery(this).parent('a').css('border', 'none');
		}
		
	});
	
}
