/**
 * This script is responsible for filtering credit cards down to a specific credit
 * type as well as handling the determine your credit level pill box
 *
 * @requires /scripts/https-common/jquery/jquery.js
 * @requires /scripts/https-creditcards/compare_cards.js
 * @version 2.0
 *
 */

var numCards = 0;

var Filter_Cards_Init = function()
{
    // remove checkboxes all of the compare filter checkboxes by default
    $('#creditTypeCategory_'+CONSTANT_CREDIT_TYPE_EXCELLENT+', #creditTypeCategory_'+CONSTANT_CREDIT_TYPE_GOOD+', #creditTypeCategory_'+CONSTANT_CREDIT_TYPE_AVERAGE+', #creditTypeCategory_'+CONSTANT_CREDIT_TYPE_LIMITED).removeAttr('checked');
    
    numCards = parseInt($('#compareAllTable tbody tr:visible').size());

    // creates event listener on the credit type checkboxes
    $('input#creditTypeCategory_' + CONSTANT_CREDIT_TYPE_EXCELLENT + ', input#creditTypeCategory_' + CONSTANT_CREDIT_TYPE_GOOD + ', input#creditTypeCategory_' + CONSTANT_CREDIT_TYPE_AVERAGE + ', input#creditTypeCategory_' + CONSTANT_CREDIT_TYPE_LIMITED).click(function()
    {
        Reset_Card_Filter();
    });

    // Adds the highlight on mouseover
    $('#compareAllWrapper #compareAllTable tbody tr').mouseover(function()
    {
        $(this).addClass('highlight');
    });

    // Removes highlight on mouse out
    $('#compareAllWrapper #compareAllTable tbody tr').mouseout(function()
    {
         $(this).removeClass('highlight');
    });

    // occurs when credit level is reset
    $('.resetCreditLevelAssignment').click(function()
    {
        for (var i = 0; i < CREDIT_TYPE_CATEGORIES.length; ++i)
        {
            Filter_Cards_By_Credit_Type('show', CREDIT_TYPE_CATEGORIES[i]);
        }

        $('form#compareAllCreditFilter input').removeAttr('checked');
    });

    // Enable the filter functionality since Javascript is enabled
    $('#CompareAllFormTitle, #creditTypeCategory_' + CONSTANT_CREDIT_TYPE_EXCELLENT + ', #creditTypeCategory_' + CONSTANT_CREDIT_TYPE_GOOD + ', #creditTypeCategory_' + CONSTANT_CREDIT_TYPE_AVERAGE + ', #creditTypeCategory_' + CONSTANT_CREDIT_TYPE_LIMITED).css({visibility:'visible'});
};

var Reset_Card_Filter = function()
{
    var numberUncheckedBoxes = 0;
    
    $('#compareAllCreditFilter input:checked').each(function()
    {
        var creditTypeId = $(this).attr('id').substring($(this).attr('id').length - 1);
        Filter_Cards_By_Credit_Type('show', creditTypeId);
    });

    $('#compareAllCreditFilter input:not(:checked)').each(function()
    {
        var creditTypeId = $(this).attr('id').substring($(this).attr('id').length - 1);
        Filter_Cards_By_Credit_Type('hide', creditTypeId);
        numberUncheckedBoxes++;
    });

    //if all boxes unchecked, then reset and show all credit levels
    if (numberUncheckedBoxes == CREDIT_TYPE_CATEGORIES.length) {
        for (var i = 0; i < CREDIT_TYPE_CATEGORIES.length; ++i)
        {
            Filter_Cards_By_Credit_Type('show', CREDIT_TYPE_CATEGORIES[i]);
        }
        // remove checkboxes all of the compare filter checkboxes 
        $('#creditTypeCategory_'+CONSTANT_CREDIT_TYPE_EXCELLENT+', #creditTypeCategory_'+CONSTANT_CREDIT_TYPE_GOOD+', #creditTypeCategory_'+CONSTANT_CREDIT_TYPE_AVERAGE+', #creditTypeCategory_'+CONSTANT_CREDIT_TYPE_LIMITED).removeAttr('checked');
    }

    Compare_Cards_Validation_Check();
};

var Filter_Cards_By_Credit_Type = function(toggle, creditTypeId)
{
    // selects all the cards in the table that match the given credit type
    var classSelect = '#compareAllTable tr.compareProductCategory_' + creditTypeId;

    if (toggle == 'hide')
    {
        $(classSelect).css({display:'none'});

        // remove the check from the checkbox
        $('input#creditTypeCategory_' + creditTypeId).removeAttr('checked');
    }
    else if (toggle == 'show')
    {
        if ($.browser.msie)
        {
            // IE doesn't support display:table-row, so we use 'block' instead
            $(classSelect).css({display:'block'});
        }
        else
        {
            $(classSelect).css({display:'table-row'});
        }

        // check the checkbox
        $('input#creditTypeCategory_' + creditTypeId).attr('checked', 'checked');
    }

    // update the number of cards text
    numCards = parseInt($('#compareAllTable tbody tr:visible').size());

    if (numCards == 0)
    {
        $('#compareAllTable').css({display:'none'});
        $('#noSelectionBox').css({display:'block'});
        $('#compareAllFindNothing').css({display:'none'});
    }
    else
    {
        $('#compareAllTable').css({display:'block'});
        $('#noSelectionBox').css({display:'none'});
        $('#compareAllFindNothing').css({display:'block'});
    }

    // update the number of cards being displayed
    $('#compareAllCardCount').html(numCards.toString());
};

$(document).ready(function()
{
    // init the comparison filter cards functionality
    Filter_Cards_Init();
});