<% if attachments.nil? || attachments.empty? %>
    <!--<p class="nodata">-->
    <!--<%#= l(:label_no_data) %>-->
    <!--</p>-->
<% else %>
    <% attachments.each do |attach| %>
        <ul class="resource-list" onmouseover="if($('#contextMenu').css('display') != 'block')$(this).children().css('background-color', '#e1e1e1')" onmouseout=" if($('#contextMenu').css('display') == 'none')$(this).children().css('background-color', 'white')">
          <li class="resource-list-checkbox fl">
            <input name="checkbox1[]" type="checkbox" data-has-history="<%= attach.attachment_histories.count == 0 ? 'N' : 'Y' %>" data-deleteble="<%= User.current.id ==attach.author_id ? 'Y' : 'N'  %>" onclick="checkAllBox($(this));" value="<%= attach.id%>" class="resourcesCheckbox" />
            <!--<input name="checkbox2" type="checkbox" value="" class="resourcesCheckbox" />-->
          </li>
          <li class="resource-list-name fl">
            <%= link_to truncate(attach.filename, :length => 30), download_named_attachment_path(attach.id, attach.filename), :title => attach.filename, :class => 'resourcesBlack resource-list-middle hidden mw280' %>
          </li>
          <li class="resource-list-time fr"><%= format_date(attach.created_on) %></li>
          <li class="resource-list-quote fr"><%= attach.quotes.nil? ? 0 : attach.quotes %></li>
          <li class="resource-list-download fr"><%= attach.downloads %></li>
          <li style="display: none"><%= attach.author_id %></li>
          <li class="resource-list-size fr"><%= (number_to_human_size(attach.filesize)).gsub("ytes", "") %></li>
          <li class="resource-list-uploader fr hidden"><%= User.find(attach.author_id).realname.blank? ? User.find(attach.author_id).nickname : User.find(attach.author_id).realname %></li>
          <li class="resource-list-type fr"><%= get_resource_type(attach.container_type)%></li>
          <li class="resource-list-from fr hidden" title="<%= get_resource_origin(attach) %>"><%= get_resource_origin(attach) %></li>
          <li style="display: none"><%= attach.id %></li>
        </ul>
        <div class="cl"></div>
    <% end %>
<% end %>

