/*
lightbox.js

VERSION 0.1
Last Modified: 09/06/2008
*/

var Lightbox = Class.create();

Lightbox.prototype = {

    initialize: function()
    {
	this.lightbox = $('lightbox');
	this.lightboxImage = $('lightboxImage');
	this.overlay = $('overlay');
	this.outerImageContainer = $('outerImageContainer');
	this.imageDataContainer = $('imageDataContainer');
	this.imageCloseContainer = $('imageCloseContainer');
	this.loading = $('loading');

	this.t = null;
	this.t2 = null;
	this.op = null;
	this.obj = null;
    },
    
    Appear: function(x)
    {
	var obj = this.obj;
	this.op = (obj.style.opacity)?parseFloat(obj.style.opacity):parseInt(obj.style.filter)/100;
	
	if (isNaN(this.op))
    	    this.op = 0.4;
    	      
	if(this.op < x)
	{
    	    clearTimeout(this.t2);  
    		this.op += 0.05;
    	    obj.style.opacity = this.op;
    	    obj.style.filter='alpha(opacity='+this.op*100+')';
    	    
	    var self = this;
	    setTimeout(function(){self.Appear(x);}, 20);
	}  
	else
    	{
//    		obj = outerImageContainer;
//    		obj.style.opacity = 0.7;
//        	obj.style.filter='alpha(opacity=70)';

//        	appear(1.0);
	    if (obj == this.obj)
	    {
		this.lightbox.style.width = '100%';
		this.lightbox.style.height = this.lightboxImage.height + 40;

    		this.lightbox.style.display = 'block';
    		this.outerImageContainer.style.width = this.lightboxImage.width + 20;
		this.outerImageContainer.style.height = this.lightboxImage.height + 10;
    		this.imageDataContainer.style.width = this.lightboxImage.width + 20;
    		this.imageCloseContainer.style.width = this.lightboxImage.width + 20;
    		
//    		this.obj = outerImageContainer;
//    		this.obj.style.opacity = 0.7;
//        	this.obj.style.filter='alpha(opacity=70)';

//        	this.Appear(1.0);
    	    }
    	}
    },
    
    HideDialog: function()
    {
	this.lightbox.style.display = 'none';
	this.overlay.style.display = 'none';
//	this.lightboxImage.style.display='none';
    },

    LoadImage: function(imgUrl, imgTitle)
    {
        var imgPreloader = new Image();
        
        if (imgTitle)
    	    $('caption').innerHTML = "<span class=\"caption\" id=\"caption\">" + imgTitle + "</span>";

        // once image is preloaded, resize image container
        imgPreloader.onload=(function(){
	    var img = $('lightboxImage');
	    img.src = imgUrl;

       	    img.width = imgPreloader.width;
	    img.height = imgPreloader.height;
	    this.loading.style.display='none';

    	    var arrayPageSize = getPageSize();

    	    this.overlay.style.width = arrayPageSize[0];
    	    this.overlay.style.height = arrayPageSize[1];

    	    var arrayPageScroll = getPageScroll();

    	    this.lightbox.style.top = arrayPageScroll[1] + (arrayPageSize[3] / 10);
    	    this.lightbox.style.left = arrayPageScroll[0];
    	    
    	    this.overlay.style.opacity = 0.4;
    	    this.overlay.style.filter = 'alpha(opacity=40)';

	    this.overlay.style.display='block';

//	    this.lightboxImage.style.display='none';
	    this.obj = this.overlay;
    	    this.Appear(0.8);

            imgPreloader.onload=function(){};       //      clear onLoad, IE behaves irratically with animated gif
        }).bind(this);
        
        imgPreloader.src = imgUrl;

	this.loading.style.display='';
	
	return false;
    }
}
