// digit.js

var imgbar_state        = 0;		// 0=normal, 1=zooming in progress, 2=panorama
var imgbar_savedheight  = 0;
var imgbar_savedlogopos = 0;
var imgbar_hlogo        = 100;
var imgbar_ylogo        = -60;
var imgbar_ytext        = 12;
var imgbar_slidesteps   = 15;

var preview;
var pano;
var currentIndex = 0;

function isIE6()
{
	if (typeof document.body.style.maxHeight != "undefined")
	{
		return false;	// IE7, FF, safari, opera
	}

	return true;		// IE6, older browsers
}

function nextImage()
{
	currentIndex += 1;

	if(currentIndex >= digit_imagebars.length)
		currentIndex = 0;
		
	updateImageBar();	
}

function prevImage()
{
	currentIndex -= 1;

	if(currentIndex < 0)
		currentIndex = digit_imagebars.length-1;
		
	updateImageBar();	
}

function randomImage()
{
	currentIndex = Math.floor(Math.random() * digit_imagebars.length);	
	updateImageBar();
}

function updateImageBar()
{
	var currentPano = digit_imagebars[currentIndex];	
	var splitterIndex = currentPano.indexOf("|");
	
	previewURL = currentPano.slice(0, splitterIndex);
	panoURL = currentPano.slice(splitterIndex+1, currentPano.length);
	
	document.getElementById("digit_imagebar").style.backgroundImage="url(" + previewURL + ")";
}

function getWindowHeight()
{
	var windowHeight=0;
	
	if (typeof(window.innerHeight)=='number')
		windowHeight=window.innerHeight; 
	else
	{
		if (document.documentElement && document.documentElement.clientHeight) 
			windowHeight=document.documentElement.clientHeight; 
		else if (document.body && document.body.clientHeight) 
			windowHeight=document.body.clientHeight;
	}
	return windowHeight;
}

function showFlash()
{
	var so = '<iframe id="iframeview" src="' + panoURL + '" name="Bildframe" width="100%" height="100%" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="z-Index:101"><p style="padding:10%;"></iframe>';

	document.getElementById("digit_imagebar").innerHTML = so;
}

function hideFlash()
{
	document.getElementById("digit_imagebar").innerHTML = '';
}

function imgbar_getspace()
{
	var hwindow = getWindowHeight();
	var hheader = document.getElementById("digit_imagebar").offsetTop;

	var hfooter = imgbar_hlogo + imgbar_ylogo;
	if (hfooter < 16)
		hfooter = 16;

	var hspace = hwindow - hheader - hfooter;
	if (hspace < imgbar_savedheight)
		hspace = imgbar_savedheight;

	return hspace;
}

function incHeightImageBar()
{
	if (imgbar_state == 0)
	{
		imgbar_state = 1;

		// hide content/footer/showpano-button
		document.getElementById("digit_showpano").style.display = "none";
		document.getElementById("digit_footerwrapper").style.display  = "none";
		document.getElementById("digit_contentwrapper").style.display = "none";
		document.getElementById("digit_logo").style.display = "none";
		document.getElementById("digit_imagebar").style.backgroundImage = "none";
		document.getElementById("digit_nametext").style.top = "6px";

		if ( isIE6() )
		{
			document.getElementById("digit_ie6bugfix").style.height = "auto";
		}

		imgbar_savedheight  = document.getElementById("digit_imagebar").offsetHeight;
		imgbar_savedlogopos = (document.getElementById("digit_imagebar").offsetTop + imgbar_savedheight) - document.getElementById("digit_logo").offsetTop;
	}

	var hheader = document.getElementById("digit_imagebar").offsetTop;
	var hspace  = imgbar_getspace();
	var hcur    = parseInt( document.getElementById("digit_imagebar").offsetHeight );
	var hinc    = hspace / imgbar_slidesteps;


	if (hcur < hspace)
	{
		hcur = hcur + hinc;
		if (hcur > hspace)
			hcur = hspace;

		document.getElementById("digit_imagebar").style.height = hcur + "px";

		if (hcur >= hspace)
		{
			// done
			if ( isIE6() )
			{
				document.getElementById("digit_hidepano").style.position = "absolute";
			}

			document.getElementById("digit_hidepano").style.top     = (hheader + hcur + imgbar_ytext) + "px";
			document.getElementById("digit_hidepano").style.display = "inline";

			imgbar_state = 2;

			setTimeout( 'showFlash()', 1 );
		}
		else
		{
			setTimeout( 'incHeightImageBar()', 1 );
		}
	}

	return;
}

function decHeightImageBar()
{
	if (imgbar_state == 2)
	{
		imgbar_state = 1;

		// hide panorama and close-pano-buttons
		hideFlash();
		document.getElementById("digit_hidepano").style.display = "none";
	}

	var hheader = document.getElementById("digit_imagebar").offsetTop;
	var hspace  = imgbar_getspace();
	var hcur    = parseInt( document.getElementById("digit_imagebar").offsetHeight );
	var hdec    = hspace / imgbar_slidesteps;


	if (hcur > imgbar_savedheight)
	{
		hcur = hcur - hdec;
		if (hcur < imgbar_savedheight)
		{
			hcur = imgbar_savedheight;
		}

		document.getElementById("digit_imagebar").style.height = hcur + "px";

		if (hcur <= imgbar_savedheight)
		{
			// done - restore hidden elements
			document.getElementById("digit_showpano").style.display = "inline";
			document.getElementById("digit_footerwrapper").style.display  = "inline";
			document.getElementById("digit_contentwrapper").style.display = "inline";
			document.getElementById("digit_logo").style.display = "inline";
			document.getElementById("digit_imagebar").style.backgroundImage="url(" + previewURL + ")";
			document.getElementById("digit_nametext").style.top = "6px";

			imgbar_state = 0;
		}
		else
		{
			setTimeout( 'decHeightImageBar()', 1 );
		}
	}

	return;
}

function showImagebarPano()
{
	if (imgbar_state == 0)
	{
		incHeightImageBar();
	}
}

function hideImagebarPano()
{
	if (imgbar_state == 2)
	{
		decHeightImageBar();
	}
}

function resizeImagebarpano()
{
	if (imgbar_state == 2)
	{
		var hheader = document.getElementById("digit_imagebar").offsetTop;

		var hspace = imgbar_getspace();

		document.getElementById("digit_imagebar").style.height = hspace + "px";
		document.getElementById("digit_hidepano").style.top    = (hheader + hspace + imgbar_ytext) + "px";

		if (document.getElementById("iframeview"))
			document.getElementById("iframeview").style.height = hspace + "px";
	}

	return true;
}

window.onresize = resizeImagebarpano;
