// JavaScript Document
function modLoadImage(container,image,width,height,name){
	if(document.getElementById(container)){
		var parentElement=document.getElementById(container);
		for(var i=0;i<parentElement.childNodes.length;i++){
			parentElement.removeChild(parentElement.childNodes[i]);	
		}
		if(document.getElementById("pageImageFooter")){
			document.getElementById("pageImageFooter").innerHTML=parseInt(modImageCount+1)+"/"+modImgArray[modActiveGallery].length;
			if(mode=="stop"){
				var progressBar=document.createElement("img");
				var now=new Date();
				progressBar.src="shared/images/progress_3s.gif?"+now.getTime();
				progressBar.id="modProgressBar";
				progressBar.style.paddingLeft="200px";
				if(document.getElementById("modProgressBar")){
					document.getElementById("modProgressBar").parentNode.removeChild(document.getElementById("modProgressBar"));	
				}
				parentElement.parentNode.appendChild(progressBar);
			}
			else{
				if(document.getElementById("modProgressBar")){
					document.getElementById("modProgressBar").parentNode.removeChild(document.getElementById("modProgressBar"));	
				}
			}
		}
		var imagePadding=(parentElement.offsetWidth-width)/2;
		var parentElementHeight=parseInt(height)+(imagePadding*2);
		parentElement.style.height=parentElementHeight+"px";
		var newImage=document.createElement("img");
		newImage.src=image;
		newImage.alt="Bild";
		newImage.id="modGalleryImage";
		newImage.style.padding=imagePadding+"px";
		if(imagePadding==0){
			newImage.style.marginTop="15px";
		}
		if(width>520){
			var newHeight=Math.round(520*height/width);
			newImage.style.width="520px";	
			newImage.style.height=newHeight;	
		}
		if(document.getElementById(modActiveGallery+"_"+modImageCount)){
			modSetTransparency(document.getElementById(modActiveGallery+"_"+modImageCount),100);
		}	
		parentElement.appendChild(newImage);
		if(newImage.style.MozOpacity!="undefined" || newImage.style.filter!="undefined"){
			//newImage.style.display="none";
			//modFadeImage(newImage.id,0,100,20,true);	
		}
		
		//preload next image
		if(modImageCount<modImgArray.length-1){
			var nextImg=new Image();
			nextImg.src=modImgArray[modImageCount+1];
		}
		
	}
	else{
		window.setTimeout(new Function("modLoadImage('"+container+"','"+image+"')"),100);	
	}
}

function modNextImage(){
	if(document.getElementById(modActiveGallery+"_"+modImageCount)){
		modSetTransparency(document.getElementById(modActiveGallery+"_"+modImageCount),50);
	}
	if(modImageCount<modImgArray[modActiveGallery].length-1){
		modImageCount+=1;
	}
	else{
		if(modActiveGallery<modImgArray.length-1){
			modActiveGallery+=1;	
		}
		else{
			modActiveGallery=0;	
		}
		modImageCount=0;
	}
	modLoadImage('pageImageContainer',modImgArray[modActiveGallery][modImageCount],modWidthArray[modActiveGallery][modImageCount],modHeightArray[modActiveGallery][modImageCount],modNameArray[modActiveGallery][modImageCount]);
	return false;
}

function modPrevImage(){
	if(document.getElementById(modActiveGallery+"_"+modImageCount)){
		modSetTransparency(document.getElementById(modActiveGallery+"_"+modImageCount),50);
	}
	if(modImageCount>0){
		modImageCount-=1;
	}
	else{
		if(modActiveGallery>0){
			modActiveGallery-=1;	
		}
		else{
			modActiveGallery=modImgArray.length-1;	
		}
		modImageCount=modImgArray[modActiveGallery].length-1;
	}
	modLoadImage('pageImageContainer',modImgArray[modActiveGallery][modImageCount],modWidthArray[modActiveGallery][modImageCount],modHeightArray[modActiveGallery][modImageCount],modNameArray[modActiveGallery][modImageCount]);
	return false;
}

function modNextGallery(){
	if(modActiveGallery<modImgArray.length-1){
		modActiveGallery+=1;
	}
	else{
		modActiveGallery=0;
	}
	modImageCount=0;
	modLoadImage('pageImageContainer',modImgArray[modActiveGallery][modImageCount],modWidthArray[modActiveGallery][modImageCount],modHeightArray[modActiveGallery][modImageCount],modNameArray[modActiveGallery][modImageCount]);
	return false;
}

