/*
	BackImage- Fullscreen Background Image
	By Aprian
*/
(function($){
	$.fn.backImage = function(options){
		
		if(options.img === undefined){
			alert('Please supply the background image');
			return false;
		} 
		
		// Create background image wrapper
		$('<div id="backImageWrapper"></div>')
	      .css({'position':'absolute','z-index':-100,'left':0,'top':0,'overflow':'hidden'})
		  .appendTo($('body'));
		var wrapper = $('#backImageWrapper'); 
	  	
		// Get Image & append to wrapper
		$('<img>')
			.attr({'src': options.img, 'id': 'backImage'})
			.css({'display': 'block', 'position': 'fixed'})
			.appendTo( wrapper );
		var img = wrapper.children('img');
		
		// Resize image;
		backImageResize(wrapper, img);
				
		// When window resize, resize the image
		$(window).resize( function(){
			backImageResize(wrapper, img);	
		} );

		
		function backImageResize(wrapper, img){
			
			img.ready( function(){
				
				var winWidth = $(window).width();
		  		var winHeight = $(window).height();
				
				var imgWidth = img.width();
				var imgHeight = img.height();
				var ratio = imgHeight / imgWidth;	
				
				//document.title = 'image:' + imgWidth + 'x' + imgHeight + ' // ' + 'window:' + winWidth + 'x' + winHeight;
				
				img.width(winWidth);
				// Scale the image
				//if ((winHeight/winWidth) > ratio){
				//	img.height(winHeight);
				//    img.width(winHeight / ratio);
				//} else {
				//    img.width(winWidth);
				//    img.height(winWidth * ratio);
				//}
				
			} );
			
		}
	}	
})(jQuery);
