(function($) {
  $.fn.fullBg = function(){
    var bgImg = $(this);		
 
    function resizeImg() {

		var ratio = bgImg.attr("data-height") / bgImg.attr("data-width");	

		// Get browser window size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		// Scale the image
		if ((browserheight/browserwidth) > ratio){
		    bgImg.height(browserheight);
		    bgImg.width(browserheight / ratio);
			//bgImg.css('width',(browserheight / ratio)+"px");
			//bgImg.css('height',browserheight+"px");
			//alert("a");
		} else {
			/*
			if(bgImg.attr("data-width")>browserwidth && bgImg.attr("data-height")>browserheight){
				if(bgImg.attr("data-width")<bgImg.attr("data-height")){
					bgImg.height(browserheight);
					bgImg.width(browserheight / ratio);
				}
			}
			else{*/
				bgImg.width(browserwidth);
				bgImg.height(browserwidth * ratio);
			/*}*/
			//bgImg.css('width',browserwidth+"px");
			//bgImg.css('height',(browserwidth * ratio)+"px");
		}
		// Center the image
		bgImg.css('left', (browserwidth - bgImg.width())/2);
		bgImg.css('top', (browserheight - bgImg.height())/2);
		bgImg.attr('left', (browserwidth - bgImg.width())/2);
		bgImg.attr('top', (browserheight - bgImg.height())/2);
		

		/*
		var imgwidth = bgImg.attr("data-width");
		var imgheight = bgImg.attr("data-height");
		var winwidth = $(window).width();
		var winheight = $(window).height();
		var widthratio = winwidth / imgwidth;
		var heightratio = winheight / imgheight;
		var ratio = imgheight / imgwidth;

		var widthdiff = heightratio * imgwidth;
		var heightdiff = widthratio * imgheight;

		var top = ((winheight-heightdiff)/2);
		var left = ((winwidth-widthdiff)/2);

		if(left>0)
		  left=0;
		if (top>0)
		  top=0;

		if ((winheight/winwidth) > ratio){
			bgImg.css({
			  width: (winheight / ratio)+'px',
			  height: winheight+'px',
			  top: top+'px',
			  left: left+'px'
			});
		} else {
			bgImg.css({
			  width: winwidth+'px',
			  height: (winwidth * ratio)+'px',
			  top: top+'px',
			  left: left+'px'
			});
		}
		*/
    } 
    resizeImg();
    $(window).resize(function() {
		resizeImg();

		windowheight=$(document).height();
		windowwidth=$(document).width();
		heightadjusted=$("#content").height();
		widthadjusted=$("#content").width();
		if(heightadjusted>0 && heightadjusted>windowheight){
			$("#backgroundstarter").css("height",(heightadjusted+$("#header").height()+1)+"px");
		}
		else
			$("#backgroundstarter").css("height",(windowheight-$("#header").height()-1)+"px");
		
		if(widthadjusted>0 && widthadjusted>windowheight){
			$("#header").css("width","100%");
			$("#backgroundstarter").css("width",widthadjusted+"px");
		}
		else{
			$("#header").css("width","100%");
			$("#backgroundstarter").css("width",windowwidth+"px");
		}

    }); 
  };
})(jQuery)


