$(document).ready(
    function () {
        var self = $(this).children().eq(0);
        $(".touchslider-viewport").mousedown(function (e) {

            var lastLeft = parseInt(self.css("left").split("px")[0]);
            var firstX = getMouseXY(e).x;
            $(this).mousemove(function (ev) {
                var xy = getMouseXY(ev);
                var pos = (lastLeft - (firstX - xy.x));
                self.css({ left: pos + "px" });
            });
        }).mouseup(function (event) {
            var self = $(this).children().eq(0);
            var slideWidth = $(this).width();
            var lastLeft = parseInt(self.css("left").split("px")[0]) * -1;

            var posicao = (lastLeft % slideWidth) > (slideWidth / 2) ? parseInt(lastLeft / slideWidth) + 1 : parseInt(lastLeft / slideWidth);
            if (posicao > 5)
                posicao = 5;
            self.animate({ left: (posicao * (slideWidth * -1)) + "px" }, 200);
            $(this).unbind("mousemove");
        });

        if ($(".touchslider-item").length > 0) {
            $(".touchslider-item").width($(".touchslider-viewport").width());
        }

    }

);

function getMouseXY(e) {
    var _x = 0;
    var _y = 0;

    if (event.clientX) { // grab the x-y pos.s if browser is IE
        _x = event.clientX + document.body.scrollLeft;
        _y = event.clientY + document.body.scrollTop;
    }
    else {  // grab the x-y pos.s if browser is NS
        _x = e.pageX + e.offsetX;
        _y = e.pageY + e.offsetY;
    }

    return { x: _x, y: _y };
}
