// JavaScript Document
window.onload=initScroller;

var arImageList=new Array();
var iImageCurrent=-1;
var oImageScroller=null;
var image=new Image();
var oTimeout=null;
var oScrollTimeout=null;
var iWidth=-1;
var iHeight=-1;

function makeImageName(img)
{
	return("/umbraco/imagegen.ashx?image=/media/"+img+".jpg");
}

function initScroller()
{
	//Rahmenobjekt holen und Startwerte setzen
	oImageScroller=document.getElementById("imagescroller");	
	if(!oImageScroller) return;
	oImageScroller.style.backgroundPosition="0px 0px";
	oImageScroller.style.backgroundRepeat="no-repeat";
	iHeight=parseInt(oImageScroller.style.height);
	iWidth=parseInt(oImageScroller.style.width);
	//Event vorbereiten
	oImageScroller.onmouseover=showIcon;
	oImageScroller.onmouseout=hideIcon;
	oImageScroller.onclick=openSlimbox;
	//Bilderliste aufbauen und Vorladen
	var iImageCount=oImageScroller.childNodes[0].childNodes.length;
	if(iImageCount<1) return;
	for(var i=0; i<iImageCount; i++)
	{
		arImageList[i]=makeImageName(oImageScroller.childNodes[0].childNodes[i].innerHTML)+"&compression=60";
		var img=new Image();
		img.src=arImageList[i]+"&height="+iHeight;
	}
	//Erstes Bild zeigen
	image.src=arImageList[0]+"&height="+iHeight;
	showNextImage();
}


function getSize() {
	var myWidth = 0, myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}


function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function openSlimbox()
{
	//Zielgröße berechnen
	/*
	var iScreenSize=getSize();
	iScreenSize[0]=parseInt((iScreenSize[0]-30)*0.85);
	iScreenSize[1]=parseInt((iScreenSize[1]-120)*0.85);
	if(iScreenSize[0]>700) iScreenSize[0]=700;
	if(iScreenSize[1]>700) iScreenSize[1]=700;
	*/
	var sSizePostfix="&height="+560+"&width="+800+"&constrain=true";
	//Array aufbauen
	var arImages=new Array();
	for(var i=0; i<arImageList.length; i++)
	{
		arImages[i]=new Array(1);
		arImages[i][0]=arImageList[i]+sSizePostfix;
	}
	jQuery.slimbox(arImages, iImageCurrent, {loop: true});
}

function showIcon()
{
	var o=document.getElementById("zoomicon");
	o.style.visibility="visible";
}

function hideIcon()
{
	var o=document.getElementById("zoomicon");
	o.style.visibility="hidden";
}

function showNextImage()
{
	//Warten bis geladen
	if(!image.complete)
	{
		setTimeout(showNextImage, 50);
		return;
	}
	//Bild wechseln
	oImageScroller.style.backgroundImage="url("+image.src+"&height="+iHeight+")";
	//Scroll starten
	iDirection=-1;
	iImageCurrent++;
	iImageWidth=image.width;
	oImageScroller.style.backgroundPosition=iWidth+"px 0px";	
	if(oScrollTimeout!=null){ clearTimeout(oScrollTimeout); oScrollTimeout=null;}
	imageScroll();	
	//Nächstes Bild vorbereiten
	if(iImageCurrent>=arImageList.length) iImageCurrent=0;
	image.src=arImageList[iImageCurrent+1]+"&height="+iHeight;
}

var iSpeed=25;
var iDirection;
function imageScroll()
{
	var iX=	parseInt(oImageScroller.style.backgroundPosition);
	iX+=iDirection;
	//if(iX>0){iX=0; iDirection=-iDirection; showNextImage(); return;}
	if(iX<-(iImageWidth)) {iX=-(iImageWidth); iDirection=-iDirection; showNextImage(); return;}
	oImageScroller.style.backgroundPosition=iX+"px 0px";	
	oScrollTimeout=setTimeout(imageScroll, iSpeed);
}

