/*****************************************************************/
/* LAOLA1.at LIGHTBOX for HSV.de				 */
/* requires slideshow.js supplied with TYPO3 slideshow extension */
/*****************************************************************/

var openLightbox = false;

var largeImageWidth = 640;
var largeImageHeight = 360;

var mousex;
var mousey;


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  var scrollHeight = document.body.scrollHeight;
  var scrollWidth = document.body.scrollWidth;
  
  //scrolly = scrollHeight;
  
//  alert(document.all.Absatz.offsetTop);

  
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  /* if( myHeight < scrollHeight){
  	myHeight = scrollHeight;
  }
  if( myWidth < scrollWidth){
  	myWidth = scrollWidth;
  } */
  
  var windowsize = Array(myWidth, myHeight, scrollWidth, scrollHeight);
  
  return windowsize;
 
}

function addStyles(element, stylestring){

	if(typeof(stylestring) != 'number') {
	
		var styleitems = stylestring.split(";");
	
		if(typeof(element) != 'number'){
	            for (i=0; i<=styleitems.length-1; i++){
	            	
           		if(styleitems[i] != ''){
        	            sitem = styleitems[i].split(":");

                	    thisname = trim(sitem[0], ' ');
	                    thisvalue = trim(sitem[1], ' ');
	                    
        	            document.getElementById(element).style[thisname] = thisvalue;
        	        }
  
	            }
        	}
        }
}

function addElement(type, newelement, parent, elementstyle) {
	
	popup = document.createElement(type);
	popup.className = newelement;
    	popup.id = newelement;
    	
    	if(type == 'a'){
    		if(newelement == 'lightboxNavPrev'){
	    		popup.href = 'javascript:prev();';
	    	} else {
	    		popup.href = 'javascript:next();';
	    	}
    	}
 
        if (newelement == 'overlay'){
        	popup.onclick = hide_lightbox;
        }
        
        if(typeof(document.getElementsByTagName(parent)[0]) == 'undefined') {
        	document.getElementById(parent).appendChild(popup);
        
        }else{
        	document.getElementsByTagName(parent)[0].appendChild(popup);
        }
    	
    	addStyles(newelement, elementstyle);
}



function init_lightbox() {
	
	windowSizeA = getWindowSize();
		
	lightboxpositionX = ((windowSizeA[0]/2)-((largeImageWidth+20)/2));
	lightboxpositionY = ((windowSizeA[1]/2)-((largeImageHeight+20)/2));

	
		
	addElement('div', 'overlay', 'body', 'width:'+windowSizeA[2]+'; height:'+windowSizeA[3]); // scrollWidth and scrollHeight vor Overlay element
	addElement('div', 'lightbox', 'body', 'width:'+(largeImageWidth+20)+'; height:'+(largeImageHeight+20)+'; top:'+lightboxpositionY+'; left:'+lightboxpositionX+'; backgroundColor:white');
	
	addElement('div', 'imageContainer', 'lightbox', 0);
	addElement('div', 'lightboxNav', 'imageContainer', 0);
	addElement('div', 'closebtn', 'lightbox', 0);
	addElement('div', 'lightboxTextContainer', 'lightbox', 'width:'+(largeImageWidth+20));
	addElement('div', 'lightboxDescription', 'lightboxTextContainer', 0);
	addElement('span', 'lightboxImgCaption', 'lightboxDescription', 0);
	addElement('span', 'lightboxImgCounter', 'lightboxDescription', 0);
	
}

function addCloseBtn() {
	addElement('div', 'closebtn', 'lightbox', 0);
	document.getElementById('closebtn').innerHTML = '<a href="javascript:hide_lightbox();"><img src="fileadmin/html/img/lightbox/lightbox-close.gif" border="0"></a>';

}

function addLightboxNav() {
	addElement('div', 'lightboxNav', 'imageContainer', 0);

	addElement('a', 'lightboxNavPrev', 'lightboxNav', 'width:'+(largeImageWidth/2)+'; height:'+largeImageHeight);
	document.getElementById('lightboxNavPrev').innerHTML = '<img src="fileadmin/html/img/lightbox/blank.gif" width="'+(largeImageWidth/2)+'" height="'+largeImageHeight+'" border="0" />';
	addElement('a', 'lightboxNavNext', 'lightboxNav', 'width:'+(largeImageWidth/2)+'; height:'+largeImageHeight);
	document.getElementById('lightboxNavNext').innerHTML = '<img src="fileadmin/html/img/lightbox/blank.gif" width="'+(largeImageWidth/2)+'" height="'+largeImageHeight+'" border="0" />';
	
}

