//
//	Configuration
//

var overlayOpacity = 0.5;	// controls transparency of shadow overlay

var overlayDuration = 1;

var label = '';

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
    for(i = 0; i < this.length; i++){
        for(j = this.length-1; j>i; j--){        
            if(this[i][0] == this[j][0]){
                this.splice(j,1);
            }
        }
    }
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

// -----------------------------------------------------------------------------------

//
//	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
//
var PopupBox = Class.create();

PopupBox.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Calls updateImageList and then
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function() {	

		// Code inserts html at the bottom of the page that looks similar to this:
		//
		//	<div id="overlay" style="display: none;">&nbsp;</div>
		//	<div id="popup">
		//		<div id="popupBox">
		//			<div class="top">&nbsp;</div>
		//			<div class="content">
		//				<span class="clear">&nbsp;</span>
		//				<div class="header">
		//					<h2 class="title" id="boxTitle">Sonnenenergie</h2>
		//					<span class="loading" id="boxLoading"><img src="images/loading.gif" width="16" height="16" alt="Lade, bitte warten..."/></span>
		//					<span class="clear">&nbsp;</span>
		//				</div>
		//				<div class="text" id="boxText">
		//					....
		//				</div>
		//				<div class="footer"><a href="javascript:void(0);" onclick="" class="button close"><b><b><b>Schlie&szlig;en</b></b></b></a></div>
		//			</div>
		//			<div class="bottom">&nbsp;</div>
		//		</div>
		//	</div>

		
		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objOverlay.style.zIndex = '9999';
		objOverlay.onclick = function() { myPopupBox.end(); }
		objBody.appendChild(objOverlay);
		
		var objPopup = document.createElement("div");
		objPopup.setAttribute('id','popup');
		objPopup.style.display = 'none';
		objPopup.style.zIndex = '99999999';
		objBody.appendChild(objPopup);
		
		var objPopupBox = document.createElement("div");
		objPopupBox.setAttribute('id','popupBox');
		objPopup.appendChild(objPopupBox);
		
		var objBorderTop = document.createElement("div");
		objBorderTop.className = 'ptop';
		objBorderTop.innerHTML = '&nbsp;';
		objPopupBox.appendChild(objBorderTop);
		
		var objContent = document.createElement("div");
		objContent.className = 'pcontent';
		objPopupBox.appendChild(objContent);
		
		var objClear = document.createElement("span");
		objClear.className = 'clear';
		objClear.innerHTML = '&nbsp;';
		objContent.appendChild(objClear);
		
		var objHeader = document.createElement("div");
		objHeader.className = 'pheader';
		objContent.appendChild(objHeader);
		
		var objH2 = document.createElement("h2");
		objH2.className = 'ptitle';
		objH2.setAttribute('id','boxTitle');
		objH2.innerHTML = 'titel';
		objHeader.appendChild(objH2);
		
		var objLoading = document.createElement("span");
		objLoading.className = 'ploading';
		objLoading.setAttribute('id','boxLoading');
		objHeader.appendChild(objLoading);
		
		var objLoadingImg = document.createElement("img");
		objLoadingImg.setAttribute('src','images/loading.gif');
		objLoading.appendChild(objLoadingImg);
		
		objClear = document.createElement("span");
		objClear.className = 'clear';
		objClear.innerHTML = '&nbsp;';
		objHeader.appendChild(objClear);
		
		var objText = document.createElement("div");
		objText.setAttribute('id','boxText');
		objText.className = 'ptext';
		objText.innerHTML = 'text';
		objContent.appendChild(objText);
		
		var objFooter = document.createElement("div");
		objFooter.className = 'pfooter';
		objContent.appendChild(objFooter);
		
		/*
		var objImprint = document.createElement("a");
		objImprint.className = 'imprint';
		objImprint.setAttribute('href',"javascript:myPopupBox.load('impressum');");
		objImprint.innerHTML = 'Impressum';
		objFooter.appendChild(objImprint);
		*/
		
		var objLink = document.createElement("a");
		objLink.className = 'button close';
		objLink.setAttribute('href','javascript:void(0);');
		objLink.onclick = function() { myPopupBox.closeit(); }
		objLink.innerHTML = '<b><b><b>Schlie&szlig;en</b></b></b>';
		objFooter.appendChild(objLink);
		
		var objBorderBottom = document.createElement("div");
		objBorderBottom.className = 'pbottom';
		objBorderBottom.innerHTML = '&nbsp;';
		objPopupBox.appendChild(objBorderBottom);
		
		correctBackgrounds();
	},


	//
	//	show()
	//	Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
	//
	show: function(sTitle, sBody, bAnimate) {

		if(typeof bAnimate == 'undefined')
		{
			var bAnimate = true;
		}
		
		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		Element.setWidth('overlay', arrayPageSize[0]);
		Element.setHeight('overlay', arrayPageSize[1]);
		if(bAnimate)
		{
			new Effect.Appear('overlay', { duration: overlayDuration, from: 0.0, to: overlayOpacity });
			
			window.setTimeout("Element.show('popup')", (overlayDuration * 1000));
		}
		else
		{
			//new Effect.Appear('overlay', { duration: 0.1, from: 0.0, to: overlayOpacity });
			
			Element.show('overlay');
			
			Element.show('popup');
		}
		
		this.load(sTitle, sBody, bAnimate);

		this.enableKeyboardNav();

	},
	
	parseLinks: function(objText) {
	
		var aLinks = $(objText).getElementsByTagName("a");
		
		for(var i = 0 ; i < aLinks.length ; i++)
		{
			if(aLinks[i].rel)
			{
				objElement = aLinks[i];
				label = objElement.rel;
				objElement.setAttribute('href', "javascript:myPopupBox.load('"+label+"')");
				//objElement.setAttribute('href', "javascript:void(0);");
				//objElement.setAttribute('title', label);
				//objElement.onclick = function() {myPopupBox.load(label)};
			}
		}
	},
	
	
	loadContent: function(sTitle, sBody) {
		
		$('boxTitle').innerHTML = sTitle;
		$('boxText').innerHTML = sBody;
		
		//this.parseLinks('boxText');
		
		correctBackgrounds();
	},
	
	
	load: function(sTitle, sBody, bAnimate) {
		
		/*
		if(typeof urchinTracker != 'undefined')
		{
			urchinTracker(objText);
		}
		*/
		
		if(typeof bAnimate == 'undefined')
		{
			var bAnimate = true;
		}
		
		$('boxLoading').innerHTML = '<img src="images/loading.gif" width="16" height="16" alt="Lade, bitte warten..."/>'
		
		if(0 && bAnimate)
		{
			window.setTimeout("myPopupBox.loadContent("+sTitle+"','"+sBody+"')", 1000);
			
			window.setTimeout("$('boxLoading').innerHTML = ''", 1200);
		}
		else
		{
			this.loadContent(sTitle, sBody);
			
			$('boxLoading').innerHTML = '';
		}
	
	},

	//
	//	enableKeyboardNav()
	//
	enableKeyboardNav: function() {
		document.onkeydown = this.keyboardAction; 
	},

	//
	//	disableKeyboardNav()
	//
	disableKeyboardNav: function() {
		document.onkeydown = '';
	},

	//
	//	keyboardAction()
	//
	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
		}
		
		key = String.fromCharCode(keycode).toLowerCase();
		
		//if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){	// close lightbox
		if(keycode == escapeKey){	// close lightbox
			myPopupBox.end();
		}
	},

	//
	//	end()
	//
	end: function()
	{
		if(typeof bCloseConfirm != 'undefined' && bCloseConfirm == true)
		{
			var bClose = confirm('M\u00F6chten Sie das Fenster wirklich schlie\u00DFen?');
		}
		else
		{
			var bClose = true;
		}
		
		if(bClose)
		{
			this.disableKeyboardNav();
			Element.hide('popup');
			new Effect.Fade('overlay', { duration: overlayDuration});
		}
	},
	
	closeit: function()
	{
		this.disableKeyboardNav();
		Element.hide('popup');
		new Effect.Fade('overlay', { duration: overlayDuration});
	}
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function 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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){
	}
}

// -----------------------------------------------------------------------------------

//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }
	
// ---------------------------------------------------

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Help from Ran Bar-On [ran2103@gmail.com]
//

function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}
/*
function pause(numberMillis) {
	var curently = new Date().getTime() + sender;
	while (new Date().getTime();	
}
*/
// ---------------------------------------------------



function initPopupBox() { myPopupBox = new PopupBox(); }
Event.observe(window, 'load', initPopupBox, false);