// JavaScript Document

$(document).ready
(
	function()
	{
		$('a.rounded').each
		(
			function(i)
			{
				
				roundCorners(this);
			}
		)
		
		//only tag links to images
		$('.post a').each
		(
			function(i)
			{
				var exts = $(this).attr('href').match(/(jpg|png|gif|jpeg)/i);
				
				
				if(
					(
					$(this).children('img').size() > 0 || //link contains an image
					$(this).hasClass('roundedImage') //link has ben transformed into a roundedImage
					) && exts != null)
				{
					var picAlt = $(this).children('img').first().attr('alt');
					$(this).addClass('gallery');
					$(this).attr('rel','post-gallery');	
					$(this).attr('title',picAlt);
				}
			}
		);
				
		$('.post a.gallery').fancybox({
										'transitionIn'	:	'elastic',
										'transitionOut'	:	'elastic',
										'speedIn'		:	200, 
										'speedOut'		:	200, 
										'titleShow'     :   true
										});	
	}
)

function roundCorners(theLink)
{
	var theLink = $(theLink);
	var theImage = theLink.children('img');
	
	//console.log('test');
	if(theImage.size() > 0)
	{
		//console.log('test');
		theImage = theImage.eq(0);	
		theLink.removeClass('rounded');
		theLink.addClass('roundedImage');
		theLink.addClass(theImage.attr('class'));
		
		theLink.css({
			'background-image' : 'url(' + theImage.attr('src') + ')',
			'width' 		   : theImage.width(),
			'height' 		   : theImage.height()
					
					});
		
		theImage.remove();
	}	
}
