var UserFoundationInfo = Class.create({
    initialize: function(args) {
		this.callback = null;	
		this.foundationInfo = null;
		this.foundationResults = null;
		this.foundationCpd = null;
		this.tabprods = [];
		this.tabskus = [];
		
		// Load up passed params
        Object.extend(this, args || {});
    },
    
    fetchFoundationAnswers: function() {
		var id = jsonRpcWrapper.fetch({
		    method: 'quiz.get', 
			params: [{"type": "foundation_finder"}],
			onSuccess: this.loadFoundationInfo.bind(this),
			onError: function () { console.log( 'FoundationInfo JSON failed to load ' ) }
		});
		return id;
    },
    
    loadFoundationInfo: function(jsonRpcResponse) {
        var data = jsonRpcResponse.getValue();
		if (data) {
    		this.foundationInfo = $H(data);
    		this.foundationInfo.answers = this.foundationInfo.get('answers');
    	}
			
		if ( this.foundationInfo && this.foundationInfo.answers ) {
			this.fetchFoundationResults ( this.foundationInfo.answers );
		} else {
			this.doCallback();
		}
    },
    
    fetchFoundationResults: function(answerArray) {
        var params = Object.extend ( {'answers': answerArray} );
        params = Object.extend ( params, productPage.DETAIL_VIEW_QUERY.PRODUCT_FIELDS );
        params = Object.extend ( params, productPage.DETAIL_VIEW_QUERY.SKU_FIELDS );
    	params = [params];
    	
		var id = jsonRpcWrapper.fetch({
		    method: 'foundation.results', 
            params: params,
			onSuccess: this.loadFoundationResults.bind(this),
			onError: function () { console.log( 'SkinResults JSON failed to load ' ); }
		});
		return id;
    },
    
    loadFoundationResults: function(jsonRpcResponse) {
        var data = jsonRpcResponse.getValue();
		if (data) {
			this.foundationResults = data;
			
			if ( this.foundationResults['results']  && this.foundationResults['results']['product'] ) {
				$H(this.foundationResults['results']['product']).each( function(pair){
					this.tabprods[this.tabprods.length] = pair.value;
				}, this);
			}
			
			if ( this.foundationResults['results']  && this.foundationResults['results']['sku'] ) {
				$H(this.foundationResults['results']['sku']).each( function(pair){
					this.tabskus[this.tabskus.length] = pair.value;
				}, this);
			}

			this.doCallback();			
		}

	},
	
    doCallback: function() {
		if ( this.callback ) {
			this.callback();
		}
    },
    
    getSkus: function() {
    	return this.tabskus;
    },
    
    getProducts: function() {
    	return this.tabprods;
    }
	
});
