<!DOCTYPE html >
<html lang="en">
<head>
  <%include widget_css.html%>
  <script type="text/javascript">
    $(document).ready(function() {
      $('a[rel=delete]').each(function() {
        $(this).click(function() {
          if (confirm("Are you are you want to delete this widget? \n\nNote: The widget will be deleted on all pages that it appears on")) {
          window.location = $(this).attr('href');
        }
          return false;
        })
      })

      /* trigger on the page dropdown */
      $('select[name=page]').change(function() {
        $('form[name=main-form]').submit();
      })

      /* trigger on posision dropdown */
      $('.lnks .action-pos select').change(function() {
        var id   = $(this).attr('name').replace('Position-', '');
        var pos  = $(this).val();
        $('form[name=main-form] input[name=func]').val('assign');
        $('form[name=main-form] input[name=pos]').val(pos);
        $('form[name=main-form] input[name=id]').val(id);
        $('form[name=main-form]').submit();
      })

      $('.widget.collapse a[rel=hottopic]').each(function() {
        $(this).click(function() {
            $(this).siblings('.tagwidget').slideToggle();
            return false;
        })
      })
    })
  </script>
</head>
<body>
  <div class="contentwrapper">
    <h1>Widgets Management</h1>
    <a href="admin.cgi?do=plugin;plugin=Widgets;func=add;Widget=external<%if in.page%>;page=<%in.page%><%endif%>" class="btn default btn-add">Add External Links Widget</a>
    <a href="admin.cgi?do=plugin;plugin=Widgets;func=add<%if in.page%>;page=<%in.page%><%endif%>" class="btn default btn-add" style="margin-right: 10px">Add Widget</a>
    <hr class="line" />
    <br />
    <p class="success"><%success || ''%></p>
    <form name="main-form" action="admin.cgi" method="post">
      <input type="hidden" name="do" value="plugin" />
      <input type="hidden" name="plugin" value="Widgets" />
      <input type="hidden" name="func" value="widgets" />
      <input type="hidden" name="id" value="" />
      <input type="hidden" name="pos" value="" />
      <div class="row">
        <label class="name">Page/Category:</label>
        <div class="value">
          <%set categories_loop = Plugins::Widgets::fetch_categories()%>
          <select name="page">
            <option value="">Select a page/category</option>
            <optgroup label="Page">
              <option value="home"<%if in.page eq 'home'%> selected="selected"<%endif%>>Home</option>
              <option value="category"<%if in.page eq 'category'%> selected="selected"<%endif%>>Category</option>
              <option value="subcategory"<%if in.page eq 'subcategory'%> selected="selected"<%endif%>>Sub Category</option>
              <option value="article"<%if in.page eq 'article'%> selected="selected"<%endif%>>Article</option>
              <option value="search_results"<%if in.page eq 'search_results'%> selected="selected"<%endif%>>Search Results</option>
              <option value="races"<%if in.page eq 'races'%> selected="selected"<%endif%>>Races</option>
            </optgroup>
            <optgroup label="Category">
              <%~loop categories_loop%>
              <option value="<%ID%>"<%if in.page eq $ID%> selected="selected"<%endif%>><%Full_Name%></option>
              <%~endloop%>
            </optgroup>
          </select>
        </div>
      </div>
    </form>
    <div class="clearfix<%if in.page%> selected<%endif%>">
      <div class="col-6">
        <h4>Available Widgets</h4>
        <br />
        <%~loop widgets.available%>
        <div class="clearfix">
            <%~if Type%>
            <%include _widget_automated.html%>
            <%~else%>
            <%include _widget_manual.html%>
            <%~endif%>
            <%include _widget_pages.html%>
        </div>
        <%~endloop%>
      </div>
      <%~if in.page%>
      <div class="col-6">
        <h4>Selected Widgets</h4>
        <br />
        <%if widgets.selected.length%>
        <%~set no_links = 1%>
        <%~loop widgets.selected%>
        <div class="clearfix">
            <%~if Type%>
            <%include _widget_automated.html%>
            <%~else%>
            <%include _widget_manual.html%>
            <%~endif%>
            <%include _widget_pages.html%>
        </div>
        <%~endloop%>
        <%~else%>
        <p>No widget was selected for this page/category</p>
        <%~endif%>
      </div>
      <%~endif%>
    </div>
  </div>
</body>
<script type="text/javascript">
    function load_poll(url) {
        $.ajax({
            url: url,
            success: function(html) {
                $('#poll_widget').html(html).find('a').click(function() {
                    load_poll($(this).attr('href'));
                    return false;
                });
            }
        })
    /* poll */
    }

    if ($('#poll_widget').length > 0) {
        load_poll('<%config.db_cgi_url%>/poll.cgi?forum_id=' + $('#poll_widget').attr('data'));
    }

    if ($('.widget-threads').length > 0) {
        $('.widget-threads').each(function() {
            var container = $(this).find('.list');
            $.ajax({
               url: "<%config.db_cgi_url%>/threads.cgi?id=" + container.attr('data'),
               success: function(html) {
                   container.html(html);
               }
            })
        });
    }
</script>
</html>
