/*
 *	Lightbox JS: Fullsize Image Overlays
 *	by Lokesh Dhakar - http://www.huddletogether.com
 */

/* --- Michael Cooling 11/05/09 ---
 * (c) Infocasa IT, www.infocasa.com
 */

var _picturesUrl="";
var _imagesFolder="";
var _currentSlideNum = 0;
var _slideLength = 0;
var _stopSlide = true;
var _slideShow = true;
var _playSpeed;
var _slideShowButtons=      {"playButton" : null,"nextButton" : null,"prevButton" : null,"pauseButton":null, "lastButton":null, "firstButton":null};
var _slideShowButtonTips=   {"playButton" : null,"nextButton" : null,"prevButton" : null,"pauseButton":null, "lastButton":null, "firstButton":null};
var _lbKeepAspect=true;
var imgPreload;
var _objLink;
var _timer1=null;
var _timer2=null;
var _fadeInImage, _fadeOutImage;
var _OpacityOut, _OpacityIn;
var objOverlay;
var nextSlideTimeOut;
var buttonClicked;

// getPageScroll
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()
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
	yScroll < windowHeight?pageHeight = windowHeight:pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	xScroll < windowWidth?pageWidth = windowWidth:pageWidth = xScroll;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


//Finds the index of the pictureName in pictureArray
function findMySlideNumber(pictureName){
    for(var i=0;i<_photoCount;i++){
        if (pictureName.indexOf(_photos[i].name)>0) {
            return(i);
        }
    }
    return (0);
}

// pause(numberMillis)
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime) return;
	}
}

function showLightbox(objLink,playSlideShow)
{
	
	//Preload image
    if(objLink!=null){
        //Finish any fade in/out operation
         _OpacityIn=100;
         _OpacityOut=0;
         fadeOut(false);
         fadeIn(false);
         
    }
	imgPreload = new Image();
    _currentSlideNum=findMySlideNumber(objLink.href)
    buttonClicked = false;
    document.getElementById('overlay').style.display = 'block';
    document.getElementById('lightbox').style.display = 'block';
	imgPreload.src = objLink.href;
	SetImageOnLoadEvent(playSlideShow);
	if (objLink.alt){
		imgPreload.title=objLink.alt;
	}else if(objLink.getAttribute("alt")){
		imgPreload.title= objLink.getAttribute("alt");
	}else if(objLink.title){
		imgPreload.title=objLink.title;
	}
}


