﻿//Ready
$(document).ready(function() {

new $("#dataList").Antibody({ id: 'dataList', index: 1, size: 15 });
});

jQuery.fn.Antibody = 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);
        var key = getRequest("keystr"); if (key == null) key = "";
        var tag = getRequest("tag"); if (tag == null) tag = "";
        var cat = getRequest("cat"); if (cat == null) cat = "";

        init = function() {
            setLoading(true);
            //set nav
            var tag2 = tag; if (tag2.toLowerCase() == "tc") tag2 = "Tags/Controls";
            $('.Navigation_Current_Item').text(tag2 + " Antibodies");
        };

        startAnti = function() {
            setParam();
            $.getJSON("../Handler/ProductHandler.ashx?r=" + Math.random(),
            { func: 'GetAntibodyList2', index: config.index, size: config.size, sort: config.sort, dir: config.dir, param: config.param },
            function(result) {
                setPath(result.table[2].rows);
                setTips(result.pager);
                setSubCategories(result.table[1].rows);
                setPager(result.pager);
                showData(result.table[0].rows);

                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 = 'tag=' + tag + '|cat=' + cat + '|keystr=' + key;

            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 alp = (getRequest("keystr") && getRequest("keystr") != 'All') ? 'whose name begins with "' + getRequest("keystr") + '"' : '';
            var type = getRequest("catName"); if (type == null) type = ""; //catName($("#abselect").val() == '') ? 'Pre-made' : document.getElementById("abselect").options[document.getElementById("abselect").selectedIndex].text;
            var tag2 = tag.toLowerCase(); if (tag2 == "tc") tag2 = "Tags/Controls";
            $('#dvTips').text('All ' + type + ' ' + tag2 + ' antibodies ' + alp + ', total: ' + pager.total + ' records. Current page ' + pager.index + '.');
        };

        makelist = function(rows) {
            if (!rows) return "";
            var str = new Array();

            $.each(rows, function(i, o) {
                str.push('<tr>');
                str.push('<td width="60"><a href="AbDatasheet.aspx?catNo=' + this.CatalogNo + '">' + this.CatalogNo + '</a></td>');
                str.push('<td width="80">' + ((this.AntigenName) ? this.AntigenName : this.Symbol) + '</td>');
                str.push('<td width="180" align="left" style="break-word">' + this.FullName + '</td>');
                str.push('<td>' + this.AbDescription + '</td>');
                //str.push('<td width="30">' + this.Package + 'ul</td>');
                str.push('<td width="70">' + this.Application + '</td>');
                str.push('<td width="80">' + this.ReactSpecies + '</td>');
                //str.push('<td width="40">' + this.Host + '</td>');
                //str.push('<td width="52"><a href="' + gotoUrl.replace("{catNo}", this.GenBankNo) + '" target="_blank">' + this.GenBankNo + '</a></td>');
                str.push('</tr>');
            });
            return str.join('');
        };

        setPager = function(pager) {
            $("#dvProPager").SimplePager({
                showNum: 12,
                pageSize: pager.size,
                nowPage: pager.index,
                totalRec: pager.total,
                callback: reload
            });
        };
        setSubCategories = function(rows) {
            var ul = $('#dvSubCat ul'); ul.empty();
            if (rows.length == 0) { return; }

            var key = getRequest("keystr"); if (key == null) key = "";
            $.each(rows, function(i, o) {
                ul.append("<li><a href='Antibody.aspx?tag=" + tag + "&cat=" + o.CategoryNo + "&catName=" + o.CategoryName + "&keystr=" + key + "' >" + o.CategoryName + " (" + o.Total + ")</a></li>");
            });

            $('#dvSubCat').show();
        };

        setPath = function(rows) {
            var dv = $('#dvPath'); dv.empty();
            if (rows.length == 0) { return; }

            var key = getRequest("keystr"); if (key == null) key = "";
            var str = new Array();

            $.each(rows, function(i, o) {
                str.push("<span><a href='Antibody.aspx?tag=" + tag + "&cat=" + o.CategoryNo + "&catName=" + o.CategoryName + "&keystr=" + key + "' ");
                str.push("onmouseover='showPath(this,\"" + o.CategoryNo + "\",\"v\");' ");
                //str.push("onmouseout='showPath(this,\"" + o.CategoryNo + "\",\"u\");' ");
                str.push(">> ");
                str.push(o.CategoryName);
                str.push("</a></span>&nbsp;&nbsp;");
                str.push(getPathSub(o));
            });

            dv.append(str.join('')).show();

            $(document).click(function() { $('#dvPath ul').hide(); });
        };

        getPathSub = function(sub) {
            if (sub.SubCategory == "" || sub.SubCategory == null) return "";

            s = sub.SubCategory.split('|');

            var str2 = new Array();

            str2.push("<ul id='ul" + sub.CategoryNo + "' >");
            str2.push("<li class='h'><a href='Antibody.aspx?tag=" + tag + "&cat=" + sub.CategoryNo + "&catName=" + sub.CategoryName + "&keystr=" + key + "'><b>" + sub.CategoryName + "</b></a></li>");

            for (var i = 0; i < s.length; i++) {
                var n = s[i].split('*');
                str2.push("<li><a href='Antibody.aspx?tag=" + tag + "&cat=" + n[1] + "&catName=" + n[0] + "&keystr=" + key + "'>" + n[0] + "</a></li>");
            }

            str2.push("</ul>");

            return str2.join('');
        };

        showPath = function(obj, no, t) {
            $('#dvPath ul').hide();
            var dv = $('#ul' + no);
            if (t == "v") {
                dv.css({ 'left': $(obj).offset().left, 'top': $(obj).offset().top });

                dv.show();
            }
            else
                $('#ul' + no).hide();
        };

        init();
        startAnti();
    });

}