/*
name: levitation
version: 0.1
author: Gyula Madarasz 
edited by: Vincent Put
last update: 29/03/2011 (added moveX, moveY, easing)
e-mail: gyula.madarasz@gmail.com
usage: $(Element).levitation(left, top, distance, moveX, moveY, easing);
*/
	
(function ($) {

    $.fn.levitation = function (left, top, distance, moveX, moveY, easing) {

        var jThis = this;
        var jThisId = '#' + $(this).attr('id');
        var jThisTop = top;
        var jThisLeft = left;
        var oldLeft = left;
        var oldTop = top;

        var easingTime = easing;
        if (easingTime == null) easingTime = 0;

        $(this).css('position', 'absolute');
        $(this).css('z-index', distance);

        $(jThis).css('top', top + 'px');
        $(jThis).css('left', left + 'px');

        $(this).parent().parent().parent().mousemove(function (e) {
            run(e);
        });

        function run(e) {
            if (moveX != false) {
                var windowWidth = $(window).width();
                var mouseLeft = e.pageX;
                var mousePerWindowX = mouseLeft / windowWidth - 0.5;
                var newLeft = jThisLeft - Math.round(mousePerWindowX * distance);

                var easingFactor = (oldLeft - newLeft) * (easingTime);
                if (easingFactor < 0) easingFactor = easingFactor * -1;

                $(jThis).animate({ left: newLeft + 'px' }, easingFactor);
                oldLeft = newLeft;
            }
            if (moveY != false) {
                var windowHeight = $(window).height();
                var mouseTop = e.pageY;
                var mousePerWindowY = mouseTop / windowHeight - 0.5;
                var newTop = jThisTop - Math.round(mousePerWindowY * distance);
                $(jThis).stop().animate({ top: newTop + 'px' }, easingTime);
            }

            return false;
        }
    }

})(jQuery);
