/* div-popup (Bildvergröung) */

var divPopup = null;
var divOverlay = null;
var divLoading = null;

function closePopup()
{
    if(divOverlay) {
      divOverlay.parentNode.removeChild(divOverlay);
      divOverlay = null;
    }
    if(divPopup) {
      divPopup.parentNode.removeChild(divPopup);
      divPopup = null;
    }
    if(divLoading) {
      divLoading.parentNode.removeChild(divLoading);
      divLoading = null;
    }
}

function openUrl(url)
{
    window.open(url, 'urlPopup', 'width=800,height=600,scrollbars=yes,toolbar=yes');
}

function createPopup()
{

    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    closePopup();

    divOverlay = document.createElement("div");
    divOverlay.setAttribute('id','overlay');
    divOverlay.onclick = function () {closePopup(); return false;}
    divOverlay.style.height = (arrayPageSize[1] + 'px');
    document.body.insertBefore(divOverlay, document.body.firstChild);

    var div = document.createElement('div');
    if(!div) return false;

    var aClose = document.createElement('a');
    var imgClose = document.createElement('img')
    imgClose.src = 'http://www.cso.net/fileadmin/templates/default/images/popup/close.gif';
    imgClose.height = imgClose.width = 14;
    aClose.appendChild(imgClose);
    aClose.href = "javascript:closePopup()";
    aClose.className = "close";
    div.appendChild(aClose);

    div.style.visibility = 'hidden'; //erst verstecken um die höberechnen zu kön

    document.body.appendChild(div);
    divPopup = div;


    divLoading = document.createElement('div');
    divLoading.id = 'popupLoading';
    imgLoading = document.createElement('img');
    imgLoading.src = loadingImage.src;
    divLoading.appendChild(imgLoading);
    
    var height = 100;
    var width = 100;

    var y = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - height) / 2);
    var x = ((arrayPageSize[0] - 20 - width) / 2);
    divLoading.style.top = y + "px";
    divLoading.style.left = x + "px";
    divLoading.style.height = height + "px";
    divLoading.style.width = width + "px";


    document.body.appendChild(divLoading);

    return div;
}



var loadingImage = new Image();
loadingImage.src = 'http://www.cso.net/fileadmin/templates/default/images/popup/loading.gif'

function openPic(href, text, url, width, height)
{
    if(!document.createElement) return(true);

    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    //allready one open?
    closePopup();

    if(text!="") var textHeight = 30; else textHeight = 0;

    if(arrayPageSize[0]-30 < width || arrayPageSize[1]-30 < height) {
        //wenn es sich um ein so kleines fenster handelt, dass das bild nicht platz hat
        //und der ganze browser unabsichtlich geschlossen werden köe (DAU) dann popup öen
        window.open(href, 'picPopup', 'width='+width+',height='+height+',scrollbars=no,toolbar=no');
        return;
    }

    var div = createPopup();
    if(!div) return(true);

    div.className = "popupPic";

    y = ((y-height)/2);
    x = ((x-width)/2);

    var y = arrayPageScroll[1] + ((arrayPageSize[3] - 15 - (height+textHeight)) / 2);
    var x = ((arrayPageSize[0] - width) / 2);
    div.style.top = (y) + "px";
    div.style.left = (x) + "px";
    div.style.width = (width)+"px";
    div.style.height = (height+textHeight)+"px";

    var aUrl = document.createElement('a');
    var img = document.createElement('img');
    img.height = height;
    img.width = width;
    img.onload = function () {
        divLoading.parentNode.removeChild(divLoading);
        divLoading = null;
    }
    img.src = href;
    aUrl.appendChild(img);

    if(url!='') {    
        aUrl.href = "javascript:openUrl('"+url+"')";
    }
    else {
    	aUrl.href = "javascript:closePopup()";
    }

    aUrl.className = "url";
    div.appendChild(aUrl);    


    if(text!='') {
        var divText = document.createElement('div');
        divText.style.top = (height)+"px";
        divText.style.width = width+"px";
        divText.style.height = textHeight+"px";
        divText.className = "text";
        p = document.createElement('p');
        p.appendChild(document.createTextNode(text));
        divText.appendChild(p);
        div.appendChild(divText);
    }
    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) {  
        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;
}
