(function($) {
	$(function() {
		// nel blog, nella pagina dettagli blog e dettagli progetto...
		roundElements('.postContent .topImageContainer, .postContent .topVideoContainer, .postContent .postTopImageVideo, .postContent p img', 10);
		// in index
		roundElements($('#indexContent').find('.topImageContainer,.topVideoContainer'), 10);
		// nella pagina dei progetti
		roundElements('.thumbList img', 10);
		
		
		// piccolo fix per eliminare i margini ai thumbnails a inizio post
		resetMarginsAfterRounding('.postContent .postTopImageVideo .imgVidWrap');
	});
	
	
	function resetMarginsAfterRounding(selector)
	{
		$(selector).css({
			'margin-top': '0',
			'margin-bottom': '0',
			'margin-left': '0',
			'margin-right': '0'
		});
	}
	
	function roundElements(selector, size)
	{	
		// se la dimensione non è 10 o 20 non viene fatto nulla
		if (size == 10 /*|| size == 20*/)
		{			
			$(selector).each( function() {
			
				// definizione del container
				var wrapper = $('<div class="imgVidWrap" style="position: relative; margin:0; padding: 0; line-height: 0; display: inline-block" />');
			
				// spostati margini (non va bene col padding) da immagine a container
				wrapper.css({
					'margin-top': $(this).css('margin-top'),
					'margin-bottom': $(this).css('margin-bottom'),
					'margin-left': $(this).css('margin-left'),
					'margin-right': $(this).css('margin-right')
					// container della stessa dimensione dell'immagine
					//'width': $(this).css('width'),
					//'height': $(this).css('height')
				});
				$(this).css({
					'margin-top': '0',
					'margin-bottom': '0',
					'margin-left': '0',
					'margin-right': '0'
				});
				
				// circonda l'immagine col container
				$(this).wrap(wrapper);
				
				// le immagini devono essere già presenti, e avere parte interna trasparente
				// ed esterna del colore dello sfondo
				var img_name = 'tc_' + size;
				var img_base_path = 'images/';
				var img_extension = 'png';
			
				// aggiunge gli angoli
				var commonStyle = 	'width: ' + size + 'px; height: ' + size +
									'px; margin: 0; padding: 0; position: absolute; z-index: 100;';
										// aggiunto z-index alto per far stare i rounded corners sopra
									
					// top left
				$('<div style="top: 0; left: 0; background: url(\'' + img_base_path +
					img_name + '_tl.' + img_extension + '\'); ' + commonStyle + ';"></div>').insertBefore($(this));
					// top right
				$('<div style="top: 0; right: 0; background: url(\'' + img_base_path +
					img_name + '_tr.' + img_extension + '\'); ' + commonStyle + ';"></div>').insertBefore($(this));
					// bottom left
				$('<div style="bottom: 0; left: 0; background: url(\'' + img_base_path +
					img_name + '_bl.' + img_extension + '\'); ' + commonStyle + ';"></div>').insertBefore($(this));
					// bottom right
				$('<div style="bottom: 0; right: 0; background: url(\'' + img_base_path +
					img_name + '_br.' + img_extension + '\'); ' + commonStyle + ';"></div>').insertBefore($(this));		
			});
		}
	}
})(jQuery);
