var iIBAnker = "#";
var iIBImageArr = new Array();
var iIBActID = null;
var iIBWindowMinHeight = 400;
var iIBWindowMinWidth = 400;

var iIBListEntryWidth = 52;
var iIBListEntryHeight = 39;

var iIBCloseText = "schlie&szlig;en";

function iIB_PreloadImages()
{
	for(var i = 0; i < iIBImageArr.length; i++)
	{
		iIBImageArr[i].image = new Image();
		iIBImageArr[i].image.src = iIBImageArr[i].url;
	}
}

function iIB_Show_From_ImageArr(id, ako)
{
	isAko = (ako != null ? ako : false);
	iIBActID = id;
	iIB_Show(iIBImageArr[id].text, iIBImageArr[id].url, iIBImageArr[id].bez, id);
}

function iIB_Show(caption, url, head, id) 
{
	var closeBtn = (isAko ? "/globalimages/ako/close_button_schatten.png" : "/globalimages/close_button_schatten.png");
	
	var orginalUrl = url;
	var iIBWindow = document.getElementById("iIB_window");
	var iIBOverlay = document.getElementById("iIB_overlay");
	if (typeof document.body.style.maxHeight === "undefined") //if IE 6
	{
		document.body.style.cssText = "height: 100%; width: 100%;";
		if (document.getElementById("iIB_HideSelect") === null) 
		{
			var iframe = document.createElement("iframe");
			iframe.id = "iIB_HideSelect";
			document.body.appendChild(iframe);
			
			iIBOverlay = document.createElement("div");
			iIBOverlay.id = "iIB_overlay";
			iIB_addEventHandler(iIBOverlay, "click", iIB_Remove);
			document.body.appendChild(iIBOverlay);
			
			iIBWindow = document.createElement("div");
			iIBWindow.id = "iIB_window";
			document.body.appendChild(iIBWindow);
		}
	}
	else //all others
	{
		if(document.getElementById("iIB_overlay") === null)
		{
			iIBOverlay = document.createElement("div");
			iIBOverlay.id = "iIB_overlay";
			iIB_addEventHandler(iIBOverlay, "click", iIB_Remove);
			document.body.appendChild(iIBOverlay);
			
			iIBWindow = document.createElement("div");
			iIBWindow.id = "iIB_window";
			document.body.appendChild(iIBWindow);
		}
	}
	
	if(iIB_detectMacXFF())
	{
		iIBOverlay.className = "iIB_overlayMacFFBGHack";
	}
	else
	{
		iIBOverlay.className = "iIB_overlayBG";
	}
	
	if(caption === null)
	{
		caption = "";
	}
	
	var baseURL = (url.indexOf("?") !== -1 ? url.substr(0, url.indexOf("?")) : url);
	var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	var urlType = baseURL.toLowerCase().match(urlString);

	if(urlType != '.jpg' && urlType != '.jpeg' && urlType != '.png' && urlType != '.gif' &&	 urlType != '.bmp')
	{
		url = "/images/download.gif";
	}
	
	var iIB_imageList = "";
	var iIB_imageCount = "";
	var iIB_FoundURL = false;
	var max_imageHeight = 0;
	var max_imageWidth = 0;
	
	if(iIBImageArr.length > 0)
	{
		for (iIB_Counter = 0; iIB_Counter < iIBImageArr.length; iIB_Counter++) 
		{
			var tmpClass = "iIB_imageListEntry";
			
			if (iIBImageArr[iIB_Counter].url == url)
			{
				iIB_FoundURL = true;
				tmpClass = "iIB_imageListEntryActive";
			}
			
			if(iIBImageArr[iIB_Counter] != null)
			{
				var resizeObj = iIB_imageResize(iIBImageArr[iIB_Counter].image.width, iIBImageArr[iIB_Counter].image.height, 39, 29);
				
				iIB_imageList += "<div class='" + tmpClass + "'>";
				iIB_imageList += "<table onclick='iIBGoTo(" + iIB_Counter + ")' cellpadding='0' cellspacing='0' border='0' style='width:39px !important; height:29px !important'><tr><td valign='middle' align='center'>"
				iIB_imageList += "<img src='"  + iIBImageArr[iIB_Counter].image.src + "' height='" + resizeObj.height + "' width='" + resizeObj.width + "' />";
				iIB_imageList += "</td></tr></table></div>";
				
				if(iIBImageArr[iIB_Counter].image.width > max_imageWidth)
				{
					max_imageWidth = iIBImageArr[iIB_Counter].image.width;
				}
				if(iIBImageArr[iIB_Counter].image.height > max_imageHeight)
				{
					max_imageHeight = iIBImageArr[iIB_Counter].image.height;
				}
			}
		}
		max_imageWidth += 10;
		max_imageHeight += 10;
		
		iIBImage = iIBImageArr[id].image;
	}
	else
	{
		iIBImage = new Image();
		iIBImage.src = url;
	}

	var pagesize = iIB_getPageSize();
	var x = pagesize[0] - 150;
	var y = pagesize[1] - 150;
	var imageWidth = iIBImage.width;
	var imageHeight = iIBImage.height;
	
	var resizeObj = iIB_imageResize(imageWidth, imageHeight, x, y);
	
	imageWidth = resizeObj.width;
	imageHeight = resizeObj.height;
	
	
	iIB_WIDTH = ((max_imageWidth + 30) > iIBWindowMinWidth ? (max_imageWidth + 30) : iIBWindowMinWidth);
	max_imageWidth = iIB_WIDTH - 30;
	
	iIBWindow.innerHTML = (head != null ? "<div id='iIB_head'>" + head + "</div>" : "") + 
		"<div id='iIB_secondLine'>" + iIB_imageCount + "</div>" + 
		"<div id='iIB_closeWindow'><a href='" + iIBAnker + "' id='iIB_closeWindowButton' title='" + iIBCloseText + "'><img src='" + closeBtn + "' /></a></div>" + 
		"<div class='clear'></div>" + 
		(caption != null ? "<div id='iIB_caption' style='width: " + (iIB_WIDTH / 2 - 10) + "px;'>" + caption + "</div>" : "") + 
		"<table cellpadding='0' cellspacing='0' border='0' style='width:" + max_imageWidth + "px !important; height:" + max_imageHeight + "px !important'><tr><td valign='middle' align='center'>" + 
		"<a href='" + orginalUrl + "' target='_blank'>" + 
		"<img id='iIB_Image' src='/images/loading.gif' width='" + imageWidth + "' height='" + imageHeight + "' alt='" + caption + "'/>" + 
		"</a></td></tr></table>" + 
		"<div id='iIB_imageList'>" + iIB_imageList + "<div class='clear'></div></div>";
	
	var heightAdd = 85;
	
	if(caption != null)
	{
		var captionDiv = document.getElementById("iIB_caption");
		if(captionDiv.clientHeight > 30)
		{
			heightAdd = 55 + captionDiv.clientHeight;
		}
	}
	
	if(iIBImageArr.length > 0)
	{
		heightAdd = 55 + Math.ceil((iIBImageArr.length * iIBListEntryWidth) / max_imageWidth)*iIBListEntryHeight;
	}
	
	iIB_HEIGHT = ((max_imageHeight + heightAdd) > iIBWindowMinHeight ? (max_imageHeight + heightAdd) : iIBWindowMinHeight);
	
	
	document.getElementById("iIB_Image").src = iIBImage.src;
	
	var iIBCloseButton = document.getElementById("iIB_closeWindowButton");
	iIB_addEventHandler(iIBCloseButton, "click", iIB_Remove);
	
	iIB_Position(iIBWindow, iIB_WIDTH, iIB_HEIGHT);
}

