/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

<http://www.gnu.org/licenses/gpl.html>

*/

/*
 * Developper : Pierrick Varin (contact@hezad.com)
 * Website 	  : blog.hezad.com
 * Thanks     : Alex for the Opera bug fix.
 * 
 * Version    : 1.1
 *  
*/

// options : 
//
// waitForStop (default : false)       => Waits for the end of the movement to move content
// speed (default : 400)              => Speed of movement when waitForStop is set to true
// opacity (default : 1)              => Well ... Opacity ;)

(function($) {

	$.fn.bgFlow = function(options)
	{
			var defaults = {
					waitForStop: false,
					speed: 400,
					opacity: 1
			};
			
			var opts = $.extend(defaults, options);
			
			$('body').css('overflow', 'hidden');    
			
			this.css({'position': 'absolute', 'z-index': '-1'});
			
			el = this;
			
			rx = 0;
			ry = 0;
			
			var im = new Image();
			im.onload = function() {
					var imTag = $('<img src="'+im.src+'" id="jFlow-bg-img" alt="'+im.src+'" />').css('opacity',0);
					el.append(imTag);
					
					imTag.animate({'opacity':opts.opacity}, 400);
			
					var w = el.width();
					var h = el.height();
					var sw = $(window).width();
					var sh = $(window).height();
					
					var cx = (sw-w)/2;
					var cy = (sh-h)/2;
					
					rx = w/sw;
					ry = h/sh;
					
					// center image
					el.css({left: cx+'px', top: cy+'px'});
			}
			im.src = opts.image;
			
			// callbacks
					$(document).mousemove(function(e){
							
							var x = e.pageX;
							var y = e.pageY;
							
							if(opts.waitForStop)
							{
									el.stop()
											   .animate({
																	left : x*(1 - rx),
																	top  : y*(1 - ry)
																	}, opts.speed);
							} else {        
									el.css({
															left : x*(1 - rx),
															top  : y*(1 - ry)
															});
							}
							
					});
					
			return this;
	}
})(jQuery);
