//-------------------------------------------
// 	Background-Image-Scale (requires JQuery)
//-------------------------------------------

// on document load & browser resize: resize background image
$(document).ready(function() { resizeBackgroundImage(); } );
$(window).resize(function() { resizeBackgroundImage(); } );

bgimage_align = "center";		
bgimage_valign = "bottom";		

function resizeBackgroundImage() 
{
	// quit if image not set or empty
	if ($('#background_image').width() < 1) return;
	if ($('#background_image').height() < 1) return;

	console.log("gemma resize");

	// determine browser dimensions
	browserWidth = $(window).width();
	browserHeight = $(window).height();
	browserRatio = browserWidth/browserHeight;

	// determine image dimensions
	imageWidth = $('#background_image').width();
	imageHeight = $('#background_image').height();
	imageRatio = imageWidth/imageHeight;

	// calculate new dimensions
	if (browserRatio > imageRatio)
	{
		newWidth = browserWidth;
		newHeight = (browserWidth/imageWidth)*imageHeight;
	}
	else
	{
		newHeight = browserHeight;
		newWidth = (browserHeight/imageHeight)*imageWidth;
	}
	
	// resize image
	$('#background_image').css({height: newHeight, width: newWidth });

	// reposition image
	switch (bgimage_align)
	{
		case "left":
			$('#background_image').css({left: 0 });
			break;
		case "center":
			$('#background_image').css({left: 0-((newWidth-browserWidth)/2) });
			break;
		case "right":
			$('#background_image').css({right: 0 });
			break;
	}
	switch (bgimage_valign)
	{
		case "top":
			$('#background_image').css({top: 0 });
			break;
		case "middle":
			$('#background_image').css({top: 0-((newHeight-browserHeight)/2) });
			break;
		case "bottom":
			$('#background_image').css({bottom: 0 });
			break;
	}
	
}



