<style>
  html {
    overflow-y: hidden;
  }
  #editor { 
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
  }
  .theme-Code{
    float:right;
    position:relative;
    top:-50px;
  }
  .page {
    background: #1D1F21;
    position: fixed;
    height: 100%;
  }
  .footer-Base {
    display: none;
  }
</style>

<div class="header-Outro editor">
  <div class="row content">
    <div class="breadcrumbs">
      <a href="/dashboard">Dashboard</a> > <span class="filename"><%= @filename %></span>
    </div>
    <div class="tools">
      <div class="theme">
        Theme:
        <select id="theme" size="1" onchange="setTheme();" onkeyup="setTheme();">
          <optgroup label="Bright">
            <option value="ace/theme/chrome">Chrome</option>
            <option value="ace/theme/clouds">Clouds</option>
            <option value="ace/theme/crimson_editor">Crimson Editor</option>
            <option value="ace/theme/dawn">Dawn</option>
            <option value="ace/theme/dreamweaver">Dreamweaver</option>
            <option value="ace/theme/eclipse">Eclipse</option>
            <option value="ace/theme/github">GitHub</option>
            <option value="ace/theme/solarized_light">Solarized Light</option>
            <option value="ace/theme/textmate">TextMate</option>
            <option value="ace/theme/tomorrow">Tomorrow</option>
            <option value="ace/theme/xcode">XCode</option>
          </optgroup>
          <optgroup label="Dark">
            <option value="ace/theme/ambiance">Ambiance</option>
            <option value="ace/theme/chaos">Chaos</option>
            <option value="ace/theme/clouds_midnight">Clouds Midnight</option>
            <option value="ace/theme/cobalt">Cobalt</option>
            <option value="ace/theme/idle_fingers">idleFingers</option>
            <option value="ace/theme/kr_theme">krTheme</option>
            <option value="ace/theme/merbivore">Merbivore</option>
            <option value="ace/theme/merbivore_soft">Merbivore Soft</option>
            <option value="ace/theme/mono_industrial">Mono Industrial</option>
            <option value="ace/theme/monokai">Monokai</option>
            <option value="ace/theme/pastel_on_dark">Pastel on dark</option>
            <option value="ace/theme/solarized_dark">Solarized Dark</option>
            <option value="ace/theme/terminal">Terminal</option>
            <option value="ace/theme/tomorrow_night" selected="selected">Tomorrow Night</option>
            <option value="ace/theme/tomorrow_night_blue">Tomorrow Night Blue</option>
            <option value="ace/theme/tomorrow_night_bright">Tomorrow Night Bright</option>
            <option value="ace/theme/tomorrow_night_eighties">Tomorrow Night 80s</option>
            <option value="ace/theme/twilight">Twilight</option>
            <option value="ace/theme/vibrant_ink">Vibrant Ink</option>
          </optgroup>
        </select>
      </div>
      <a id="saveButton" class="btn btn-Action" href="#" onclick="saveTextFile(false); return false" style="opacity: 0.5"><i class="fa fa-save"></i>Save</a>
      <a class="btn btn-Action" href="//<%= current_site.host %>/<%= @filename %>" target="_blank">View</a>
    <!-- <a id="saveAndExitButton" class="btn-Action" href="#" onclick="saveTextFile(true); return false" style="opacity: 0.5"><i class="fa fa-save"></i>&nbsp;&nbsp;Save and Exit</a> -->
      <div id="editorUpdates" class="tooltip fade bottom in hidden" style="top: 90px;right: 46px;">
        <div class="tooltip-arrow"></div>
        <div class="tooltip-inner">
          <span></span>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="row editor">
  <div class="col col-100">
    <div id="editor"><%==encoding_fix(@file_data) %>
    </div>
  </div>
</div>

<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8"></script>

<script>
  var unsavedChanges = false

  function setTheme(name) {
    var editorTheme = $('#theme option:selected')
    editor.setTheme(editorTheme.val())
    $.post('/site/<%= current_site.username %>/set_editor_theme', {
      csrf_token: '<%= csrf_token %>',
      editor_theme: editorTheme.text()
    })
  }

  <% if current_site.editor_theme %>
    $('#theme option').filter(function() {
      return ($(this).text() == '<%= current_site.editor_theme %>')
    }).prop('selected', true)
  <% end %>

  function saveTextFile(quit) {
    if(unsavedChanges == false)
      return
    $.ajax({
      url: '/site_files/save/<%= @filename %>?csrf_token=<%= csrf_token %>',
      data: editor.getValue(),
      processData: false,
      contentType: false,
      type: 'POST',
      error: function(jqXHR, textStatus, errorThrown) {
        alert('There has been an error saving your file, please try again. If it continues to fail, make a copy of the file locally so you don\'t lose your changes!')
      },
      success: function(response){
        if(response == 'ok') {
          unsavedChanges = false
          if(quit === true) {
            window.location = '/dashboard'
          } else {
            $('#editorUpdates span').html('<i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Your file has been saved.')
            $('a#saveButton,a#saveAndExitButton').css('opacity', 0.5)
            setTimeout(function() {
              $('#editorUpdates').fadeOut();  //.addClass('hidden');
            }, 2000)
          }
          
        } else {
          $('#saveButton').tooltip('show')
          $('#editorUpdates span').text(response)
        }
        $('#editorUpdates').fadeIn()
        $('#editorUpdates').removeClass('hidden')
        
      }
    })
  }

  var editor = {}

  $(document).ready(function() {
    editor = ace.edit("editor")
    setTheme()
    editor.getSession().setMode("ace/mode/html")
    editor.getSession().setTabSize(2)
    editor.getSession().setUseWrapMode(true)
    editor.setFontSize(14)
    editor.setShowPrintMargin(false)
    editor.setOptions({
      maxLines: Infinity,
      autoScrollEditorIntoView: true
    })

    editor.on('change', function(obj) {
      $('a#saveButton,a#saveAndExitButton').css('opacity', 1)
      unsavedChanges = true
    })
  })

  $(window).keypress(function(event) {
    if(event.ctrlKey == true && event.key == 's') {
      saveTextFile(false)
      event.preventDefault()
      return false
    }
  })

  window.onbeforeunload = function() {
    if(unsavedChanges == true)
      return "You have unsaved changes!"
  }

  $('a#saveButton,a#saveAndExitButton').click(function() {
    if(!unsavedChanges)
      return false
    return true
  })

</script>