/*#
======================================================================================
preVista: CMS - Sistema di Gestione Contenuti Web
======================================================================================
                                                                      
Copyright (c) 2004-2009 by Vista Tecnologie 
Tutti i diritti riservati.
http://www.vista.it
                                                                      
Questo software e' di proprità di Vista Tecnologie ed è concesso in uso 
all'azienda/utente in base ad apposito contratto esistente tra Vista Tecnologie
e suddetta/o azienda/utente. E' vietato qualsiasi utilizzo al di fuori 
del contratto sopramenzionato non autorizzato espressamente da Vista Tecnologie.
This program is not free software. 
======================================================================================
#*/
(function($) {
    $.fn.fullBg = function(options){
        
        var defaults = {
            fade: true,
            afterLoad: function(){}
        }
        if (typeof (options) == 'string') options = { imageSrc: options }
        options = $.extend(defaults, options)
        
        var bgDiv = $(this)
        var innerDiv =$('div:first',bgDiv)
        var bgImg = $('img',innerDiv)
        var ratio = 1
 
        function init(){
            
            bgDiv.css({                                
                position: 'absolute',
                top: 0,
                left: 0,
                zIndex: -10
            })
            
            innerDiv.css({
                overflow: 'hidden',
                position: 'fixed',
                top: 0,
                left: 0,
                right: 0,
                bottom: 0
            })
            
            bgImg.each(function(){
                $(this).css('display','none')
                })
            
        }
 
        function bgResize() {                                
            var wW = $(window).width()
            var wH = $(window).height()              
            var minH = bgDiv.height()
            var minW = bgDiv.width()     
            bgImg.each(function(){                
                var img = $(this)
                var ratio = img.width()/img.height()                
                var toW = wW
                var toH = wH                
                if( (toW<minW) || (toH<minH) ){
                    toH = minH;
                    toW = minW;
                }
                img.width( toW );
                img.height( toW / ratio );
                if ( img.height() < toH ){
                    img.height( toH );
                    img.width( toH * ratio);
                }
                //centro altezza            
                img.css('margin-top',(wH - bgImg.height())/2)
                img.css('margin-left',(wW - bgImg.width())/2)            
            })
        }
                  
        init()
    
        $(window).load(function(){
            bgResize()
            
            bgImg.each(function(){
                if(options.fade) $(this).fadeIn('slow')
                else $(this).css('display','inline')
            })

            options.afterLoad()
        })
    
        $(window).resize(function() {
            bgResize();
        })
        
    };
})(jQuery)
