(function($) {

  $.backstretch = function(src, options, callback) {
    var settings = {
       		hideUntilReady: true, // Hide the image until it's finished loading
       		speed: 'fast', // fadeIn speed for background after image loads (e.g. "fast" or 500)
			container: window, // define the container
			minWidth: 920, //define minimum width for site
			minHeight: 768
        },
        imgRatio;
    
    // Extend the settings with those the user has provided
    if(options && typeof options == "object") $.extend(settings, options);
    
    // Initialize
    $(document).ready(_init);
  
    // For chaining
    return this;
    
    function _init() {
      // Prepend image, wrapped in a DIV, with some positioning and zIndex voodoo
      if(src) {
        var commonCSS = {left: 0, top: 0},
            wrap = $("<div />").attr("id", "backstretch-wrap")
                               .css( $.extend(commonCSS, {position: "absolute", zIndex: -1}) ),
            container = $("<div />").attr("id", "backstretch")
                                    .css( $.extend(commonCSS, {overflow: "hidden", zIndex: -1}) )
                                    .appendTo(wrap),
            img = $("<img />").attr("src", src)
                              .bind("load", function() {
                                    var self = $(this);
                                    imgRatio = self.width() / self.height();
                                    _adjustBG(function() {
                                      if( settings.hideUntilReady )
                                        self.fadeIn(settings.speed, function(){
                                          // Callback, if necessary
                                          if(typeof callback == "function") callback();
                                        });
                                    });
                                  }).css( $.extend(commonCSS, {position:"relative"}) );
        
        if(settings.hideUntilReady) img.hide();          
        img.appendTo(container);
          
        $("body").prepend(wrap);

        // Adjust the background size when the window is resized
        $(window).resize(_adjustBG);
      }
      
    }
    
    function _adjustBG(callback) {
	 var baseWidth = (window.innerWidth > settings.minWidth) ? window.innerWidth : settings.minWidth;
      var bgWidth = baseWidth;
	 var bgHeight = bgWidth / imgRatio;

      if(bgHeight < window.innerHeight) {
        bgHeight = window.innerHeight;
        bgWidth = bgHeight * imgRatio;
      }
	
	 $("#backstretch").width( baseWidth ).height( window.innerHeight );
      $("#backstretch img").width( bgWidth ).height( bgHeight ).css('top',(window.innerHeight-bgHeight)/2.5);
      
      if (typeof callback == "function") callback();
    }
  };
  
})(jQuery);

function adjustBG() {
	var minWidth = 1680;
	var baseWidth;
	var leftPos;
	var windowWidth;
	if(window.innerWidth) {
		windowWidth = window.innerWidth;
	} else if(document.body.offsetWidth) {
		windowWidth = document.body.offsetWidth;
		
	} else if(document.documentElement.offsetWidth) {
		windowWidth = document.body.offsetWidth;
	} else {
		windowWidth =screen.width;
	}
	
	baseWidth = (windowWidth > minWidth) ? windowWidth : minWidth;
	leftPos = (windowWidth / 2) - (baseWidth/2);
	
	var baseHeight = $(".image_bg img").height();
 	$(".image_bg img").width( baseWidth ).height(baseHeight);
	$(".image_bg img").css('left',leftPos);
}
	
$(function() {
	
	$(window).resize(adjustBG);
	adjustBG();

});
