(function($) {
	
$.fn.gallery = function(settings,observer){
	
	var defaults = { file: 'xml/products_gallery.xml', galleryName: '#foobar', thumbnailSize: '20px',  loader: '.loader',   thumbnailSize: '60px',opacity: '0.8'};
	
	settings = jQuery.extend(defaults,settings);

	return this.each(function(){

			 var container = $(this);

			 jQuery.data(this,"gallery",settings);

			 var file = $.Settings.get(this,"gallery","file");
			 var loader = $.Settings.get(this,"gallery","loader");
			 var callback = $.Settings.get(this,"gallery","callback"); 
			 
			 var galleryname = $.Settings.get(this,"gallery","galleryName");
			 
			 galleryName = $.util.removeFirstLetter(galleryname);
		
			 var _opacity = $.Settings.get(this,"gallery","opacity");
			 
			var _thumbSize = $.Settings.get(this,"gallery","thumbnailSize"); 
				_thumbSize = _thumbSize.match(/\d{1,3}/i) ; 
		
			 jQuery(container).XmlImageLoader(file,this, loader, function(image)
			 {
			 	var w = image.width;
				var h = image.height;
				 
				w = Math.ceil( (image.width / image.height)  * _thumbSize );
				h = Math.ceil( (image.height / image.width) * _thumbSize * 10 );
				
				if (w < h){
					$(image).css({ height: 'auto', width: w ,opacity: _opacity,cursor: 'pointer'});
		 		}
				else {
					$(image).css({ width: 'auto', height: h ,opacity: _opacity, cursor: 'default'});
				}
				
			 	$(image).attr("width",w).attr("height",h)
				
				$(image).hover(function(){
					$(image).css({opacity: '1'});
				}, 
				function()
				{
					$(image).not('.active').css({opacity: _opacity});
				});

				$(image).fadeIn();
		    });
		    
		    var handle = setTimeout(function(){
		    	
		    	
		    	if (observer)
		    	{
		    		jQuery(observer);
		    	}
		     
		    	 clearTimeout(handle);
		    },500);
		  
	    });
	    
};
 
})(jQuery); 
	
