	
	/*
	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>
	
	*/


	// 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;
			
			$('<img src="'+opts.image+'" id="jFlow-bg-img" alt="'+opts.image+'" />')
				.css('opacity',0)
				.one('load',function(){
				
				el.append($(this));
				
				$(this).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;
				
				//var cx = -250;
				//var cy = -550;
				
				rx = w/sw;
				ry = h/sh;
				
				// center image
				el.css({left: cx+'px', top: cy+'px'});

			}).each(function(){
			if(this.complete) $(this).trigger("load");
			});
			
			// callbacks
				$('*').mousemove(function(e){
										  
				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;
				
				//var cx = -250;
				//var cy = -550;
				
				rx = w/sw;
				ry = h/sh;
				
				// center image
				el.css({ top: cy+'px'});
					
					var x = e.pageX;
					var y = e.pageY;
					
					if(opts.waitForStop)
					{
						el.stop()
							   .animate({
										left : x*(1 - rx),
										
										}, opts.speed);
					} else {	
						el.css({
									left : x*(1 - rx),
									
									});
					}
					
				});
				
			return this;
		}
	})(jQuery);

