﻿//Ready
$(document).ready(function() {

    new $("#dvProMain").AbDatasheet({ id: 'dvProMain' });
});


jQuery.fn.AbDatasheet = function(config) {

    //参数
    config = jQuery.extend({

        id: null,     //ID
        param: '',
        result: null,
        tabload: [true, false, false, false],
        callback: function() { return false; } //回调函数

    }, config || {});

    return this.each(function() {

        var me = $(this);
        var GeneIDUrl = "http://www.ncbi.nlm.nih.gov/sites/entrez?db=gene&cmd=Retrieve&dopt=Graphics&list_uids={GeneID}";
        var GenBankNoUrl = "http://www.ncbi.nlm.nih.gov/sites/entrez?term={GenBankNo}&cmd=Search&db=nuccore";


        init = function() {

            $('#country').change(function() { countryChgEvt($(this)); });
            //tabs
            $("#dvProTabs").Tabs({ id: 'dvProTabs', showN: 0, callback: subLoad });
        };

        //set nav
        setNavText = function(r) {
            $('.Navigation_Current_Item').text(((r.AntigenName) ? r.AntigenName : r.Symbol) + " Antibody");
            //document.title = document.title + '--' + ((r.Symbol) ? r.Symbol : r.AntigenName) + ' Antibody';
        };
        mTopInfo = function(r) {
            $("#spnAbName").text(((r.AntigenName) ? r.AntigenName : r.Symbol) + " Antibody");
            $("#spnCatNo").text(r.CatalogNo);
        };

        mPrice = function(r) {
            if (r.IsSellable == 'true' || r.IsSellable == 'True') {
                if (r.ReplaceCatNo != "") {
                    $('#dvProPriceResReplace').empty().append('This Product has been replaced with CatNo. <a href="AbDataSheet.aspx?CatNo=' + r.ReplaceCatNo + '"  class="extlink" target="_blank">' + r.ReplaceCatNo + '</a>').show();
                    //$('#dvProAddCart').hide();
                } else {
                    $('#dvProPriceResHas').Price({ id: 'dvProPriceResHas', catNo: getRequest("catNo"), row: r });
                    $('#dvProPriceResHas').show();
                    //$('#dvProAddCart').show();
                }
            }
            else {
                $('#dvProPriceResNo').show();
            }
        };

        mProInfo = function(r) {
            var str = new Array();
            str.push('<table border="0" cellspacing="0" cellpadding="0" class="table">');
            str.push('<tr><td width="130" class="titleBg">Source:</td>');
            str.push('<td width="150">' + r.Host + '</td>');
            str.push('<td width="130" class="titleBg">Purify:</td>');
            str.push('<td width="150">' + r.PurifyMethod + '</td></tr>');
            str.push('<tr><td class="titleBg">IsoType:</td>');
            str.push('<td>' + r.IsoType + '</td>');
            str.push('<td class="titleBg">Storage:</td>');
            str.push('<td width="223">' + r.AntibodyBuffer + ' -20oC, Avoid freeze / thaw cycles.</td></tr>');
            str.push('</table>');

            $('#dvProInfo').empty().append(str.join(''));
        };
        mProApp = function(r) {
            var str = new Array();
            str.push('<table border="0" cellspacing="0" cellpadding="0" class="table">');
            str.push('<tr><td width="135" class="titleBg">Tested Applications:</td>');
            str.push('<td width="510">');
            str.push(r.Application);
            //          ELISA.   str.push(((r.WesternTiter) ? 'WB(Recommended Titer 1:' + r.WesternTiter + ')' : ''));
//            str.push(((r.IHCTiter) ? ', IHC(Recommended Titer 1:' + r.IHCTiter + ')' : ''));
//            str.push(((r.IFTiter) ? ', IF(Recommended Titer 1:' + r.IFTiter + ')' : ''));
            str.push('</td></tr>');
            str.push('<tr><td class="titleBg">Specificity Reaction:</td>');
            str.push('<td>' + r.ReactSpecies + '; Other species are not tested.Please decide the specificity by homology</td></tr>');

            if (r.PositiveWB) {
                str.push('<tr><td class="titleBg">Positive WB detected In:</td>');
                str.push('<td>' + r.PositiveWB + ((r.PositiveControls) ? (',' + r.PositiveControls) : '') + '</td></tr>');
            }
            if (r.PositiveIHC) {
                str.push('<tr><td class="titleBg">Positive IHC detected In:</td>');
                str.push('<td>' + r.PositiveIHC + ((r.IHCPositiveControls) ? (',' + r.IHCPositiveControls) : '') + '</td></tr>');
            }
            if (r.PositiveIF) {
                str.push('<tr><td class="titleBg">Positive IF detected In:</td>');
                str.push('<td>' + r.IFCell + ((r.IFPositiveControls) ? (',' + r.IFPositiveControls) : '') + '</td></tr>');
            }
            str.push('</table>');
            str.push('Application key: WB=Western Blotting ,IHC=Immunohistochemistry, IF=Immunofluorescence');

            $('#dvProApp').empty().append(str.join(''));
        };
        mProImg = function() {
            $("#dvImageShow").ImagesShow({ id: 'dvImageShow', catNo: getRequest("catNo") });
        };
        mProImm = function(r) {
            var str = new Array();
            str.push('<table border="0" cellpadding="0" cellspacing="0" class="table">');
            str.push('<tr><td class="titleBg" width="130">Immunogen:</td>');
            str.push('<td width="150">' + ((r.AgNo) ? '<a href="AgDatasheet.aspx?catNo=' + r.AgNo + '" class="extlink" target="_blank">' + r.AgNo + '</a>' : r.Immunogen) + '</td>');
            str.push('<td class="titleBg" width="130">Full Name:</td>');
            str.push('<td width="223">' + ((r.FullName) ? r.FullName : r.GenbankDefline) + '</td></tr>');
            str.push('<tr><td class="titleBg">Predicted Molecular <br />Weight:</td>');
            str.push('<td>' + r.CaculatedSize + '</td>');
            str.push('<td class="titleBg">Observed Molecular <br />Weight:</td>');
            str.push(' <td>' + r.MwInWestern + '</td></tr>');
            str.push('<tr><td class="titleBg">GenbankNo</td>');
            str.push('<td><a href="' + GenBankNoUrl.replace('{GenBankNo}', r.GenBankNo) + '" class="extlink" target="_blank">' + r.GenBankNo + '</a></td>');
            str.push('<td class="titleBg">GeneID:</td>');
            str.push('<td><a href="' + GeneIDUrl.replace('{GeneID}', r.GeneID) + '" class="extlink" target="_blank">' + r.GeneID + '</a></td></tr>');

            str.push('<tr><td class="titleBg">Symbol</td>');
            str.push('<td>' + r.Symbol + '</td>');
            str.push('<td class="titleBg">Synonyms</td>');
            str.push('<td>' + r.Synonyms + '</td></tr>');
            str.push(' </table>');

            $('#dvProImm').empty().append(str.join(''));
        };
        mProBg = function(r) {
            $.getJSON("../Handler/ProductHandler.ashx?r=" + Math.random(),
            { func: 'GetBackground', param: 'geneID=' + config.result.GeneID },
            function(res) {
                var isshow = false;
                var str = new Array();

                if (r.Background) {
                    isshow = true;
                    str.push('<table border="0" cellpadding="0" cellspacing="0" class="table">');
                    str.push('<tr>');
                    str.push('<td width="660" style="padding:3px;"><p style="padding:0 5px 0 5px; line-height:150%;">' + r.Background + '<p>');
                }
                if (res.table[0].rows.length > 0) {
                    isshow = true;

                    $.each(res.table[0].rows, function(i, o) {
                        str.push('<p style="padding-left:20px; line-height:80%;">');
                        str.push(this.PMID + " " + this.Reference + ', ');
                        str.push('</p>');
                    });

                }
                if (!isshow) {
                    $('#dvProBg').hide();
                    $('#dvProBg').prev().hide();
                }
                else {
                    str.push('</td></tr> </table>');
                    $('#dvProBg').empty().append(str.join(''));
                }

                adjustMain();
            });
        };
        mProRelatedAb = function(r) {
            var str = new Array();
            str.push('<table border="0" cellpadding="0" cellspacing="0" class="table">');
            $.each(r, function(i, o) {
                str.push('<tr>');
                str.push('<td class="titleBg" width="130"><a href="ProductRedirect.aspx?w=viewpro&catNo=' + this.CatalogNo + '" class="extlink" target="_blank">' + this.CatalogNo + '</a></td>');
                str.push('<td width="660">' + this.ProductName + '</td>');
                str.push('</tr>');
            });
            str.push('</table>');
            $('#relatedProAb').append(str.join(''));

            adjustMain();
        };
        mProRelatedFp = function(r) {
            var str = new Array();
            str.push('<table border="0" cellpadding="0" cellspacing="0" class="table">');
            $.each(r, function(i, o) {
                str.push('<tr>');
                str.push('<td class="titleBg" width="130"><a href="ProductRedirect.aspx?w=viewpro&catNo=' + this.AgNo + '" class="extlink" target="_blank">' + this.AgNo + '</a></td>');
                str.push('<td width="660">' + this.ProductName + '</td>');
                str.push('</tr>');
            });
            str.push('</table>');
            $('#relatedProFp').append(str.join(''));

            adjustMain();
        };

        countryChgEvt = function(obj) {
           
        };

        subLoad = function(sub) {

            if (sub == null) {
                sub = $('#dvProTabs_sub_1');
            }
            if (sub.text() == "" || sub.text()) {

                setLoading(true);

                if ($(sub).attr("id") == "dvProTabs_sub_2") {
                    if (!config.tabload[1]) {
                        $("#dvProTabs_sub_2").empty().append('<div id="dvPubPager"></div><div id="dvPubList"></div>');
                        new $("#dvPubList").Publication({ id: 'dvPubList', index: 1, size: 8, pager: '#dvPubPager', param: 'catNo=' + getRequest("catNo") });
                        config.tabload[1] = true;
                    }
                }
                else if ($(sub).attr("id") == "dvProTabs_sub_3") {
                    if (config.tabload[2]) return;
                }
                else if ($(sub).attr("id") == "dvProTabs_sub_4") {
                    if (!config.tabload[3]) {
                        if (!getRequest("catNo")) { alert("No Param"); return; }
                        $.getJSON("../Handler/ProductHandler.ashx?r=" + Math.random(),
                        { func: 'GetRelatedPro', param: 'catNo=' + getRequest("catNo") }, function(res) {
                            if (!res) return;
                            mProRelatedAb(res.table[0].rows);
                            mProRelatedFp(res.table[1].rows);

                            config.tabload[3] = true;
                        });
                    }
                }
                setLoading(false);
            }

            sub.show();
            adjustMain();
        };

        printPage = function() {
            doPrint('aa', 'htm');
        };

        load = function() {
            if (!getRequest("catNo")) { alert("No Param"); return; }
            $.getJSON("../Handler/ProductHandler.ashx?r=" + Math.random(),
            { func: 'GetAbInfo', param: 'catNo=' + getRequest("catNo") },
            function(result) {
                config.result = result.table[0].rows[0];
                if (result.table[0].rows[0]) {
                    setNavText(result.table[0].rows[0]);
                    mPrice(result.table[0].rows[0]);
                    mTopInfo(result.table[0].rows[0]);
                    mProApp(result.table[0].rows[0]);
                    mProImg(result.table[0].rows[0]);
                    mProInfo(result.table[0].rows[0]);
                    mProImm(result.table[0].rows[0]);
                    mProBg(result.table[0].rows[0]);
                }

                adjustMain();

                setLoading(false);
            });
        };
        start = function() {
            setLoading(false);
            init();
            load();
        };

        start();
    });
}
    