function iIB_Position(iIBWindow, width, height)
{
	iIBWindow.style.cssText = "width: " + width + "px; height: " + height + "px; margin: -" + parseInt((height / 2), 10) + "px " + parseInt((width / 2), 10) + "px 0px -" + parseInt((width / 2), 10) + "px;";
}

function iIBGoPrev()
{
	iIB_Remove();
	iIB_Show_From_ImageArr(iIBActID-1, isAko);
	return false;	
}

function iIBGoNext()
{
	iIB_Remove();
	iIB_Show_From_ImageArr(iIBActID+1, isAko);
	return false;	
}

function iIBGoTo(id)
{
	iIB_Remove();
	iIB_Show_From_ImageArr(id, isAko);
	return false;
}

function iIB_Remove() 
{
	var iIBPrev = document.getElementById("iIB_prev");
	var iIBNext = document.getElementById("iIB_next");
	var iIBOverlay = document.getElementById("iIB_overlay");
	var iIBWindow = document.getElementById("iIB_window");
	var iIBHideSelect = document.getElementById("iIB_HideSelect");
	var iIBCloseButton = document.getElementById("iIB_closeWindowButton");
	
	if (iIBPrev != null)
	{
		iIB_removeEventHandler(iIBPrev, "click", iIBGoPrev);
	}
	if (iIBNext != null)
	{
		iIB_removeEventHandler(iIBNext, "click", iIBGoNext);
	}
	if (iIBCloseButton != null)
	{
		iIB_removeEventHandler(iIBCloseButton, "click", iIB_Remove);
	}
	if (iIBOverlay != null)
	{
		iIB_removeEventHandler(iIBOverlay, "click", iIB_Remove);
		iIBOverlay.parentNode.removeChild(iIBOverlay);
	}
	if (iIBWindow != null)
	{
		iIBWindow.parentNode.removeChild(iIBWindow);
	}
	if (iIBHideSelect != null)
	{
		iIBHideSelect.parentNode.removeChild(iIBHideSelect);
	}
	
	return false;
}

