//var elementoWithTouch = null;

//$(document).ready(
//    function () {
//        $('.sinoticos-card').select(function (event) { event.stopPropagation(); return false; })
//        $('.sinoticos-card').mouseout(function (event) {
//            reconfiguraSlide(elementoWithTouch);
//        });
//    }
//);

function aplicarTouch(elemento, codigo) {
    var self = $(elemento).children().eq(0);
    $(elemento).bind("touchstart click", function (e) {
        this.eventos = [];
    });
    $(elemento).bind("touchmove", function (e) {
        this.eventos.push(getMouseXY(e));
    });
    $(elemento).bind("touchend click", function (e) {
        if (this.eventos.length > 1) {
            var deltaY = this.eventos[0].y - this.eventos[this.eventos.length - 1].y;
            if (deltaY > 20 || deltaY < -20)
                return;
        }
        var firstX = getMouseXY(e).x;
        var orientacao = this.eventos.length == 1 ? firstX > ($(this).width() / 2) : this.eventos[0].x - this.eventos[this.eventos.length - 1].x >= 0;

        //elementoWithTouch = this;
        //var lastLeft = parseInt(self.css("left").split("px")[0]);

        irSlide(this, orientacao, codigo);

        //        $(elementoWithTouch).bind("touchmove", function (ev) {
        //            ev.stopPropagation();
        //            var xy = getMouseXY(ev);
        //            var pos = (lastLeft - (firstX - xy.x));
        //            self.css({ left: pos + "px" });
        //        });
    });
    //    $(elemento).bind("touchstop", function (event) {
    //        reconfiguraSlide(this);
    //    });
    //    $(elemento).bind("touchend", function (event) {
    //        reconfiguraSlide(this);
    //    });
    //    $(elemento).bind("touchcancel", function (event) {
    //        reconfiguraSlide(this);
    //    });

    //        elemento.addEventListener("touchstart", function (e) {
    //            elementoWithTouch = this;
    //            var lastLeft = parseInt(self.css("left").split("px")[0]);
    //            var firstX = getMouseXY(e).x;
    //            this.addEventListener("touchmove", function (ev) {
    //                var xy = getMouseXY(ev);
    //                var pos = (lastLeft - (firstX - xy.x));
    //                self.css({ left: pos + "px" });
    //            });
    //        });
    //        elemento.addEventListener("touchend", function (event) {
    //            reconfiguraSlide(this);
    //        });
    //        elemento.addEventListener("touchcancel", function (event) {
    //            reconfiguraSlide(this);
    //        });
    //        elemento.addEventListener("handleLeave", function (event) {
    //            reconfiguraSlide(this);
    //        });
}

//function reconfiguraSlide(elemento) {
//    var self = $(elemento).children().eq(0);
//    var slideWidth = $(elemento).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);
//    $(elemento).unbind("mousemove");
//}

function irSlide(elemento, next, codigo) {
    var self = $(elemento).children().eq(0);
    var slideWidth = $(elemento).width();

    var lastLeft = parseInt(self.css("left").split("px")[0]) * -1;
    var posicao = next ? parseInt(lastLeft / slideWidth) + 1 : parseInt(lastLeft / slideWidth) - 1;
    if (posicao > 5)
        posicao = 5;

    if (posicao == -1) {
        posicao = 0;
    }

    eventoClickBotaoAlarmeMobile({ Codigo: codigo, Slide: posicao });
    //self.animate({ left: (posicao * (slideWidth * -1)) + "px" }, 250);
    //$(elemento).unbind("mousemove");
}

function irParaSlide(elemento, posicao) {
    var self = $(elemento).children().eq(0);
    var slideWidth = $(elemento).width();

    var lastLeft = parseInt(self.css("left").split("px")[0]) * -1;

    if (posicao > 6)
        posicao = 6;

    if (posicao == -1) {
        posicao = 0;
    }

    self.animate({ left: (posicao * (slideWidth * -1)) + "px" }, 250);
    //$(elemento).unbind("mousemove");
}

function getMouseXY(e) {
    var _x = 0;
    var _y = 0;
    if (e.originalEvent) {
        _x = e.originalEvent.changedTouches[0].pageX;
        _y = e.originalEvent.changedTouches[0].pageY;
    } else 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 };
}
