<%= form_for @site_construct, :url => {:action=>"update_super_manager_management_for_site"}, :html => {:class => 'form-horizontal main-forms super_manager_form'} do |f| %>
<% content_for :page_specific_css do %>
  <%= stylesheet_link_tag "lib/main-forms" %>
<% end %>
<% content_for :page_specific_javascript do %>
<% end %>
<%#= f.error_messages %>
<fieldset>
  <h3><%= "#{f.object.site_name} (Domain: #{f.object.get_default_domain(true)}) " %></h3>
  <!-- Input Area -->
  <div class="input-area">
    <%= render :partial => "super_manager_form", :locals=>{:f=>f,:type=>"site_super_user"} %>
    <div class="form-actions">
      <%= f.submit t('submit'), class: 'btn btn-primary' %>
    </div>
  </div>
</fieldset>
<style type="text/css">
  .remove_btn{
    float: left;
    color: red;
    line-height: 2rem;
    cursor: pointer;
    font-weight: bold;
  }
  .remove_btn:hover{
    font-size: 1.3em;
  }
</style>
<script type="text/javascript">
  var close_info = false;
  var timeout_id;
  var status_relation = {"creating":"<span style=\"color: skyblue;\">creating</span>","error":"<span style=\"color: red;\">error</span>","finish": "<span style=\"color: darkseagreen;\">finish</span>","closed":"<span style=\"color: red;\">closed</span>"};
  var need_close_info = false;
  function see_infos(id){
    if(!close_info){
      var request = $.post("<%=admin_site_panel_site_infos_path%>",{"id":id});
      request.done(function(data){
        var infos = request.responseJSON.infos;
        var status = request.responseJSON.status;
        if($("#info_texts").length == 0)
          return infos.join("\n");
        else{
          $("#info_texts").html(infos.join("\n"));
          if(status == "")
            var status_text = "not yet create";
          else
            var status_text = status_relation[status];
          if(status == "finish"){
            var params = {}
            var params_text = window.location.search.split(/[?&]/).slice(1);
            params_text.forEach(function(text){
              if(text.split("=")[0] != "")
                params[text.split("=")[0]] = text.split("=")[1].split("#")[0].split("&")[0];
            });
            var new_params_text = "?";
            Object.keys(params).forEach(function(key){
              var value = params[key];
              if(key == "id"){
                if(value != id){
                  new_params_text += (key+"="+value+"&");
                }
              }else
                new_params_text += (key+"="+value+"&");
            });
            if(new_params_text[new_params_text.length -1] == "&")
              new_params_text = new_params_text.substr(0,new_params_text.length - 1);
            if(params["id"] == id || window.refresh_flag || need_close_info){
              close_info = true;
              window.setTimeout(function(){
                window.location.search = new_params_text;
              }, 1000);
            }
          }else if(status == "changing"){
            window.refresh_flag = true
          }else{
            need_close_info = true;
          }
          $(".see_infos[data-id="+id+"]").parent().siblings(".site_status").html(status_text);
          //msg_end.scrollIntoView();
          timeout_id = window.setTimeout(see_infos(id),1000);
        }
      }).fail(function() {
        window.location.reload();
      })
    }else{
      window.clearTimeout(timeout_id);
    }
  };
  function show_infos_dialog(item){
    close_info = true;
    window.clearTimeout(timeout_id);
    close_info = false;
    var id = item.data("id");
    if($("#dialog-confirm").length == 0){
      $("#main-wrap").before("<div id='dialog-confirm' title='site infos'>"+
                    "<div style='clear:both;'></div><div id='info_texts'>"+see_infos(id)+"</div><div id='msg_end' style='height:0px; overflow:hidden'></div>"+
                    "</div>");
    }else{
      see_infos(id);
    };
    $( "#dialog-confirm" ).dialog({
      resizable: true,
      minHeight: 300,
      maxHeight: 400,
      modal: true,
      width: '80%',
      close: function(){$( this ).dialog( "close" );close_info = true;},
      buttons: {
      "<%= t('client_management.confirm') %>": function(){$( this ).dialog( "close" );close_info = true;},
      "stop update": function(){close_info = true;}
      }
    });
  }
  $(document).ready(function(){
    $('.apply_change_users').click(function(){
      var item = $(this);
      $.post("<%=admin_site_panel_edit_site_path%>",{'id': item.attr("data-id"),'type':'apply_change_users','is_server': item.data('is-server')}).done(function(){
        show_infos_dialog(item);
      });
    });
  })
</script>
<% end %>