
/**
	ProductPageBehavior class
	encapsulates bahavior specific to new users
**/
function ProductPageBehavior(pageController) {
	var self = this;

	self.pageController = pageController;
	self.currentProduct = null;
}



//*** Public methods ***//

ProductPageBehavior.prototype.drawUI = function(productID_Intuit, industryID) {
	var self = this;

	self._setCurrentProduct(productID_Intuit, industryID);
	
	self._setProductsToDispaly();

	var inProductFeatures = self.getInProductFeatures(self.pageController.features, self.pageController.productsToDisplay);
	
	self.pageController._drawProductFeatureTable(inProductFeatures, self.pageController.productsToDisplay);

	/*
	Temporary way to highlight the selected product column, which is always the first product column.
	*/
	var jqAllRows = $("#" + Constants.ID_ProductFeatureTable + " tr");

	jqAllRows.each(function () {
		var jqFirstProductCol;
		jqFirstProductCol = $(this).children().get(1);

		// Skip product feature group row
		if (jqFirstProductCol == undefined)
			return true;
		
		$(jqFirstProductCol).addClass("selected");
	});

}


ProductPageBehavior.prototype.getInProductFeatures = function(allFeatures, productsToDisplay) {
	var self = this;
	var inProductFeatures = new Array();
	
	// Loop over all features and remove the ones that 
	// should not be displayed on the in-product chart
	for (var f = 0; f < allFeatures.length; f++) {
		
		for (var p = 0; p < productsToDisplay.length;  p++) {

			// Check if feature is supported
			if ( productsToDisplay[p].showFeatureOnInProductPage(allFeatures[f]) && ! DataUtil.ExistsInArray( inProductFeatures, allFeatures[f] ) ) {
				inProductFeatures.push(allFeatures[f]);
			}
		}
	}
	return inProductFeatures;
}


//*** Private methods ***//

ProductPageBehavior.prototype._setCurrentProduct = function(productID_Intuit, industryID) {
	var self = this;

	// Get the product and from then on use the internal ProductID
	self.currentProduct = self.pageController.dataMapper.getProductByProductID_IntuitAndIndustryID(productID_Intuit, industryID);
}


ProductPageBehavior.prototype._setProductsToDispaly = function() {
	var self = this;

	self.pageController.productsToDisplay = new Array();
	self.pageController.productsToDisplay.push(self.currentProduct);

	// Only show 1 comparison by default. Simple Start free and Simple Start 2009 show 2 comparisons
	for (var i = 1; i <= 2; i++) {
		var product4Comparison = self.pageController.dataMapper.getProductByProductID(eval("self.currentProduct.productIDToCompare_" + i));

		if (DataUtil.HasValue(product4Comparison.productID)) {
			self.pageController.productsToDisplay.push(product4Comparison);
		}

		if (product4Comparison.productID != 'SimpleStartFree' && product4Comparison.productID != 'SimpleStart2009')
			break;
	}
}


ProductPageBehavior.prototype._drawProductHeaders = function(products) {
	var self = this;

	for (var i = 0; i < products.length; i++) {
		// Last column
		if (i + 1 == products.length) {
			self._drawProductHeader(Constants.HTML_ProductHeaderItemLast, products[i]);
		}
		// Middle columns
		else {
			self._drawProductHeader(Constants.HTML_ProductHeaderItemMiddle, products[i]);
		}	
	}
}


ProductPageBehavior.prototype._drawProductHeader = function(template, product) {
	var self = this;
	
	// Get the header row
	var jqRow = jQueryUtility.GetJQObject("#" + Constants.ID_ProductFeatureTable + " tr");

	// Process the template
	var currentProductHeader = Template.replaceProductName(template, product);

	jqRow.append(currentProductHeader);
}