/*
Author: Michiel Snijder
Front-end Developer @ www.kees-tm.nl
License: Free for everybody =)

$('.bgScaler').fullBg({												// you can leave .bgScaler as default
	imageLocation:'images/your-image.jpg',							// the location of your image
	imageWidth:0,													// specify the width of your image (needed for ratio-scaling)
	imageHeight:0,													// specify the height of your image (needed for ratio-scaling)
	ratioScaling:true,												// en- or disable apect-ratio scaling
	autoCss:true,													// would you like that bgScaler generates your css
	containerClass:'bgScaler',										// classname of the container
	imageAlt:'Full background image using fullBg jQuery plugin',	// alt text for your image
	loaderLocation:'images/loader-image.gif',						// if you have set time to load, specify the loader-image here
	loadingTime:0,													// time you want to wait before background image shows
	fadeInSpeed:500													// The image will fade in, how fast do you want it to go? time in ms
});
	
*/

(function($){jQuery.fn.fullBg=function(options){var defaults={imageLocation:'images/your-image.jpg',imageWidth:0,imageHeight:0,ratioScaling:true,autoCss:true,containerClass:'bgScaler',imageAlt:'Loading background please wait...',loaderLocation:'images/loader-image.gif',loadingTime:0,fadeInSpeed:500};var bgOptions=$.extend(defaults,options);var element=$(this);var container=null;var aspRatio=0;var viewportWidth=0;var viewportHeight=0;getSizes();placeInto();$(window).resize(onRezise);function placeInto(){if(bgOptions.loadingTime!=0){var loadingImage='<div class="loadingImage" style="position:absolute; left:20px; top:20px;"><img src="'+bgOptions.loaderLocation+'" alt="loading background-image" /></div>';$(loadingImage).appendTo('body').delay(bgOptions.loadingTime).fadeOut(0);}
setTimeout(function(){updateSize(bgOptions.ratioScaling);var img='<img src="'+bgOptions.imageLocation+'" width="'+imageWidth+'" height="'+imageHeight+'" alt="'+bgOptions.imageAlt+'" />';var container=getContainer();if(bgOptions.loadingTime!=0){$(container).css({'opacity':'0'});$(container).animate({'opacity':'1'},bgOptions.fadeInSpeed);}
container.append(img);if(bgOptions.autoCss){$(container).css({'width':imageWidth,'height':imageHeight,'position':'fixed','top':'0','left':'0','overflow':'hidden','zIndex':'-1'});}else{$(container).css({'width':imageWidth,'height':imageHeight});}},bgOptions.loadingTime);}
function getContainer(){if(!container){if(element.length){container=element;}
else{container=$('<div class="'+bgOptions.containerClass+'"></div>');$('body').append(container);}}
return container;}
function onRezise(){getSizes();updateSize(bgOptions.ratioScaling);var container=getContainer();$(container).css({'width':imageWidth,'height':imageHeight});$(container).find('img').width(imageWidth).height(imageHeight);}
function getSizes(){aspRatio=bgOptions.imageHeight/bgOptions.imageWidth;viewportWidth=$(window).width();viewportHeight=$(window).height();}
function updateSize(state){switch(state){case false:imageWidth=viewportWidth;imageHeight=viewportHeight;break;default:imageWidth=viewportWidth;imageHeight=viewportWidth*aspRatio;if(imageHeight<=viewportHeight){imageHeight=viewportHeight;imageWidth=imageHeight/aspRatio;}
else{imageWidth=viewportWidth;imageHeight=viewportWidth*aspRatio;}
break;}}}})(jQuery);
