// 
//  jquery.sitePreLoader.js
//  A jQuery plugin por preload websites
//  
//  Created by Javier Sánchez - Marín (vieron) 
//  http://github.com/vieron/sitePreLoader
//  Free distribution.
// 

(function($) {

  $.fn.sitePreLoader = function(options) {

    var opts = $.extend({}, $.fn.sitePreLoader.defaults, options);
    $.fn.sitePreLoader.options = opts;
    
    var win = $(this); 
    var htmlbody = $('html, body');
    
    //get window size
    var w = win.width()+20;
    var h = win.height()+20;  
    
    //TODO: resize overlay when window resizes
    
    //show overlay with opts.html and adds needed css properties to html and body
    $('body').attr('style','margin:0;').prepend('<div id="pl_overlay" style="position:absolute; margin:0; width:'+w+'px; height:'+h+'px; background: '+opts.bg+'; z-index:9999999999;"><div id="pl_content" style="'+opts.contentStyles+'">'+opts.html+'</div></div>');
    htmlbody.css('overflow','hidden');

    return win.load(function(e){
      if (opts.onloadCallback(win,e) === false) {
        opts.onloadCallback(win,e);
      };
      opts.hiddenEffect($('#pl_overlay'));
      // reset html and body overflow
      htmlbody.attr('style','overflow:static; margin:0;');
    })
        
  }; 


  // plugin defaults
  $.fn.sitePreLoader.defaults = {
    bg: '#000', //also supports url('path/to/img.jpg) no-repeat top left;
    onhiddenEffectCallback : function(target){
      return false;
    },
    hiddenEffect: function(target){
      var a = this;
      target.fadeOut('slow', function(){
        a.onhiddenEffectCallback(target);
    });
    },
    html:'<img src="images/loader.gif"/>',
    contentStyles: 'top:40%; width:100%; position:relative; text-align:center;',
    onloadCallback: function(target, event){
      return false;
    }
  };


})(jQuery);
