﻿function compareProducts() {
    var $btnCompare = $('.btnCompare');
    var ttDelay;

    if ($('#infoTipError').size() <= 0) {
        var infoTipMsg = '';
        var infoTipError = '<span id="infoTipError" class="toolTip_content"><span class="toolTip_span"></span><span class="toolTip_bottom">&nbsp;</span></span>';
        $('body').append(infoTipError);
        $infoTipError = $('#infoTipError');
    }

    if ($btnCompare.attr('disabled') == 'disabled' || $btnCompare.attr('disabled') == true) {
        $btnCompare.fadeTo(0, .4);
    } else {
        $btnCompare.fadeTo(0, 100);
    }

    $('.compare').click(function () {
        if ($(this).find('span[disabled="disabled"]').size() > 0 || $(this).attr('disabled') === "disabled" || $(this).attr('disabled') === "true") {
            throwError('You can compare up to 5 products at one time, please <a href="/Products/Compare-Products.aspx">remove a product here</a> and try again.', $(this).offset());
        }
    });

    $('.compare input[disabled="disabled"]').change(function () {
        if ($(this).find('span[disabled="disabled"]').size() > 0 || $(this).attr('disabled') === "disabled") {
            alert('this button is disabled');
            throwError('You can compare up to 5 products at one time, please <a href="/Products/Compare-Products.aspx">remove a product here</a> and try again.', $(this).offset());
        }
    });

    $btnCompare.click(function () {
        if ($btnCompare.attr('disabled') === 'disabled') {
            throwError('Please select at least two products to compare.', $(this).offset());
        }
    });

    function throwError(errorMsg, pos) {
        clearTimeout(ttDelay);
        $infoTipError.find('.toolTip_span').html(errorMsg);
        $infoTipError.css({ 'left': pos.left - 75, 'top': pos.top - 100 }).fadeIn(300);
        ttDelay = setTimeout(removeError, 3000);
    }

    function removeError() {
        $infoTipError.fadeOut(400);
    }
}
