/*
Dieses Script benötigt jQuery und jQuery Preload.
Außerdem ein Image das geresized wird und ein Image,
dass buffert (kann diplay:none sein)
*/

var wWidth, wHeight, wRatio;
var iWidth, iHeight, iRatio;
var scaleRatio = 2;
var CssSelector, CssBuffer;
var newSrc;

function fitToWindow(aCssSelector, aImageBuffer, aNewSrc, aScaleRatio) {
	//alert("fitToWindow");
	CssSelector = aCssSelector;
	CssBuffer = aImageBuffer;
	scaleRatio = aScaleRatio;
	var tSrc = new Array();
	tSrc[0] = aNewSrc;
	$(tSrc).preload({
		/*
		onRequest:function(data) {
			$('#loading').fadeIn();
		},
		
		onComplete:function(data) {
			$('#loading').fadeOut();
		},
		*/
		
		onFinish:function(data) {
			$(CssBuffer).attr("src", imgSrc);
			
			$(CssSelector).attr("src", imgSrc);
			//alert ($(CssBuffer).width());
			getImageRatio();
			getSize();
			scaleObj();
		}
	});	
}
function getImageRatio() {
	iWidth = $(CssBuffer).width();
	iHeight = $(CssBuffer).height();
	iRatio = iWidth.toFixed(2)/iHeight.toFixed(2);
}

function getSize() {
	wWidth = $(window).width();
	wHeight = $(window).height();
	wRatio = wWidth/wHeight;
	iWidth = $(CssSelector).width();
	iHeight = $(CssSelector).height();
}
function scaleObj() {
	
	// background ist verdeckt (längste seite)
	
	/*
	if (iRatio < wRatio) {
		var newWidth = Math.round(wWidth/scaleRatio);
		var newHeight = Math.round((wWidth/scaleRatio)/iRatio);
	}else{
		var newHeight = Math.round(wHeight/scaleRatio);
		var newWidth = Math.round(wHeight/scaleRatio*iRatio);
	}
	*/
	
	
	
	// background ist sichtbar (kürzeste seite)
	
	if (iRatio > wRatio) {
		var newWidth = Math.round(wWidth/scaleRatio);
		var newHeight = Math.round((wWidth/scaleRatio)/iRatio);
	}else{
		var newHeight = Math.round(wHeight/scaleRatio);
		var newWidth = Math.round(wHeight/scaleRatio*iRatio);
	}
	
	
	
	
	$(CssSelector).css({					
		'width': newWidth,
		'height': newHeight,
	});
}