function modPrevGallery(){
	if(modActiveGallery>0){
		modActiveGallery-=1;
	}
	else{
		modActiveGallery=modImgArray.length-1;
	}
	modImageCount=0;
	modLoadImage('pageImageContainer',modImgArray[modActiveGallery][modImageCount],modWidthArray[modActiveGallery][modImageCount],modHeightArray[modActiveGallery][modImageCount],modNameArray[modActiveGallery][modImageCount]);
	return false;
}

function modLoadGallery(gallery_id){
	modActiveGallery=gallery_id;
	modImageCount=0;
	modLoadImage('pageImageContainer',modImgArray[modActiveGallery][modImageCount],modWidthArray[modActiveGallery][modImageCount],modHeightArray[modActiveGallery][modImageCount],modNameArray[modActiveGallery][modImageCount]);
	return false;
}

function modFadeImage(id,start,end,speed,firstCall){
	var image=document.getElementById(id);
	if(image.complete){
		image.style.display="";
		if(image.style.MozOpacity!="undefined"){
			if(firstCall==true){
				start=start/100;
				end=end/100;
				speed=speed/100;
			}
			image.style.MozOpacity=start;
			if(start<end){
				start+=speed;
				window.setTimeout(new Function("modFadeImage('"+id+"',"+start+","+end+","+speed+")"),50);	
			}
		}
		if(image.style.filter!="undefined"){
			image.style.filter="alpha(opacity="+start+")";	
			if(start<end){
				start+=speed;
				window.setTimeout(new Function("modFadeImage('"+id+"',"+start+","+end+","+speed+")"),50);		
			}
		}
	}
	else{
		window.setTimeout(new Function("modFadeImage('"+id+"',"+start+","+end+","+speed+")"),50);	
	}
}

var mode="start";
function modSlideShow(){
	if(mode=="start"){
		mode="stop";
		modNextImage();
		slideShow=window.setInterval("modNextImage()",3500);
		
	}
	else{
		window.clearInterval(slideShow);	
		mode="start";
	}
	document.getElementById("galleryControl").src="shared/images/gallery_control_"+mode+".jpg";
	return false;
}

function modSetTransparency(element,value){
	if(element.style.MozOpacity!="undefined"){
		element.style.MozOpacity=value/100;
	}
	if(element.style.filter!="undefined"){
		element.style.filter="alpha(opacity="+value+")";	
	}	
}

function modSetActiveId(gallery_id,image_id){
	modActiveGallery=gallery_id;
	modImageCount=image_id;
}

function modAddGalleryControl(gallery_id,gallery_name){
	if(document.getElementById("galleryList")){
		var parentElement=document.getElementById("galleryList");
		var galleryLink=document.createElement("li");
		galleryLink.style.textAlign="center";
		//galleryLink.onclick=new Function("modLoadGallery("+gallery_id+")");
		var galleryName=document.createElement("h3");
		galleryName.appendChild(document.createTextNode(gallery_name));
		galleryLink.appendChild(galleryName);
		for(var i=0;i<modImgArray[gallery_id].length;i++){
			var previewImg=document.createElement("img");	
			previewImg.id=gallery_id+"_"+i;
			previewImg.alt="";
			previewImg.style.width="30px";
			previewImg.style.backgroundColor="#666666";
			previewImg.style.height="30px";
			previewImg.style.margin="5px";
			previewImg.style.cursor="pointer";
			previewImg.src=modImgArray[gallery_id][i];	
			previewImg.onmouseover=new Function("modSetTransparency(this,100)");
			previewImg.onmouseout=new Function("modSetTransparency(this,50)");
			previewImg.onclick=new Function("modSetActiveId("+gallery_id+","+i+");modLoadImage('pageImageContainer',this.src,"+modWidthArray[gallery_id][i]+","+modHeightArray[gallery_id][i]+",'')");
			modSetTransparency(previewImg,50);
			galleryLink.appendChild(previewImg);
		}
		parentElement.appendChild(galleryLink);
	}
	else{
		window.setTimeout(new Function("modAddGalleryControl("+gallery_id+",'"+gallery_name+"')"),50);
	}
}