
var bgWidth = 1;
var bgHeight = 1;

function onReady () {
	
	$("#imageHolder").hide ( );
	$("#facebookLink").hide ( );
	$("#bgImage").hide ( );
	$(window).resize ( function () { onResize ( true ); } );

}

function onResize ( doTwice ) {
	
	var viewWidth = $(window).width();
	var viewHeight = $(window).height();
	
	var minWidth = $("#innerImage").width();
	var minHeight = $("#innerImage").height();
	
	if ( viewWidth < minWidth ) viewWidth = minWidth;
	if ( viewHeight < minHeight ) viewHeight = minHeight;
	
	$("#main").css ( "width", viewWidth );
	$("#main").css ( "height", viewHeight );
	
	$("#imageHolder").css ( "margin-left", (viewWidth-$("#innerImage").width())/2 );
	$("#imageHolder").css ( "margin-top", (viewHeight-$("#innerImage").height())/2 );
	
	$("#facebookLink").css ( "margin-left", (viewWidth-$("#innerImage").width())/2 );
	$("#facebookLink").css ( "margin-top", ((viewHeight-$("#innerImage").height())/2)+$("#innerImage").height()+10 ); 
	
	if ( bgWidth != 1 && bgHeight != 1 ) resizeMinCenter ( "#innerBG", viewWidth, viewHeight, bgWidth, bgHeight );
	
	if ( doTwice ) onResize ( false );
	
}

function resizeMinCenter ( target, width, height, orgWidth, orgHeight ) {

	var newWidth;
	var newHeight;
	
	var posX;
	var posY;
	
	
	newHeight = height;
	newWidth = newHeight*orgWidth/orgHeight;
	
	if ( newWidth < width ) {
	
		newWidth = width;
		newHeight = newWidth*orgHeight/orgWidth;
	
	}
	
	posX = (width-newWidth)/2;
	posY = (height-newHeight)/2;
	
	$(target).width ( newWidth );
	$(target).height ( newHeight );
	$(target).css ( "margin-left", posX );
	$(target).css ( "margin-top", posY );

}

function onBGLoaded ( ) {
	
	$("#bgImage").fadeIn ( 500 );
	
	bgWidth = $("#innerBG").width();
	bgHeight = $("#innerBG").height();
	
	onResize ( true );

}

function onImageLoaded ( ) {
	
	onResize ( false );
	$("#imageHolder").fadeIn ( 500 );
	onResize ( true );
	
}

function onIconLoaded ( ) {
	
	onResize ( false );
	$("#facebookLink").fadeIn ( 500 );
	onResize ( true );

}

jQuery(document).ready ( onReady );
