Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JavaScript Document
- var isUserAuthorized = false;
- (function(d){
- var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
- if (d.getElementById(id)) {return;}
- js = d.createElement('script'); js.id = id; js.async = true;
- js.src = "//connect.facebook.net/et_EE/all.js";
- ref.parentNode.insertBefore(js, ref);
- }(document));
- window.fbAsyncInit = function() {
- FB.init({
- appId : '403431009745901',
- status : true,
- cookie : true,
- xfbml : true
- });
- FB.Canvas.setAutoGrow();
- FB.getLoginStatus(function(response) {
- if (response.status === 'connected') {
- isUserAuthorized = true;
- $("#currentUserId").val(response.authResponse.userID);
- } else if (response.status === 'not_authorized') {
- isUserAuthorized = false;
- } else {
- isUserAuthorized = false;
- }
- });
- };
- $(document).ready(function() {
- new game();
- });
- function game(){
- var currentUserId = getCurrentUserId(),
- isUserLiked = isUserLiked(),
- is18 = is18(),
- sitePath = location.protocol + '//' + location.host + '/viruvalge/kohvrikampaania';
- fbAccessPolicy = {scope: 'publish_stream'},
- restoreGameTimeout = 0;
- addEvents();
- function addEvents(){
- $('.rules').click(function() {
- showRules();
- return false;
- });
- $('body').click(function() {
- if ($(this).hasClass('show-age')){
- set18();
- return false;
- }
- });
- $('input').click(function() {
- if (isUserLiked == true){
- if (is18){
- if(isUserAuthorized == true){
- insertPinNumber($(this));
- } else {
- authorizeUser();
- }
- } else {
- show18Warning();
- }
- } else {
- showNotLikedOverlay();
- }
- return false;
- });
- $('.inviteFriends').click(function() {
- inviteFriends();
- return false;
- });
- }
- function set18(){
- is18 = true;
- $('#is18').val('1');
- $('body').removeClass('show-age').addClass('show-submit');
- }
- function authorizeUser(){
- FB.login(function(response) {
- if (response.authResponse){
- isUserAuthorized = true;
- var accessToken = response.authResponse.accessToken;
- currentUserId = response.authResponse.userID;
- FB.api('/me', function(response) {
- registerUser(response);
- postPhotoToFbAlbum(accessToken);
- });
- }
- }, fbAccessPolicy);
- }
- function postPhotoToFbAlbum(accessToken){
- var params = {};
- FB.api('/me/albums', 'post',
- { name: 'Viru Valge', message: 'Viru Valge' }, function(response) {
- params.message = 'Hei sƵber! Ma Ć¼ritan koodi Ƥra arvata. Kui see Ƶnnestub saadab Viru Valge mind Vahemere kruiisile koos kaaslasega! Tule aita mind ja vƵid ka reisile minna saada. http://www.viruvalge.ee/kohvrimang';
- params.url = sitePath + '/seinale.jpg';
- params.access_token = accessToken;
- FB.api('/' + response.id + '/photos', 'post',
- params, function(response) {}
- );
- }
- );
- }
- function registerUser(user){
- $.ajax({
- url: sitePath + '/_ajax/register',
- data: {id: currentUserId, name: user.name},
- success: function (data) {
- },
- dataType: 'json'
- });
- }
- function showNotLikedOverlay(){
- $('body').removeClass('show-submit').addClass('show-like');
- }
- function show18Warning(){
- $('body').removeClass('show-submit').addClass('show-age');
- }
- function insertPinNumber(buttonPressed){
- var pin = setPinVal(buttonPressed.val());
- if (pin.length == 6){
- submitPin(pin);
- }
- }
- function submitPin(pin){
- $.ajax({
- url: sitePath + '/_ajax/pin',
- data: {id: currentUserId, pin: pin},
- success: function (data) {
- setGameStatus(data.status, data.tries);
- },
- dataType: 'json'
- });
- }
- function setGameStatus(status, tries){
- switch (status){
- case 'win':
- setGameWinStatus();
- break;
- case 'over':
- setGameOverStatus();
- break;
- case 'reset':
- setGameResetStatus();
- break;
- default:
- setGameRunningStatus(tries);
- break;
- }
- }
- function setGameResetStatus(){
- setPinVal('');
- $('body').attr('class', '').addClass('show-submit');
- }
- function setPinVal(val){
- var pinVal = '',
- pin,
- pinNumber,
- valSet = false;
- $('.pin').find('p').each(function(){
- pin = $(this);
- pinNumber = pin.html();
- if (pinNumber == '' && !valSet && val != ''){
- pin.html(val);
- pinVal += val;
- valSet = true;
- } else if (val == ''){
- pin.html('');
- }
- pinVal += pinNumber;
- });
- return pinVal;
- }
- function setGameRunningStatus(tries){
- clearTimeout(restoreGameTimeout);
- $('.tries_left').text(tries);
- if (tries == 1) {
- $('.tries_text').text('katse jƤƤnud');
- }
- $('body').removeClass('show-submit').addClass('show-wrong');
- restoreGameTimeout = setTimeout(function(){ setGameStatus('reset'); }, 3000);
- }
- function setGameOverStatus(){
- $('body').removeClass('show-submit').addClass('show-gameover');
- }
- function setGameWinStatus(){
- $('body').removeClass('show-submit').addClass('show-win');
- }
- function inviteFriends(){
- $.ajax({
- url: sitePath + '/_ajax/extra',
- data: {id: currentUserId},
- dataType: 'json'
- });
- FB.ui({ method: 'apprequests',
- message: 'Arva Ƥra kohvri kood ja lƤhme Vahemere kruiisile!'
- });
- }
- function showRules(){
- $('#rules').dialog({
- autoOpen: false,
- modal: true
- });
- $('#rules').dialog('option', 'title', 'Kampaania reeglid');
- $('#rules').dialog('option', 'width', '700');
- $('#rules').dialog({
- buttons: {'Sulge': function() { $(this).dialog('close'); } }
- });
- $('#rules').dialog('open');
- }
- function isUserLiked(){
- if ($("#isUserLiked").val() == "1")
- return true;
- else
- return false;
- }
- function is18(){
- if ($("#is18").val() == "1")
- return true;
- else
- return false;
- }
- function getCurrentUserId(){
- return $("#currentUserId").val();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement