function Gallery() {
	var imageNum =  $('#galleryImages li').length;
	var currentImage = 0;
	var pos =  $('#galleryImages').position().left;
		
	$('#galleryInfo [action="page"]').click(function(event) {
		var dir = parseFloat($(this).attr('dirr'));
		page(dir);
		event.preventDefault();
	});
	
	function page(dir) {
		var w = $('#galleryImages li').eq( (dir == 1) ? currentImage : currentImage - 1).width() + 10;
		
		if ((dir == -1 && currentImage > 0) || dir == 1) {
			removeVideo();
			currentImage += dir;
			if (dir == -1) w *= -1;
			pos -= w;
			
			if ((dir == 1 && currentImage > imageNum-1)) {
				pos = 0;
				currentImage = 0;
			}
						
			$('#galleryImages').stop();
			$('#galleryImages').animate({ 
				left:pos + 'px'
			}, 1000, 'easeInOutExpo');
			setInfo();
			setVideo();
		}
	}
	
	$('#galleryImages li').click(function(event) {
		var imagePos = $(this).index();
		if (imagePos >= currentImage) page(1);
		else page(-1);
	});
	
	$(window).resize(resizeGallery);
	
	function setInfo() {
		$('#galleryInfo .counter').html((currentImage + 1) + ' / ' + imageNum);
		if ($('#galleryImages li:eq('+ currentImage +') .container').length > 0) {
			$('#galleryInfo .container[type="text"]').html($('#galleryImages li:eq('+ currentImage +') .container').html());
			
			var h = stageHeight - $('#galleryInfo .container[type="text"]').height() - 240;
			$('#galleryInfo .container[type="text"]').css('top', h + 'px');
		}	
	}
	
	function setVideo() {
		var video_id = $('#galleryImages li:eq('+ currentImage +')').attr('video');
		if (video_id) {
			var w = $('#galleryImages li').eq(currentImage).width();
			$('#galleryImages li:eq('+ currentImage +')').append('<div class="videoItem" style="width:' + w + 'px;"><iframe src="http://player.vimeo.com/video/' + video_id + '?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="100%" height="100%" frameborder="0"></iframe></div>');
		}
	}
	function removeVideo() {
		$('#galleryImages li .videoItem').remove();
	}
	
	function resizeGallery() {
		var _stageHeight = $('body').height();
		if (_stageHeight < 500) _stageHeight = 500;
		$('#galleryImages').height(_stageHeight - 190 - 50);
		
		var imagesWidth = 0;
		$('#galleryImages li').each(function() {
	        var width  = $(this).width();
	        var height = $(this).height();
	        var parentWidth  = $(this).parent().width();
	        var parentHeight = $(this).parent().height();
	        
	        newHeight = parentHeight;
	        newWidth  = Math.round(newHeight/height*width);
	       
	        $(this).css({'height'     :newHeight   + 'px',
	                     'width'      :newWidth    + 'px'});
	        
	        $(this).find('.videoItem').width(newWidth);
	        
	        imagesWidth += newWidth + 10;
	    });
		$('#galleryImages ul').width(imagesWidth);
				
		var imageXpos = 0;
		$('#galleryImages li').each(function(i) {
			if (i < currentImage) imageXpos -= $(this).width() + 10
		})
		
		pos = imageXpos;
		$('#galleryImages').css({left:pos + 'px'});
	}
	
	createImages($('#galleryImages li'));
	resizeGallery();
	setInfo();
	setVideo();
}



function createImages(_items) {
	var loadedNum = 0;
	var items = _items;
	function loadImage(i) {
		
		var img = new Image();
		var curr = items.eq(i);
		
		if (curr.find('img').length == 0) {
			$(img).load(function () {
				$(this).css('display','none'); // since .hide() failed in safari
				$(curr).append(this);
				$(this).fadeIn('fast');
				nextImage();
	        }).error(function () {
	        	nextImage();
	        }).attr('src', curr.attr('img'));
		} else {
			//nextImage();
		}
		function nextImage() {
			if (items.length > loadedNum + 1) {
				loadedNum++;
				loadImage(loadedNum);
			}
		}
	}
	
	loadImage(0);
	resize();
}

