/**

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/setcapture.asp
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/releasecapture.asp

http://developer.mozilla.org/en/docs/DOM:window.captureEvents
http://developer.mozilla.org/en/docs/DOM:window.releaseEvents

*/
(function ($) {

    $.fn.toSplitBar = function (settings) {
        alert(this.id)
        this.settings = {
            direction: 'y',
            range: {
                min: 0,
                max: 9999
            },
            move: function () { },
            style: {
                position: 'fixed',
                top: 0,
                left: 0
            }
        };

        $.extend(this.settings, settings);
        $.extend(this.settings.style, {
            width: this.settings.direction == 'x' ? '5px' : 0,
            height: this.settings.direction == 'y' ? '5px' : 0
        });

        // dressing
        this.css(this.settings.style);

        // can move
        this.capture = false;

        // moved pixels of horizontal
        this.moveX = 0;

        // moved pixels of longitudinal
        this.moveY = 0;

        this.mousedown = (function (splitbar) {
            if (splitbar.setCapture) {
                return function () {
                    splitbar.setCapture();

                    splitbar.capture = true;
                };
            } else {
                return function () {
                    window.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN);

                    splitbar.capture = true;
                };
            }
        })(this);

        this.mousemove = (function (splitbar) {
            if (splitbar.settings.direction === 'x') {
                return function (e) {
                    var movex = e.clientX - splitbar.width() / 2;

                    if (splitbar.settings.range.min < movex && movex < splitbar.settings.range.max) {
                        splitbar.css({ 'left': movex });

                        splitbar.settings.move(movex);
                    }
                };
            } else {
                return function (e) {
                    var movey = e.clientY - splitbar.height() / 2;

                    if (splitbar.settings.range.min < movey && movey < splitbar.settings.range.max) {
                        splitbar.css({ 'top': movey });

                        splitbar.settings.move(movey);
                    }
                };
            }
        })(this);

        this.mouseup = (function (splitbar) {
            if (splitbar.releaseCapture) {
                return function () {
                    splitbar.releaseCapture();

                    splitbar.unbind('mousemove').unbind('mouseout').unbind('mouseup');
                };
            } else {
                return function () {
                    window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);

                    splitbar.unbind('mousemove').unbind('mouseout').unbind('mouseup');
                };
            }
        })(this);

        $(document).bind('mousemove', this.mousemove).bind('mouseup', { splitbar: this }, function (splitbar) {
            
            if (splitbar.capture) {

            }
        });
    };

})(jQuery);


$('#splitbar').toSplitBar();