$(window).load(function(){
	$('.sifr-bg').each(function(count, element){

		var width = 0;
		var height = 0;
		var xPos = 99999;
		var yPos = 99999;
		var wideTitle = false;
		var topHeight = 0;
	
		$(this).siblings('.main-image-title, .main-image-paragraph').each(function(count, element){
			var w = $(element).innerWidth();
			var h = $(element).innerHeight();
			var pos = $(element).position();
			
			//sets the background top-left position according to the content
			if(pos.left < xPos) { xPos = pos.left; }
			if(pos.top < yPos) { yPos = pos.top; }
			
			//adapts the size of the background to the size of its content
			if(w > width){ 
				width = w;
				if(h > topHeight){ 
					//wideTitle = true;
					topHeight = h;
				}
				else
				{
					wideTitle = false;
				}
			}
			
			height += h;
			
			$(this).css('z-index','20');
			
		});
		
		if(sIFR.isActive)
		{
			$(this).css({
				'width': wideTitle ? ((width/100) * 88) + 20 : ((width/100) * 97) + 50, // Widths changed depending on whether the title is the widest element. Differences are due to kerning applied to the title text.
				'height':height + 20, //so the background extends more to the left than the text
				'top':yPos - 5,
				'left':xPos - 10 //so the background starts a bit before the text
			});
		}
		else
		{
			$(this).css({
				'width':width + 20,
				'height':height + 20,
				'top':yPos - 10,
				'left':xPos - 10
			});				
		}
	
	});
});
