function animateDivHeight(div, newHeight, waitToEnd) {
	//alert(div + ' : ' + newHeight + ' | ' + waitToEnd);
	div.set('tween', {duration: 'long'}); /* Fx.Tween class */
	div.tween('height', newHeight);  /* Fx.Tween class */


}
function replaceGalleryFullImg(newImg, container) {
	container.getElement('img[id*=gallery_full_img]').dispose();
	newImg.inject(container);
}
window.addEvent('domready', function(){
	/* VARS */
	var images = $$('.image');
	var anchors = images.getChildren('a');
	var fullGalleryDiv = $('gallery_fullscreen');
	var firstGalleryImgWidth = fullGalleryDiv.getElement('img')
	var fullImg = new Element('img', {
		'src': anchors[0],
		'id': 'gallery_full_img'
	});
	/* /VARS */
	window.addEvent('load', function(){
		var fullGalleryDiv = $('gallery_fullscreen');
		var firstGalleryImgWidth = fullGalleryDiv.getElement('img')
		var newImgParentContainerHeight = firstGalleryImgWidth.getStyle('height').toInt();
		animateDivHeight(fullGalleryDiv, newImgParentContainerHeight, true);
	}); 
	function showProgressBar() {
		if($('progress') == null) {
			var progressBar = new Element('div', {
				'id': 'progress'
			});
			var imgParent = $('gallery_fullscreen')
			progressBar.inject(imgParent, 'top');
			//imgContainer.getParent().grab(progressBar);
			$('progress').show();
		}
		else {
			$('progress').show();
		}
	}
	function hideProgressBar() {
		$('progress').hide();
	}
	
	fullGalleryDiv.grab(fullImg);
	images.each(function(element) {
		element.addEvent('click', function(){
			
			
			function loadNewImg(newSrc,imgContainer,imgParentContainer,imgParentContainerNewWidth){
			    var myChain = new Chain();
				imgContainer.set('tween', {duration: 400, onComplete: function(){myChain.callChain()}});
				
			    myChain.chain(
			        function() { imgContainer.tween('opacity', 0); },
					function() {
						showProgressBar();
			            var myImage = new Asset.images(newSrc, {
							properties : {id: 'gallery_full_img'}, 
			                onComplete: function() {
								hideProgressBar();
							//	imgContainer.setProperty('src', newSrc);
								replaceGalleryFullImg(myImage, fullGalleryDiv);
			                    this.callChain();
			                }.bind(this),
							onProgress: function() {
								
							}
			            });
						
					
			        },
					function() { 
						var newImgParentContainerHeight = $('gallery_fullscreen').getElement('img').getStyle('height').toInt();
						var newImgParentContainerWidth = $('gallery_fullscreen').getElement('img').getStyle('width').toInt();
						var newImgParentContainerSize = $('gallery_fullscreen').getElement('img').getSize();
					//	alert('new width: '+newImgParentContainerSize.x+' height:'+newImgParentContainerSize.y);
						animateDivHeight(fullGalleryDiv, newImgParentContainerHeight, true); 
						this.callChain(); 
					},
					function() { imgContainer.tween('opacity', 1); }
			    );
				myChain.callChain();
			 
			};
			
			
			var imgContainer = fullGalleryDiv.getChildren('img');
			var imgParentContainerHeight = fullGalleryDiv.getStyle('height').toInt();
			var newSrc = element.getChildren('a');
						
			loadNewImg(newSrc,imgContainer,fullGalleryDiv,imgParentContainerHeight);

			return false;
		}); 
	});


}); 
