/*
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 imagefit(aCssSelector, aImageBuffer, aNewSrc, aScaleRatio) {
	//alert ("imagefit()");
	CssSelector = aCssSelector;
	CssBuffer = aImageBuffer;
	scaleRatio = aScaleRatio;
	var tSrc = new Array();
	tSrc[0] = aNewSrc;
	//alert ($(CssBuffer));
	//alert (tSrc[0]);
	$(tSrc).preload({
		onRequest:function(data) {
			//alert ("request: "+data.image);
		},
		onFinish:function(data) {
			//alert ("finish: "+data.image);
			$(CssBuffer).attr("src", data.image);
			$(CssSelector).attr("src", data.image);
			getImageRatio();
			getSize();
			scaleObj();
		}
	});	
}
function getImageRatio() {
	iWidth = $(CssBuffer).width();
	iHeight = $(CssBuffer).height();
	iRatio = iWidth.toFixed(2)/iHeight.toFixed(2);
	//alert ("iRatio: "+iRatio);
}

function getSize() {
	wWidth = $(window).width();
	wHeight = $(window).height();
	wRatio = wWidth/wHeight;
	iWidth = $(CssSelector).width();
	iHeight = $(CssSelector).height();
	//alert ("wRatio: "+wRatio);

}
function scaleObj() {
	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);
	}
	var newTop = (wHeight-newHeight)/2;
	//alert ("newSize: "+newWidth+"x"+newHeight);
	/*
	$(CssSelector).animate({
		'marginTop': newTop,			  
		width: newWidth,
		height: newHeight,
	}, 300);
	*/
	
	$(CssSelector).css({
		'margin-top': newTop,			   
		'width': newWidth,
		'height': newHeight,
	});
	
	$("#BU").css("width", newWidth);
	
}