var count_rotate = 20,
  delay_timer = 2e4,
  begin_circle = 18,
  radius = 36,
  begin_angle = begin_circle - radius / 2,
  fadein_timer = 1e3,
  eas = [],
  i = 0,
  j = 0;
function randomXToY(t, e, i) {
  return (
    (t += Math.random() * (e - t)), void 0 === i ? Math.round(t) : t.toFixed(i)
  );
}
function roundNumber(t, e) {
  return Math.round(t * Math.pow(10, e)) / Math.pow(10, e);
}
$(".castx-start").click(function () {
  $(this).hide();
  var t = 0 + randomXToY(1, -8) * radius,
    e = -360 * count_rotate + t,
    t = 360 * count_rotate + t,
    i =
      (i = roundNumber((s = Math.abs((e % 360) - begin_angle) / 36), 0)) % 10 ==
      0
        ? i
        : i % 10,
    s = randomXToY(0, eas.length - 1),
    a = 0 + randomXToY(1, -8) * radius,
    n = -360 * count_rotate + a,
    a = 360 * count_rotate + a,
    n =
      (n = roundNumber((t = Math.abs((n % 360) - begin_angle) / 36), 0)) % 10 ==
      0
        ? n
        : n % 10,
    t = randomXToY(0, eas.length - 1);
  $("#castx-img1").rotate({
    angle: begin_angle,
    animateTo: e,
    duration: delay_timer,
    easing: $.easing[eas[s]],
  }),
    $("#castx-img2").rotate({
      angle: begin_angle,
      animateTo: a,
      duration: delay_timer,
      easing: $.easing[eas[t]],
    });
  n =
    "<span class='lucky_spin_resultTxt'>เลขนำโชคของท่านคือ</span><br/><font class='lucky_spin_resultNumber'>" +
    (i % 10) +
    " " +
    (n % 10) +
    "</font>";
  window.setTimeout(function () {
    $("#castx-img-pointer")
      .delay(delay_timer - fadein_timer + 1e3)
      .show();
  }, delay_timer - fadein_timer),
    $("#castx_result")
      .hide()
      .delay(delay_timer - fadein_timer + 500)
      .html(n)
      .fadeIn(fadein_timer);
}),
  (function ($) {
    for (
      var supportedCSS,
        supportedCSSOrigin,
        styles = document.getElementsByTagName("head")[0].style,
        toCheck =
          "transformProperty WebkitTransform OTransform msTransform MozTransform".split(
            " "
          ),
        a = 0,
        da;
      a < toCheck.length;
      a++
    )
      void 0 !== styles[toCheck[a]] && (supportedCSS = toCheck[a]);
    supportedCSS &&
      ((supportedCSSOrigin = supportedCSS.replace(
        /[tT]ransform/,
        "TransformOrigin"
      )),
      "T" == supportedCSSOrigin[0] && (supportedCSSOrigin[0] = "t")),
      eval('IE = "v"=="\v"'),
      jQuery.fn.extend({
        rotate: function (t) {
          if (0 !== this.length && void 0 !== t) {
            "number" == typeof t &&
              (t = {
                angle: t,
              });
            for (var e = [], i = 0, s = this.length; i < s; i++) {
              var a,
                n = this.get(i);
              n.Wilq32 && n.Wilq32.PhotoEffect
                ? n.Wilq32.PhotoEffect._handleRotation(t)
                : ((a = $.extend(!0, {}, t)),
                  (a = new Wilq32.PhotoEffect(n, a)._rootObj),
                  e.push($(a)));
            }
            return e;
          }
        },
        getRotateAngle: function () {
          for (var t = [0], e = 0, i = this.length; e < i; e++) {
            var s = this.get(e);
            s.Wilq32 &&
              s.Wilq32.PhotoEffect &&
              (t[e] = s.Wilq32.PhotoEffect._angle);
          }
          return t;
        },
        stopRotate: function () {
          for (var t = 0, e = this.length; t < e; t++) {
            var i = this.get(t);
            i.Wilq32 &&
              i.Wilq32.PhotoEffect &&
              clearTimeout(i.Wilq32.PhotoEffect._timer);
          }
        },
      }),
      (Wilq32 = window.Wilq32 || {}),
      (Wilq32.PhotoEffect = supportedCSS
        ? function (t, e) {
            (t.Wilq32 = {
              PhotoEffect: this,
            }),
              (this._img = this._rootObj = this._eventObj = t),
              this._handleRotation(e);
          }
        : function (t, e) {
            var i;
            (this._img = t),
              (this._onLoadDelegate = [e]),
              (this._rootObj = document.createElement("span")),
              (this._rootObj.style.display = "inline-block"),
              (this._rootObj.Wilq32 = {
                PhotoEffect: this,
              }),
              t.parentNode.insertBefore(this._rootObj, t),
              t.complete
                ? this._Loader()
                : ((i = this),
                  jQuery(this._img).bind("load", function () {
                    i._Loader();
                  }));
          }),
      (Wilq32.PhotoEffect.prototype = {
        _setupParameters: function (t) {
          (this._parameters = this._parameters || {}),
            "number" != typeof this._angle && (this._angle = 0),
            "number" == typeof t.angle && (this._angle = t.angle),
            (this._parameters.animateTo =
              "number" == typeof t.animateTo ? t.animateTo : this._angle),
            (this._parameters.step = t.step || this._parameters.step || null),
            (this._parameters.easing =
              t.easing || this._parameters.easing || this._defaultEasing),
            (this._parameters.duration =
              "duration" in t
                ? t.duration
                : t.duration || this._parameters.duration || 1e3),
            (this._parameters.callback =
              t.callback || this._parameters.callback || this._emptyFunction),
            (this._parameters.center = t.center ||
              this._parameters.center || ["50%", "50%"]),
            "string" == typeof this._parameters.center[0]
              ? (this._rotationCenterX =
                  (parseInt(this._parameters.center[0], 10) / 100) *
                  this._imgWidth *
                  this._aspectW)
              : (this._rotationCenterX = this._parameters.center[0]),
            "string" == typeof this._parameters.center[1]
              ? (this._rotationCenterY =
                  (parseInt(this._parameters.center[1], 10) / 100) *
                  this._imgHeight *
                  this._aspectH)
              : (this._rotationCenterY = this._parameters.center[1]),
            t.bind &&
              t.bind != this._parameters.bind &&
              this._BindEvents(t.bind);
        },
        _emptyFunction: function () {},
        _defaultEasing: function (t, e, i, s, a) {
          return -s * ((e = e / a - 1) * e * e * e - 1) + i;
        },
        _handleRotation: function (t, e) {
          supportedCSS || this._img.complete || e
            ? (this._setupParameters(t),
              this._angle == this._parameters.animateTo
                ? this._rotate(this._angle)
                : this._animateStart())
            : this._onLoadDelegate.push(t);
        },
        _BindEvents: function (t) {
          if (t && this._eventObj) {
            if (this._parameters.bind) {
              var e = this._parameters.bind;
              for (i in e)
                e.hasOwnProperty(i) && jQuery(this._eventObj).unbind(i, e[i]);
            }
            for (var i in (this._parameters.bind = t))
              t.hasOwnProperty(i) && jQuery(this._eventObj).bind(i, t[i]);
          }
        },
        _Loader: IE
          ? function () {
              var t,
                e = this._img.width,
                i = this._img.height;
              for (
                this._imgWidth = e,
                  this._imgHeight = i,
                  this._img.parentNode.removeChild(this._img),
                  this._vimage = this.createVMLNode("image"),
                  this._vimage.src = this._img.src,
                  this._vimage.style.height = i + "px",
                  this._vimage.style.width = e + "px",
                  this._vimage.style.position = "absolute",
                  this._vimage.style.top = "0px",
                  this._vimage.style.left = "0px",
                  this._aspectW = this._aspectH = 1,
                  this._container = this.createVMLNode("group"),
                  this._container.style.width = e,
                  this._container.style.height = i,
                  this._container.style.position = "absolute",
                  this._container.style.top = "0px",
                  this._container.style.left = "0px",
                  this._container.setAttribute(
                    "coordsize",
                    e - 1 + "," + (i - 1)
                  ),
                  this._container.appendChild(this._vimage),
                  this._rootObj.appendChild(this._container),
                  this._rootObj.style.position = "relative",
                  this._rootObj.style.width = e + "px",
                  this._rootObj.style.height = i + "px",
                  this._rootObj.setAttribute(
                    "id",
                    this._img.getAttribute("id")
                  ),
                  this._rootObj.className = this._img.className,
                  this._eventObj = this._rootObj;
                (t = this._onLoadDelegate.shift());

              )
                this._handleRotation(t, !0);
            }
          : function () {
              this._rootObj.setAttribute("id", this._img.getAttribute("id")),
                (this._rootObj.className = this._img.className),
                (this._imgWidth = this._img.naturalWidth),
                (this._imgHeight = this._img.naturalHeight);
              var t,
                e = Math.sqrt(
                  this._imgHeight * this._imgHeight +
                    this._imgWidth * this._imgWidth
                );
              for (
                this._width = 3 * e,
                  this._height = 3 * e,
                  this._aspectW =
                    this._img.offsetWidth / this._img.naturalWidth,
                  this._aspectH =
                    this._img.offsetHeight / this._img.naturalHeight,
                  this._img.parentNode.removeChild(this._img),
                  this._canvas = document.createElement("canvas"),
                  this._canvas.setAttribute("width", this._width),
                  this._canvas.style.position = "relative",
                  this._canvas.style.left =
                    -this._img.height * this._aspectW + "px",
                  this._canvas.style.top =
                    -this._img.width * this._aspectH + "px",
                  this._canvas.Wilq32 = this._rootObj.Wilq32,
                  this._rootObj.appendChild(this._canvas),
                  this._rootObj.style.width =
                    this._img.width * this._aspectW + "px",
                  this._rootObj.style.height =
                    this._img.height * this._aspectH + "px",
                  this._eventObj = this._canvas,
                  this._cnv = this._canvas.getContext("2d");
                (t = this._onLoadDelegate.shift());

              )
                this._handleRotation(t, !0);
            },
        _animateStart: function () {
          this._timer && clearTimeout(this._timer),
            (this._animateStartTime = +new Date()),
            (this._animateStartAngle = this._angle),
            this._animate();
        },
        _animate: function () {
          var t,
            e = +new Date(),
            i = e - this._animateStartTime > this._parameters.duration;
          i && !this._parameters.animatedGif
            ? clearTimeout(this._timer)
            : ((this._canvas || this._vimage || this._img) &&
                ((e = this._parameters.easing(
                  0,
                  e - this._animateStartTime,
                  this._animateStartAngle,
                  this._parameters.animateTo - this._animateStartAngle,
                  this._parameters.duration
                )),
                this._rotate(~~(10 * e) / 10)),
              this._parameters.step && this._parameters.step(this._angle),
              ((t = this)._timer = setTimeout(function () {
                t._animate.call(t);
              }, 10))),
            this._parameters.callback &&
              i &&
              ((this._angle = this._parameters.animateTo),
              this._rotate(this._angle),
              this._parameters.callback.call(this._rootObj));
        },
        _rotate:
          ((da = Math.PI / 180),
          IE
            ? function (t) {
                (this._angle = t),
                  (this._container.style.rotation = (t % 360) + "deg"),
                  (this._vimage.style.top =
                    -(this._rotationCenterY - this._imgHeight / 2) + "px"),
                  (this._vimage.style.left =
                    -(this._rotationCenterX - this._imgWidth / 2) + "px"),
                  (this._container.style.top =
                    this._rotationCenterY - this._imgHeight / 2 + "px"),
                  (this._container.style.left =
                    this._rotationCenterX - this._imgWidth / 2 + "px");
              }
            : supportedCSS
            ? function (t) {
                (this._angle = t),
                  (this._img.style[supportedCSS] =
                    "rotate(" + (t % 360) + "deg)"),
                  (this._img.style[supportedCSSOrigin] =
                    this._parameters.center.join(" "));
              }
            : function (t) {
                (t = ((this._angle = t) % 360) * da),
                  (this._canvas.width = this._width),
                  (this._canvas.height = this._height),
                  this._cnv.translate(
                    this._imgWidth * this._aspectW,
                    this._imgHeight * this._aspectH
                  ),
                  this._cnv.translate(
                    this._rotationCenterX,
                    this._rotationCenterY
                  ),
                  this._cnv.rotate(t),
                  this._cnv.translate(
                    -this._rotationCenterX,
                    -this._rotationCenterY
                  ),
                  this._cnv.scale(this._aspectW, this._aspectH),
                  this._cnv.drawImage(this._img, 0, 0);
              }),
      }),
      IE &&
        (Wilq32.PhotoEffect.prototype.createVMLNode = (function () {
          document
            .createStyleSheet()
            .addRule(".rvml", "behavior:url(#default#VML)");
          try {
            return (
              document.namespaces.rvml ||
                document.namespaces.add(
                  "rvml",
                  "urn:schemas-microsoft-com:vml"
                ),
              function (t) {
                return document.createElement("<rvml:" + t + ' class="rvml">');
              }
            );
          } catch (t) {
            return function (t) {
              return document.createElement(
                "<" + t + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">'
              );
            };
          }
        })());
  })(jQuery);