function hideLightbox()
{
    _stopSlide=true;
    _showSlide=false;
    //Cleanup slideshow
    clearTimeout(nextSlideTimeOut);
    clearInterval(_timer2);
    clearInterval(_timer1);
    _timer2=null;
    _timer1=null;
    nextSlideTimeOut = null;

    //Clear images
    var img = document.getElementById('backImage');
    setOpacity(img,0)
    img.style.visibility="hidden";
    img.removeAttribute('src');
    
      
    img = document.getElementById('lightboxImage');
    setOpacity(img,0);
    img.style.visibility="hidden";
    img.removeAttribute('src');
    
	//Hide everything
	document.getElementById('overlay').style.visibility = 'hidden';
    document.getElementById('lightbox').style.visibility = 'hidden';
    document.getElementById('overlay').style.display = 'none';
    document.getElementById('lightbox').style.display = 'none';
    //Set the play button back to play
    _slideShowButtons['playButton'].src=playButton;
    _slideShowButtons['playButton'].title= _slideShowButtonTips['playButton'];
}
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
function initLightbox()
{
	//Image preloaders for all buttons
	var imgPreloadCloseButton = new Image();
    var imgPreloadPlayButton = new Image();
    var imgPreloadNextButton = new Image();
    var imgPreloadPrevButton = new Image();
    var imgPreloadFirstButton = new Image();
    var imgPreloadLastButton = new Image();

    var loadingImage;
    var closeButton;
    
    //Button and loading images images must exist or nothing will work
    loadingImage = _imagesFolder + 'ajax-loader_32.gif';
    closeButton = _imagesFolder + 'close.gif';
    playButton = _imagesFolder + 'lb_play.gif';
    pauseButton = _imagesFolder + 'lb_pause.gif';
    firstButton =_imagesFolder + 'lb_first.gif';
    prevButton = _imagesFolder + 'lb_prev.gif';
    nextButton = _imagesFolder + 'lb_next.gif';
    lastButton =_imagesFolder + 'lb_last.gif';

    //Loop through all anchor tags adding the onclick function
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
            //If the link is to the slideShow the press the play button;
            if(anchor.getAttribute('id')=="slideLink") {
                anchor.onclick = function () {showLightbox(this,true); return (false);}
            }else{
                anchor.onclick = function () {showLightbox(this,false); return (false);}
            }
		}
	}

	var objBody = document.getElementsByTagName("body").item(0);

	//Create overlay div
	objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return (false);}
	objOverlay.style.visibility = 'hidden';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';

	objBody.insertBefore(objOverlay, objBody.firstChild);

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
	var imgPreloader = new Image();

	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloader.onload=function(){
        this.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs
        var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}

        //This is the loading image img element displayed first time before the lightbox slode
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = this.src;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '250';
        objLoadingImage.style.display = "none"

        //Center the loading image
        objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
        objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
        objOverlay.style.height = (arrayPageSize[1] + 'px');
        //objLoadingImage.style.display="";
        objLoadingImageLink.appendChild(objLoadingImage);       
        objOverlay.appendChild(objLoadingImageLink);

        showLoadingImage('lbImageHolder','lightboxLoadingSlide',this.src,false)
		return (false);
	}

	imgPreloader.src = loadingImage;

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
    objLightbox.style.visibility = 'hidden';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '1000';
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);

    //Add a div to hold the image as we need the image centered
    var objDiv = document.createElement("div");
    objDiv.setAttribute('id','lbImageHolder');
    objLightbox.appendChild(objDiv);

	// create link
	_objLink = document.createElement("a");
	_objLink.setAttribute('href','#');
	_objLink.onclick = function () {hideLightbox(); return (false);}
	objDiv.appendChild(_objLink);

	// if close button image found,
	imgPreloadCloseButton.onload=function(){
        this.onload=function(){};
		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButton;
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
        objCloseButton.className="lightboxButton";

		_objLink.appendChild(objCloseButton);
		return (false);
	}

	imgPreloadCloseButton.src = closeButton;

    //Create images
    var objImage = document.createElement('img');
    objImage.setAttribute("id","backImage");
    objImage.style.position="absolute";
    objImage.style.visibility="hidden";
	objImage.style.left="0px";
    setOpacity(objImage,10);
    _objLink.appendChild(objImage);
    
	objImage = document.createElement("img");
	objImage.setAttribute('id','lightboxImage');
    objImage.style.position="relative";
    objImage.style.visibility="hidden";
    setOpacity(objImage,10);
	_objLink.appendChild(objImage);
   
	// Create details div, a container for the photo caption, message and the buttons
	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetails');

	objLightbox.appendChild(objLightboxDetails);

    //Create table to hold buttons and caption
	var objTable = document.createElement("table");
    var objRow=objTable.insertRow(0);
    objTable.className="buttonHolder"; 
    objTable.setAttribute("id","tblButtonHolder");
    //Add the cells
    var objCell = objRow.insertCell(0);
    objCell.setAttribute("id","firstButtonHolder");
    objCell = objRow.insertCell(1);
    objCell.setAttribute("id","prevButtonHolder");
    objCell = objRow.insertCell(2);
    objCell.setAttribute("id","playButtonHolder");
    objCell = objRow.insertCell(3);
    objCell.setAttribute("id","nextButtonHolder");
    objCell = objRow.insertCell(4);
    objCell.setAttribute("id","lastButtonHolder");
    objCell = objRow.insertCell(5);
    objCell.setAttribute("id","captionHolder");

	objTable.className="buttonHolder";
    objCell = objRow.insertCell(6);
    objCell.setAttribute("id","positionHolder")
	objLightboxDetails.appendChild(objTable);
    objTable.style.width = objDiv.clientWidth + "px";

    if (_slideShow){
        var objLightboxButtons = document.createElement("div");
        objLightboxButtons.id="lightboxButtons";
        addButtonImage('firstButton',imgPreloadFirstButton,objLightboxButtons,'firstLightboxSlide');
        addButtonImage('prevButton',imgPreloadPrevButton,objLightboxButtons,'prevLightboxSlide');
        addButtonImage('playButton',imgPreloadPlayButton,objLightboxButtons,'startLightboxSlide');
        addButtonImage('nextButton',imgPreloadNextButton,objLightboxButtons,'nextLightboxSlide');
        addButtonImage('lastButton',imgPreloadLastButton,objLightboxButtons,'lastLightboxSlide');
        objLightbox.appendChild(objLightboxButtons);
    }
}

