﻿//Ready
$(document).ready(function() {
new $("#dataList").Lysate({ id: 'dataList', index: 1, size: 15 });
});

jQuery.fn.Lysate = function(config) {

    //参数
    config = jQuery.extend({

        id: null,     //ID
        index: 1,     //Current page
        size: 10,     //page Size
        sort: '',
        dir: 'asc',
        param: '',
        callback: function() { return false; } //回调函数

    }, config || {});

    return this.each(function() {

        var gotoUrl = 'http://www.ncbi.nlm.nih.gov/sites/entrez?term={catNo}&cmd=Search&db=nuccore';

        var me = $(this);

        init = function() {
            setLoading(true);
            var cat = getRequest("cat");
            if (cat) {
                $('#abselect').attr('value', cat);
            }
        };

        startAnti = function() {
            setParam();
            $.getJSON("../Handler/ProductHandler.ashx?r=" + Math.random(),
            { func: 'GetLysateList', index: config.index, size: config.size, sort: config.sort, dir: config.dir, param: config.param },
            function(result) {
                setTips(result.pager);
                setPager(result.pager);
                showData(result.rows);
                //set main
                adjustMain();

                setLoading(false);
            });
        };

        reload = function(p, pSize) {
            setLoading(true);
            if (pSize && config.size != pSize) {
                config.size = pSize;
                p = 1;
            }
            config.index = p;
            startAnti();
        };

        setParam = function() {

            var sch = 'cat=' + $('#abselect').val();

            if (sch != config.param) {
                config.param = sch;
                config.index = 1;
            }
        };


        showData = function(rows) {
            new $("#dataList").TableSetter({ id: 'dataList', innerHtml: makelist(rows) });
        };

        setTips = function(pager) {
        var type = ($("#abselect").val() == '') ? 'lysates prepared from tissues and cells' : document.getElementById("abselect").options[document.getElementById("abselect").selectedIndex].text;

            $('#dvTips').text('All ' + type + '.');
        };

        makelist = function(rows) {
            if (!rows) return "";
            var str = new Array();

            $.each(rows, function(i, o) {
                str.push('<tr>');
                str.push('<td width="80"><a href="LysateDataSheet.aspx?catNo=' + this.CatalogNo + '">' + this.CatalogNo + '</a></td>');
                str.push('<td width="100">' + this.Species + '</td>');
                str.push('<td>' + this.Tissue + '</td>');
                str.push('</tr>');
            });
            return str.join('');
        };

        setPager = function(pager) {

            $("#dvProPager").SimplePager({
                showNum: 10,
                pageSize: pager.size,
                nowPage: pager.index,
                totalRec: pager.total,
                copyTo: 'dvProPagerCopy',
                callback: reload
            });
        };

        init();
        startAnti();
    });

}
