(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: 'full', // full, width, height
			listMode: 'target', // target, image, none
		    live: true, // live follow resize of the window
		    imgs: 'auto' //image selector auto, none or a css selector 
		}		
		if (typeof (options) == 'string')
			options = {
				imageSrc : options
			}
		options = $.extend(defaults, options)
		var bgDiv = $(this)
		
		var bgImg =$()		
		if(options.imgs=='auto')
			bgImg=$('img', bgDiv)
		else if(options.imgs!='none')
			bgImg=$(options.imgs)
			
		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
				
				if(options.coverMode=='full'){
					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);	
				}				
				
				// 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)
