/* Variables */
var ti_pluginstatus = false;
var ti_trackingstatus = false;
var ti_podolskispeechstatus = false;
var ti_postermode = '';
var ti_snapshot_data = '';
var ti_debug = false;
var ti_current_player = '';

// FUNCTIONS THAT CANNOT BE CALLED FROM JAVASCRIPT
// THOSE FUNCTIONS ARE AUTOMATICALLY CALLED BY D'FUSION PLUGIN
// DO NOT RENAME OR MODIFY THOSE FUNCTIONS
function DFUSION_EVENT_Error(status) { }
function CmdStatus() { updateInfoCmdStatus(arguments[0],arguments[1]); }
function SnapshotStatus() { updateInfoSnapshotStatus(arguments[0],arguments[1]); }
function TrackingStatus() { updateInfoTrackingStatus(arguments[0],arguments[1]); }
function GameModeScore() { updateInfoGameModeScore(arguments[0],arguments[1]); }
function Base64Picture() { updateImage(arguments[0]); }

// FUNCTIONS THAT CAN BE CALLED FROM JAVASCRIPT
// DO NOT RENAME OR MODIFY THOSE FUNCTIONS
function Snapshot() { ExecuteCommand("snapshot '0'"); }
function LeftHanded() { ExecuteCommand("LeftHanded '0'"); }
function RightHanded() { ExecuteCommand("RightHanded '0'"); }
function StartPlaying(player) { ExecuteCommand("StartPlaying '" + player + "'"); }
function PlayAgain() { ExecuteCommand("PlayAgain '0'"); }
function SetPosterMode(speech) { ExecuteCommand("SetPosterMode '" + speech + "'"); }
function SetStickerMode(player) { ExecuteCommand("SetStickerMode '" + player + "'"); }
function SetCoverMode() { ExecuteCommand("SetCoverMode '0'"); }
function RepeatSpeech() { ExecuteCommand("RepeatSpeech '0'"); }


// CUSTOMIZABLE FUNCTIONS
// DO NOT RENAME THOSE FUNCTIONS
// YOU CAN JUST CUSTOMIZE THE BODY OF THOSE FUNCTIONS
function Init() { }

// This function is called once the D'Fusion plugin is started
// If you rename this function, do not forget to updtae the parametrs of tiGenerateDFusionWebHTML()
function PersonalizedAction() {
	// initialize your variables
	// communication with the plug-in should be initiated after that call
}

// This function refreshes the image on the website
function updateImage(base_64_picture) {
  if(terms_check()) {
    submit_snapshot(base_64_picture);
  } else {
    ti_snapshot_data = base_64_picture;
    $('.plugin').hide();
    $('.terms').show();
  }
}

function updateInfoCmdStatus() {
  if(ti_debug) console.log(arguments[0]);
}

function updateInfoTrackingStatus() {
  if(ti_debug) console.log('Tracking:', arguments[0]);
}

function updateInfoGameModeScore() {
  if(ti_debug) console.log('Score:', arguments[0]);
  $('.currentscore').html(arguments[0]);
}

function updateInfoSnapshotStatus() {
  if(ti_debug) console.log('Snapshot:', arguments[0]);
}

function PodolskiSpeechStatus() {
	// arguments[0] is 'TRUE' or 'FALSE'
  if(ti_debug) console.log('Podolski Speech:', arguments[0]);
  ti_podolskispeechstatus = arguments[0];
}

// MB FUNCTIONS
// GO CRAZY!
function terms_check() {
  if (getCookie('mal_de_terms')==1) {
    return true;
  } else {
    return false
  }
}

function terms_accept() {
  $('.terms').hide();
  $('.plugin').show();
  setCookie('mal_de_terms', 1);
  if(ti_snapshot_data!='') {
    data = ti_snapshot_data;
    ti_snapshot_data = '';
    submit_snapshot(data);
  }
}

function terms_decline() {
  $('.terms').hide();
  $('.plugin').show();
  ti_snapshot_data = '';
  setCookie('mal_de_terms', 0);
}

function submit_snapshot(data) {
  $('img', this).attr( 'src', $('.button-snapshot a img').attr("src").replace('3dlive-button-snapshot', '3dlive-button-pleasewait') );

  $.post('snapshot.php', { data: data }, function(response) {
    if(response['success']) {
      window.location = vrootdir+"/3dlive/download.php?img="+response['filename'];
    } else {
      alert(response['msg']);
      $('.button-snapshot a img').attr( 'src', $('.button-snapshot a img').attr("src").replace('3dlive-button-pleasewait', '3dlive-button-snapshot') );
    }
  }, 'json');
}

// COOKIE FUNCTIONS FROM W3SCHOOLS
// http://www.w3schools.com/JS/js_cookies.asp
function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

// PAGE INIT FUNCTIONS
// BINDING EVENTS TO THE PAGE BUTTONS
$(document).ready(function() {

  $('.button-snapshot a').click(function() {
    Snapshot();
    return false;
  });
  
  $('.button-replay-album a').click(function() {
    RepeatSpeech();
    return false;
  });

  $('.button-playnow a').click(function() {
    // Need to figure out which player is playing
    StartPlaying(ti_current_player);
    return false;
  });

  $('.button-playagain a').click(function() {
    PlayAgain();
    return false;
  });

  $('.button-replay a').click(function() {
    RepeatSpeech();
    return false;
  });

  $('.button-poster1 a').click(function() {
    SetPosterMode('bravo');
    return false;
  });

  $('.button-poster2 a').click(function() {
    SetPosterMode('tv');
    return false;
  });

  /* Not for launch
  $('.button-poster3 a').click(function() {
    SetPosterMode('kicker');
    return false;
  });
  */

  $('.button-sticker1 a').click(function() {
    ti_current_player = 'adler';
    SetStickerMode(ti_current_player);
    return false;
  });

  $('.button-sticker2 a').click(function() {
    ti_current_player = 'naldo';
    SetStickerMode(ti_current_player);
    return false;
  });

  $('.button-sticker3 a').click(function() {
    ti_current_player = 'kehl';
    SetStickerMode(ti_current_player);
    return false;
  });

  $('.button-sticker4 a').click(function() {
    ti_current_player = 'misimovic';
    SetStickerMode(ti_current_player);
    return false;
  });

  $('.button-sticker5 a').click(function() {
    ti_current_player = 'podolski';
    SetStickerMode(ti_current_player);
    return false;
  });

  $('.terms-accept a').click(function() {
    terms_accept();
    return false;
  });

  $('.terms-decline a').click(function() {
    terms_decline();
    return false;
  });
  /*
  $('.jqlog').ajaxStart(function() {
    $(this).append('[sending image...]<br />');
  });


*/

});
