var limitReached = false;
var payfull = false;

preloads = new Object();
preloads[0] = new Image(); preloads[0].src = "/images/gl/copy_hover.gif";
preloads[1] = new Image(); preloads[1].src = "/images/gl/delete_hover.gif";
preloads[2] = new Image(); preloads[2].src = "/images/gl/edit_hover.gif";

function toggleMore() {
  if ($('addFeatures').style.display == 'none') {
    new Effect.Appear('addFeatures');
  }
  else {
    $('addFeatures').hide();
  }
}

function toggleAddFeaturesLink() {
  if ($('addFeaturesLink').style.display == 'none' && $('addFeatures').style.display == 'none' && $('addFeaturesOn').style.display == 'none') {
    Element.show('addFeaturesLink');
    new Effect.Pulsate('addFeaturesLink', { duration: 1, pulses: 2 } );
  }
}

function activateCode() {
  var code = $('code').value;
  if (code == '') {
    alert('Нужно ввести код');
  }
  else {
      new Ajax.Updater(
      'debug',
          '/animations/activate', {
            evalScripts: true,
              parameters: { code: code },
              onSuccess: function() { }
          }
      );
  }
}

function activateCodeDone(result) {
  if (result == 1) {
    Element.hide("limitReached");
    Element.hide("addFeatures");
    new Effect.Appear("addBlock");
    limitReached = false;
    payfull = true;
  }
  else {
    alert('Введён неверный код');
  }
}

function startEditor(i) {
  width = 990;
  height = 680;
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;
//	if (window.showModalDialog) {
  //	window.showModalDialog('/animations/editor/'+i, 'editorWin', 'dialogWidth:'+width+'px;dialogHeight:'+height+'px');
  //}
  //else {
    window.open('/animations/editor/'+i, 'editorWin', 'top='+top+',left='+left+',width='+width+',height='+height+',scrollbars=yes,resizable=yes,location=no');
  //}

}

function addFrame(frame) {
  new Effect.Appear('settings');
  new Effect.Appear('framesBlock');
  $('frames').innerHTML += frame;
  Sortable.create(
      'frames', {
          tag: 'div',
          overlap: 'horizontal',
          constraint: false
      }
  );
}

function removeFrame(i) {
  if (confirm('Удалить кадр?')) {
      new Ajax.Updater(
      'debug',
          '/animations/remove', {
                evalScripts: true,
                parameters: { frame: i },
                onSuccess: function() {
                  Element.remove('frame_'+i);

                  if( $$('.frame').length == 0 )
                  {
                    Element.hide("framesBlock");
                    Element.hide("settings");
                  }
                }
          }
      );
      limitReached = false;
  }
}

function cloneFrame(i) {
  if (limitReached) {
    alert('Лимит на количество картинок в одной GIFке исчерпан');
  }
  else {
      new Ajax.Updater(
      'debug',
          '/animations/clone', {
              evalScripts: true,
              parameters: { frame: i }
            }
      );
  }
}

function hideHint() {
  Element.hide('firstHint');
}

function toggleUpload() {
  hideHint();
    Element.hide('urlInput');
  //Element.hide('camInput');
    new Effect.Appear('uploadInput', { duration: .4 });
  //toggleAddFeaturesLink();
    $('urlLink').className = 'urlLink';
    $('uploadLink').className = 'uploadLink uploadLinkActive';
}

function toggleUrl() {
  hideHint()
    Element.hide('uploadInput');
  new Effect.Appear('urlInput', { duration: .4 });
  //toggleAddFeaturesLink();
    $('uploadLink').className = 'uploadLink';
    $('urlLink').className = 'urlLink urlLinkActive';
}

function uploadFromFlickr(file, obj) {
  uploadUrl(file, obj);
}

function uploadUrl(file, obj) {
  loadingStart(loadingText);
    new Ajax.Updater(
        'debug',
        '/animations/upload',
        {
          method: 'post',
            evalScripts: true,
            parameters: { source: 'url', url: file },
            onSuccess: function() {
              if (obj) new Effect.Fade(obj);
      }
        }
    );
}

function uploadFile() {
  loadingStart(loadingText);
  $('form').submit();
}

function uploadDone(result, message, frame, finish) {
  if (result == '1') {
    loadingEnd();
    addFrame(frame);
  }
  else {
    message += '<p><a href="javascript:loadingEnd()">Продолжить работу</a></p>';
    loadingStart(message);
  }
  if (finish) {
    Element.hide('addBlock');
    if ($('addFeatures').style.display == 'none' && !payfull) {
      toggleMore();
    }
    new Effect.Appear('limitReached');
    new Effect.Highlight('limitReached', { startcolor: '#ff0000', endcolor: '#f0f0f0', duration: 2 });
    limitReached = true;
  }
}

function previewGIF() {
  $('preview').innerHTML = loadingText;
  updateDims();
  toggleOverlay(true);
  $('preview').style.top=100+st+'px';
  $('preview').style.left=((dw/2)-230)+'px';
  $('preview').setOpacity(1);
  Element.show('preview');

    var frames = Sortable.serialize('frames');
    var size = $('size').value;
    var delay = $('delay').value;
    new Ajax.Updater(
        'preview',
        '/animations/premake?size='+size+'&delay='+delay+'&'+frames, {
              evalScripts: true,
              onSuccess: function() { }
        }
    );
}

function cancelGIF() {
  toggleOverlay();
  new Effect.Fade('preview');
}

function makeGIF() {
  Element.hide('overlay');
  Element.hide('preview');
    loadingStart(loadingText);
    var frames = Sortable.serialize('frames');
    new Ajax.Updater(
        'result',
        '/animations/make?'+frames, {
              evalScripts: true,
              onSuccess: function() {
                loadingEnd();
                Element.hide('editor');
                new Effect.Appear('result');
              }
        }
    );
}