var lightboxInited = false;
var imageTitle = "";

function lightbox_init() {
    $('overlay').hide().observe('click', (function() { lightbox_end(); }).bind(this));
  $('lightbox').hide().observe('click', (function(event) { if (event.element().id == 'lightbox') lightbox_end(); }).bind(this));

  $('bottomNavClose').observe('click', (function(event) { event.stop(); lightbox_end(); }).bind(this));

}

function lightbox_end() {
    $('lightbox').hide();
    var overlay = $('overlay');
    new Effect.Fade(overlay, { duration: 0.5 });
    
    $('lightboxImage').hide();
    return false;
}

function show_image(url, title) {
    imageTitle = title;

    $('imageDataContainer').hide();

    if (lightboxInited == false) {
        lightboxInited = true;
        lightbox_init();
    }

    var arrayPageScroll = document.viewport.getScrollOffsets();
    var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 20);
    var lightboxLeft = arrayPageScroll[0];
    $('lightbox').setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();


    var overlay = $('overlay');
    new Effect.Appear(overlay, { duration: 0.5, from: 0.0, to: 0.5 });
    $('lightbox').show();

    var imgPreloader = new Image();

    var lightboxImage = $('lightboxImage');
    lightboxImage.hide();
    imgPreloader.onload = (function(){
            lightboxImage.src = url;
            resizeImageContainer(imgPreloader.width, imgPreloader.height);
    }).bind(this);
    imgPreloader.src = url;


    return false;
}



function resizeImageContainer(imgWidth, imgHeight) {
    var outerImageContainer = $('outerImageContainer');
    var imageDataContainer = $('imageDataContainer');

    var borderSize = 10;

    // get current width and height
    var widthCurrent  = outerImageContainer.getWidth();
    var heightCurrent = outerImageContainer.getHeight();

    // get new width and height
    var widthNew  = (imgWidth  + borderSize * 2);
    var heightNew = (imgHeight + borderSize * 2);

    // scalars based on change from old to new
    var xScale = (widthNew  / widthCurrent)  * 100;
    var yScale = (heightNew / heightCurrent) * 100;

    // calculate size difference between new and old image, and resize if necessary
    var wDiff = widthCurrent - widthNew;
    var hDiff = heightCurrent - heightNew;

    if (hDiff != 0) new Effect.Scale(outerImageContainer, yScale, {scaleX: false, duration: 0.3, queue: 'front'}); 
    if (wDiff != 0) new Effect.Scale(outerImageContainer, xScale, {scaleY: false, duration: 0.3, delay: 0.5 }); 

    // if new and old image are same size and no scaling transition is necessary, 
    // do a quick pause to prevent image flicker.
    var timeout = 0;
    if ((hDiff == 0) && (wDiff == 0)){
        timeout = 200;
        if (Prototype.Browser.IE) timeout = 350;   
    }

    (function(){
        //prevLink.setStyle({ height: imgHeight + 'px' });
        //nextLink.setStyle({ height: imgHeight + 'px' });
        imageDataContainer.setStyle({ width: widthNew + 'px' });

        showImage();
    }).bind(this).delay(timeout / 1000);
}


function showImage() {
    $('loading').hide();
    new Effect.Appear($('lightboxImage'), { 
        duration: 0.5, 
        queue: 'end', 
        afterFinish: (function(){ updateDetails(); }).bind(this) 
    });
}


function updateDetails() {
    var imageDataContainer = $('imageDataContainer');

    $('caption').update(imageTitle);

    
    new Effect.Parallel(
        [ 
            new Effect.SlideDown(imageDataContainer, { sync: true, duration: 0.5, from: 0.0, to: 1.0 }), 
            new Effect.Appear(imageDataContainer, { sync: true, duration: 0.5 }) 
        ], 
        { 
            duration: 1, 
            afterFinish: (function() {
	            // update overlay size and update nav
	            var arrayPageSize = getPageSize();
                $('overlay').setStyle({ height: arrayPageSize[1] + 'px' });
                //this.updateNav();
            }).bind(this)
        } 
        );

}



function getPageSize() {
        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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 = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}
