var errorExist = Boolean(false);
var currentCoommonGame = 1;
var commoTimerID = 1;

$(document).ready(function(){
  // notify me init
  $('.notify-me-button').click(getNotifyMe);
  setTimeout(function () {commonGamesScroller()},7500);
});

function commonGamesScroller() {
  if (coommonGamesCount == 1)
    return false;
  
  currentCoommonGame = parseInt(currentCoommonGame);
  if (currentCoommonGame >= coommonGamesCount) 
    var nextItem = 1;
  else
    var nextItem = parseInt(currentCoommonGame) + 1;
  
  $('.head-box-main .coming-box.game-'+currentCoommonGame).fadeOut('300');
  $('.head-box-main .coming-box.game-'+nextItem).fadeIn('300');
  currentCoommonGame = nextItem;  
  clearTimeout(commoTimerID);
  commoTimerID = setTimeout(function () {commonGamesScroller()},5000);
}

function getNotifyMe() {
  var args = $(this).attr('id').split(/\./);
  var data = {
    game_id    : args[1],
    sendtype : 'ajax',
    action   : 'getgame'
  }
  $.ajax({
      'url'         : '/index.php',
      'dataType'    : 'json',
      'data'        : data,
      'cache'       : false,
      'success'     : showNotifyMe,
      'type'        : 'post'
  });
  return false;
}
function showNotifyMe(request) {
  clearTimeout(commoTimerID);
  $('.notify-me').fadeIn(300);
  $('.dialog-box.notify-me .close').click(hideNotifyMe);
  $('.dialog-box.notify-me .icon img').attr('src', 'http://www.g5e.com/game/icons/'+request.id+'_big.png');
  $('.dialog-box.notify-me .description #game').html(request.name);

// catch escape button press
  $(document).keydown(function(event){
    var e;
    if (event.which !="") {e = event.which;}
    else if (event.charCode != "") {e = event.charCode;}
    else if (event.keyCode != "") {e = event.keyCode;}
    if (e == 27) hideNotifyMe();
  });

  $('.dialog-box.notify-me .submit').click(sendNotifyMe);
}

function hideNotifyMe() {
  if (errorExist)
  {
    $('.dialog-box.notify-me .error').fadeOut(0).html('');
    $('.dialog-box.notify-me').height($('.dialog-box.notify-me').height() - 39);
    errorExist = false;
  }

  $('.notify-me').fadeOut(300);
  $('.dialog-box.notify-me input').val('');
  $('.dialog-box.notify-me .close').unbind('click');
  $('.dialog-box.notify-me .submit').unbind('click');
  $(document).unbind('keydown');
  commoTimerID = setTimeout(function () {commonGamesScroller()},5000);
  
}

function sendNotifyMe() {
  var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
  if (!emailPattern.test($('.dialog-box.notify-me input').val()))
  {
    if(!errorExist)
      $('.dialog-box.notify-me').height($('.dialog-box.notify-me').height() + 39);
    errorExist = true;
    $('.dialog-box.notify-me .error').fadeIn(300).html(subscribe_not_valid);
    return false;
  }
  
  var data = {
    email    : $('.dialog-box.notify-me input').val(),
    sendtype : 'ajax',
    action   : 'notifyme'
  }
  
  $.ajax({
      'url'         : '/index.php',
      'dataType'    : 'json',
      'data'        : data,
      'cache'       : false,
      'success'     : notifyMeRequest,
      'type'        : 'post'
  });
  return false;
}

function notifyMeRequest(request) {
  switch(request.result) {
    case 'exist_err':
    case 'ok':
      $('.dialog-box.notify-me .error').hide();
      $('.dialog-box.notify-me .subscribe').hide();
      $('.dialog-box.notify-me .line').hide();
      $('.dialog-box.notify-me .terms').hide();
      $('.dialog-box.notify-me').height(138);
      $('.dialog-box.notify-me .description').fadeIn(300).html('Thank you for your interest in Virtual City Playground. We will let you know once it launches!');
      //138
      break;
      
    case 'valid_err':
    case 'email_empty':
      if(!errorExist)
        $('.dialog-box.notify-me').height($('.dialog-box.notify-me').height() + 39);
      errorExist = true;
      $('.dialog-box.notify-me .error').fadeIn(300).html(subscribe_not_valid);
      break;
  }
}
