(function($){
  $.fn.backgroundScale = function(customOptions){
    //Merge default and user options
    var options = $.extend({}, $.fn.backgroundScale.defaultOptions, customOptions);
    return this.each(function(i){
      var $this = $(this);
      var $ttImage = $(options.imageSelector);
      
      
      
      //Set some basic CSS positioning rules
      $ttImage.css({position: "relative"});
      
      //Define out vars
      var containerWidth, containerHeight, containerRatio;
      var imageWidth, imageHeight, imageRatio;
      
      //Manipulation function
      var imageManipulation = function(){
        //Set the container details
        containerWidth = $this.width() + options.containerPadding;
        containerHeight = $this.height() + options.containerPadding;
        containerRatio = containerWidth / containerHeight;
        
        //Set the image details
        imageWidth = $ttImage.width();
        imageHeight = $ttImage.height();
        imageRatio = imageWidth / imageHeight;
        
        
        //Center the image within the container?
  
          var width = containerWidth;
          var height = containerWidth * (1/imageRatio);
          if(containerWidth >= 1300){ // stop stretching at 1300
            width = 1300;
            height= 1300 * (1/imageRatio);
            
          } else if(containerWidth < 920){// stop at 920
            width = 920;
            height = 920 * (1/imageRatio);
          
          } else {// stretch the image above 920 and 1300
            width = containerWidth;
            height = containerWidth * (1/imageRatio);
          }

          $ttImage.css({
            width: width,
            height: height
          });          
          var li = $ttImage.parent().css({
            width: width,
            height: height
          });    
            
          $("div#footer").css({
             width: width
            });    
            
         $("object#ytplayer0").css({
             width: width,
             height: width * 0.56
            });
            
      }
      
      //Fire on page load
      imageManipulation();
      
      //Add the browser resize event
      $(window).bind("resize.backgroundScale", function(){
        imageManipulation();
      })
    });
  }
  
  //Set our plugin defaults
  $.fn.backgroundScale.defaultOptions = {
    imageSelector: "#ttImage"
  };
})(jQuery);
