<head>
    <%= stylesheet_link_tag "lib/wrap-nav"%>
    <%= stylesheet_link_tag "lib/main-forms"%>
    <%= stylesheet_link_tag "lib/fileupload"%>
    <%= stylesheet_link_tag "lib/togglebox"%>

    <%= javascript_include_tag 'lib/bootstrap-fileupload' %>  
    <%= javascript_include_tag 'lib/site_set' %>
</head>


<%= form_for @site, :url => admin_site_path(@site.id), :html => {:class => "form-horizontal main-forms"} do |f| %>

          <fieldset>

            <!-- Input Area -->
            <div class="input-area">
              <div class="nav-name"><strong><%= t('preferences.classification') %></strong></div>    
              <ul class="nav nav-pills language-nav">
              <% if current_user.user_name == "rulingcom" || current_user.beta_tester %>
                <li>
                  <a href="#sidebar-nav" data-toggle="tab"><%= t('preferences.sidebar_nav') %></a>
                </li>
              <% end %>
                <li class="active">
                  <a href="#system-email" data-toggle="tab"><%= t('preferences.system_email') %></a>
                </li>
                <li>
                  <a href="#openness" data-toggle="tab"><%= t('preferences.openness') %></a>
                </li>
                <li>
                  <a href="#language-set" data-toggle="tab"><%= t('preferences.language') %></a>
                </li>
                <li>
                  <a href="#orbitBar-them" data-toggle="tab"><%= t('preferences.orbitbar_theme') %></a>
                </li>
                 <li>
                  <a href="#google-oauth" data-toggle="tab">Google OAuth</a>
                </li>
                 <li>
                  <a href="#social-sharer" data-toggle="tab">Social Share</a>
                </li>
                <li>
                  <a href="#sign-up" data-toggle="tab">Sign Up</a>
                </li>
              </ul>
              
              <div class="tab-content">

                <!-- Sidebar Nav -->
                <% if current_user.user_name == "rulingcom" || current_user.beta_tester %>
                  <div id="sidebar-nav" class="tab-pane fade in active">
                    <div class="control-group">
                      <label class="control-label muted"><%= t("preferences.nav_enabled") %></label>
                      <div class="controls">
                        <input type="checkbox" class="toggle-check set-sidebar-state" data-disabled="true">
                      </div>
                    </div>
                  </div>
                <% end %>

                <!-- System Email -->
                <%= f.fields_for :site_settings, @site['site_settings'] do |f| %>

                <div id="system-email" class="tab-pane fade in active">

                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.service_email' %></label>
                    <div class="controls">
            <%= f.text_field :service_email, :value => (@site['site_settings']['service_email'] rescue nil), :class=>"input-large", :placeholder=>"Administrator Mail" %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.reply_email' %></label>
                    <div class="controls">
            <%= f.text_field :reply_email, :value => (@site['site_settings']['reply_email'] rescue nil), :class=>"input-large", :placeholder=>"Reply Mail" %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.address' %></label>
                    <div class="controls">
 						<%= f.text_field :address, :value => (@site['site_settings']['address'] rescue nil), :class=>"input-large", :placeholder=>"Email Address" %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.domain' %></label>
                    <div class="controls">
						<%= f.text_field :domain, :value => (@site['site_settings']['domain'] rescue nil), :class=>"input-large", :placeholder=>"Domain" %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.authentication' %></label>
                    <div class="controls">
                     <%= f.text_field :authentication, :value => (@site['site_settings']['authentication'] rescue nil), :class=>"input-large", :placeholder =>"Authentication" %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.user_name' %></label>
                    <div class="controls">
                  	  <%= f.text_field :user_name, :value => (@site['site_settings']['user_name'] rescue nil), :class=>"input-large", :placeholder=>"User Name"  %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.password' %></label>
                    <div class="controls">
                      <%= f.password_field :password, :value => (@site['site_settings']['password'] rescue nil), :class=>"input-large", :placeholder=>"Password" %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.port' %></label>
                    <div class="controls">
                      <%= f.text_field :port, :value => (@site['site_settings']['port'] rescue nil),:placeholder=>"Port", :class=>"input-mini" %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.tls' %></label>
                    <div class="controls">
             		  <%= f.check_box :tls, :checked => (@site['site_settings']['tls'].eql?('1') rescue false),:class=>"toggle-check", :data=> {  disabled: true } %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= t 'mail.enable_starttls_auto' %></label>
                    <div class="controls">
               		 <%= f.check_box 'enable_starttls_auto', :checked => (@site['site_settings']['enable_starttls_auto'].eql?('1') rescue false), :class=>"toggle-check", :data=> {  disabled: true } %>
                    </div>
                  </div>

                </div>
            <% end %>


                <!-- Openness -->
                <div id="openness" class="tab-pane fade">
                  <div class="control-group">
                    <label class="control-label muted"><%= I18n.t('site.frontend_open')%></label>
                    <div class="controls">
                   	  <%= f.check_box :frontend_open , :class=>"toggle-check", :data=> {  disabled: true } %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted"><%= I18n.t('site.backend_openness_on') %></label>
                    <div class="controls">
                      <%= f.check_box :backend_openness_on , :class=>"toggle-check", :data=> { disabled: true } %>
                    </div>
                  </div>
                  <div class="control-group hide">
                    <label class="control-label muted"><%= I18n.t('site.disable_personal_desktop')%></label>
                    <div class="controls">
                      <%= f.check_box :desktop_closed , :class=>"toggle-check", :data=> { disabled: true } %>
                    </div>
                  </div>
                </div>

                <!-- Language -->
                <div id="language-set" class="tab-pane fade">
                  <!-- <div class="control-group">
                    <label class="control-label muted">Enable User  Language  Detection</label>
                    <div class="controls">
                      <input type="checkbox" class="toggle-check" data-disabled="true">
                    </div>
                  </div> -->
                  <!-- Terms of Use -->
                  <div class="control-group">
                    <label class="control-label muted"><%= t('preferences.lang_detection') %>
                    </label>
                    <div class="controls">
                      <%= f.check_box :enable_language_detection, :class => "toggle-check reset_default_locale", data: {disabled: true} %> 
                    </div>
                  </div>

                  <div class="control-group">
                    <label class="control-label muted"><%= t('preferences.enable_zh_cn') %>
                    </label>
                    <div class="controls">
                      <%= f.check_box :enable_zh_cn, :class => "toggle-check", data: {disabled: true} %> 
                    </div>
                  </div>

                  <div class="control-group">
                    <label class="control-label muted"><%= t('preferences.lang_enabled') %></label>
                    <div class="controls">
                     <% I18n.available_locales.each do |valid_locale| %>
                      <%= check_box_tag "site[in_use_locales][#{valid_locale}]", valid_locale, @site.in_use_locales.include?(valid_locale), :class=>"toggle-check in_use_locales", :data => { :title => "#{t(valid_locale.to_s)}", disabled: true }, "data-deploy"=>"inline" %>
					            <% end %>
                    </div>
                  </div>

                  <div class="control-group">
                    <label class="control-label muted"><%= t('preferences.lang_default') %></label>
                    <div class="controls">
                     <% I18n.available_locales.each do |locale| %>
                      <%= radio_button_tag "site[default_locale]", locale, @locale == locale ,:class=>"toggle-check", :data => { :title => "#{t(locale.to_s)}" }, "data-deploy"=>"inline", :checked => (@site.default_locale.eql?("#{locale}")) %>
                     <% end %>
                    </div>
                  </div>

                  <div class="control-group">
                    <label class="control-label muted"><%= t('preferences.redirect_index') %>
                    </label>
                    <div class="controls">
                      <%= f.check_box :enable_redirect_index, :class => "toggle-check reset_default_locale", data: {disabled: true} %> 
                    </div>
                  </div>

                    <div class="control-group">
                    <label class="control-label muted"><%= t('preferences.enable_language_options') %>
                    </label>
                    <div class="controls">
                      <%= f.check_box :enable_language_options, :class => "toggle-check", data: {disabled: true} %> 
                    </div>
                  </div>

                </div>

                <!-- OrbitBar Them -->
                <div id="orbitBar-them" class="tab-pane fade">
                  <div class="control-group">
                    <label class="control-label muted">OrbitBar Logo <%= t('preferences.icon') %></label>
                    <div class="controls">
                      <!-- if this page editing please add class "fileupload-edit" -->
                      <div class="fileupload fileupload-new clearfix" data-provides="fileupload">
                        <div class="fileupload-new thumbnail pull-left" style="background:black;">
                        <% if @site.default_image.file.present? %>
                   		   <%= image_tag( @site.default_image, :size=>"120x120") rescue ''%>
                	      <% else %>
                         <%= image_tag "orbit-logo.png",:class => "pull-left upload-picture"%>
                 	      <% end %>
                        </div>
                        <div class="fileupload-preview fileupload-exists thumbnail pull-left"></div>
                        <% if @site.default_image.file.present?%>
                        <span class="btn btn-file">
                         <%= f.file_field :default_image, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.change') %>
                         </span>
                          <%= f.check_box :remove_default_image %>
                          <%= t(:remove) %> OrbitBar Logo
                         <%else%>
                        <span class="btn btn-file">
                         <%= f.file_field :default_image, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.select_image') %>
                         </span>
                         <%end%>
                        <a href="#" class="btn fileupload-exists" data-dismiss="fileupload"><%= t(:cancel) %></a>
                      
                      </div>
                    </div>

                    <div class="control-group">
                    <label class="control-label muted">Orbit <%= t('preferences.favicon') %></label>
                    <div class="controls">
                      <!-- if this page editing please add class "fileupload-edit" -->
                      <div class="fileupload fileupload-new clearfix" data-provides="fileupload">
                        <div class="fileupload-new thumbnail pull-left">
                        <% if @site.favicon.file.present? %>
                          <%= image_tag( @site.favicon, :size=>"120x120") rescue ''%>
                        <% else %>
                          <%= image_tag "favicon.ico", :class => "pull-left upload-picture"%>
                        <% end %>
                        </div>
                        <div class="fileupload-preview fileupload-exists thumbnail pull-left"></div>
                        <% if @site.favicon.file.present?%>
                         <span class="btn btn-file">
                         <%= f.file_field :favicon, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.change') %>
                        </span>
                            <%= f.check_box :remove_favicon %>
                            <%= t(:remove) %> <%= t('preferences.favicon') %>
                         <%else%>
                        <span class="btn btn-file">
                         <%= f.file_field :favicon, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.select_image') %>
                         </span>
                         <%end%>

                        <a href="#" class="btn fileupload-exists" data-dismiss="fileupload"><%= t(:cancel) %></a>
                      
                      </div>
                    </div>

                  </div>
                  <div class="control-group">
                    <label class="control-label muted">Orbit <%= t('preferences.site_logo') %>(second image or the only one image)</label>
                    <div class="controls">
                      <!-- if this page editing please add class "fileupload-edit" -->
                      <div class="fileupload fileupload-new clearfix" data-provides="fileupload">
                        <div class="fileupload-new thumbnail pull-left">
                        <% if @site.site_logo.file.present? %>
                          <%= image_tag( @site.site_logo, :size=>"120x120") rescue ''%>
                        <% else %>
                          <%= image_tag "site-logo.png", :class => "pull-left upload-picture"%>
                        <% end %>
                        </div>
                        <div class="fileupload-preview fileupload-exists thumbnail pull-left"></div>
                        <% if @site.site_logo.file.present?%>
                         <span class="btn btn-file">
                         <%= f.file_field :site_logo, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.change') %>
                        </span>
                            <%= f.check_box :remove_site_logo %>
                            <%= t(:remove) %> <%= t('preferences.site_logo') %>
                         <%else%>
                        <span class="btn btn-file">
                         <%= f.file_field :site_logo, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.select_image') %>
                         </span>
                         <%end%>

                        <a href="#" class="btn fileupload-exists" data-dismiss="fileupload"><%= t(:cancel) %></a>
                      
                      </div>
                    </div>

                  </div>
				  <div class="control-group">
                    <label class="control-label muted">Orbit <%= t('preferences.site_logo_1') %></label>
                    <div class="controls">
                      <!-- if this page editing please add class "fileupload-edit" -->
                      <div class="fileupload fileupload-new clearfix" data-provides="fileupload">
                        <div class="fileupload-new thumbnail pull-left">
                        <% if @site.site_logo_1.file.present? %>
                          <%= image_tag( @site.site_logo_1, :size=>"120x120") rescue ''%>
                        <% else %>
                          <%= image_tag "site-logo.png", :class => "pull-left upload-picture"%>
                        <% end %>
                        </div>
                        <div class="fileupload-preview fileupload-exists thumbnail pull-left"></div>
                        <% if @site.site_logo_1.file.present?%>
                         <span class="btn btn-file">
                         <%= f.file_field :site_logo_1, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.change') %>
                        </span>
                            <%= f.check_box :remove_site_logo_1 %>
                            <%= t(:remove) %> <%= t('preferences.site_logo_1') %>
                         <%else%>
                        <span class="btn btn-file">
                         <%= f.file_field :site_logo_1, :id => "input-upload", :class => 'fileupload-new', :onchange => "document.getElementById('fu').innerHTML = this.form.fu.value = this.value;" %><%= t('preferences.select_image') %>
                         </span>
                         <%end%>

                        <a href="#" class="btn fileupload-exists" data-dismiss="fileupload"><%= t(:cancel) %></a>
                      </div>
                    </div>
					<div class="control-group"><label class="control-label muted">home_link(for site logo)</label><div class="controls"><%= f.text_field :home_link %></div></div>
					<div class="control-group"><label class="control-label muted">home_link_1(for site logo 1)</label><div class="controls"><%= f.text_field :home_link_1 %></div></div>
					<% @site_in_use_locales.each do |locale| %>
					   <%= f.fields_for :site_title do |f| %>
						<div class="control-group">
						  <label class="control-label muted">Site title (for site logo) (<%= t(locale) %>)</label>
						  <div class="controls">
							<%= f.text_field locale, :value => (@site.site_title[locale.to_s] rescue nil), :placeholder=>"site_title" %>
						  </div>
						</div>
					  <% end %>
					  <%= f.fields_for :site_title_1 do |f| %>
						<div class="control-group">
						  <label class="control-label muted">Site title 1 (for site logo 1) (<%= t(locale) %>)</label>
						  <div class="controls">
							<%= f.text_field locale, :value => (@site.site_title_1[locale.to_s] rescue nil), :placeholder=>"site_title_1" %>
						  </div>
						</div>
					  <% end %>
					<% end %>
                  </div>
                  <% @site_in_use_locales.each do |locale| %>
                   <%= f.fields_for :mobile_orbit_bar_title_translations do |f| %>
                    <div class="control-group">
                      <label class="control-label muted">Orbit bar title (<%= t(locale) %>)</label>
                      <div class="controls">
                        <%= f.text_field locale, :value => (@site.mobile_orbit_bar_title_translations[locale.to_s] rescue nil), :placeholder=>"Orbit bar title" %>
                      </div>
                    </div>
                  <% end %>
                  <% end %>
                </div>
              </div>

               <div id="google-oauth" class="tab-pane fade">
                  <div class="control-group">
                    <label class="control-label muted">Enable Google OAuth</label>
                    <div class="controls">
                      <%= f.check_box :google_oauth_enabled %> 
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted">Google client id</label>
                    <div class="controls">
                     <%= f.text_field :google_client_id, :value => (@site['google_client_id'] rescue nil), :class=>"input-large google-oauth-fields", :placeholder=>"Google client id", :disabled => !@site[:google_oauth_enabled] %>
                    </div>
                  </div>
                  <div class="control-group">
                    <label class="control-label muted">Google client secret</label>
                    <div class="controls">
                      <%= f.text_field :google_client_secret, :value => (@site['google_client_secret'] rescue nil), :class=>"input-large google-oauth-fields", :placeholder=>"Google client secret", :disabled => !@site[:google_oauth_enabled] %>
                    </div>
                  </div>
                  <div class="alert alert-info">Please restart the website after modifying these settings. Click on the restart button and refresh the page in sometime. <a class="btn btn-small btn-primary" id="restart_server">Restart server</a></div>
               </div>
                <div id="social-sharer" class="tab-pane fade">
                   <div class="control-group">
                    <label class="control-label muted">Enable Social Share</label>
                    <div class="controls">
                      <input type="checkbox" value="facebook" name="site[every_page_sharer][]" <%= @site.every_page_sharer.include?("facebook") ? "checked=checked" : "" %> > Facebook
                      <input type="checkbox" value="twitter" name="site[every_page_sharer][]" <%= @site.every_page_sharer.include?("twitter") ? "checked=checked" : "" %> > Twitter
                      <input type="checkbox" value="google" name="site[every_page_sharer][]" <%= @site.every_page_sharer.include?("google") ? "checked=checked" : "" %> > 
                      Google
                      <input type="checkbox" value="printer" name="site[every_page_sharer][]" <%= @site.every_page_sharer.include?("printer") ? "checked=checked" : "" %> > 
                      Printer
                      <input type="hidden" value="disable" name="site[every_page_sharer][]">
                    </div>
                  </div>
                </div>
                <!-- sign up -->
                <div id="sign-up" class="tab-pane fade">
                  <div class="control-group">
                    <label class="control-label muted"><%= I18n.t('site.roles_for_sign_up')%></label>
                    <div class="controls">
                      <% Role.where(:disabled => false).each do |r| %>
                        <input type="checkbox" value="<%= r.id.to_s %>" name="site[sign_up_roles][]" <%= @site.sign_up_roles.include?(r.id.to_s) ? "checked=checked" : "" %> > <%= r.title %><br />
                      <% end %>
                    </div>
                  </div>
                </div>

            </div>
            <!-- Form Actions -->
            <div class="form-actions" style="background: none;">
              <%= f.submit t("submit"), :class => "btn btn-primary" %>
              <%= f.submit t("cancel"), :class => "btn ", :type => 'reset' %>
            </div>

          </fieldset>
