draggable box little glitch
<p>Usually I prefer to write my own solutions for trivial problems because generally plugins add a lot of unneeded functionality and increase your project in size. Size makes a page slower and a 30k difference (compared to jquery draggable) in a 100k pageviews / day website makes a big difference in the bill. I already use jquery and I think that's all I need for now, so please, don't tell me to use another plugin or framework to drag things around.</p>

<p>Whit that in mind I wrote the following code, to allow a box to be draggable around. The code works just fine (any tip about the code itself will be great appreciate), but I got a small glitch when I drag the mouse cursor to the left of the box limit: the text inside the box gets selected.</p>

<p>How can I solve it? Thanks.</p>

<p>JavaScript:</p>

<pre><code>$(document).ready(function () {

    var x = 0;
    var y = 0;

    $("#d").live("mousedown", function () {

        var element = $(this);
        var parent = element.parent();

        $(document).mousemove(function (e) {

            var x_movement = 0;
            var y_movement = 0;

            if (x == e.pageX || x == 0) {
                x = e.pageX;
            } else {
                x_movement = e.pageX - x;
                x = e.pageX;
            }

            if (y == e.pageY || y == 0) {
                y = e.pageY;
            } else {
                y_movement = e.pageY - y;
                y = e.pageY;
            }

            var left = parseFloat(element.css("left")) + x_movement;

            // hold inside left
            if (left &lt;= 0) left = 0;

            // hold inside right
            var max_right = (parseFloat(element.outerWidth()) - parent.width()) * -1;
            if (left &gt;= max_right) left = max_right;

            element.css("left", left);

            var top = parseFloat(element.css("top")) + y_movement;

            // hold inside top
            if (top &lt;= 0) top = 0;

            // hold inside bottom
            var max_bottom = (parseFloat(element.outerHeight()) - parent.height()) * -1;
            if (top &gt;= max_bottom) top = max_bottom;

            element.css("top", top);

            return false;

        });

    });

    $(document).mouseup(function () {
        x = 0;
        y = 0;
        $(document).unbind("mousemove");
    });

});
</code></pre>

<p>HTML:</p>

<pre><code>&lt;div style="position: relative; width: 500px; height: 500px; background-color: Red;"&gt;
    &lt;div id="d" style="position: absolute; width: 100px; left: 0px; height: 100px; top: 0px; cursor: move; border: 1px solid black;"&gt;a&lt;/div&gt;
&lt;/div&gt;
</code></pre>
