
(function($){

  var settings = {
    image:'blank',
    delay: 2000,
    fadeSpeed: 800,
    doFadeOut: true,
    imageToCopy:''
  };

  var data = {
  };

  var funcs = {


    // *************************************************************************
    init:function(opt){
    
      if (opt){ $.extend(settings, opt); }
    
      var divObj = $('<div id="splashIMAGE"/>').css({
        backgroundColor:'#FFF',
        position:"fixed",
        width: $(window).width(),
        height:$(window).height(),
        top:"0px",
        left:"0px"
      });
      $(this).append(divObj);
    
      data.curImage =  $('<img />').attr('src',settings.image).load(funcs.appendImage);  
          
    },

    
    // *************************************************************************
    appendImage:function(e){
      $('#splashIMAGE').append(data.curImage);
      
      
      if (settings.imageToCopy != ''){        
        data.curOverlay = $(settings.imageToCopy).clone();
        $(data.curOverlay).css({ position:'absolute' });
        $('#splashIMAGE').append(data.curOverlay);
      }
      
      
      $(data.curImage).css('position','absolute');
      
      data.imageWi = $(data.curImage).width();
      data.imageHi = $(data.curImage).height();
      
      funcs.scaleImage();       
      $(window).bind('resize',funcs.scaleImage);
      
      
      if (settings.doFadeOut){
        $('#splashIMAGE').delay(settings.delay).fadeOut(settings.fadeSpeed, funcs.removeAndClose);
      }
      
    },


    // *************************************************************************
    scaleImage:function(){
      fx1 = data.imageHi / data.imageWi;
      fx2 = $(window).height() / $(window).width();
            
      if (fx1 < fx2){
        fx = $(window).height() / data.imageHi; 
      }else{
        fx = $(window).width() / data.imageWi; 
      }
            
      var xx = Math.round(($(window).width() / 2) - ((data.imageWi * fx) / 2)); 
      var yy = Math.round(($(window).height() / 2) - ((data.imageHi * fx) / 2)); 
      
      $(data.curImage).css({
        width: Math.round(data.imageWi * fx)+"px",
        height: Math.round(data.imageHi * fx)+"px",
        top: yy+"px",
        left: xx+"px"
      });
      
      if (data.curOverlay){
        var p = $(settings.imageToCopy).offset();
        $(data.curOverlay).css({
         left: p.left,
         top: p.top 
        });
      
      }
      
    },
    
    
    // *************************************************************************
    removeAndClose:function(){
     $(window).unbind('resize',funcs.scaleImage);
     $('#splashIMAGE').remove();
    },
    
    
    empty:function(){}
  }
  
  // *************************************************************************
  $.fn.rsSplashImage = function(funcObj){ 
  
    if (funcs[funcObj]){
      return funcs[funcObj].apply(this, Array.prototype.slice.call( arguments, 1 ));
    }else if (typeof funcObj == 'object' || !funcObj){
      return funcs.init.apply(this,arguments);
    }else{
      $.error( 'Method ' + funcObj + ' Does not exist');
    }
    
  }

})(jQuery);  
