//**************************************************************************************************/
//	Terminbox v3.10
//	<a href="'img datei" rel="Terminbox"  onclick="myTerminbox.start(this, false); return false;" target="_blank" title="'name des objektes">
//	 Author: Markus F. Hay
//  Website: http://www.dolem.com/Terminbox
//	   Date: May 28, 2007
//	License: Creative Commons Attribution 2.5 License (http://creativecommons.org/licenses/by/2.5/)
//**************************************************************************************************/
Array.prototype.removeDuplicates = function () { for (var i = 1; i < this.length; i++) { if (this[i][0] == this[i-1][0]) { this.splice(i,1); } } }
Array.prototype.empty = function () { for (var i = 0; i <= this.length; i++) { this.shift(); } }

function Terminbox() {
	/*** Start Global Configuration ***/
		this.loadedName 		= '';
		this.imageArray	 		= new Array();
		this.imgurl				= '';
		this.aktiv_number		= 0;
		
		this.theme				= 'grey';	// themes: grey (default), red, green, blue, gold
		this.hideFlash			= true;		// controls whether or not Flash objects should be hidden
		this.outerBorder		= false;	// controls whether to show the outer grey (or theme) border
		this.resizeSpeed		= 8;		// controls the speed of the image resizing (1=slowest and 10=fastest)
		this.borderSize			= 12;		// if you adjust the padding in the CSS, you will need to update this variable
		this.maxOpacity			= 80;		// higher opacity = darker overlay, lower opacity = lighter overlay
		this.navType			= 1;		// 1 = "Prev/Next" buttons on top left and left (default), 2 = "<< prev | next >>" links next to image number
		this.autoResize			= true;		// controls whether or not images should be resized if larger than the browser window dimensions
	/*** End Global Configuration ***/
	
	/*** Configure Slideshow Options ***/
		this.slideInterval		= 5000;		// Change value (milliseconds) to increase/decrease the time between "slides"
		this.showNavigation		= true;		// true to display Next/Prev buttons/text during slideshow, false to hide
		this.showClose			= true;		// true to display the Close button, false to hide
		this.showDetails		= true;		// true to display image details (caption, count), false to hide
		this.showPlayPause		= true;		// true to display pause/play buttons next to close button, false to hide
		this.autoEnd			= true;		// true to automatically close Terminbox after the last image is reached, false to keep open
		this.pauseOnNextClick	= false;	// true to pause the slideshow when the "Next" button is clicked
        this.pauseOnPrevClick 	= true;		// true to pause the slideshow when the "Prev" button is clicked
	/*** End Slideshow Configuration ***/
	
	if(this.resizeSpeed > 10) { this.resizeSpeed = 10; }
	if(this.resizeSpeed < 1) { resizeSpeed = 1; }
	this.resizeDuration = (11 - this.resizeSpeed) * 0.15;	
	this.resizeWTimerArray		= new Array();
	this.resizeWTimerCount		= 0;
	this.resizeHTimerArray		= new Array();
	this.resizeHTimerCount		= 0;
	this.showImageTimerArray	= new Array();
	this.showImageTimerCount	= 0;
	this.overlayTimerArray		= new Array();
	this.overlayTimerCount		= 0;
	this.imageTimerArray		= new Array();
	this.imageTimerCount		= 0;
	this.timerIDArray			= new Array();
	this.timerIDCount			= 0;
	this.slideshowIDArray		= new Array();
	this.slideshowIDCount		= 0;
	//this.imageArray	 = new Array();
	this.activeImage = null;
	this.slideArray	 = new Array();
	this.activeSlide = null;
	this.checkFrame();
	this.isSlideshow = false;
	/*@cc_on
		/*@if (@_jscript)
			this.ie = (document.all && !window.opera) ? true : false;
		/*@else @*/
			this.ie = false;
		/*@end
	@*/
	
	this.ie7 = (this.ie && window.XMLHttpRequest);
	this.initialize();
}
Terminbox.prototype.initialize = function() {
	
	if (!document.getElementsByTagName) { return; }
	var anchors = (this.isFrame) ? window.parent.frames[window.name].document.getElementsByTagName('a') : document.getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];	
		var relAttribute = String(anchor.getAttribute('rel'));
		if (anchor.getAttribute('href')) {
			if (relAttribute.toLowerCase().match('terminbox')) {
				anchor.onclick = function () { myTerminbox.start(this, false); return false; }
			}
		}
	}

	var objBody = this.doc.getElementsByTagName("body").item(0);	
	if (this.doc.getElementById('tpOverlay')) {
		objBody.removeChild(this.doc.getElementById("tpOverlay"));
		objBody.removeChild(this.doc.getElementById("tpMain"));
	}
	var objOverlay = this.doc.createElement("div");
		objOverlay.setAttribute('id','tpOverlay');
		objOverlay.setAttribute((this.ie ? 'className' : 'class'), this.theme);
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);	
		
	var objMain = this.doc.createElement("div");
		objMain.setAttribute('id','tpMain');
		objMain.style.display = 'none';
		objBody.appendChild(objMain);	
	
	
	
	var objMainContainerholder = this.doc.createElement("div");
		objMainContainerholder.setAttribute('id','tpMainContainerholder');
		//objOuterImageContainer.setAttribute((this.ie ? 'className' : 'class'), this.theme);
		objMain.appendChild(objMainContainerholder);
	
	var objMainContainer = this.doc.createElement("div");
		objMainContainer.setAttribute('id','tpMainContainer');
		//objOuterImageContainer.setAttribute((this.ie ? 'className' : 'class'), this.theme);
		objMainContainerholder.appendChild(objMainContainer);
		
	var objTextContainer = this.doc.createElement("div");
		objTextContainer.setAttribute('id','tpTextContainer');
		objMainContainer.appendChild(objTextContainer);
		
	var objPicContainer = this.doc.createElement("div");
		objPicContainer.setAttribute('id','tpPicContainer');
		objMainContainer.appendChild(objPicContainer);
	
	
	var objImage = this.doc.createElement("img");
		objImage.setAttribute('id','tpImage');
		objPicContainer.appendChild(objImage);		
		
	var objCloseContainer = this.doc.createElement("div");
		objCloseContainer.setAttribute('id','tpCloseContainer');
		objMainContainer.appendChild(objCloseContainer);
	
	var objClose = this.doc.createElement("a");
		objClose.setAttribute('id','tpClose');
		objClose.setAttribute('href','#');
		objCloseContainer.appendChild(objClose);	
		
		
};

