if(ess) {
}else{
	var ess={};
}
ess.Product = function(productJSONObject) {
	this.colorId = '';
	this.skuId = '';
	this.quantity = 0;
	this.cp = productJSONObject.cp;
	this.globalCategoryId = productJSONObject.globalCategoryId;
	this.skus = productJSONObject.skus;
	this.productId = productJSONObject.productId;
	this.productTitle = productJSONObject.productTitle;
	this.colorSliceValues = productJSONObject.colorSliceValues;
	this.alternateViews = productJSONObject.alternateViews;
	this.mainImageURL = productJSONObject.mainImageURL;
	this.enhancedImageURL = productJSONObject.enhancedImageURL;
	this.zoomURL = productJSONObject.zoomURL;
	this.familyImageURL = productJSONObject.familyImageURL;
	this.largeSwatchImageURL = productJSONObject.largeSwatchImageURL;
	this.pageURL = productJSONObject.pageURL;
	this.colorChangeListeners = new Array();
	this.viewChangeListeners = new Array();
	this.qtyChangeListeners = new Array();
	this.skuChangeListeners = new Array();
	this.viewIndex = 0;
	this.viewId = '';
	this.defaultViewId = '';
	this.currentView = this;
	var i = 0;
	for(viewId in this.alternateViews) {
		if(i==0) {
			this.viewId = viewId;
			this.defaultViewId = viewId;
			this.currentView = this.alternateViews[viewId];
		}
		i++;
	}
	this.currentColorSlice = null;


// Add this code to set the main image to be used on each slice
	for(var i=0; i++; i< this.colorSliceValues.length) {
		this.colorSliceValues[i].viewId = "main";
	}


	this.addColorChangeListener = function(listener) {
		this.colorChangeListeners[this.colorChangeListeners.length] = listener;
	};
	this.addViewChangeListener = function(listener) {
		this.viewChangeListeners[this.viewChangeListeners.length] = listener;
	};
	this.addQtyChangeListener = function(listener) {
		this.qtyChangeListeners[this.qtyChangeListeners.length] = listener;
	};
	this.addSkuChangeListener = function(listener) {
		this.skuChangeListeners[this.skuChangeListeners.length] = listener;
	};
	this.getAvailableSizes = function() {
		var sizes = new Array();
		if(this.colorId != '') {
			var skus = this.getCurrentColorSliceValue().availableSkuIds;
			for(var i=0; i < skus.length; i++) {
				sizes[i] = this.getSkuById(skus[i]).size;
			}
		}
		return sizes;
	}

	this.getCurrentColorSliceValue = function() {
		return this.currentColorSliceValue;
	}
	this.getSkuById = function(id) {
		return this.skus["sku_"+id];
	}
	this.getCurrentView = function() {
		return this.currentView;	
	}
	this.changeColor = function(colorId) {
		if(this.colorId!=colorId) {
			this.colorId = colorId;
			//this should be updated to select the correct sku id
			this.changeSku('');
			this.currentColorSliceValue = null;
			for(var i=0; i < this.colorSliceValues.length; i++) {
				if(this.colorSliceValues[i].colorId == this.colorId) {
					this.currentColorSliceValue = this.colorSliceValues[i];
					break;
				}
			}
	//		this.changeSku(this.getCurrentColorSliceValue().availableSkuIds[0]);
	
			this.changeView(this.defaultViewId);
			for(var i=0; i < this.colorChangeListeners.length; i++) {
				this.colorChangeListeners[i].observeProductColorChange(this);
			}
		}
	}
	this.setQuantity = function(qty) {
		this.quantity = qty;
		for(var i=0; i < this.qtyChangeListeners.length; i++) {
			this.qtyChangeListeners[i].observeProductQtyChange(this);
		}
	}
	
	this.changeSku = function(skuId) {
		this.skuId = skuId;
		for(var i=0; i < this.skuChangeListeners.length; i++) {
			this.skuChangeListeners[i].observeProductSkuChange(this);
		}
	}
	
	this.changeView = function(viewId) {
		this.viewId = viewId;
		this.currentView = this.alternateViews[viewId];
		if(this.getCurrentColorSliceValue()!=null) {
			if(this.getCurrentColorSliceValue().alternateViews[viewId]) {
				this.currentView = this.getCurrentColorSliceValue().alternateViews[viewId];
			} else {
				this.currentView = this.getCurrentColorSliceValue();
			}
		}
		for(var i=0; i < this.viewChangeListeners.length; i++) {
			if(this.viewChangeListeners[i].observeProductViewChange)
				this.viewChangeListeners[i].observeProductViewChange(this);
		}
	}
	

}
