
/* div-popup (Lexikon + Bildvergrößerung) */
var divPopup = null;
var divOverlay = null;
var divLoading = null;
function closePopup()
{
    if(divOverlay) {
      divOverlay.parentNode.removeChild(divOverlay);
      divOverlay = null;
	  
	  if(openFrame == true){
		location.reload();
	  }
    }
    if(divPopup) {
      divPopup.parentNode.removeChild(divPopup);
      divPopup = null;
    }
    if(divLoading) {
      divLoading.parentNode.removeChild(divLoading);
      divLoading = null;
    }
}

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 = "img/close.gif";
    imgClose.height = imgClose.width = 15;
    aClose.appendChild(imgClose);
    aClose.href = "javascript:closePopup()";
    aClose.className = "close";
    div.appendChild(aClose);

    div.style.visibility = 'hidden'; //erst verstecken um die höhe berechnen zu können

    document.body.appendChild(div);
    divPopup = div;


    divLoading = document.createElement('div');
    divLoading.id = 'popupLoading';
	
	if(openFrame == true){

		imgLoading = document.createElement('div');
		imgLoading.width = frameWidth;
		imgLoading.height = parseInt(frameHeight) + 15;
		imgLoading.style.margin = '15px 0px 0px 0px';
		imgLoading.height = parseInt(imgLoading.height);
		//imgLoading.frameborder = false;
		//imgLoading.scrolling = 'no';
		//imgLoading.src = href;
		//imgLoading.style.textAlign = 'left';
		imgLoading.innerHTML = '<iframe src="'+href+'" width="'+imgLoading.width+'" height="'+(imgLoading.height-15)+'" align="left" frameborder="no" scrolling="no"></iframe>';
		//swfInspirationInside();
		//imgLoading.innerHTML = fOutput;
		
		//imgLoading.style.position = 'absolute';
		//imgLoading.style.visibility = 'hidden';
		//imgLoading.style.backgroundColor = 'yellow';
		//imgLoading.style.left = (imgLoading.style.left.split('px')[0]-Math.floor(imgLoading.width/2))+'px';
		//imgLoading.style.top = (imgLoading.style.top.split('px')[0]-Math.floor(imgLoading.height/2))+'px';
		
		imgLoading.appendChild(aClose);
	}else{
    	imgLoading = document.createElement('img');
		imgLoading.src = loadingImage.src;
	}
    
	if(openFrame == true){
		var width = imgLoading.width;
		var height = imgLoading.height;
	}else{
		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);

	divLoading.appendChild(imgLoading);
	
    return div;
}



var loadingImage = new Image();
loadingImage.src = 'img/loading.gif';

//function openPic(href, text, width, height)
function openPic(thumb) {
	
	if(thumb.indexOf("|")>=0){
		frameVarsArr = thumb.split("|");
		frameVars = frameVarsArr[1];
		href = frameVarsArr[0]+'?'+frameVars;
		frameGetArr = frameVars.split("&");
		for(var i=0; i<frameGetArr.length; i++){
			var tmpAry = frameGetArr[i].split("=");
			if(tmpAry[0] == "w"){
				frameWidth = tmpAry[1];
			}else if(tmpAry[0] == "h"){
				frameHeight = tmpAry[1];
			}
			//getStr += "&"+tmpAry[0]+"="+tmpAry[1];
		}
		text = '';
		//window.open('inspiration/index.html', 'winPopup', 'width=720,height=480,scrollbars=no,toolbar=no');
		openFrame = true;
	}else{
		href = thumb.href;
		text = thumb.title;
		openFrame = false;
	}
	
	width = 0;
	height = 0;
	
    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önnte (DAU) dann popup öffnen
        window.open(href, 'picPopup', 'width='+width+',height='+height+',scrollbars=no,toolbar=no');
        return;
    }

    var div = createPopup();
    if(!div) return(true);

    div.className = "popupPic";
    div.onclick = function() { closePopup(); }

    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 img = document.createElement('img');
    //img.height = height;
    // img.width = width;
    img.onload = function () {
        divLoading.parentNode.removeChild(divLoading);
        divLoading = null;
		//alert(div.style.left.split('px')[0]+' '+img.width);
		div.style.visibility = 'hidden';
		div.style.left = (div.style.left.split('px')[0]-Math.floor(img.width/2))+'px';
		div.style.top = (div.style.top.split('px')[0]-Math.floor(img.height/2))+'px';
		
		div.style.width = img.width+'px';
		div.style.height = img.height+'px';
		
		div.style.visibility = '';
    }
    img.src = href;
    div.appendChild(img);

    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);
}

function switchPic(a, prefix, picSize, link, largeSize, largeText)
{
    var i;
    if(typeof prefix=="undefined") prefix="pic"

    //bild ändern
    document.getElementById(prefix+'Pic').src = a.href;

    if(typeof picSize!="undefined") {
        //bildgröße anpassen
        document.getElementById(prefix+'Pic').width = picSize[0];
        document.getElementById(prefix+'Pic').height = picSize[1];
    }

    //text ändern
    if(a.title) i=a.title; else i="";
    if(document.getElementById(prefix+'Text')) {
        document.getElementById(prefix+'Text').innerHTML = i;
    }

    //link für large-bild ändern
    if(typeof link!="undefined" && document.getElementById(prefix+'Link')) {
        document.getElementById(prefix+'Link').href = link;
        if(typeof largeText!="undefined") {
            document.getElementById(prefix+'Link').title = largeText;
        } else {
            document.getElementById(prefix+'Link').title = i;
        }
    }

    //wenn eigene größe, onclick ändern
    if(typeof largeSize!="undefined" && document.getElementById(prefix+'Link'))
    {
        var link = document.getElementById(prefix+'Link');

        //neue onclick funktion erstellen mit neuer größe
        var onclick = function() {
            openPic(this.href, this.title, largeSize[0], largeSize[1]);
            return false;
        };

        if(link.oldOnClick) {
            //wegen überschriebener onclick funktion (statistik):
            link.oldOnClick = onclick;
        } else {
            //wenns das nicht gibt, ganz normal den onclick ändern
            link.onclick = onclick;
        }
    }

    //css-klasse für alle thumbnails "nicht-aktiv" setzen
    for(i=0;i<a.parentNode.childNodes.length;i++) {
        if(a.parentNode.childNodes[i].className) {
            a.parentNode.childNodes[i].className = a.parentNode.childNodes[i].className.replace(" active", "");
            a.parentNode.childNodes[i].className = a.parentNode.childNodes[i].className.replace("active", "");
        }
    }

    //Bei FarbenStoffe sind die bilder in mehreren kategorien (css-klasse 'kat')
    //diese müssen alle nicht-aktiv gesetzt werden
    if(a.parentNode.className=='kat')
    {
        for(j=0;j<a.parentNode.parentNode.childNodes.length;j++)
        {
            if(a.parentNode.parentNode.childNodes[j].className=='kat')
            {
                for(i=0;i<a.parentNode.parentNode.childNodes[j].childNodes.length;i++) {
                    if(a.parentNode.parentNode.childNodes[j].childNodes[i].className) {
                        a.parentNode.parentNode.childNodes[j].childNodes[i].className = a.parentNode.parentNode.childNodes[j].childNodes[i].className.replace(" active", "");
                        a.parentNode.parentNode.childNodes[j].childNodes[i].className = a.parentNode.parentNode.childNodes[j].childNodes[i].className.replace("active", "");
                    }
                }
            }
        }
    }

    //und für das angeklickt aktiv setzen
    a.className = "active";

    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;
}

var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	
	closeWin();
	
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=no,location=no,scrollbars=yes,menubar=no,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}


