function dfGallery() {
	var _imagesCollection = null;
	
	var _currentImageIndex = 0;
	var _totalImages = 0;
	var _mainElement = null;
	var _container = null;
	var _currentIndexElement = null;
	var _totalCountElement = null;
	
	var _cachedImgs = new Array();
	
	var _slideshowEnabled = false;
	var _slideshowPaused = false;
	
	var _slideshowFrequency = 5000;
	
	var _slideshowOutEffect = "fadeOut";
	var _slideshowInEffect = "fadeIn";
	var _slideshowEffectInterval = 500;
	
	var _switchOutEffect = "fadeOut";
	var _switchInEffect = "fadeIn";
	var _switchEffectInterval = 500;
	var _interval = null;
	
	var _setImage = function(href) {
		
		if(_cachedImgs[_currentImageIndex]==null) {
			_mainElement.css("width",_mainElement.width()).css("height",_mainElement.height());
			_container[_switchOutEffect].apply(_container,[_switchEffectInterval]);
			_mainElement.block({css: {border: 0, background: 'none', padding: '5px', width: '45px', marginTop: '-15px'}, message: "<img src='/public/images/loading-animation.gif' />"});
			img = new Image();
			img.onload = function() {
				_cachedImgs[_currentImageIndex] = img;
				_displayImage(img,_switchOutEffect,_switchInEffect,_switchEffectInterval);
				_mainElement.unblock()
			}
			img.src = href;
			
			
		}
		else {
			_displayImage(_cachedImgs[_currentImageIndex],_switchOutEffect,_switchInEffect,_switchEffectInterval);
		}
		window.setInterval(_nextSlide,_slideshowFrequency);		
    }
	
	
	
	var _displayImage = function(img,outEffect,inEffect,effectInterval) {
		var elemWidth  = parseInt(_mainElement.width());
		var elemHeight = parseInt(_mainElement.height());
		var imgWidth   = parseInt(img.width);
		var imgHeight  = parseInt(img.height);

		if(imgWidth && imgWidth > elemWidth) {
			var percent = parseInt((imgWidth - elemWidth) * 100 / imgWidth);
			var newHeight = parseInt(imgHeight - (imgHeight * percent / 100));
			img.style.width = elemWidth + 'px';
			img.style.height = newHeight + 'px';
			imgHeight = newHeight;
			imgWidth = elemWidth;
		}

		if(imgHeight && imgHeight > elemHeight) {
			var percent = parseInt((imgHeight - elemHeight) * 100 / imgHeight);
			var newWidth = parseInt(imgWidth - (imgWidth * percent / 100));
			img.style.height = elemHeight + 'px';
			img.style.width = newWidth + 'px';
		}

		_mainElement.css("width",_mainElement.width()).css("height",_mainElement.height());
		_container[outEffect].apply(_container,[effectInterval,function() {
			_container.html(img);
			_container[inEffect].apply(_container,[effectInterval]);
			_currentIndexElement.html(_currentImageIndex+1);
		}]);
	}
	
	var _nextSlide = function() {
		if(!_slideshowEnabled || _slideshowPaused) return;
		
		nextImageIndex  = _currentImageIndex==_totalImages-1?0:_currentImageIndex+1;
		if(_cachedImgs[nextImageIndex]==null) {
			//preloading image and wait for a next loop
			img = new Image();
			img.onload = function() {
				_cachedImgs[nextImageIndex] = img;
			}	
			img.src = jQuery(_imagesCollection[nextImageIndex]).attr('href');
		}
		else {
			_currentImageIndex = nextImageIndex;
			_displayImage(_cachedImgs[nextImageIndex],_slideshowOutEffect,_slideshowInEffect,_slideshowEffectInterval);
		}
	}
	
    return {
    	switchImage: function() {
    		_currentImageIndex = _imagesCollection.index(this);
    		_setImage(jQuery(this).attr('href'));
    		
    		return false;
    	},
    	nextImage: function() {
    		_currentImageIndex = _currentImageIndex==_totalImages-1?0:_currentImageIndex+1;
    		_setImage(jQuery(_imagesCollection[_currentImageIndex]).attr('href'));
    		
    		return false;
    	},
    	previousImage: function() {
    		_currentImageIndex = _currentImageIndex==0?_totalImages-1:_currentImageIndex-1;
    		_setImage(jQuery(_imagesCollection[_currentImageIndex]).attr('href'));
    		
    		return false;
    	},
    	setThumbs: function(thumbs) {
    		_imagesCollection = thumbs;
    		_totalImages = _imagesCollection.length;
    		
    		_totalCountElement.html(_totalImages);    		
    		_interval = window.setInterval(_nextSlide,_slideshowFrequency)
    	},
    	setMainImageElement: function(el) {
    		_mainElement = el;
    		_mainElement.mouseover(function() {_slideshowPaused=true}).mouseout(function() {_slideshowPaused=false});
    		
    		_container = _mainElement.children('div');
    		
    		_cachedImgs[0] = new Image();
    		_cachedImgs[0].src = _mainElement.find("img").attr("src"); 
    	},
    	setCurrentIndexElement: function(el) {
    		_currentIndexElement = el;
    	},
    	setTotalCountElement: function(el) {
    		_totalCountElement = el;
    	},    	
    	runSlideshow: function() {
    		_slideshowEnabled = true;
    	},
    	stopSlideshow: function() {
    		_slideshowEnabled = false;
    	}	
    };
}
