$(function() { // USER INFORMATION var spinsRemaining = ''; var accumulatedPoints = ''; var placeValue = new Array(); var pointToEnglish = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; var awarded = 0; var earnedCredits = $('.earned-credits'); var spinning = false; var spinDate = ''; var lastSpinDate = ''; // [DESKTOP] Containers var Landing = $('#152199381'); var Spin = $('#152199382'); var Results = $('#152199383'); // [DESKTOP] Elements var btnStartSpinning = $('.StartSpinning'); var btnSpinIt = $('.SpinIt'); var btnActivities = $('.Activities'); var btnSpinners = $('.Spinner'); var bgSpin = $('.before-the-spin'); var containerVideo = ''; var containerRemaining = $('.your-spins-left'); var animation = ''; var sound = ''; var loadcheck = ''; // [MOBILE] Elements var btnMobileSpinIt = $('.MobileSpinIt, .ActuatorSpin'); var bgMobileSpin = $('.mobile-before-the-spin'); var containerAnimation = $('.animation'); var containerMobileRemaining = $('.mobile-your-spins-left'); // [MOBILE] Animation Elements var triggeredMobile = false; var currentFrame = 0; var lastFrame = 0; var maxFrame = 43; var frameIncrement = 1; var framePad = '0'; var frameName = '_Coins'; var frameExtension = '.jpg'; var frameUrl = 'http://spintowinstage.com/dev/animations/'; var prizeName = ''; var playAnim = null; var toPreload = new Array(); var frames = new Array(); var soundToggle = $('.sound-toggle'); var soundState = 'playing'; var spinInfo = ''; invokeGetSpin(function(response) { accumulatedPoints = response.response.credits_earned; lastSpinDate = response.response.last_spin_date; // Reset setLastSpinDate = new Date(lastSpinDate); //use api last spin date var lastSpinDateDay = setLastSpinDate.getDay(); var lastSpinDateUNIX = setLastSpinDate.getTime(); var resetDate = new Date(lastSpinDate); //use api last spin date var today = new Date(); var todayDay = today.getDay(); var todayUNIX = today.getTime(); if (lastSpinDateDay == 5) { resetDate.setDate(resetDate.getDate() + (lastSpinDateDay+1)); } else if(lastSpinDateDay == 6) { resetDate.setDate(resetDate.getDate() + (lastSpinDateDay-1)); } else { resetDate.setDate(resetDate.getDate() + (4 - lastSpinDateDay)) } resetDate.setMilliseconds(0); resetDate.setSeconds(59); resetDate.setMinutes(59); resetDate.setHours(23); var resetUNIX = resetDate.getTime(); if(todayUNIX < resetUNIX) { spinsRemaining = 5-(response.response.num_spins_since); } else { spinsRemaining = 5; } if(spinsRemaining === 0) { setTimeout(function() { var remaining = containerRemaining.find('.'+pointToEnglish[spinsRemaining]).position().top; containerRemaining.animate({ 'top' : '-'+remaining }, 700, 'easeInOutQuint'); showCurrentPoints(awarded); }, 500); Landing.hide(); Spin.show(); spinning = true; $('.MobileSpinIt').fadeTo('slow', 0.35); $('.SpinIt').fadeTo('slow', 0.35); showModal(accumulatedPoints); } soundToggle.on('click', function(e) { e.preventDefault(); var _self = $(this); _self.toggleClass('on'); if(_self.hasClass('on')) { if(containerVideo) { containerVideo.muted = false; } soundState = 'playing'; } else { if(containerVideo) { containerVideo.muted = true; } soundState = 'muted'; } }); // [DESKTOP] Hide Screen 1; Show Screen 2 btnStartSpinning.on('click', function(e) { e.preventDefault(); // Hide the landing screen and show the spin screen Landing.fadeOut(); Spin.fadeIn(); var remaining = containerRemaining.find('.'+pointToEnglish[spinsRemaining]).position().top; containerRemaining.animate({ 'top' : '-'+remaining }, 700, 'easeInOutQuint'); // Wait and display the users accumulated points setTimeout(function() { showCurrentPoints(accumulatedPoints); }, 200); }); // [DESKTOP] Spin btnSpinIt.on('click', function(e) { e.preventDefault(); if(spinning === false) { spinning = true; spinDate = new Date(); // Prize draw var prize = randomInt(1, 10000); // Prize selection if (prize <= 4500) { animation = '01'; awarded = 25; //ct_trck_spin_to_win_25_points(); } else if (prize >= 4501 && prize <= 8500) { animation = '10'; awarded = 250; //ct_trck_spin_to_win_250_points(); } else if (prize >= 8501 && prize <= 9750) { animation = '250'; awarded = 1000; //ct_trck_spin_to_win_1000_points(); } else if (prize >= 9751 && prize <= 9996) { animation = '750'; awarded = 3000; //ct_trck_spin_to_win_3000_points(); } else if (prize >= 9997 && prize <= 10000) { animation = 'jackpot'; awarded = 100000; //ct_trck_spin_to_win_100000_points(); } containerVideo = document.getElementById('video_'+animation); // Recalculate spin remaining spinsRemaining = spinsRemaining-1; // Display remaining spins var remaining = containerRemaining.position().top; containerRemaining.animate({ 'top' : remaining-51 }, 1000, 'easeInOutQuint'); var remaining = containerMobileRemaining.position().top; containerMobileRemaining.animate({ 'top' : '-100%' }, 1000, 'easeInOutQuint'); // Fade spin buttons btnSpinners.addClass('Spinning').fadeTo('slow', 0.35); // Play animation containerVideo.play(); switch(soundState) { case 'muted': containerVideo.muted = true; break; case 'playing': containerVideo.muted = false; break; } $containerVideo = $(containerVideo); // Hide the background and show the slot animation bgSpin.hide(); $('video').hide(); $containerVideo.css({ 'display' : 'block' }); /* UPDATE USER */ invokeUpdateSpin(spinDate.toUTCString(), (5-spinsRemaining), (accumulatedPoints+awarded)); // On animation end containerVideo.onended = function(e) { // Recalculate the points awared and display them showCurrentPoints(awarded, 1); if(spinsRemaining == 0) { // Show the congratulatory overlay showModal(accumulatedPoints); } else { btnSpinners.removeClass('Spinning').fadeTo('slow', 1); spinning = false; } }; } }); //********************// // [MOBILE] Animate remaining spins on load var remaining = containerMobileRemaining.find('.'+pointToEnglish[spinsRemaining]).index(); containerMobileRemaining.animate({ 'top' : '-'+(remaining*100)+'%' }, 700, 'easeInOutQuint'); if ($(window).width() < 980) { triggeredMobile = true; showCurrentPoints(accumulatedPoints); } // [MOBILE] Spin btnMobileSpinIt.on('click', function(e) { e. preventDefault(); if(spinning === false) { spinning = true; spinDate = new Date(); // Prize draw var prize = randomInt(1, 10000); // Prize selection if (prize <= 4500) { animation = '0'; awarded = 25; //ct_trck_spin_to_win_25_points(); } else if (prize >= 4501 && prize <= 8500) { animation = '1'; awarded = 250; //ct_trck_spin_to_win_250_points(); } else if (prize >= 8501 && prize <= 9750) { animation = '2'; awarded = 1000; //ct_trck_spin_to_win_1000_points(); } else if (prize >= 9751 && prize <= 9996) { animation = '3'; awarded = 3000; //ct_trck_spin_to_win_3000_points(); } else if (prize >= 9997 && prize <= 10000) { animation = '4'; awarded = 100000; //ct_trck_spin_to_win_100000_points(); } triggeredMobile = true; spinsRemaining = spinsRemaining-1; // Hide the Desktop landing page and show spin Landing.hide(); Spin.css('display', 'block'); // Fade spin buttons $('.MobileSpinIt').fadeTo('slow', 0.35); var remaining = containerMobileRemaining.find('.'+pointToEnglish[spinsRemaining]).index(); containerMobileRemaining.animate({ 'top' : '-'+(remaining*100)+'%' }, 1000, 'easeInOutQuint'); createAnimationImages(animation); containerAnimation.show(); /* UPDATE USER */ invokeUpdateSpin(spinDate.toUTCString(), (5-spinsRemaining), (accumulatedPoints+awarded)); playAnim = setInterval(function() { revealFrames(awarded); },70); } }); }); // Countdown function updateCountdown() { var currentDate = new Date(); var currentDay = currentDate.getDay(); var currentUNIX = currentDate.getTime(); var resetDate = new Date(); if (currentDay == 5) { resetDate.setDate(resetDate.getDate() + (currentDay + 1)); } else if(currentDay == 6) { resetDate.setDate(resetDate.getDate() + (currentDay - 1)); } else { resetDate.setDate(resetDate.getDate() + (4 - currentDay)) } resetDate.setMilliseconds(0); resetDate.setSeconds(59); resetDate.setMinutes(59); resetDate.setHours(23); var resetUNIX = resetDate.getTime(); var diff = Math.floor((resetUNIX - currentUNIX) / 1000); var seconds = diff % 60; diff = Math.floor(diff / 60); var minutes = diff % 60; diff = Math.floor(diff / 60); var hours = diff % 24; diff = Math.floor(diff / 24); var days = diff; // pad single digits if (days < 10) days = "0" + days; if (hours < 10) hours = "0" + hours; if (minutes < 10) minutes = "0" + minutes; if (seconds < 10) seconds = "0" + seconds; // insert values into table $('.days').html(days); $('.hours').html( hours); $('.minutes').html(minutes); $('.seconds').html(seconds); //} } updateCountdown(); setInterval(updateCountdown, 1000); //********************// // Show the points the user has earned function showCurrentPoints(points, spun) { // Establish the accumulated points if(spun) { accumulatedPoints += points; } // Count the place values of the accumalted points var valuesOfPoints = accumulatedPoints.toString().split(''); var vopLength = valuesOfPoints.length; // Get how many place values there are in the current points switch(vopLength) { case 6: placeValue.push('hundred-thousands', 'ten-thousands', 'thousands', 'hundreds', 'tens', 'ones'); break; case 5: placeValue.push('ten-thousands', 'thousands', 'hundreds', 'tens', 'ones'); break; case 4: placeValue.push('thousands', 'hundreds', 'tens', 'ones'); break; case 3: placeValue.push('hundreds', 'tens', 'ones'); break; case 2: placeValue.push('tens', 'ones'); break; case 1: placeValue.push('ones'); break; } // Convert each integer to its string equivalent // Move counters based on place value for(var n = 0; n < vopLength; n++) { // Store the point value var intPoint = valuesOfPoints[n]; // Select the string equivalent of the integer var convertedPoint = pointToEnglish[valuesOfPoints[n]]; // Establish the place value container var pointValueContainer = $('.'+(triggeredMobile ? 'Mobile' : 'Desktop')+' .'+placeValue[n]+''); // Establish the position of point within the defined container from previous step if(triggeredMobile === true) { var pointValueIndex = pointValueContainer.find('.'+convertedPoint).index(); var pointValuePosition = ((pointValueIndex)*100)+'%'; } else { var pointValuePosition = pointValueContainer.find('.'+convertedPoint).position().top; } // Display the points (function(pointValuePosition, convertedPoint, intPoint, n) { pointValueContainer.animate({ 'top' : '-'+pointValuePosition }, 1000, 'easeInOutBack', function() { var _self = $(this); var clonedElements = _self.find('li:nth-child(2)').nextUntil('.'+convertedPoint).addBack().clone(true, true).end(); clonedElements.appendTo(_self); if(triggeredMobile === true) { var adjustedPointValuePosition = _self.find('.'+convertedPoint).index(); adjustedPointValuePosition = ((adjustedPointValuePosition)*100)+'%'; } else { var adjustedPointValuePosition = _self.find('.'+convertedPoint).position().top; } _self.animate({ 'top' : '-'+adjustedPointValuePosition }, 0); if(intPoint == 0) { _self.find('.cosmetic').html('9'); } else { _self.find('.cosmetic').html((intPoint-1)); } clonedElements = ''; }); })(pointValuePosition, convertedPoint, intPoint, n); } // Close // Reset the array of place values placeValue = []; } // Display the after spin congratulations overlay function showModal(pointsEarned) { Results.show(); earnedCredits.html(pointsEarned); $('body').append('
'); $('.em').find('a').attr('href', 'mailto: ?subject=Join the Family – Assassin’s Creed&body=I got '+pointsEarned+' credits this week on Assassin’s Creed Spin-to-Win. Join the family today at https://previewsites.ubi.com/ac/en-us/rewards/'); $('.tw').find('a').attr('href', 'http://twitter.com/share?text=I got '+pointsEarned+' credits this week on Assassin’s Creed Spin-to-Win. Join the family today at&url=https://previewsites.ubi.com/ac/en-us/rewards/&hashtags=AssassinsCreed'); $('.fb').find('a').attr('href', 'https://www.facebook.com/sharer/sharer.php?u=https://previewsites.ubi.com/ac/en-us/rewards/'); } // The odds function randomInt(min,max) { return Math.floor(Math.random()*(max-min+1)+min); } // [MOBILE] Animation function revealFrames(prize) { containerAnimation.find('img').eq(currentFrame).css('opacity', 1); lastFrame = currentFrame; currentFrame+=frameIncrement; if(currentFrame>maxFrame) { clearInterval(playAnim); var last_still = containerAnimation.find('img').eq(maxFrame).attr('src'); bgMobileSpin.attr('src', last_still); containerAnimation.find('img').css('opacity', 0); containerAnimation.empty(); // Pseudo flash to ease the image reset of the slot machine //bgMobileSpin.fadeTo('fast', 0.35).fadeTo('fast', 1); // Recalculate the points awared and display them showCurrentPoints(awarded, 1); if(spinsRemaining == 0) { showModal(accumulatedPoints); // btnActivities.on('click', function(e) { // Results.hide(); // $('.modal-transparency').remove(); // }); } else { spinning = false; btnMobileSpinIt.fadeTo('slow', 1); } // Reset frames currentFrame = 0; } } function createAnimationImages(p) { for(var i = 0; i < frames[p].length; i++) { containerAnimation.append(frames[p][i]); } } function preload() { for(var o = 0;o <= 4;o++) { frames[o] = new Array(); for (var i = 0, len = toPreload[o].length; i < len; i++) { frames[o].push(new Image()); frames[o][i].src = toPreload[o][i]; frames[o][i].className = 'animation-frames'; } } } for(var o = 0;o <= 4;o++) { switch(o) { case 0: var prize = '01'; break; case 1: var prize = '10'; break; case 2: var prize = '250'; break; case 3: var prize = '750'; break; case 4: var prize = 'jackpot'; break; } toPreload[o] = new Array(); for(var i = 0; i <= maxFrame; i++) { if(i >= 10) { framePad = ''; } else { framePad = 0; } var file = frameUrl+prize+'/acs_loyalty_spintowin_'+prize+frameName+framePad+i+frameExtension+'?v2'; //toPreload[o].push(file); } } preload(); // UBI API Functions function invokeGetSpin(success) { var getSpinCallbackInfo = { onSuccess: success, onError: function(response) {} }; ubiCrowdTwist.getSpin(getSpinCallbackInfo); } function invokeUpdateSpin(spin_date, spins_made, awarded) { var updateSpinCallbackInfo = { onSuccess: function(response) { }, onError: function(response) { } }; var newSpinInfo = { last_spin_date: spin_date, num_spins_since: spins_made, credits_earned: awarded }; ubiCrowdTwist.updateSpin(newSpinInfo, updateSpinCallbackInfo); } });