//Start the slide show, play must have been pressed
function startLightboxSlide(withTimeout){
     if (!buttonClicked){
         if (_slideShowButtons['playButton'].title==_slideShowButtonTips['pauseButton']){
              stopLightboxSlide();
             _slideShowButtons['playButton'].src=playButton;
             _slideShowButtons['playButton'].title= _slideShowButtonTips['playButton'];
         }else{
             _stopSlide =false;
             _slideShowButtons['playButton'].src=pauseButton;
             _slideShowButtons['playButton'].title= _slideShowButtonTips['pauseButton'];
             if (withTimeout!=false){
                clearTimeout(nextSlideTimeOut);
                nextSlideTimeOut=setTimeout("nextLightboxSlide(1)",_playSpeed);
             }
        }
    }
}

//Previous Slide
function prevLightboxSlide(){
   if (!buttonClicked){
       buttonClicked=true;
       stopLightboxSlide();
       if(_currentSlideNum>0){
           _currentSlideNum--;
           showLightboxSlide();
       }else{
            buttonClicked=false;
       }
   }
}
//Last Slide
function lastLightboxSlide(){
   if (!buttonClicked){
       buttonClicked=true;
       stopLightboxSlide();
       if(_currentSlideNum>=0 && _currentSlideNum<_slideLength){
           _currentSlideNum=_slideLength;
           showLightboxSlide();
       }else{
            buttonClicked=false;
       }
   }
}

//First Slide
function firstLightboxSlide(){
   if (!buttonClicked){
       buttonClicked=true;
       stopLightboxSlide();
       if(_currentSlideNum>0){
           _currentSlideNum=0;
           showLightboxSlide();
       }else{
            buttonClicked=false;
       }
   }
}

//When internal is set it means that it has been called by the previous or next buttons
function nextLightboxSlide(internal){
    if(!buttonClicked){
        buttonClicked=true;
        if (internal==null) {
            if(_currentSlideNum<_slideLength){
                stopLightboxSlide();
                _currentSlideNum++;
                showLightboxSlide();
            }else{ 
				buttonClicked=false;
			}

        }else if (_currentSlideNum==-1){
           //End of slideshow
            stopLightboxSlide();
            _currentSlideNum++;
            showLightboxSlide();
        }else if(_currentSlideNum<_slideLength && !_stopSlide){
            _currentSlideNum++;
            showLightboxSlide();
        }else if (_currentSlideNum==_slideLength && !_stopSlide){
            stopLightboxSlide();
            _currentSlideNum=0;
            showLightboxSlide();
            _stopSlide=true;
        }

        if (!_stopSlide){
        //    checkTimer = setInterval('checkTimeToShowNextSlide()',1000);
        }
    }
}

//Stops the slideshow if it was playing
function stopLightboxSlide(){
        _stopSlide =true;
        clearTimeout(nextSlideTimeOut);
        //Make the button a play button again
        _slideShowButtons['playButton'].src=playButton;
        _slideShowButtons['playButton'].title= _slideShowButtonTips['playButton'];  
}

//Sets the images source to the current slide source and sets the buttons state
function showLightboxSlide(){
    var lightboxImage = document.getElementById('lightboxImage');
    var u=_picturesUrl;

    u=u.replace(/\{0\}/g,_photos[_currentSlideNum].name);
	u=u.replace(/\{1\}/g,'large');
    
    // preload image
	imgPreload = new Image();
    SetImageOnLoadEvent();
    imgPreload.title= _photos[_currentSlideNum].caption;
    imgPreload.src=u;
}

