(function($) {
	$.fn.fullBg = function(options) {
		var defaults = {
			fade : true, // usare l'effetto di fadein delle immagini?
			coverTarget: $(window), // oggetto che deve esser coperto, di
									// default la window
			afterLoad : function() {}, // callback da eseguire quando viene
										// chiamato fullbg
			beforeResize : function() {},// callback da eseguire prima di
											// ogni resize
			afterResize : function() {}, // callback da eseguire dopo ogni
											// resize
			srcW: 'auto', // forza la dimensione width delle immagini, non
							// attende loading immagini per partire
			srcH: 'auto', // forza la dimensione height delle immagini, non
							// attende loading immagini per partire
			coverMode: 'cover', // cover, width, height, contain
			listMode: 'target', // target, image, none
			//lazyload: false, //lazy load of images (require lazyload plugin http://www.appelsiini.net/projects/lazyload)
		    live: true, // live follow resize of the window            
            maxW: 'auto', //dimensione massima larghezza per bg
            maxH: 'auto'   //dimensione massima altezza per bg
		}		
		if (typeof (options) == 'string')
			options = {
				imageSrc : options
			}
		options = $.extend(defaults, options)
		var bgDiv = $(this)
		var bgImg = $('img', bgDiv)
		var ul = $('ul:first', bgDiv)
		var li = $('li', ul)
		var ratio = 1
		function init() {
			bgDiv.css({
				position : 'absolute',
				overflow : 'hidden',
				top : 0,
				bottom: 0,
				left : 0,
				right : 0
			})
			bgImg.each(function() {
				$(this).css('display', 'none')				
			})
		}
		function bgResize() {
			options.beforeResize()
			var wW = options.coverTarget.width()
			var wH = options.coverTarget.height()			
			bgImg.each(function() {
				var img = $(this)
				var imgW = img.width()
				var imgH = img.height()
				if(options.srcW!='auto')
					imgW = options.srcW
				if(options.srcH!='auto')
					imgH = options.srcH
						
				var ratio = imgW / imgH
				
				var toW = wW 
				var toH = wH
               //verifico se ho una dimensione massima per altezza e larghezza
                if(options.maxW!='auto' && wW > options.maxW)
                    toW = options.maxW
                if(options.maxH!='auto' && wH > options.maxH)
                    toH = options.maxH
                
				if(options.coverMode=='cover'){
					img.width(toW);
					img.height(toW / ratio);				
					if (img.height() < toH) {
						img.height(toH);
						img.width(toH * ratio);
					}	
				}
				if(options.coverMode=='width'){
					img.width(toW);
					img.height(toW / ratio);
				}				
				if(options.coverMode=='height'){					
					img.height(toH);
					img.width(toH * ratio);	
				}
				if(options.coverMode=='contain'){
					img.width(toW);
					img.height(toW / ratio);				
					if (img.height() > toH) {
						img.height(toH);
						img.width(toH * ratio);
					}	
				}
				
				// centro altezza			
				if(ul.length>0 && options.listMode=='image'){	
					img.css('margin-top', 0)
					img.css('margin-left', 0)
				} else{
					img.css('margin-top', (wH - img.height()) / 2)
					img.css('margin-left', (wW - img.width()) / 2)	
				}
				
			})			
			
			if(ul.length>0){
				if(options.listMode=='target'){		
					ul.width( wW )
					ul.height( wH )
					li.width( wW )
					li.height( wH )
					}
				if(options.listMode=='image'){		
					var w = bgImg.eq(0).width()
					var h = bgImg.eq(0).height()
					ul.width( w )
					ul.height( h )
					li.width( w )
					li.height( h )					
					}
			}
			
			options.afterResize()
		}
		init()
		var readyGo = function() {
			bgResize()
			bgImg.each(function() {
				if (options.fade)
					$(this).fadeIn('slow')
				else
					$('img', bgDiv).css('display', 'inline')
			})
			options.afterLoad()
		}
		
		if((options.srcW != 'auto') || (options.srcH != 'auto'))
			$().ready(readyGo)
		else
			$(window).load(readyGo)
		
		if(options.live){
			$(window).resize(function() {
				bgResize()
			})
		}
		
	};
})(jQuery)