<%end%>

<% content_for :page_specific_javascript do %>
<%end%>
<script type="text/javascript">

  $(".language-nav li a").click(function(){window.location.hash = $(this).attr("href")});

  $(document).ready(function(){
    var language_detection = $("#site_enable_language_detection");
    var default_locale = $( "input[name='site[default_locale]']" );

    $(language_detection).click(function(){
      if ( !$(this).parent().hasClass("disable") ){
        // Disable default language if language detection enbaled
        $.each($(default_locale),function(){
          if ( !$(this).parent().hasClass("disable") ){
            $(this).parent().toggleClass('disable');
            $(this).toggleClass('disable');
          }
          $(language_detection).val(1);
        });
      }else{
        // Enable a default language when language detection is disabled
        $(default_locale).first().click();
      }
    });

    // Disable language detection if default language enbaled
    $.each($(default_locale),function(){
      $(this).change(function(){
        if(!$(language_detection).parent().hasClass("disable")) {
          $(language_detection).parent().toggleClass('disable');
          $(language_detection).val(0);
        }
      });
    });
  });

  var locales = <%= @site.in_use_locales.to_json.html_safe %>;

  $(document).ready(function(){
    $( ".in_use_locales" ).each( function(){
      $( this ).parent( "div.togglebox" ).addClass( "disabled" );
      var val = $( this ).val();
      if( locales.filter(function(a){ return a == val }).length == 1){
        $( this ).parent( "div.togglebox" ).removeClass( "disabled" );
      }
    })
  });

  delete locales;

  $(function() { 
    $('a[data-toggle="tab"]').on('click', function (e) {
      var tab = $(this).attr('href');
      localStorage.setItem('lastTab', tab);
    });

    //go to the latest tab, if it exists:
    var lastTab = localStorage.getItem('lastTab');
    if (lastTab) {
      $('a[href='+lastTab+']').click();
    }
  });

  $("#site_google_oauth_enabled").on("click",function(){
    if($(this).is(":checked")){
      $(".google-oauth-fields").removeAttr("disabled");
    }else{
      $(".google-oauth-fields").attr("disabled","disabled");
    }
  })
  $("#restart_server").on("click",function(){
    $.ajax({
      url : "<%= admin_site_restart_server_path(@site) %>",
      type : "get"
    })
    return false;
  })
</script>