var aCurrentIds;
var iCurrentPosition;
var bPlayActive;
var oPlayTimer;
var iPlayCurrent=0;
var iPlaySeconds=5;
var iPlayStepIntervall=100;
var iMaxWidth=947;
var myAjax;
var bImageloaderActive=false;

function imageLoader_getPageSize () {

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function imageLoader_init(iCurrentId,aAllIds) {
	aCurrentIds=aAllIds;
	iCurrentPosition=0;
	bPlayActive=false;
	bImageloaderActive=true;
	iTmp=-1;
	aAllIds.each(function (iId){iTmp++;if(iCurrentId==iId) iCurrentPosition=iTmp;});
	arrayPageSize = imageLoader_getPageSize();
	$('myDummyImage').src="";
	$('overlay').setStyle({width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px'});
	aScrollOffsets=document.viewport.getScrollOffsets();
	$('divImageLoader').setStyle({top: (aScrollOffsets[1])+"px" })
	$('overlay').appear({to: 0.8});
	$('divImageLoader').appear();
	imageLoader_loadImage();
}

function imageLoader_nextImage() {
	iCurrentPosition=iCurrentPosition+1;
	if (iCurrentPosition>(aCurrentIds.size()-1)) iCurrentPosition=0;
	imageLoader_loadImage();
}

function imageLoader_prevImage() {
	iCurrentPosition=iCurrentPosition-1;
	if (iCurrentPosition<0) iCurrentPosition=(aCurrentIds.size()-1);
	imageLoader_loadImage();
}

function imageLoader_togglePlay() {
	if (bPlayActive===false) {
		$('myDummyPlay').src="/img/imageloader/imageloader_stop.png";
		iPlayCurrent=0;
		imageLoader_updateTimeProgressbar();
		$('myDummyPlayTimer').show();
		oPlayTimer=window.setInterval("imageLoader_playStep()", iPlayStepIntervall);
		bPlayActive=true;
	} else {
		$('myDummyPlayTimer').hide();
		$('myDummyPlay').src="/img/imageloader/imageloader_play.png";
		window.clearInterval(oPlayTimer);
		bPlayActive=false;
	}
}

function imageLoader_updateTimeProgressbar() {
	$('myDummyPlayTimer').setStyle({ width: (70*((iPlayCurrent)/((1000/iPlayStepIntervall)*(iPlaySeconds+1))))+"px" });
}

function imageLoader_playStep() {
	iPlayCurrent++;
	imageLoader_updateTimeProgressbar();
	if (iPlayCurrent>=((1000/iPlayStepIntervall)*iPlaySeconds)) {
		iPlayCurrent=0;
		imageLoader_updateTimeProgressbar();
		imageLoader_nextImage();
	}
}

function imageLoader_loadImage() {
	aViewportSize=document.viewport.getDimensions();
	$('myDummyImageLink').href="/image.php?id="+aCurrentIds[iCurrentPosition];
	$('myDummyImage').src="/image.php?id="+aCurrentIds[iCurrentPosition]+"&maxwidth="+iMaxWidth+"&maxheight="+((aViewportSize.height)-100);
	$('myDummyPosition').innerHTML=(iCurrentPosition+1)+" VON "+aCurrentIds.size();
	myAjax=new Ajax.Updater('myDummyInfoText','/image.php?id='+aCurrentIds[iCurrentPosition]+'&info');
	//window.setTimeout("imageLoader_resizeImage()",100);
}

function imageLoader_resizeImage() {
	if ($('myDummyImage').width>iMaxWidth) {
		//alert("resize img to max");
		$('myDummyImage').setStyle({width: iMaxWidth+"px"});
	}
	else {
		$('myDummyImage').setStyle({width: "auto"});
	}
}

function imageLoader_destruct() {
	bImageloaderActive=false;
	aCurrentIds=[];
	iCurrentPosition=0;
	if (bPlayActive===true) imageLoader_togglePlay();
	$('divImageLoader').fade();
	$('overlay').fade();
	$('myDummyImage').setStyle({width: "auto"}); 
}

document.observe('keydown',function(e) {
	if (bImageloaderActive===true) {
		if (e.keyCode==Event.KEY_PAGEDOWN) { imageLoader_nextImage(); Event.stop(e); }
		if (e.keyCode==Event.KEY_PAGEUP) { imageLoader_prevImage(); Event.stop(e); }
		if (e.keyCode==32) { imageLoader_togglePlay(); Event.stop(e); }
		if (e.keyCode==Event.KEY_ESC) { imageLoader_destruct(); Event.stop(e); }
	}
});

