jQuery(document).ready(function ($) {
    if ($('.compare-table').length) {
        $.get('/secure/Content/PriceComparison.xml', null, parseProductsXML, 'xml');
    }

    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (obj, start) {
            for (var i = (start || 0); i < this.length; i++) {
                if (this[i] == obj) return i;
            }
            return -1;
        }
    }
});

function parseProductsXML(data, textStatus) {
    var list_length = data.getElementsByTagName("product").length;
    var sizes = ['Twin Single Chamber (38x75)', 'Twin XL Single Chamber (38x80)', 'Full Single Chamber (54x75)', 'Queen Single Chamber (60x80)', 'E. King Dual Chambers (78x80 Standard)'];
    var sizes_length = sizes.length;
    var models = ['3-Series', '4-Series', '5-Series', '6-Series', '8-Series', '10-Series'];
    var models_length = models.length;

    var ourBasePrice = 0;
    var ourLowPrices = [];
    var ourHighPrices = [];
    var compBasePrice = 0;
    var compLowPrices = [];
    var compHighPrices = [];

    // pre-populate price arrays
    var j = -1;
    while (++j < models_length) {
        ourLowPrices.push(0);
        ourHighPrices.push(0);
        compLowPrices.push(0);
        compHighPrices.push(0);
    }

    // find the matching product
    var i = -1;
    while (++i < list_length) {
        var productNode = data.getElementsByTagName("product")[i];
        var modelIndex = models.indexOf(productNode.getElementsByTagName('basicInfo')[0].getElementsByTagName('name')[0].childNodes[0].nodeValue);
        if (modelIndex !== -1) {

            ourBasePrice = parseFloat(productNode.getElementsByTagName('basicInfo')[0].getElementsByTagName('price')[0].childNodes[0].nodeValue);
            compBasePrice = parseFloat(productNode.getElementsByTagName('basicInfo')[0].getElementsByTagName('competitorPrice')[0].childNodes[0].nodeValue);

            var attributes = productNode.getElementsByTagName('attribute');
            var attributes_length = attributes.length;

            // now find the needed attributes
            j = -1;
            while (++j < attributes_length) {
                var attribute = productNode.getElementsByTagName('attribute')[j];
                var attributeName = attribute.getElementsByTagName('name')[0].childNodes[0].nodeValue;
                if (attributeName == 'Size') {
                    var k = -1;
                    var options = attribute.getElementsByTagName('option');
                    var options_length = options.length;

                    while (++k < options_length) {
                        var option = options[k];
                        if (sizes.indexOf(option.getElementsByTagName('name')[0].childNodes[0].nodeValue) == -1) continue; 

                        var ourPrice = parseFloat(option.getElementsByTagName('price')[0].childNodes[0].nodeValue) + ourBasePrice;
                        var compPrice = parseFloat(option.getElementsByTagName('competitorPrice')[0].childNodes[0].nodeValue) + compBasePrice;

                        if (ourLowPrices[modelIndex] > ourPrice || (ourLowPrices[modelIndex] == 0 && ourPrice > 0)) {
                            ourLowPrices[modelIndex] = ourPrice;
                        }
                        if (ourHighPrices[modelIndex] < ourPrice) {
                            ourHighPrices[modelIndex] = ourPrice
                        }
                        if (compLowPrices[modelIndex] > compPrice || (compLowPrices[modelIndex] == 0 && compPrice > 0)) {
                            if (compPrice != -1) {
                                compLowPrices[modelIndex] = compPrice;
                            }
                        }
                        if (compHighPrices[modelIndex] < compPrice) {
                            compHighPrices[modelIndex] = compPrice
                        }
                    }
                }
            }
        }
    }

    // calculate savings
    $('#compare-prices .compare-table tr:gt(0)').each(function (index) {
        var cells = $('td', $(this));
        $('span', $(cells[0])).html(formatMoney(compLowPrices[index]) + '-' + formatMoney(compHighPrices[index]));
        $('span', $(cells[1])).html(formatMoney(ourLowPrices[index]) + '-' + formatMoney(ourHighPrices[index]));
        $(cells[2]).html(formatMoney(compHighPrices[index] - ourHighPrices[index]));
    });

    function formatMoney(number) {
        number = String(number);
        var regex = /(\d+)(\d{3})/;
        while (regex.test(number)) {
            number = number.replace(regex, '$1,$2');
        }
        return '$' + number;
    }
}