function SetImageOnLoadEvent(playSlideShow){
    if (playSlideShow==null) playSlideShow=false;
    document.getElementById('overlay').style.visibility = 'visible';
    //Center the loading image
    objLoadingImage=document.getElementById('lightboxLoadingSlide');
    //Center on the screen, in case we have scrolled down or something
    showLoadingImage('lbImageHolder','lightboxLoadingSlide','',true)
    if (objLoadingImage){
        objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.clientHeight) / 2) + 'px');
        objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.clientWidth) / 2) + 'px');
    }
    
    imgPreload.onload=function(){
        this.onload=function(){};

        var objLightbox = document.getElementById('lightbox');
        //var objLoadingImage = document.getElementById('lightboxLoadingSlide');
       // var objLightboxDetails = document.getElementById('lightboxDetails');
        var objImageHolder = document.getElementById("lbImageHolder")
        var objOverlayLoadingImg = document.getElementById('loadingImage');
        var wantedWidth= objImageHolder.clientWidth;
        var wantedHeight=objImageHolder.clientHeight;

        var arrayPageSize = getPageSize();
        var arrayPageScroll = getPageScroll();
        
        //We need to wait for the current slides to finish displaying
        clearInterval(_timer1);
        clearInterval(_timer2);
        if (objOverlayLoadingImg) objOverlayLoadingImg.style.display="none";
        // set height of Overlay to take up whole page and show
        objOverlay.style.height = (arrayPageSize[1] + 'px');
        
        //Use a temp image to build and display after
        var objImageNew =document.getElementById('backImage');
        var objImage = document.getElementById('lightboxImage');

        if (objImage.getAttribute("rel")<objImageNew.getAttribute("rel")){
            objImageNew = document.getElementById('lightboxImage');
            objImage =document.getElementById('backImage');
        }
    
        objImageNew.setAttribute('title',imgPreload.getAttribute('title'));

        objImageNew.src = imgPreload.src;

        if (_lbKeepAspect){
            if(imgPreload.height>wantedHeight){
                var aspect = wantedHeight/imgPreload.height;
                imgPreload.width = wantedWidth*aspect;
                imgPreload.height= wantedHeight;
                objImageNew.style.height = wantedHeight+ "px";
                objImageNew.style.width =  wantedWidth*aspect + "px";
            }else{
                objImageNew.style.height ="";
                objImageNew.style.width ="";
            }
            //Horizontal - Center of image
            objImageNew.style.marginTop =(wantedHeight - imgPreload.height)/2+ "px"
            objImageNew.style.marginLeft =(wantedWidth - imgPreload.width)/2+ "px";
        }else{
            objImageNew.style.marginTop =(wantedHeight - objImageNew.height)/2+ "px"
            objImageNew.style.marginLeft =(wantedWidth - objImageNew.width)/2+ "px";
        }
        

        var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - wantedHeight) / 2);
        var lightboxLeft = ((arrayPageSize[0] - 20 - wantedWidth) / 2);

        //Swap image attribute so we know which one to use next time
        objImageNew.setAttribute("rel","1");
        objImage.setAttribute("rel","0");

        //Set the caption in the cell - done this way due to ie issues
        var objTable = document.getElementById('tblButtonHolder');
        if(objImageNew.getAttribute('title')){
			objTable.rows[0].cells[5].innerHTML = objImageNew.getAttribute('title');
		} else {
			objTable.rows[0].cells[5].innerHTML = "";
		}
        document.getElementById('positionHolder').innerHTML=(_currentSlideNum+1) + " " + _ofLabel + " " + (_slideLength+1);
        
        //Setting this starts the slideshow as if we had pushed the play button but without the play button timeout
        //We must have more than one image too
        if(playSlideShow && _slideLength>0){
            startLightboxSlide(false);
        }
		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		//if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		//}
        fadeImage(objImageNew,objImage,true);
        buttonClicked=false;
        setButtonsState();

		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

		showLoadingImage('lbImageHolder','lightboxLoadingSlide','',false)

		// Hide select boxes as they will 'peek' through the image in IE
		selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }
		objLightbox.style.visibility = 'visible';

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		

		return (false);
	}
}

//Adds a navigation button for the slide show
function addButtonImage(imageName,imagePreload,objLightboxDetails,onClickFunctionName){
    imagePreload.onload=function(){
        this.onload=function (){};
        var objButton = document.createElement("img");
        objButton.src = this.src;
        objButton.setAttribute('id',imageName);
        objButton.style.zIndex = '200';
		objButton.onclick = function(){eval(onClickFunctionName + '()');}
        objButton.className="lightboxButton";
        objButton.title= _slideShowButtonTips[imageName];
        
        document.getElementById(imageName + "Holder").appendChild(objButton);
        _slideShowButtons[imageName]=objButton;
        return false;
    }
    imagePreload.src = eval(imageName);
}

//Set the style to disabledButton on the buttons so they look disabled
function setButtonsState(){
        if(_currentSlideNum==0){
            if (_slideShowButtons['prevButton'].className.indexOf(" disabledButton")<0){
                _slideShowButtons['prevButton'].className+=" disabledButton";
                _slideShowButtons['prevButton'].title="";
            }
            if (_slideShowButtons['firstButton'].className.indexOf(" disabledButton")<0){
                _slideShowButtons['firstButton'].className+=" disabledButton";
                _slideShowButtons['firstButton'].title="";
            }
        }else{
            _slideShowButtons['prevButton'].className = _slideShowButtons['prevButton'].className.replace(" disabledButton","");
            _slideShowButtons['prevButton'].title=_slideShowButtonTips['prevButton'];
            _slideShowButtons['firstButton'].className = _slideShowButtons['firstButton'].className.replace(" disabledButton","");
            _slideShowButtons['firstButton'].title=_slideShowButtonTips['firstButton'];
        }
        if (_currentSlideNum==_slideLength){
            if (_slideShowButtons['nextButton'].className.indexOf(" disabledButton")<0){
              _slideShowButtons['nextButton'].className+=" disabledButton";
              _slideShowButtons['nextButton'].title="";
             }
            if (_slideShowButtons['lastButton'].className.indexOf(" disabledButton")<0){
               _slideShowButtons['lastButton'].className+=" disabledButton";
               _slideShowButtons['lastButton'].title="";
            }
        }else{
            _slideShowButtons['nextButton'].className = _slideShowButtons['nextButton'].className.replace(" disabledButton","");
            _slideShowButtons['nextButton'].title=_slideShowButtonTips['nextButton'];
            _slideShowButtons['lastButton'].className = _slideShowButtons['lastButton'].className.replace(" disabledButton","");
            _slideShowButtons['lastButton'].title=_slideShowButtonTips['lastButton'];
        }
}

