var divPopup = null;
var divLoading = null;
function closePopup()
{
    if(divPopup) {
      divPopup.parentNode.removeChild(divPopup);
      divPopup = null;
    }
    if(divLoading) {
      divLoading.parentNode.removeChild(divLoading);
      divLoading = null;
    }
}

function createPopup(width, height, xoffset, yoffset)
{

    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    closePopup();
	
    var div = document.createElement('div');
    if(!div) return false;

    div.style.visibility = 'hidden'; //erst verstecken um die höhe berechnen zu können
	
    document.body.appendChild(div);
	
    divPopup = div;
	divLoading = div;

    divLoading = document.createElement('div');
    divLoading.id = 'popupLoading';
    imgLoading = document.createElement('img');
    imgLoading.src = loadingImage.src;
    divLoading.appendChild(imgLoading);
    
    var y = arrayPageScroll[1] + ((arrayPageSize[3] - height) / 2);
    if (y < yoffset) y = yoffset;
    var x = ((arrayPageSize[0] - width) / 2) + xoffset;
	divLoading.style.top = y + "px";
    divLoading.style.left = x + "px";
    //divLoading.style.height = height + "px";
    //divLoading.style.width = width + "px";
	divLoading.style.zIndex = 1;

    document.body.appendChild(divLoading);

    return div;
}



var loadingImage = new Image();
loadingImage.src = '/fileadmin/elk/images/ajax-loader.gif'

var spaceImage = new Image();
spaceImage.src = '/fileadmin/elk/images/spacer.gif'

function openPic(href, width, height, xoffset, yoffset)
{
    if(!document.createElement) return(true);

    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();		
	if (xoffset == 0) {
		if (arrayPageSize[0] < 740) return(true);
		if (arrayPageSize[0] < 900)
			xoffset = xoffset + 80;
	}
	else if (xoffset < 0) {
		if (arrayPageSize[0] < 600) return(true);
	}
	else {
		if (arrayPageSize[0] < 790) return(true);
	}			
	
	if (yoffset != 0) {
		if (arrayPageSize[3] < 460) return(true);
	}
	
    var div = createPopup(width, height, xoffset, yoffset);
    if(!div) return(true);
	//div1.className = "popupPic1";
    div.className = "popupPic";
    div.onclick = function() { closePopup(); }
	
    y = ((y-height)/2); //nicht definiert
    x = ((x-width)/2) + xoffset; //nicht definiert
	
    var y = arrayPageScroll[1] + ((arrayPageSize[3] - 15 - (height)) / 2); 
    if (y < yoffset) y = yoffset;
    var x = ((arrayPageSize[0] - width) / 2) + xoffset;
	div.style.top = (y) + "px";
    div.style.left = (x) + "px";
    //div.style.width = (width)+"px";
    //div.style.height = (height)+"px";
	div.style.background = "url("+href+") no-repeat center";
	
    var divlio = document.createElement('div');
	var divreo = document.createElement('div');
	var divliu = document.createElement('div');
	var divreu = document.createElement('div');
	divlio.className = "popdivlio";
	divreo.className = "popdivreo";
	divliu.className = "popdivliu";
	divreu.className = "popdivreu";
	
	var img = document.createElement('img');
    
    img.onload = function () {
    	div.style.zIndex = 2;
		if (divLoading) {
	        divLoading.parentNode.removeChild(divLoading);
	        divLoading = null;
		}
    }
	
    img.src = spaceImage.src;
	img.style.height = "240px";
    img.style.width = "320px";
	div.appendChild(divlio);
	div.appendChild(divreo);
	div.appendChild(divliu);
	div.appendChild(divreu);
    div.appendChild(img);

    div.style.visibility = '';

    return(false);
}

//
// 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) {  // funkt in Firefox 3
        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, funkt in IE 6 und 7
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer, funkt in Firefox 3
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode, IE 7
        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;
}
