function close() {
	$('BIVBackground').effect('opacity', {transition : Fx.Transitions.quadOut, 'duration' : 800}).start(0.5, 0);
	removeBackground.delay(850);
			
	$('BIVWrapper').effect('opacity', {transition : Fx.Transitions.quadOut, 'duration' : 800}).start(1, 0);
	removeWrapper.delay(850);
		
	$('BIVImgDiv').effect('opacity', {transition : Fx.Transitions.quadOut, 'duration' : 800}).start(1, 0);
		
	return false;
}
	
function	removeBackground() {
	$('BIVBackground').remove();
}
	
function	removeWrapper() {
	$('BIVWrapper').remove();
}

function displayImage(src) {
	//alert("test");
	
	var margin = 0;
	var img = new Image();
	img.src = src;
	img.onload = function() {
		margin = (this.width / 2) * -1;
			
		var imgWrapper = new Element('div').setStyles({
			'background' : '#fff',
			'padding'    : '10px'
		});
				
		var i = new Element('img').setProperties({
			'src' : src,
			'id'  : 'BIVImgDiv'
		}).injectInside(imgWrapper);
			
				
		var background = new Element('div').setProperties({
			'id' : 'BIVBackground'
		}).setStyles({
			'position' : 'absolute',
			'top' : '0px',
			'left' : '0px',
			'width' : window.getWidth() + 'px',
			'height' : window.getHeight() + 'px',
			'z-index' : '10000',
			'background-color' : '#000',
			'opacity' : 0
		}).addEvent('click', function() {
			close();
			//return false;
		}).injectInside(document.body);
				
		background.effect('opacity', {'transition': Fx.Transitions.quadOut, 'duration' : 800}).start(0,0.5);
					
		var BIVWrapper = new Element('div').setProperties({
			'id' : 'BIVWrapper'
		}).setStyles({
			'position'   : 'absolute',
			'left'       : '50%',
			'overflow'   : 'hidden',
			'z-index'    : '10001',
			'background' : '#fff',
			'margin-left': margin,
			'opacity'    : 0,
			'top'        : '100px'
		});
			
		imgWrapper.injectInside(BIVWrapper);
		
		BIVWrapper.injectInside(document.body).effect('opacity', {transition : Fx.Transitions.linear, 'duration' : 500}).start(0, 1);
	}
}


var Gallery = new Class({
	browser : null,
	images  : new Array(),
	nImages : 0,
		
	initialize: function() {
		this.browser = $('galleryBrowser');
		this.images = $$('#galleryImages img');
		this.nImages = this.images.length;
		
		for(var i = 0; i < this.nImages; i++) {
			var p = this.images[i];
			

			$A($$('#galleryImages a')).each(function(el){

				el.onclick = this.stop.pass(el, this);

			}, this);

		
			var obj = {
				'image'   : this.images[i],
				'browser' : this.browser
			}
			
			//this.images[i].addEvent('click', this.setImageIntoBrowser.bindWithEvent(obj));
			this.images[i].addEvent('click', this.setImageIntoBrowser.bindWithEvent(obj));
		}
		
	},
	
	stop : function(el) {
		return false;
	},
		
	_displayImage : function(src) {
		
		//alert("test");
		
		var margin = 0;
		var img = new Image();
		img.src = src;
		img.onload = function() {
			margin = (this.width / 2) * -1;
			
			
			var imgWrapper = new Element('div').setStyles({
				'background' : '#fff',
				'padding'    : '10px'
			});
				
			var i = new Element('img').setProperties({
				'src' : src,
				'id'  : 'BIVImgDiv'
			}).injectInside(imgWrapper);
			
				
			var background = new Element('div').setProperties({
				'id' : 'BIVBackground'
			}).setStyles({
				'position' : 'absolute',
				'top' : '0px',
				'left' : '0px',
				'width' : window.getWidth() + 'px',
				'height' : window.getHeight() + 'px',
				'z-index' : '10000',
				'background-color' : '#000',
				'opacity' : 0
			}).addEvent('click', function() {
				g._close();
				//return false;
			}).injectInside(document.body);
				
			background.effect('opacity', {'transition': Fx.Transitions.quadOut, 'duration' : 800}).start(0,0.5);
					
			var BIVWrapper = new Element('div').setProperties({
				'id' : 'BIVWrapper'
			}).setStyles({
				'position'   : 'absolute',
				'left'       : '50%',
				'overflow'   : 'hidden',
				'z-index'    : '10001',
				'background' : '#fff',
				'margin-left': margin,
				'opacity'    : 0,
				'top'        : '100px'
			});
				
			imgWrapper.injectInside(BIVWrapper);
		
			BIVWrapper.injectInside(document.body).effect('opacity', {transition : Fx.Transitions.linear, 'duration' : 500}).start(0, 1);
		}
	},
	
	_close : function() {
		$('BIVBackground').effect('opacity', {transition : Fx.Transitions.quadOut, 'duration' : 800}).start(0.5, 0);
		g._removeBackground.delay(850);
			
		$('BIVWrapper').effect('opacity', {transition : Fx.Transitions.quadOut, 'duration' : 800}).start(1, 0);
		g._removeWrapper.delay(850);
		
		$('BIVImgDiv').effect('opacity', {transition : Fx.Transitions.quadOut, 'duration' : 800}).start(1, 0);
		
		return false;
	},
		
	_removeBackground : function() {
		$('BIVBackground').remove();
	},
	
	_removeWrapper : function() {
		$('BIVWrapper').remove();
	},
		
	setImageIntoBrowser : function(obj) {
			
		if(this.browser.getChildren() != "") {
			this.browser.setHTML('');
		}
		
		$A($$('#galleryImages a')).each(function(el){
			el.setProperty('class', 'mini_off');
		});
		
		var p = this.image.getParent();
		p.setProperty('class', 'mini_on');
		
		
		/*		
		var newElement = new Element('img').setStyles({
			'opacity' : '0',
			'border'	 : '0',
			'cursor'  : 'pointer'
		}).setProperty('src', p.href).injectInside(
			new Element('a').setProperties({
			'href' : p.href,
			'rel'  : 'lightbox'
			}).injectInside(this.browser)
		);
		*/
		
		var newElement = new Element('img').setStyles({
			'opacity' : '0',
			'border'	 : '0',
			'cursor'  : 'pointer'
		}).setProperty('src', p.href).injectInside(this.browser);
		
		/*	
		$('galleryFooter').setHTML('');
		$('galleryFooter').setHTML('&raquo; ' + p.title);
		*/
		
		newElement.addEvent('click', function(){
			//g._displayImage(this.src);
			//alert('aaa');
			Lightbox.show(this.src, this.alt);
		});

		var myEffect = newElement.effect('opacity', {duration: 1500});
		myEffect.start(0, 1);		
				
	}
	
});

var g = null;
Window.onDomReady(function() {
	g = new Gallery();
});


