var counterFlashReady = false;
/* QP1 = state field */
$(function() {
	//$("select[name=QP1]").change(calculateCardCount);
	$("input[name=QP4], input[name=QP2]").click(calculateCardCount);
    var credit   = $("input[name=QP4]:checked").val();
    if(!credit || credit == -1){
        updateCardCount("--");
    } else {
        calculateCardCount();
    }
});


// 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;
}

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

// Called when the Flash is initialized
function flashReady() {
    counterFlashReady = true;
    calculateCardCount();
	//$("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(!credit || credit == -1) {
		updateCardCount("--");
	} else {
        //var matchingCards = arrayIntersect(cardData.personal.states[state], cardData.personal.credit[credit]);
        var matchingCards = 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
        var featuredCards = [];
        $.each(features, function(i, val) {
            featuredCards = arrayUnique(featuredCards, cardData.personal.features[val]);
	});
        if(featuredCards.length > 0) {
            matchingCards = arrayIntersect(matchingCards, featuredCards);
        }
        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);
	}
}
