﻿//Ready
$(document).ready(function() {

    new $("#dvPubList").Publication({ id: 'dvPubList', index: 1, size: 10 });

});

jQuery.fn.Publication = function(config) {

    //参数
    config = jQuery.extend({
        id: null,    //ID
        index: 1,     //Current page
        size: 10,     //page Size
        sort: '',
        dir: 'asc',
        param: '',
        pager: '#dvPubPager',
        callback: function() { return false; } //回调函数
    }, config || {});


    return this.each(function() {

        var me = $(this);

        init = function() {
            //搜索事件
            $("#btnPub").click(function() { setLoading(true); search(); return false; });
        }

        start = function() {

            $.getJSON("../Handler/PublicationHandler.ashx?r=" + Math.random(),
            { func: 'GetDataByPCD', index: config.index, size: config.size, sort: config.sort, dir: config.dir, param: config.param },
            function(result) {
                setPager(result.pager);
                showData(result.rows);

                adjustMain();

                setLoading(false);
            });
        };

        reload = function(p, pSize) {
            setLoading(true);
            if (pSize && config.size != pSize) {
                config.size = pSize;
                p = 1;
            }

            config.index = p;

            start();
        };

        search = function() {

            config.index = 1;
            config.param = 'catNo=' + $('#txtCatNo').val();
            start();
        }

        showData = function(rows) {
            me.empty();
            $.each(rows, function(i, o) {

                me.append(makeOneModule(this));
            });
        };

        setPager = function(pager) {

            $(config.pager).SimplePager({
                showNum: 12,
                pageSize: pager.size,
                nowPage: pager.index,
                totalRec: pager.total,
                callback: reload
            });
        };


        makeOneModule = function(one) {
            if (!one) return "";
            var str = new Array();
            str.push('<div class="dropshadow"><table width="320">');
            str.push('<tr>');
            str.push('<td width="80"><strong>Catalog No.</strong> </td>');
            str.push('<td width="130"><a href="AbDatasheet.aspx?catNo=' + one.CatalogNo + '">' + one.CatalogNo + '</a></td>');
            str.push('<td width="70" align="right" ><strong>Pubmed ID:</strong></td>');
            str.push('<td width=""><a href="http://www.ncbi.nlm.nih.gov/pubmed/' + one.PMID + '?dopt=Abstract" target="_blank">' + one.PMID + '</a></td>');
            str.push('</tr>');
            str.push('<tr>');
            str.push('<td width="80"><strong>Author:</strong></td>');
            str.push('<td width="240" colspan="3"><div style="width:240px;height:15px;overflow:hidden; white-space:nowrap;text-overflow:ellipsis">' + one.Author + '</div></td>');
            str.push('</tr>');
            str.push('<tr>');
            str.push('<td width="80"><strong>Journal:</strong></td>');
            str.push('<td width="240" colspan="3"><div style="width:240px;height:15px;overflow:hidden; white-space:nowrap;text-overflow:ellipsis">' + one.Journal + one.PubDate + one.Volume + one.Pagination + '</div></td>');
            str.push('</tr>');
            str.push('<tr>');
            str.push('<td width="80" valign="top"><strong>Title:</strong></td>');
            str.push('<td width="240" colspan="3"><div style="width:240px;height:60px;overflow:auto">' + one.Subject + '</div></td>');
            str.push('</tr>');
            str.push('</table></div>');

            return str.join('');
        };

        init();
        start();
    });

}