/* Requires jQuery! */

function roundTwoDecimals(val) {
	val = val * 100;
	val = Math.round(val);
	val = val / 100;
	return val;
}

function scaleImage(origWidth,origHeight,resize) {
	winW = findLivePageWidth();
	winH = findLivePageHeight();

	maxW = Math.round( winW * 0.85 );
	maxH = Math.round( winH * 0.85 );

	var tmpHeight = origHeight;
	var tmpWidth = origWidth;

	if (tmpHeight > maxH) {
		tmpHeight = maxH;
		tmpWidth = Math.round((maxH / origHeight) * tmpWidth);
	} else if (tmpWidth > maxW) {
		tmpWidth = maxW;
		tmpHeight = Math.round((maxW / origWidth) * tmpHeight);
	}

	if (resize == true && document.getElementById('zoomimage-file')) {
		writeImage(imgpath,caption,tmpWidth,tmpHeight);
	}

	arr_dimensions = new Array();
	arr_dimensions[0] = tmpWidth;
	arr_dimensions[1] = tmpHeight;
	return arr_dimensions;
}

function writeImage(imgpath,caption,tmpWidth,tmpHeight) {
	//alert('WRITE');
	document.getElementById('zoomimage-cell').innerHTML = '<img id="zoomimage-file" src="'+imgpath+'" width="'+tmpWidth+'" height="'+tmpHeight+'" alt="0" border="0" title="Zum Schließen bitte das Bild anklicken." />';
	if (caption != undefined && caption != '') document.getElementById('zoomimage-cell').innerHTML += '<div class="caption">'+caption+'</div>';
	return true;
}

function zoomImage(imgpath,caption,origWidth,origHeight) {
	//alert('TEST');
	arr_dimensions = new Array();
	arr_dimensions = scaleImage(origWidth,origHeight,false);
	tmpWidth = arr_dimensions[0];
	tmpHeight = arr_dimensions[1];

	zoomimage = new Image();
	zoomimage.src = imgpath;
	
	$('#zoomimage-background').fadeIn('slow');

	zoomimage.onload = function() {
		if (writeImage(imgpath,caption,tmpWidth,tmpHeight)) $('#zoomimage-foreground').fadeIn('slow');
	}
}

var tmpWidth = 0;
var tmpHeight = 0;

window.onresize = function() {
	//alert('RESIZE');
	scaleImage(tmpWidth,tmpHeight,true);
}

function hideZoomImage() {
	$('#zoomimage-foreground').fadeOut('slow');
	$('#zoomimage-background').fadeOut('fast');
}

function findLivePageHeight() {
	return window.innerHeight != null ? window.innerHeight : document.body.clientHeight != null ? document.body.clientHeight : null;
}

function findLivePageWidth() {
	return window.innerWidth != null ? window.innerWidth : document.body.clientWidth != null ? document.body.clientWidth : null;
}


