﻿var productPricing = {
    hasType: function(x, data) {
        if (data.types != null) {
            for (var i = 0; i < data.types.length; i++) {
                if (data.types[i].id == x)
                    return true;
            }
        } else {
            return false;
        }
        return false;
    },
    extra: 0,
    showComapreAt: function(data) {
        var extra = productPricing.extra;
        if (data.showSamplePrice == false) {
            //$('#product ul.desc').css('height', $('#product ul.desc').height() - colHeight + 'px');
        }

        var pricing;

        pricing = data.printed;
        $('#cpPriceCode').text(' (' + data.priceCode + ')');
        for (var i = 0; i < (product.maxPriceColumn + extra); i++) {
            $('#cpQty' + (i + 1)).text(data.printed.qty[i] + '-' + (data.printed.qty[i + 1] - 1));
            $('#cpPrice' + (i + 1)).text((pricing.price[i] / ss.data.priceMultiplier(data.priceCodeString.substr(i, 1))).toMoney());
            if (extra != 0) $('#cpPrice' + (i + 1)).parent().show();
        }
        pricing = data.blank;
        $('#cbPriceCode').text(' (' + data.priceCode + ')');
        for (var i = 0; i < product.maxPriceColumn; i++) {
            $('#cbQty' + (i + 1)).text(data.blank.qty[i] + '-' + (data.blank.qty[i + 1] - 1));
            $('#cbPrice' + (i + 1)).text((pricing.price[i] / ss.data.priceMultiplier(data.priceCodeString.substr(i, 1))).toMoney());
        }
    },
    justBlank: false,
    getPricing: function(data) {
        function hasType(x) {
            return productPricing.hasType(x, data);
        }

        productPricing.extra = 0;
        if (cx.ua.intranet == true) {
            productPricing.extra = 2;
            data.priceCodeString = data.priceCodeString + 'CC';
        }
        var extra = productPricing.extra;

        function showPrinted(isSpecial) {
            $('#product .printedPricing').show()
            $('#grdPrinted').addClass('showGrid');
            $('#pPriceCode').text(' (' + data.priceCode + ')');
            var pricing;
            if (isSpecial == false) {
                pricing = data.printed;
            } else {
                pricing = data.specialPrinted;
            }

            for (var i = 0; i < (product.maxPriceColumn + extra); i++) {
                if ((i == product.maxPriceColumn - 1) && extra == 0) {
                    $('#pQty' + (i + 1)).text(pricing.qty[i] + '+');
                    //if ($('#colspc:hidden').length > 0) $('#product ul.desc').css('height', $('#product ul.desc').height() + colHeight + 'px');
                } else {
                    $('#pQty' + (i + 1)).text(pricing.qty[i] + '-' + (pricing.qty[i + 1] - 1));
                }
                $('#pPrice' + (i + 1)).text((pricing.price[i] / ss.data.priceMultiplier(data.priceCodeString.substr(i, 1))).toMoney());
                if (pricing.price[i] == 0) {
                    //$('#product ul.desc').css('height', $('#product ul.desc').height() + colHeight + 'px');
                    $('#pPrice' + (i + 1)).parent().hide();
                } else {
                    $('#pPrice' + (i + 1)).parent().show();
                }
            }

        }
        function showBlank(isSpecial) {
            $('#product .blankPricing').show().addClass('showGrid');
            $('#grdBlank').addClass('showGrid');
            var pricing;
            if (isSpecial == false) {
                pricing = data.blank;
            } else {
                pricing = data.specialBlank;
            }

            var firstQty = 0;
            for (var i = 0; i < product.maxPriceColumn; i++) {
                if (i == product.maxPriceColumn - 1) {
                    $('#bQty' + (i + 1)).text(pricing.qty[i] + '+');
                } else {
                    $('#bQty' + (i + 1)).text(pricing.qty[i] + '-' + (pricing.qty[i + 1] - 1));
                }
                $('#bPrice' + (i + 1)).text((pricing.price[i] / ss.data.priceMultiplier(data.priceCodeString.substr(i, 1))).toMoney());
                if (pricing.price[i] == 0) {
                    //if ($('#product .printedPricing').css('display') != 'block'){
                    //$('#product ul.desc').css('height', $('#product ul.desc').height() + colHeight + 'px');


                    $('#bPrice' + (i + 1)).parent().hide();
                } else {
                    if (firstQty == 0)
                        firstQty = pricing.qty[i];
                }
            }

            if (data.showSamplePrice == true) {
                if (is.num(data.priceCode.substr(0, 1))) {
                    $('#bPriceCode').text(' (' + (cx.ua.intranet == true ? 'A' : '') + (parseInt(data.priceCode.substr(0, 1)) + 1) + data.priceCode.substring(1, data.priceCode.length) + ')');
                }
                $('#sampleQty').text(data.sampleMin + '-' + (firstQty - 1));
                $('#samplePrice').text((data.samplePrice / ss.data.priceMultiplier(data.priceCodeString.substr(0, 1))).toMoney());
                $('#colspc').show();
                $('#bSample').show();
                if (cx.ua.intranet == true) {
                    $('#minSampleQty').text('1-' + (data.sampleMin - 1));
                    $('#minSamplePrice').text(((data.printed.price[3] * 0.9) * 2).toMoney());
                    $('#bMinSample').show();
                }
            } else {
                $('#bPriceCode').text(' (' + data.priceCode + ')');
                //$('#product ul.desc').css('height', $('#product ul.desc').height() - colHeight + 'px');
            }
        }

        if (product.closeout > 0) {
            g.log('color closeout');
            //show blank pricing
            productPricing.justBlank = true;
            showBlank(false);
            $('#tabBlank').text($('#tabPrinted').text());
            $('#tabBlank').attr('id', 'tabBlankToPrinted');
        } else {
            if (product.closeout == 0) {
                g.log('all closeout');
                //show printed pricing
                productPricing.justBlank = false;
                showPrinted(false);
            } else {
                if (data.imprints.length == 0) {
                    if (hasType(39)) {
                        g.log('special blank product');
                        //show special blank pricing
                        productPricing.justBlank = true;
                        $("a[href='#ib-2']").parent().show();
                        showBlank(true);
                    } else {
                        g.log('blank product');
                        //show blank pricing
                        productPricing.justBlank = true;
                        showBlank(false);
                    }
                } else {
                    if (data.printed.price[3] == data.blank.price[3]) {
                        if (hasType(38)) {
                            g.log('special printed product');
                            //show special printed pricing
                            $("a[href='#ip-2']").parent().show();
                            productPricing.justBlank = false;
                            showPrinted(true);
                        } else {
                            g.log('printed product');
                            //show printed pricing
                            productPricing.justBlank = false;
                            showPrinted(false);
                        }
                    } else {
                        if (hasType(39) && hasType(38)) {
                            g.log('special printed & blank');
                            //show special printed and special blank pricing
                            $("a[href='#ip-2']").parent().show();
                            $("a[href='#bp-2']").parent().show();
                            productPricing.justBlank = false;
                            showBlank(true);
                            showPrinted(true);
                        } else {
                            g.log('printed & blank');
                            //show printed and blank pricing
                            productPricing.justBlank = false;
                            showBlank(false);
                            showPrinted(false);
                        }
                    }
                }
            }
        }
    }

}
