/*
jQuery(function($) {
	$('a.image-swapper-link').live('click', function(e) {
		var width = $(this).closest('.article').find('.article-media img').attr('width');
		width = (width) ? ' width="'+width+'"' : '';

		$(this).closest('.article').find('a.image-swapper-link.active').removeClass('active');
		$(this).addClass('active');

		var image = '<img src="' + $(this).attr('href').replace('/icon','') + '" alt="' + $(this).attr('title') + '"'+width+' />';
		var caption = '<p class="article-media-caption">' + $(this).attr('title') + '</p>';

		$(this).closest('.article').find('.article-media').html(image + caption);

		e.preventDefault();
	});
});
*/
var target = 1;
jQuery(function($) {
	$( 'a.image-swapper-link' ).live('click', function(e) {
		$( 'a.image-swapper-link' ).removeClass('active');
		$(this).addClass('active');
		target = $(this).closest( ".entry" ).find( ".entry-media" );

		// Find arguments
		var key = $(this).attr( 'id' ).substring(1);
		var articleId = $(this).closest('.entry').attr( 'id' ).substring(8);

		// Lock container to current size
		var width = target.width();
		var height = target.height();
		target.css({ 'width' : width + 'px', 'height' : height + 'px' });

		// Show spinning ajax gif
		target.html( '<img src="/i/ajax-loader.gif" alt="loading..." class="ajax-loader" />' );
		var loaderWidth = target.children( 'img.ajax-loader' ).width();
		var loaderHeight = target.children( 'img.ajax-loader' ).height();
		var marginTop = (height - loaderHeight) / 2;
		var marginLeft = (width - loaderWidth) / 2;
		target.children( 'img.ajax-loader' ).css({ 'margin-top' : marginTop + 'px', 'margin-left' : marginLeft + 'px' });

		var url = ($('body#portfolio').length > 0) ? '/inc/loadImagePortfolio.php' : '/inc/loadImage.php';

		// Load content
		$.ajax({
			async:	false,
			type:		'POST',
			url:		url,
			cache:	false,
			data:		'key=' + key + '&articleId=' + articleId,
			success:	function(html) {
				// Hide spinner
				target.children( 'img.ajax-loader' ).hide();
				// Load content
				target.html(html);
			},
			complete:	function() {		
				// Show content
				target.children().show();
			}
		});

		e.preventDefault();
	});
});
