/*
	DarkBox Image Viewer
	by Johannes Koch - http://www.misterjs.de.vu

	Viele weitere Scripte und genaue Anleitungen zur Nutzung finden sie auf:
	http://www.misterjs.de.vu

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	Das Script darf verändert werden, jedoch muss mein Name und ein Link auf meine Website erscheinen.

	Teile des Scripts stammen von:
    Lightbox JS -- Fullsize Image Overlays
	by Lokesh Dhakar - http://www.huddletogether.com

*/

createOverlay = function(){
// erstellt den halbdurchsichtigen Hintergrund
 var ObjectOverlay = document.createElement("div");
 ObjectOverlay.setAttribute('id','Overlay');
 ObjectOverlay.style.display = "none";

 ObjectOverlay.onclick = closeViewer;
 document.getElementsByTagName("Body").item(0).insertBefore(ObjectOverlay, document.getElementsByTagName("Body").item(0).firstChild);



// erstellt das Fenster
 var PicWindow = document.createElement("div");
 PicWindow.setAttribute("id","window");
 PicWindow.style.display = "none";

// FensterObjekt in das DOM einfügen
 document.getElementsByTagName("Body").item(0).insertBefore(PicWindow, document.getElementsByTagName("Body").item(0).firstChild);

//Preloading des Ladebalkens
 progressBar = new Image();

 progressBar.onload = function(){
  document.getElementById("progressBar").style.marginTop = "-11px";
  document.getElementById("progressBar").style.marginLeft = "-63px";
  document.getElementById("progressBar").style.Top = "50%";
  document.getElementById("progressBar").style.Left = "50%";
 }
 progressBar.src = "../img/darkbox/loading.gif";

//Den Ladebalken hinzufügen
 var progressBarObject = document.createElement("img");
 progressBarObject.setAttribute("id","progressBar");
 document.getElementById("window").appendChild(progressBarObject);

 document.getElementById("progressBar").src = progressBar.src;

//Prev- und Nextbutton hinzufügen
 var nextButton = document.createElement("img");
 var prevButton = document.createElement("img");

 nextButton.setAttribute("id","nextButton");
 nextButton.src = "../img/darkbox/next.gif";

 prevButton.setAttribute("id","prevButton");
 prevButton.src = "../img/darkbox/prev.gif";

 document.getElementById("window").appendChild(nextButton);
 document.getElementById("window").appendChild(prevButton);

// Create Close Button
 var CloseImage = document.createElement("img");

 CloseImage.setAttribute("id","closeImage");
 CloseImage.src = "../img/darkbox/close.gif";

// OnClick Event hinzufügen
 CloseImage.onclick = closeViewer;

// Bild erstellen und laden
 var image = document.createElement("img");
 image.setAttribute("id","image");
 document.getElementById("window").appendChild(image);

// Bildbeschreibug erstellen und hinzufügen
 var beschreibung = document.createElement("div");
 beschreibung.setAttribute("id","Notice");
 document.getElementById("window").appendChild(beschreibung);


// Schließbutton zum DOM hinzufügen
 document.getElementById("window").appendChild(CloseImage);
}

showWindow = function(object){
	arrayPageScroll = getPageScroll();
	arrayPageSize = getPageSize();
	document.getElementById("progressBar").style.display = "block";
	document.getElementById("image").style.visibility = "hidden";

	// Top ud Left Abstände berechnen
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 606) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 806) / 2);

	document.getElementById("window").style.top = (lightboxTop < 0) ? "0px" :  lightboxTop + "px";
	document.getElementById("window").style.left = (lightboxLeft < 0) ? "0px" :  lightboxLeft + "px";

	// Bild vorladen
	imgPreload = new Image();
	imgPreload.onload = function(){
		 document.getElementById("image").src = object.href;

		document.getElementById("window").style.height = imgPreload.height+ "px";
		document.getElementById("window").style.width  = imgPreload.width + "px";

		// Top ud Left Abstände berechnen
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 6-imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 6-imgPreload.width) / 2);

		document.getElementById("window").style.top = (lightboxTop < 0) ? "0px" :  lightboxTop + "px";
		document.getElementById("window").style.left = (lightboxLeft < 0) ? "0px" :  lightboxLeft + "px";

		document.getElementById("progressBar").style.display = "none";
		document.getElementById("image").style.visibility='visible';

		try{
	//Next- und PrevEvents hinzufügen
	                 if(object.nextSibling.nextSibling != null && object.nextSibling.nextSibling.rel == "DarkBox"){
	                         document.getElementById("nextButton").onclick = function(){
	                                 showWindow(object.nextSibling.nextSibling);
	                         }
	                 }
	                 if(object.previousSibling.previousSibling!= null && object.previousSibling.previousSibling.rel == "DarkBox"){
	                         document.getElementById("prevButton").onclick = function(){
	                                 showWindow(object.previousSibling.previousSibling);
	                         }
	                 }
	        }catch(err){}

	}
	imgPreload.src = object.href;


	// Backgroundlayer an richtige Stelle rücken wenn gescrollt wurde
	document.getElementById("Overlay").style.height = arrayPageSize[1]+"px";

	var not = document.getElementById("Notice");

	// Beschreibung hinzufügen und ausrichten
	if((object.title) && (object.title.length>0)){
	  not.innerHTML = object.title;
	  not.style.display = "block";
	}else{
	  not.style.display = "none";
	}

	// Overlay und Bild sichtbar machen
	document.getElementById("window").style.display = "block";
	document.getElementById("Overlay").style.display = "block";
}

closeViewer = function(){
 document.getElementById("window").style.display = "none";
 document.getElementById("Overlay").style.display = "none";
}

//-------------------------------------------------------------------------------------------------------

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}