$(document).ready(function() {
	initFancyBox();
	highlightNav();
	showAndTell();
	resizeBlogImages();
});



// Apply fancybox to all a's with the class 'fancy'
function initFancyBox(){
	$("a.fancy").fancybox({
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'speedIn'		: 400, 
		'speedOut'		: 200, 
		'overlayShow'	: true,
		'titlePosition': 'over',
		'titleShow'	: false
	});
}


// highlight correct nav item
function highlightNav(){
	if(document.URL.indexOf('blog') > -1){
		$('#nav li#blog').addClass('selected');
	} else if(document.URL.indexOf('print') > -1){
		$('#nav li#print').addClass('selected');
	} else if(document.URL.indexOf('photography') > -1){
		$('#nav li#photography').addClass('selected');
	} else if(document.URL.indexOf('web') > -1){
		$('#nav li#web').addClass('selected');
	} else if(document.URL.indexOf('resume') > -1){
		$('#nav li#resume').addClass('selected');
	}


	var defaultColor;

	$('#nav li a').hover(
		function(){
			if(!$(this).parent().hasClass('selected')){
				defaultColor = $(this).css('color');
				$(this).animate({
					color: '#666666'
				})
			}
		},
		function(){
			if(!$(this).parent().hasClass('selected')){
				$(this).animate({
					color: defaultColor
				})
			}
		}
	)

}


// hide & show full portfolio feature
function showAndTell(){
	$('.feature ul li .description div').after('<div class="background"></div>');
	$('.feature ul li .description').after('<a class="openDescription">&raquo;</div>');
	$('.feature ul li .description').after('<a class="closeDescription" style="display:none;">&laquo;</div>');

	$('.feature ul li a.openDescription').click(function() {
		console.log('open');
		$(this).parent().animate({
			width: '200%'
		});
		$(this).hide();
		$(this).parent().find('a.closeDescription').show();
	});
	
	$('.feature ul li a.closeDescription').click(function() {
		console.log('close');
		$(this).parent().animate({
			width: '100%'
		});
		$(this).hide();
		$(this).parent().find('a.openDescription').show();
	});
}

function resizeBlogImages(){
	var blogImage = $('.entry-content img.alignnone, .entry-content embed, .entry-content iframe');
	blogImage.wrap('<div class="entry-image" />');
	var blogImageContainer = $('.entry-image');

	blogImage.each(function(){
		var imgWidth = $(this).width();
		var imgHeight = $(this).height();
		var containerWidth = blogImageContainer.width();
		var conversionFactor = containerWidth/imgWidth;
		if(imgWidth > containerWidth){
			$(this).attr('width',imgWidth*conversionFactor)
			$(this).attr('height',imgHeight*conversionFactor)	
		}
	})
}