function center(o) {
    var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
    
    var win_h = (window.innerHeight || parseInt(body.clientHeight )) / 2;
    var win_w = (window.innerWidth || parseInt(body.clientWidth)) / 2;
    var height = (largeImageHeight/2); // (o.offsetHeight / 2);
    var width = (o.offsetWidth / 2);
 
    var t = win_h - height + parseInt(window.pageYOffset || body.scrollTop) ;
    var l = win_w  - width  + parseInt(window.pageXOffset || body.scrollLeft);
    // o.style.top = t + 'px';
    // o.style.left = l + 'px';
    topposition = t + 'px';
    return topposition;
   
    
}    

function show_lightbox(){

	if(document.getElementById("overlay")){	
		topposition = center(document.getElementById("imageContainer"));
		document.getElementById("overlay").style.display = "block";
		document.getElementById("imageContainer").innerHTML = images_lightbox[imgcount];
		document.getElementById("lightbox").style.top = topposition;
		
		addCloseBtn();
		addLightboxNav();
	
		// Picture Counter
	    	text = (imgcount+1)+' / '+total_images;
    		document.getElementById("lightboxImgCounter").innerHTML = text;
    	
		if (image_texts[imgcount] != ''){
        		document.getElementById("lightboxImgCaption").innerHTML = image_texts[imgcount] + '<br />';
	    	}else{
        		document.getElementById("lightboxImgCaption").innerHTML = '';
	    	}
	
		document.getElementById("lightbox").style.display = "block";
	
    	}

}

function thumb_lightbox(img){
	imgcount = img;
	
	if(document.getElementById("overlay")){	
		topposition = center(document.getElementById("imageContainer"));
		document.getElementById("overlay").style.display = "block";
		document.getElementById("imageContainer").innerHTML = images_lightbox[imgcount];
		document.getElementById("lightbox").style.top = topposition;
		
		addCloseBtn();
		addLightboxNav();
	
		// Picture Counter
	    	text = (imgcount+1)+' / '+total_images;
    		document.getElementById("lightboxImgCounter").innerHTML = text;
    	
		// if (image_texts[imgcount] != ''){
		if (typeof(image_texts) != 'undefined'){
        		document.getElementById("lightboxImgCaption").innerHTML = image_texts[imgcount] + '<br />';
	    	}else{
        		document.getElementById("lightboxImgCaption").innerHTML = '';
	    	}
	
		document.getElementById("lightbox").style.display = "block";
	
    	}

}

function hide_lightbox(){
	document.getElementById("overlay").style.display = "none";
	document.getElementById("lightbox").style.display = "none";
}

function show_singleImage(element,n){
	var element = document.getElementById("href"+n);
	sHref = element.href;
	sText = element.title;
	
	var singleImage = '<img src="'+sHref+'" border="0" alt="" />';
	// alert(sHref);
	
	if(document.getElementById("overlay")){	
		topposition = center(document.getElementById("imageContainer"));
		document.getElementById("overlay").style.display = "block";
		document.getElementById("imageContainer").innerHTML = singleImage;
		document.getElementById("lightbox").style.top = topposition;
		
		addCloseBtn();
		// addLightboxNav();
	
		// Picture Counter
	    	// text = (imgcount+1)+' / '+total_images;
    		document.getElementById("lightboxImgCounter").innerHTML = ' ';
    	
		// if (image_texts[imgcount] != ''){
		// if (typeof(image_texts) != 'undefined'){
        	//	document.getElementById("lightboxImgCaption").innerHTML = image_texts[imgcount] + '<br />';
	    	// }else{
        		document.getElementById("lightboxImgCaption").innerHTML = sText;
	    	// }
	
		document.getElementById("lightbox").style.display = "block";
	
    	}

	
	return false;

}
