// frampi-interactive-backgrounds.js

var mouseCaptor = { x: 0, y: 0 };

var bgs = [];
var win_middle = 0;

function create_interactive_bg(id, img, coef,  params)
{
	var el = $("body #" + id);
	$(el).css('background', "transparent url('"+ img +"') no-repeat");
	bgs.push( { _el: el, coeff:coef, left: params.left, width: params.width } );
	updateBgsPos();
}

$(window).resize(function() { $('#compteur').html(mouseCaptor.x); } );


function updateBgsPos()
{
	
	var pos = 0;
	var posY = 0;
	
	$.each(bgs, function(k,bg)
	{
		pos = win_middle + (bg.left - bg.width*0.5) + (-1 * mouseCaptor.x * bg.coeff);
		posY = (1 * mouseCaptor.y * 40);
		
		//$('#compteur').html(pos);
		$(bg._el).css('background-position' ,  pos +"px " +posY + "px");
	});
}


function updateMouse(e)
{
	if (e)
	{
		var valX =  2 * (( e.pageX / $(window).width() ) - 0.5) ;
		if (valX > 1) valX = 1; else if (valX < -1) valX = -1;
		
		var valY =  2 * (( e.pageY / $(window).height() ) - 0.5) ;
		if (valY > 1) valY = 1; else if (valY < 0) valY = 0;
		
		mouseCaptor.x = valX;
		mouseCaptor.y =  valY;
	}
	
	win_middle = ($(window).width() * 0.5);
		
	updateBgsPos();
	
}

$(document).mousemove( function(e) { updateMouse(e); });
$(window).resize( function() { updateMouse(false);  });
updateMouse(false);