/*start des popup*/
Terminbox.prototype.start = function(imageLink, doSlide) {
	if (this.ie && !this.ie7) {	this.toggleSelects('hide');	}
	if (this.hideFlash) { this.toggleFlash('hide'); }
	var pageSize	= this.getPageSize();
	var objOverlay	= this.doc.getElementById('tpOverlay');
	var objBody		= this.doc.getElementsByTagName("body").item(0);
	//mask
	objOverlay.style.height = pageSize[1] + "px";
	objOverlay.style.display = '';
	this.appear('tpOverlay', 0);
	//close mask
	this.doc.getElementById('tpOverlay').onclick = function() { myTerminbox.end(); return false; }
	this.doc.getElementById('tpClose').onclick = function() { myTerminbox.end(); return false; }
	
	
	this.imageArray = new Array();

	 var object = this.doc.getElementById('tpMain');
		object.style.top = (this.getPageScroll() + (pageSize[3] / 15)) + "px";
		object.style.display = '';
		
		
	this.doc.getElementById('tpTextContainer').style.display = 'none';
	this.doc.getElementById('tpPicContainer').style.display = 'none';
	this.doc.getElementById('tpImage').style.display = 'none';
	this.doc.getElementById('tpCloseContainer').style.display = 'none';

	if (!document.getElementsByTagName){ return; }
		//laden der Textinhalte!
		var Txtobject = this.doc.getElementById('tpTextContainer');
		if((imageLink.getAttribute('rel') == 'terminbox')) {
			this.loadedName = imageLink.getAttribute('name');
			this.loadContent(this.loadedName, Txtobject);
			this.loadingImageCon(this.loadedName);
		}
};
Terminbox.prototype.loadContent = function (url, conObj){
	
	url = "./templates/termine_content.php?id=" + url;
	var page_request = false;
	if (window.XMLHttpRequest){
		// if Mozilla, Safari etc
		page_request = new XMLHttpRequest();	
	}else if (window.ActiveXObject){ // if IE
			try {
				page_request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e){}
		}
	}else{
		return false;
	}
	page_request.onreadystatechange=function(){
		loadpage(page_request);
	}
		page_request.open('GET', url, true);
		page_request.send(null);

	function loadpage(page_request){
		if (page_request.readyState == 4){
			conObj.innerHTML = page_request.responseText;
			conObj.style.display = '';
		}
	}
};
Terminbox.prototype.loadingImageCon = function (url){
	
	url = "./templates/termine_img.php?id=" + url;
	var page_request = false;
	if (window.XMLHttpRequest){
		// if Mozilla, Safari etc
		page_request = new XMLHttpRequest();	
	}else if (window.ActiveXObject){ // if IE
			try {
				page_request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e){}
		}
	}else{
		return false;
	}
	
	page_request.onreadystatechange=function(){
		loadpage(page_request);
	}
		page_request.open('GET', url, true);
		page_request.send(null);
	function loadpage(page_request){
		if (page_request.readyState == 4){
			myTerminbox.explodeImgData(page_request.responseText);
		}
	}
};
Terminbox.prototype.explodeImgData = function(txtimg) {
	
	if(txtimg == 'null'){
		this.imageArray = new Array();
		this.imageArray.push(new Array('spacer.gif', 'none'));
	}else{
		this.imageArray = new Array();
		var img = txtimg.split("#");	
		this.imageArray.push(new Array(img[0], img[1]));
	}
		//laden des img
		this.loadingImage(0);
}
Terminbox.prototype.loadingImage = function(imageNum) {
	
	
	imgPreloader = new Image();
	imgPreloader.onload = function() {
		var imageWidth = imgPreloader.width;
		var imageHeight = imgPreloader.height;
		var tpImage = myTerminbox.doc.getElementById('tpImage');
		tpImage.src = myTerminbox.imgurl;
		tpImage.width = imageWidth;
		tpImage.height = imageHeight;
		myTerminbox.showImage();
		imgPreloader.onload = function() {};
	}
	//this.aktiv_number = imageNum;
	this.imgurl = "./content/" + terminbox_termine + "/" + this.imageArray[imageNum][0];
	imgPreloader.src = this.imgurl;
};
Terminbox.prototype.showImage = function() {
		//this.updateNav();
		this.doc.getElementById('tpTextContainer').style.display = '';
		this.doc.getElementById('tpPicContainer').style.display = '';
		this.doc.getElementById('tpImage').style.display = '';
		this.doc.getElementById('tpCloseContainer').style.display = '';
		this.appear('tpImage', 0);
};
/*alle Navigationspunkte setzen*/
Terminbox.prototype.updateNav = function() {

	if(this.imageArray.length > 1){
			//prev
			var object =  this.doc.getElementById('tpPrev');
			object.onclick = function() {
				myTerminbox.loadingImage(Number(myTerminbox.aktiv_number) - 1); return false;
			}
			//next
			var object =  this.doc.getElementById('tpNext');
			object.onclick = function() {
				myTerminbox.loadingImage(Number(myTerminbox.aktiv_number) + 1); return false;
			}
			
		for (var i = 0; i < this.imageArray.length; i++) {
			var object =  this.doc.getElementById('tpImg_' + (i+1));
			object.onclick = function() {
				myTerminbox.loadingImage(this.title); return false;
			}
		}
	}
	
};
Terminbox.prototype.togglePlayPause = function(hideID, showID) {
	if (this.isSlideshow && hideID == "tpPause") {
		for (var i = 0; i < this.slideshowIDCount; i++) { window.clearTimeout(this.slideshowIDArray[i]); }
	}
	this.doc.getElementById(hideID).style.display = 'none';
	this.doc.getElementById(showID).style.display = '';
	if (hideID == "tpPlay") {
		this.isPaused = false;
		if (this.activeSlide == (this.slideArray.length - 1)) {
			this.end();
		} else {
			this.changeImage(this.activeSlide + 1);
		}
	} else {
		this.isPaused = true;
	}
};
Terminbox.prototype.end = function(caller) {
	var closeClick = (caller == 'slideshow' ? false : true);
	if (this.isSlideshow && this.isPaused && !closeClick) { return; }	
	//this.disableKeyboardNav();
	this.doc.getElementById('tpMain').style.display = 'none';
	this.fade('tpOverlay', this.maxOpacity);
	this.toggleSelects('visible');
	if (this.hideFlash) { this.toggleFlash('visible'); }
	if (this.isSlideshow) {
		for (var i = 0; i < this.slideshowIDCount; i++) { window.clearTimeout(this.slideshowIDArray[i]); }
	}
};
Terminbox.prototype.checkFrame = function() {
	if (window.parent.frames[window.name] && (parent.document.getElementsByTagName('frameset').length <= 0)) {
		this.isFrame = true;
		this.Terminbox = "window.parent." + window.name + ".myTerminbox";
		this.doc = parent.document;
	} else {
		this.isFrame = false;
		this.Terminbox = "myTerminbox";
		this.doc = document;
	}
};
Terminbox.prototype.getPixelRate = function(cur, img) {
	var diff = (img > cur) ? img - cur : cur - img;	
	if (diff >= 0 && diff <= 100) { return 4; }
	if (diff > 100 && diff <= 200) { return 8; }
	if (diff > 200 && diff <= 300) { return 12; }
	if (diff > 300 && diff <= 400) { return 16; }
	if (diff > 400 && diff <= 500) { return 20; }
	if (diff > 500 && diff <= 600) { return 24; }
	if (diff > 600 && diff <= 700) { return 28; }
	if (diff > 700) { return 32; }
};
Terminbox.prototype.appear = function(id, opacity) {
	var object = this.doc.getElementById(id).style;
	object.opacity = (opacity/100);
	object.MozOpacity = (opacity/100);
	object.KhtmlOpacity = (opacity/100);
	object.filter = "alpha(opacity=" + (opacity+10) + ")";	
	if (opacity == 100 && id == 'tpImage') {
		//this.updateDetails();
	} else if (opacity == this.maxOpacity && id == 'tpOverlay') {
		for (var i = 0; i < this.overlayTimerCount; i++) { window.clearTimeout(this.overlayTimerArray[i]); }
		return;
	} else if (opacity == 100 && id == 'tpPicContainer') {
		for (var i = 0; i < this.imageTimerCount; i++) { window.clearTimeout(this.imageTimerArray[i]); }
		this.doc.getElementById('tpOverlay').style.height = this.getPageSize()[1] + "px";
	} else {
		if (id == 'tpOverlay') {
			this.overlayTimerArray[this.overlayTimerCount++] = setTimeout("myTerminbox.appear('" + id + "', " + (opacity+20) + ")", 1);
		} else {
			this.imageTimerArray[this.imageTimerCount++] = setTimeout("myTerminbox.appear('" + id + "', " + (opacity+10) + ")", 1);
		}
	}
};
Terminbox.prototype.fade = function(id, opacity) {
	var object = this.doc.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";	
	if (opacity == 0) {
		try {
			object.display = 'none';
		} catch(err) { }
	} else if (id == 'tpOverlay') {
		this.overlayTimerArray[this.overlayTimerCount++] = setTimeout("myTerminbox.fade('" + id + "', " + (opacity-20) + ")", 1);
	} else {
		this.timerIDArray[this.timerIDCount++] = setTimeout("myTerminbox.fade('" + id + "', " + (opacity-10) + ")", 1);
	}
};
Terminbox.prototype.resizeW = function(id, curW, maxW, pixelrate, speed) {
	if (!this.hDone) {
		this.resizeWTimerArray[this.resizeWTimerCount++] = setTimeout("myTerminbox.resizeW('" + id + "', " + curW + ", " + maxW + ", " + pixelrate + ")", 100);
		return;
	}	
	var object = this.doc.getElementById(id);
	var timer = speed ? speed : (this.resizeDuration/2);	
	object.style.width = (curW) + "px";	
	if (curW < maxW) {
		curW += (curW + pixelrate >= maxW) ? (maxW - curW) : pixelrate;
	} else if (curW > maxW) {
		curW -= (curW - pixelrate <= maxW) ? (curW - maxW) : pixelrate;
	}
	this.resizeWTimerArray[this.resizeWTimerCount++] = setTimeout("myTerminbox.resizeW('" + id + "', " + curW + ", " + maxW + ", " + pixelrate + ", " + (timer+0.02) + ")", timer+0.02);	
	if (parseInt(object.style.width) == maxW) {
		this.wDone = true;
		for (var i = 0; i < this.resizeWTimerCount; i++) { window.clearTimeout(this.resizeWTimerArray[i]); }
	}
};
Terminbox.prototype.resizeH = function(id, curH, maxH, pixelrate, speed) {
	var timer = speed ? speed : (this.resizeDuration/2);
	var object = this.doc.getElementById(id);	
	object.style.height = (curH) + "px";	
	if (curH < maxH) {
		curH += (curH + pixelrate >= maxH) ? (maxH - curH) : pixelrate;
	} else if (curH > maxH) {
		curH -= (curH - pixelrate <= maxH) ? (curH - maxH) : pixelrate;
	}
	this.resizeHTimerArray[this.resizeHTimerCount++] = setTimeout("myTerminbox.resizeH('" + id + "', " + curH + ", " + maxH + ", " + pixelrate + ", " + (timer+.02) + ")", timer+.02);	
	if (parseInt(object.style.height) == maxH) {
		this.hDone = true;
		for (var i = 0; i < this.resizeHTimerCount; i++) { window.clearTimeout(this.resizeHTimerArray[i]); }
	}
};
Terminbox.prototype.getPageScroll = function() {
	if (self.pageYOffset) {
		return this.isFrame ? parent.pageYOffset : self.pageYOffset;
	} else if (this.doc.documentElement && this.doc.documentElement.scrollTop){
		return this.doc.documentElement.scrollTop;
	} else if (document.body) {
		return this.doc.body.scrollTop;
	}
};
Terminbox.prototype.getPageSize = function() {	
	var xScroll, yScroll, windowWidth, windowHeight;	
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = this.doc.scrollWidth;
		yScroll = (this.isFrame ? parent.innerHeight : self.innerHeight) + (this.isFrame ? parent.scrollMaxY : self.scrollMaxY);
	} else if (this.doc.body.scrollHeight > this.doc.body.offsetHeight){
		xScroll = this.doc.body.scrollWidth;
		yScroll = this.doc.body.scrollHeight;
	} else {
		xScroll = this.doc.getElementsByTagName("html").item(0).offsetWidth;
		yScroll = this.doc.getElementsByTagName("html").item(0).offsetHeight;
		xScroll = (xScroll < this.doc.body.offsetWidth) ? this.doc.body.offsetWidth : xScroll;
		yScroll = (yScroll < this.doc.body.offsetHeight) ? this.doc.body.offsetHeight : yScroll;
	}	
	if (self.innerHeight) {
		windowWidth = (this.isFrame) ? parent.innerWidth : self.innerWidth;
		windowHeight = (this.isFrame) ? parent.innerHeight : self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = this.doc.documentElement.clientWidth;
		windowHeight = this.doc.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = this.doc.getElementsByTagName("html").item(0).clientWidth;
		windowHeight = this.doc.getElementsByTagName("html").item(0).clientHeight;
		windowWidth = (windowWidth == 0) ? this.doc.body.clientWidth : windowWidth;
		windowHeight = (windowHeight == 0) ? this.doc.body.clientHeight : windowHeight;
	}
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;	
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
};
Terminbox.prototype.toggleFlash = function(state) {
	var objects = this.doc.getElementsByTagName("object");
	for (var i = 0; i < objects.length; i++) {
		objects[i].style.visibility = (state == "hide") ? 'hidden' : 'visible';
	}
	var embeds = this.doc.getElementsByTagName("embed");
	for (var i = 0; i < embeds.length; i++) {
		embeds[i].style.visibility = (state == "hide") ? 'hidden' : 'visible';
	}	
	if (this.isFrame) {
		for (var i = 0; i < parent.frames.length; i++) {
			objects = parent.frames[i].window.document.getElementsByTagName("object");
			for (var j = 0; j < objects.length; j++) {
				objects[j].style.visibility = (state == "hide") ? 'hidden' : 'visible';
			}
			embeds = parent.frames[i].window.document.getElementsByTagName("embed");
			for (var j = 0; j < embeds.length; j++) {
				embeds[j].style.visibility = (state == "hide") ? 'hidden' : 'visible';
			}
		}
	}
};
Terminbox.prototype.toggleSelects = function(state) {
	var selects = this.doc.getElementsByTagName("select");
	for (var i = 0; i < selects.length; i++ ) {
		selects[i].style.visibility = (state == "hide") ? 'hidden' : 'visible';
	}
	if (this.isFrame) {
		for (var i = 0; i < parent.frames.length; i++) {
			selects = parent.frames[i].window.document.getElementsByTagName("select");
			for (var j = 0; j < selects.length; j++) {
				selects[j].style.visibility = (state == "hide") ? 'hidden' : 'visible';
			}
		}
	}
};
Terminbox.prototype.pause = function(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime) { return; }
	}
};
/*--- init ----*/
if (window.addEventListener) {
	window.addEventListener("load",initTerminbox,false);
} else if (window.attachEvent) {
	window.attachEvent("onload",initTerminbox);
} else {
	window.onload = function() {initTerminbox();}
}
function initTerminbox() { myTerminbox = new Terminbox(); }
