var counterFlashReady = false;

$(function() {
	$("select[name=QP1]").change(calculateCardCount);
	$("input[name=QP4], input[name=QP2]").click(calculateCardCount);
	updateCardCount("--");
    $("select[name=QP1]").change();
});


// Returns the Flash movie by name
function thisMovie(movieName) {
     if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName];
     } else {
         return document[movieName];
     }
 }
		     
// Returns the intersection of two arrays
function arrayIntersect(firstArray, secondArray) {
	var intersectArray = [];

	// Loop through the first array
	$.each(firstArray, function(i, val) {
		if($.inArray(val, secondArray) != -1) {
			intersectArray.push(val);
		}
	});

	return intersectArray;
}

// Called when the Flash is initialized
function flashReady() {
	counterFlashReady = true;

	$("select[name=QP1]").change();
}

// Calculates the card count by finding the intersection of cards with the given criteria
function calculateCardCount() {
	var credit   = $("input[name=QP4]:checked").val();
	var features = [];
	var state    = $("select[name=QP1]").val();

    // Convert all checked features into an array
	$.each($("input[name=QP2]:checked"), function(i, feature) {
		features.push($(feature).val());
	});

	// If the state or credit is not set
	if(!state || !credit || state == -1 || credit == -1) {
		updateCardCount("--");
	} else {
		var matchingCards = arrayIntersect(cardData.personal.states[state], cardData.personal.credit[credit]);

        //if credit is poor or limited and no card match, display overlay
        if(matchingCards.length < 1 && (credit == "1" || credit == "2")){

            if($.nyroModalManual){

                $.nyroModalSettings({
                    minWidth:0, minHeight:0,
                    resizable: false,
                    showLoading: function(elts, settings, callback) {
                        callback();
                        },
                    showContent: function(elts, settings, callback) {
                        console.log("marginLeft = " + settings.marginLeft);
                        console.log("margin-top = " + settings.marginTop);
                        callback();
                        elts.contentWrapper
                        .css({
                            width: settings.width+'px',
                            height: settings.height+'px',
                            marginTop: settings.marginTop+'px',
                            marginLeft: settings.marginLeft+'px'
                        })
                        .show();
                        elts.loading.fadeOut(200, callback);
                        $('#nyroModalFull').css('zIndex',9999);
                    },
                    hideContent: function(elts, settings, callback) {
                        elts.contentWrapper.hide();
                        callback();
                    }

                });
                $('#no-card-msg-link').nyroModalManual({
                      bgColor: 'transparent'
                });
            }
            window.setTimeout(function(){
                if($.browser.msie){
                    $('.no-card-msg .modal-close').hover(function(){$(this).addClass("hover");}, function(){$(this).removeClass("hover")});
                }
            }, 1000);
            
        }
        
        // Loop through the features
		$.each(features, function(i, val) {
			matchingCards = arrayIntersect(matchingCards, cardData.personal.features[val]);
		});

        updateCardCount(matchingCards.length);

    }
}

// Updates the card count using either the Flash or the HTML
function updateCardCount(count) {
	// If the user has Flash
	if(findFlashVersion()) {
		// If the counter Flash is ready
		if(counterFlashReady) {
			thisMovie("VisaCardAdvisorNumberDisplay").showNumber(count);
		}
	} else {
		$("#card-count").html(count);
	}
}