<script>


    function show_upload(){
        $("#ajax-modal").html('<%= escape_javascript( render :partial => 'upload_resource' ,:locals => {:user=>User.current.id,:type=>@type,:status=>@status})%>');
        showModal('ajax-modal', '452px');
        $('#ajax-modal').siblings().remove();
        $('#ajax-modal').before("<a href='javascript:void(0)' onclick='closeModal();' style='margin-left: 435px;' class='resourceClose'></a>");
        $('#ajax-modal').parent().css("top","50%").css("left","50%").css("position","absolute");
        $('#ajax-modal').parent().addClass("resourceUploadPopup");
        $('#ajax-modal').css("padding-left","16px").css("padding-bottom","16px");
    }
    //批量删除
    function batch_delete(){
        var data = $("#resources_list_form").serialize();
        if($("input[type=checkbox][data-deleteble=N]:checked").length != 0){
            alert("您无法删除此资源!");
            return;
        }
        if($("input[type=checkbox][data-has-history=Y]:checked").length != 0){
            alert("您只能删除没有历史记录的资源,请重新选择后再删除。");
            return;
        }
        if(data != "" && confirm('确认要删除这些资源吗?')) {
            $.post(
                    '<%= user_resource_delete_user_path(User.current.id,:type=>@type,:status=>@status)%>',
                    $("#resources_list_form").serialize(),//只会对选中的控件进行序列化提交
                    function (data) {

                    }
            );
        }
    }

    function show_send(){
        $("#contextMenu").hide();
        document.oncontextmenu = function() {return true;}
        line.children().css("background-color",'white');
        id = line.children().last().html();
        user_id = line.children().eq(5).html();
        allow = line.children().eq(13).html();
        if( allow == 0){
            alert('您无权发送私有资源')
        }else{
            if (lastSendType === '2'){  //如果已经发送过一次了,那么就应该沿用上次发送的类型。
                $.ajax({
                    type: 'get',
                    url: '<%= search_user_project_user_path(User.current.id) %>' + "?send_id=" + id + "&type=<%= @type %>",
                    data:{send_type:'file'}
                });
            }else{
                $.ajax({
                    type: 'get',
                    url: '<%= search_user_course_user_path(User.current.id)%>' + "?send_id=" + id + "&type=<%= @type %>",
                    data:{send_type:'file'}
                });
            }
        }

    }

    function batch_send(){
        if($("#resources_list_form").serialize() == ""){
            alert('暂时不支持多页选择,您当前页没有选择任何资源');
            return ;
        }
        if (lastSendType === '2'){  //如果已经发送过一次了,那么就应该沿用上次发送的类型。
            $.ajax({
                type: 'get',
                url: '<%= search_user_project_user_path(User.current.id) %>' + '?' +  $("#resources_list_form").serialize() + "&type=<%= @type %>",
                data:{send_type:'file'}
            });
        }else if (lastSendType === '1'){
            $.ajax({
                type: 'get',
                url: '<%= search_user_course_user_path(User.current.id) %>' + '?' +  $("#resources_list_form").serialize() + "&type=<%= @type %>",
                data:{send_type:'file'}
            });
        }else if (lastSendType === '3'){
            $.ajax({
                type: 'get',
                url: '<%= search_user_org_user_path(User.current.id) %>' + '?' +  $("#resources_list_form").serialize() + "&type=<%= @type %>",
                data:{send_type:'file'}
            });
        }
        else{
            $.ajax({
                type: 'get',
                url: '<%= search_user_course_user_path(User.current.id)%>' + '?'+ $("#resources_list_form").serialize() + "&type=<%= @type %>",
                data:{send_type:'file'}
            });

        }
    }

    function preview(){
        $("#contextMenu").hide();
        document.oncontextmenu = function() {return true;}
        line.children().css("background-color",'white');
        id = line.children().last().html();
        $.ajax({
            type:'get',
            url:'<%= resource_preview_user_path(User.current.id)%>'+"?resource_id="+id
        });
    }


    String.prototype.trim = function() {
        var str = this,
                str = str.replace(/^\s\s*/, ''),
                ws = /\s/,
                i = str.length;
        while (ws.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }



    //恢复编辑状态到链接状态
    //如果当前是编辑状态,任何的不在输入框里的单击右键事件都需要将编辑状态变回链接状态
    //如果是编辑状态,且做了修改,那么久要进行修改,并且将修改值经过处理替换到页面显示
    function restore(){
        //上一行不为空 且链接不为空
        if( last_line != null && res_link != null && res_link != '') {
            name = $("#res_name").lenght != 0 && $("#res_name").val() != undefined  ?  $("#res_name").val().trim() : undefined ;
            if (name == undefined || name === 'undefined' ){ //只要res_name没有值,那么就不是编辑状态
                return;
            }
            if( name && name != res_name.trim()){

                if(confirm('确定修改为 '+name)){
                    $.ajax({
                        url:  '<%=rename_resource_user_path(User.current.id) %>'+  "?res_name="+$('#res_name').val() + "&res_id=" + $("#res_id").val(),
                        type:'get',
                        success:function (data)
                        {
                            if (data != 'fail' && name != undefined && name != 'undefined') {//修改成功,那么将链接恢复,并且将链接的显示内容改变。链接可以不变
                                last_line.children().eq(1).html(res_link);
                                last_line.children().eq(1).children().attr('title', name);
                                last_line.children().eq(1).children().attr('href', data);
                                last_line.children().eq(1).children().html(name.length > 17 ? name.substring(0, 17) + '...' : name);
                            } else {
                                last_line.children().eq(1).html(res_link);
                                res_link = null; //如果修改失败,恢复之后将res_link置空
                            }
                        }
                    } );
                }else{
                    last_line.children().eq(1).html(res_link);
                    res_link = null; //如果没有做修改,恢复之后将res_link置空
                }
            }else {
                last_line.children().eq(1).html(res_link);
                res_link = null;//如果没有做修改,恢复之后将res_link置空
            }


        }
    }

    function delete_file() {
        $("#contextMenu").hide();
        document.oncontextmenu = function () {
            return true;
        }
        line.children().css("background-color", 'white');
        id = line.children().last().html();
        user_id = line.children().eq(5).html();
        if(user_id === '<%= User.current.id%>') {
            if(line.children().first().children().data('hasHistory') == 'Y'){
                alert('该资源存在历史版本,不能删除');
                return;
            }
            if (confirm('确定要删除资源"' + line.children().eq(1).children().attr('title').trim() + '"么?')) {
                $.ajax({
                    type: 'post',
                    url: '<%= user_resource_delete_user_path(User.current.id) %>' + '?resource_id=' + id + '&type=<%= @type %>&status=<%= @status %>'
                });
            }

        }else{
            alert('您无法删除此资源!')
        }

    }
</script>