// Product Object
// Assumptions: 
Product = {
productType: '', // to know which featuers are set, set by template file on initialization
capacities: [], // internally (dynamically created by this js file)
capacitiesFormatted: [], // set by template file on initialization
colors: [],  // set by template file on initialization
marketingColors: [], // set by template file on initialization
featureToIDMap: [],	// featureToIDMap has a feature string which points to a temp product id
tempToRealIDMap: [], // tempToRealIDMap has a mapping from temp id to real product id
retailPrices: [], // set by template file on initialization
unitPrices: [], // set by template file on initialization
priceDrop: [], // set by template file on initialization
hasPriceDropValue:[],
rebateSavings: [], // set by template file on initialization
rebateAmt: [], // set by template file on initialization
hasRebateAmt: [],
availability: [], // set by template file on initialization
shippingInfo: [], // set by template file on initialization
basicToMarketingColors: [],
hasColorsOption: false,
hasCapacitiesOption: false,
hasLengthsOption: false,
hasFreeShippingValue: false,
hasMarketingColorsOption: false,
hasRetailPricesShown: false,
categoryID: false, // set by template file on initialization
hasUpsell: false, // does product have an upsell product
init:function(prodType, catID, featureToIDMapArr, tempToRealIDMapArr, capacitiesFormattedArr, colorsArr, mColorsArr, rPricesArr, uPricesArr, rebateAmtArr, rebateSavingsArr, priceDropArr, availabilityArr, shippingInfoArr, upsell) {
	if (!document.getElementsByTagName || !document.getElementById) { return false; }

	Product.productType = prodType;
	if (catID) {
		Product.categoryID = catID;
	}

	if (upsell) {
		Product.hasUpsell = upsell;
	}

	if (tempToRealIDMapArr.length > 0) {
		Product.setFeatureToIDMap(featureToIDMapArr);
		Product.setTempToRealIDMap(tempToRealIDMapArr);
	}

	switch(Product.productType) {
		case("Capacity and Color"): 
			if (colorsArr.length > 0) {
				Product.setColors(colorsArr);
				Product.hasColorsOption = true;
			}
			if (mColorsArr.length > 0) {
				Product.setMarketingColors(mColorsArr);
				Product.hasMarketingColorsOption = true;
			}
			if (colorsArr.length > 0 && mColorsArr.length > 0) {
				for(i=0;i<colorsArr.length;i++){
					Product.basicToMarketingColors[colorsArr[i].toLowerCase()] = mColorsArr[i];
				}
			} 
			if (capacitiesFormattedArr.length > 0) {
				Product.setCapacitiesFormatted(capacitiesFormattedArr);
				Product.setCapacities(capacitiesFormattedArr);
				Product.hasCapacitiesOption = true;
			}
			break;
		case("Capacity"):
			if (capacitiesFormattedArr.length > 0) {
				Product.setCapacitiesFormatted(capacitiesFormattedArr);
				Product.setCapacities(capacitiesFormattedArr);
				Product.hasCapacitiesOption = true;
			}
			break;
		case("Color"):
			if (colorsArr.length > 0) {
				Product.setColors(colorsArr);
				Product.hasColorsOption = true;
			}
			if (mColorsArr.length > 0) {
				Product.setMarketingColors(mColorsArr);
				Product.hasMarketingColorsOption = true;
			}
			if (colorsArr.length > 0 && mColorsArr.length > 0) {
				for(i=0;i<colorsArr.length;i++) {
					Product.basicToMarketingColors[colorsArr[i].toLowerCase()] = mColorsArr[i];
				}
			}
			break;
		
		case("None"): break;
		default: break;
	}

	// Set Prices
	// Initial - retail 
	if (rPricesArr.length > 0) {
		Product.setRetailPrices(rPricesArr);
		Product.hasRetailPricesShown = true;
	}

	// Final - unit 
	if (uPricesArr.length > 0) {
		Product.setUnitPrices(uPricesArr);
	}	

	// Set (if) Rebates
	if (rebateAmtArr.length > 0) { // if any product id has a rebate
		Product.setRebateAmt(rebateAmtArr);
		Product.setHasRebate(rebateAmtArr);
	}

	if (rebateSavingsArr.length > 0) {
		Product.setRebateSavings(rebateSavingsArr);
	}

	// Set (if) Savings
	if (priceDropArr.length > 0) { // if any product id has a price drop
		Product.setPriceDrop(priceDropArr);
		Product.setHasPriceDrop(priceDropArr);
	}

	// Availability
	if (availabilityArr.length > 0) {
		Product.setAvailability(availabilityArr);
	}

	// Shipping
	if (shippingInfoArr.length > 0) {
		Product.setShipping(shippingInfoArr);
	}

},
getFirstCapacity:function() {
	return Product.capacities[0];
},
getFirstCapacityFormatted:function() {
	return Product.capacitiesFormatted[0];
},
getFirstColor:function() {
	return Product.colors[0].toLowerCase();
},
getFirstColorFormatted:function() {
	var c;
	c = Product.colors[0].substr(0,1).toUpperCase() + Product.colors[0].substr(1,Product.colors[0].length).toLowerCase();
	if (Product.hasMarketingColors()) {
		return c;
	}
	else {
		return Product.basicToMarketingColors[c] + " (" + c + ") ";
	}
},
getColorFormatted:function(col) {
	var basicColor, markColor;
	basicColor = col.toLowerCase();
	basicColor = basicColor.substr(0,1).toUpperCase() + basicColor.substr(1,basicColor.length).toLowerCase();
	if (Product.hasMarketingColors()) {
		markColor = Product.basicToMarketingColors[col.toLowerCase()] + " (" + basicColor + ") ";
		return markColor;
	}
	else {
		return basicColor;		
	}
},
getCapacityFormatted:function(c) {
	// given a capacity, get it with its kb, mb, gb, or tb
	var stripped = new String();
	var units = new String();
	var len;
	// was just capacities.length
	for(i=0;i<Product.capacitiesFormatted.length;i++) {
		len = Product.capacitiesFormatted[i].length;
		stripped = Product.capacitiesFormatted[i].substring(0, len-2);
		stripped = stripped.replace(" ", "");
		units = Product.capacitiesFormatted[i].substring(len-2,len);
		if(stripped = c) { 
			return Product.capacitiesFormatted[i];
		}
		else {
			return 0;
		}
	}
},
getCapacity:function(cf) {
	// given a capacity, get it without mb or gb
	var stripped = new String();
	var units = new String();
	var len;
	len = cf.length;
	stripped = cf.substring(0, len-2);
	stripped = stripped.replace(" ", "");
	units = cf.substring(len-2,len);
	return stripped;
},
getProductFeatureString:function(f) {
	// given an array of features, create the proper index into the id array
	// if there are no features then the index is a number instead
	var stringy = '';
	for (i=0; i<f.length; i++) {
		if(f[i] == "") {
		}
		else {
			if(i+1 == f.length) {
				stringy = stringy + f[i].toLowerCase();
			}
			else {
				stringy = stringy + f[i].toLowerCase() + "," ;
			}
		}
	}
	return stringy;
},
getTempProductId:function(f) {
	var stringy = Product.getProductFeatureString(f);
	var tempid = Product.featureToIDMap[stringy];
	return tempid;
},
getProductId:function(f) {
	var stringy = Product.getProductFeatureString(f);
	var tempid = Product.featureToIDMap[stringy];
	var realid = Product.tempToRealIDMap[tempid];
	return realid;
},
getUnitPrice:function(f) {
	var id = Product.getTempProductId(f);
	var up = Product.unitPrices[id];
	return up;
},
getRetailPrice:function(f) {
	var id = Product.getTempProductId(f);
	var rp = Product.retailPrices[id];
	return rp;
},
getBeforeRebatePrice:function(f) {
	var id = Product.getTempProductId(f);
	var up = Product.unitPrices[id];
	return up;
},
getAfterRebatePrice:function(f) {
	var id = Product.getTempProductId(f);
	var rp = Product.retailPrices[id];
	return rp;
},
getRebateAmt:function(f) {
	var id = Product.getTempProductId(f);
	var rv = Product.rebateAmt[id];
	return rv;
},
getRebateSavings:function(f) {
	var id = Product.getTempProductId(f);
	var rs = Product.rebateSavings[id];
	return rs;
},
getShipping:function(f) {
	var id = Product.getTempProductId(f);
	var s = Product.shippingInfo[id];
	return s;
},
getAvailability:function(f) {
	var id = Product.getTempProductId(f);
	var a = Product.availability[id];
	return a;
},
getPriceDrop:function(f) {
	var id = Product.getTempProductId(f);
	var pd = Product.priceDrop[id];
	return pd;
},
getCategoryID:function() {
	if (Product.categoryID) {
		return Product.categoryID;
	}
	else {
		return false;
	}
},
hasUpsellProducts:function() {
	return Product.hasUpsell;
},
hasRebate:function(f) {
	var id = Product.getTempProductId(f);
	var r = Product.hasRebateAmt[id];
	return r;
},
hasPriceDrop:function(f) {
	var id = Product.getTempProductId(f);
	var hpd = Product.hasPriceDropValue[id];
	return hpd;
},
hasFreeShipping:function(f) {
	var id = Product.getTempProductId(f);
	var fs = Product.shippingInfo[id];
	if (fs.toLowerCase() == "free") {
		return true;
	}
	else {
		return false;
	}
},
setCapacities:function(cfa) {
	// Capacity comes in with MB,GB etc strip out these bytes and put a raw number into capacities
	var stripped = new String();
	var units = new String();
	var len;
	for(i=0;i<cfa.length;i++) {
		len = cfa[i].length;
		stripped = cfa[i].substring(0, len-2);
		stripped = stripped.replace(" ", "");
		units = cfa[i].substring(len-2,len);
		Product.capacities[i] = stripped;
	}
},
setCapacitiesFormatted:function(cf) {
	Product.capacitiesFormatted = cf;
},
setColors:function(c) {
	Product.colors = c;
},
setRetailPrices:function(rp) {
	Product.retailPrices = rp;
},
setUnitPrices:function(up) {
	Product.unitPrices = up;
},
setPriceDrop:function(pd) {
	Product.priceDrop = pd;
},
setHasPriceDrop:function(pd) {
	for(i=0;i<pd.length; i++) {
		if (pd[i] != 0) {
			Product.hasPriceDropValue[i] = true;
		}
		else {
			Product.hasPriceDropValue[i] = false;
		}
	}
},
setHasRebate:function(rebates) {
	for(i=0;i<rebates.length; i++) {
		if (rebates[i] > 0) {
			Product.hasRebateAmt[i] = true;
		}
		else {
			Product.hasRebateAmt[i] = false;
		}
	}
},
setMarketingColors:function(marketingColors) {
	Product.marketingColors = marketingColors;
},
setFeatureToIDMap:function(pids) {
	Product.featureToIDMap = pids;
},
setTempToRealIDMap:function(ids) {
	Product.tempToRealIDMap = ids;
},
setShipping:function(s) {
	Product.shippingInfo = s;
},
setAvailability:function(a) {
	Product.availability = a;
},
setRebateAmt:function(rebates) {
	Product.rebateAmt = rebates;
},
setRebateSavings:function(rebates) {
	Product.rebateSavings = rebates;
},
hasColors:function() {
	return Product.hasColorsOption;
},
hasMarketingColors:function() {
	return Product.hasMarketingColorsOption;
},

hasCapacity:function() {
	return Product.hasCapacityOption;
},
hasLength:function() {
	return Product.hasLengthOption;
},
hasRetailPrices:function() {
	return Product.hasRetailPricesShown;
}


}	






