// Replace Show Function
function change(id, newClass) {
	identity=document.getElementById(id);
	identity.className=newClass;
}
tg = {
    // CSS classes
    linkClass:'show-link',
    hideClass:'hide-content',
	showClass:'show-content',
	toggleClass:'toggle-content',
    
    init:function(){
        // Check DOM Support
        if(!document.getElementById || !document.createElement){return;}
        tg.allDivs = document.getElementsByTagName('div');
		for(i=0;i<tg.allDivs.length;i++)
		{
			if(tg.allDivs[i].className == tg.toggleClass)
			{
				tg.allDivs[i].className = tg.hideClass;
			}
		}
		
		tg.allLinks = document.getElementsByTagName('a');
		for(i=0;i<tg.allLinks.length;i++)
		{
			if(tg.allLinks[i].className == tg.linkClass)
			{
				tg.allLinks[i].onclick = function(){tg.show(this); return false;}
			}
		}
    },
	
	show:function(objLink){
		tg.linkParent = objLink.parentNode;
		
		// next Sibling auch für FF
		tg.toggleDiv = tg.closestSibling(tg.linkParent);
		if(tg.toggleDiv.className == tg.hideClass)
		{
			//tg.toggleDiv.setAttribute('className',tg.showClass);
			tg.toggleDiv.className = tg.showClass;
		}
		else if(tg.toggleDiv.className == tg.showClass){
			//tg.toggleDiv.setAttribute('className',tg.hideClass);
			tg.toggleDiv.className = tg.hideClass;
		}
	},
	
	closestSibling:function(node){
		tg.tempObj = '';
		if(node.nextSibling!=null){
			tg.tempObj=node.nextSibling;
			while(tg.tempObj.nodeType!=1 && tg.tempObj.nextSibling!=null){
				tg.tempObj=tg.tempObj.nextSibling;
			}
		}
		return tg.tempObj.nodeType==1?tg.tempObj:false;
	}
}


// ib replaces old imgPupUp

ib =  {

    // CSS classes
    anchorClass:'imgBox', // Klasse auf den Link setzen der als PopUp geöffnet werden soll
    popDivId:'PopDiv',
    loadingId:'loading-popup',
    loadingSrc:'../img/layout/loading.gif',
    closeLinkId:'closeWindow',
    captionId:'imgCaption',
    newImgId:'newImg',
    closeLabelFr:'Fermer cette image',
    closeLabelDe:'Bildansicht schliessen',
	closeLabelEn:'Close image',

    init:function(){
        
        ib.anchors = document.getElementsByTagName('a');
        // Sprache des HTML Elements auslesen
        ib.htmltag = document.getElementsByTagName('html')[0];
        ib.language = ib.htmltag.lang;
        
        // Div als PopUp Container erstellen
        ib.popDiv = document.createElement("div");
        ib.popDiv.id = ib.popDivId;
        
		document.body.appendChild(ib.popDiv);
		
		ib.popDiv.style.display = "none";
		
        // Schliessen Link erstellen
        ib.closeLink = document.createElement("a");
        ib.closeLink.setAttribute('id',ib.closeLinkId);
        ib.closeLink.setAttribute('href','#');
        if(ib.language == "de" || ib.language == "DE")
        {
            ib.closeLinkText = ib.closeLabelDe;
        }
        else if(ib.language == "fr" || ib.language == "FR")
        {
            ib.closeLinkText = ib.closeLabelFr;
        }
		else
		{
            ib.closeLinkText = ib.closeLabelEn;
        }
        ib.closeLink.appendChild(document.createTextNode(ib.closeLinkText));
        ib.popDiv.appendChild(ib.closeLink);
        // Alle Links im Dokument auf Klasse überprüfen
        //ib.anchors = document.getElementsByTagName('a');
        
        for(i=0;i<ib.anchors.length;i++)
        {
            anchor = ib.anchors[i];
            if(anchor.href && (anchor.className == ib.anchorClass))
            {
                anchor.onclick = function(){ib.show(this); return false;}
            }
        }
    },
    
    show:function(objLink){
        
        if(document.getElementById(ib.newImgId))
        {
            ib.popDiv.removeChild(document.getElementById(ib.newImgId));
        }
        if(document.getElementById(ib.captionId))
        {
            ib.popDiv.removeChild(document.getElementById(ib.captionId));
        }
        ib.objImage = document.createElement("img");
        ib.objImage.setAttribute('id',ib.newImgId);
        ib.popDiv.appendChild(ib.objImage);
        
        // read alt property and set value as caption
        ib.Caption = '';
        if(objLink.firstChild.nodeName.toLowerCase() == 'img')
        {
            ib.altNode = objLink.firstChild;
            ib.Caption = (ib.altNode.getAttribute('alt') == '') ? '' : ib.altNode.getAttribute('alt');
        }
        if(ib.Caption != '')
        {
            ib.objCaption = document.createElement("p");
            ib.objCaption.setAttribute('id',ib.captionId);
            ib.popDiv.appendChild(ib.objCaption);
            ib.altCaption = document.getElementById(ib.captionId);
            ib.altCaption.appendChild(document.createTextNode(ib.Caption));
        }
        var toploading =  (insyma.window.getScrollTop() + parseInt((insyma.window.getInnerHeight() - 16) / 2));
        toploading  = (toploading > 0) ? toploading : 0;
        var leftloading = (parseInt((insyma.window.getInnerWidth() - 16) / 2));
        leftloading = (leftloading > 0) ? leftloading : 0;
        
        
        // Loading erstellen und anzeigen
        ib.loading = document.createElement('img');
        ib.loading.setAttribute('id',ib.loadingId);
        ib.loading.src = ib.loadingSrc;
        ib.loading.style.top = toploading + 'px';
        ib.loading.style.left = leftloading + 'px';
        ib.loading.style.display = 'block';
        document.body.appendChild(ib.loading);
        
        ib.newImg = new Image();
        ib.newImg.onload = function(){
            ib.objImage.src = objLink.href;
            var top =  (insyma.window.getScrollTop() + parseInt((insyma.window.getInnerHeight() - ib.newImg.height) / 2));
            top  = (top > 0) ? top : 0;
            var left = (parseInt((insyma.window.getInnerWidth() - ib.newImg.width) / 2));
            left = (left > 0) ? left : 0;
            
            if(document.getElementById(ib.loadingId))
            {   
                document.getElementsByTagName('body')[0].removeChild(ib.loading);
            }
            
            ib.close = document.getElementById(ib.closeLinkId);
            ib.close.onclick = function(){ib.hide(this); return false;}
            
            document.getElementById(ib.popDivId).style.top = top + 'px';
            document.getElementById(ib.popDivId).style.left = left + 'px';
            document.getElementById(ib.popDivId).style.display = 'block';

            return false;
        }
        ib.newImg.src = objLink.href;
    },
    
    hide:function(closeAnchor){
            ib.divImg = document.getElementById(ib.popDivId);
            if(ib.divImg.style.display != 'none'){
                ib.divImg.style.display = 'none';
            }
            ib.divImg.removeChild(ib.objImage);
            if(document.getElementById(ib.captionId)){
                ib.divImg.removeChild(ib.altCaption);
            }
            return false;
    },
    
    addEvent:function(elm, evType, fn, useCapture){
		if (elm.addEventListener){
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
    }
}
ib.addEvent(window,'load',tg.init,false);
ib.addEvent(window,'load',ib.init,false);