function iIB_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function iIB_detectMacXFF()
{
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1)
	{
		return true;
	}
}

function iIB_addEventHandler(elem, eventName, handler, useCapture)
{
	var isExplorer = ((navigator.userAgent.indexOf("MSIE 6") > -1) ||(navigator.userAgent.indexOf("MSIE 7") > -1) ||(navigator.userAgent.indexOf("MSIE 8") > -1)) && 
		(navigator.userAgent.indexOf("Opera") == -1) && (navigator.platform.indexOf("Win") == 0);
	var handlerRetVal = null;
	if (typeof useCapture === "undefined")
	{
		useCapture = false;
	}
	if (isExplorer)
	{
		handlerRetVal = elem.attachEvent("on"+eventName, handler);
	}
	else
	{
		handlerRetVal = elem.addEventListener(eventName, handler, useCapture);
	}
	return handlerRetVal;
}

function iIB_removeEventHandler(elem, eventName, handler, useCapture)
{
	var isExplorer = ((navigator.userAgent.indexOf("MSIE 6") > -1) ||(navigator.userAgent.indexOf("MSIE 7") > -1) ||(navigator.userAgent.indexOf("MSIE 8") > -1)) && 
		(navigator.userAgent.indexOf("Opera") == -1) && (navigator.platform.indexOf("Win") == 0);
	if (typeof useCapture === "undefined")
	{
		useCapture = false;
	}
	if (isExplorer)
	{
		elem.detachEvent("on"+eventName, handler);
	}
	else
	{
		elem.removeEventListener(eventName, handler, useCapture);
	}
}

function iIB_imageResize(imageWidth, imageHeight, maxWidth, maxHeight)
{
	var retObj = new Object();
		retObj.width = imageWidth;
		retObj.height = imageHeight;
	
	if (retObj.width > maxWidth)
	{
		retObj.height = retObj.height * (maxWidth / retObj.width); 
		retObj.width = maxWidth; 
		if (retObj.height > maxHeight)
		{
			retObj.width = retObj.width * (maxHeight / retObj.height); 
			retObj.height = maxHeight; 
		}
	} 
	else if (retObj.height > maxHeight) 
	{
		retObj.width = retObj.width * (maxHeight / retObj.height); 
		retObj.height = maxHeight; 
		if (retObj.width > maxWidth)
		{
			retObj.height = retObj.height * (maxWidth / retObj.width); 
			retObj.width = maxWidth;
		}
	}
	
	return retObj;
}