function fadeImage(fadeImageIn,fadeImageOut,isLightBox)
{
     if (isLightBox==null) isLightBox=false;
    _OpacityOut=100;
    _OpacityIn=0;
    _fadeOutImage=fadeImageOut;
    _fadeInImage=fadeImageIn;
    _timer1=null;
    _timer2=null;
    
    if (fadeImageOut.src>''){
        //alert("fadeImageOut: " + fadeImageOut.src + " visibility=" + fadeImageOut.style.visibility);
        fadeImageOut.style.visibility='visible';
        _timer1 = setInterval("fadeOut(" + (isLightBox?"true":"false") + ")", 100);
    }
    if (fadeImageIn.src>''){
        //alert("fadeImageIn: " + fadeImageIn.src + " visibility=" + fadeImageIn.style.visibility);
        fadeImageIn.style.visibility='visible';
        _timer2 = setInterval("fadeIn(" + (isLightBox?"true":"false") + ")", 100);
    }
}

function fadeOut(isLightbox)
{
  if (_OpacityOut > 0){
     setOpacity(_fadeOutImage,_OpacityOut);
    _OpacityOut-= 10;
  }  else{
    setOpacity(_fadeOutImage,_OpacityOut);
    clearInterval(_timer1);
    if (isLightbox && _timer2==null && !_stopSlide) nextSlideTimeOut=setTimeout("nextLightboxSlide(1)",_playSpeed);
    _timer1=null;
  }
}

function fadeIn(isLightbox)
{
  if (_OpacityIn < 100){
      _OpacityIn+= 10;
      setOpacity(_fadeInImage,_OpacityIn);
  } else{
    setOpacity(_fadeInImage,_OpacityIn);
    clearInterval(_timer2);
    if (isLightbox && _timer1==null && !_stopSlide) nextSlideTimeOut=setTimeout("nextLightboxSlide(1)",_playSpeed);
     _timer2=null;
  }
  
}

// lightBoxAddLoadEvent()
function lightBoxAddLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
        func();
		oldonload();
		}
	}
}

//Main function that sets all the needed variable values and adds a function to the onload event of the document
//@param <string> thumbnailUrl    - path to the thumbnail handler
//@param <string> imagesFolder    - folder where the images are i.e. buttons etc
//@param <bool> slideShow         - true to enable the slideshow and slideshow buttons on the lightboximageviewer
//@param <int> photoCount         - number of images in the slide show
//@optional <int> playSpeed       - Time between slides when in slide show (miliseconds)

function lightBoxInit(thumbnailUrl,imagesFolder,slideShow,photoCount,lblPlay,lblPause,lblPrev,lblNext,lblFirst,lblLast,lblOf,playSpeed,keepAspect){
    if (!playSpeed){
        playSpeed=3000; //Default 3 seconds between slides
    }
    if(keepAspect!=null){
        _lbKeepAspect = keepAspect;
    }
    _picturesUrl = thumbnailUrl.replace("&amp;","&");
    _imagesFolder = imagesFolder;
    _slideShow = slideShow;
    _slideLength = photoCount-1;
    _playSpeed = playSpeed;
    _slideShowButtonTips["playButton"] = lblPlay;
    _slideShowButtonTips["pauseButton"] = lblPause;
    _slideShowButtonTips["prevButton"] = lblPrev;
    _slideShowButtonTips["nextButton"] = lblNext;
    _slideShowButtonTips["firstButton"] = lblFirst;
    _slideShowButtonTips["lastButton"] = lblLast;
    _ofLabel = lblOf;
    lightBoxAddLoadEvent(initLightbox);	// run initLightbox onLoad
}


