var products = {
	cache : [],
	get : function(prod){
		if(products.cache[prod.id]==null) {
			this.load(prod.id,prod.family);	
		} else {
			this.loadCache(prod.id);
		}
	},
	load : function(id,ftype) {
		var _product;
		var thisClass = this;
		
		new Ajax.Request('product_data.aspx', {
			method: 'get',
			parameters: {
				prd_product_id : id,
				family : ftype
			},
			onSuccess: function(transport) {
				_product = new Product(ftype, transport.responseText.evalJSON());
				if(_product.ProductID!=null) thisClass.cache[_product.ProductID] = _product;
				thisClass.build(_product);
			}
		});
	},
	loadCache : function(id) {
		this.build(this.cache[id]);
	},
	build : function(obj) {
		obj.build()
	}
}

var Product = Class.create({
    _newBurstImage : '<img src="images/condensed/new_burst.png" width="69" height="69" alt="NEW" id="newburst" class="png" />',
    
	initialize : function(family, data) {
		if(data===undefined) return;
		this.family = family;
		var nutrition = data.nutrition.first();

		this.soupcan = data.soupcan;
		this.ProductID = data.id || null;
		this.ProductName1 = data.ProductName;
		this.ServingSize = data.ServingSize;
		this.NewBurst = data.NewBurst;
		
		this.Calories = nutrition.Calories;
		this.TotalFat = nutrition.TotalFat;
		this.TransFat = nutrition.TransFat;
		this.TotalCarbs = nutrition.TotalCarbs;
		this.SaturatedFat = nutrition.SaturatedFat;
		this.Cholesterol = nutrition.Cholesterol;
		this.Cholesterol2 = nutrition.Cholesterol2;
		this.Sodium = nutrition.Sodium;
		this.Calcium = nutrition.Calcium;
		this.VitaminA = nutrition.VitaminA;
		this.VitaminC = nutrition.VitaminC;
		this.Iron = nutrition.Iron;
		this.DietaryFiber = nutrition.DietaryFiber;
		this.Sugars = nutrition.Sugars;
		this.Protein = nutrition.Protein;
	},
	build : function() {
		for(prop in this) {
		    if($(prop)) {
				if($(prop).nodeName.toLowerCase()=="img") {
					$(prop).writeAttribute("src",eval("this." + prop));
				} else {
					$(prop).update(eval("this." + prop));
				}
			}
		}
		
		this.addBurst();
	},
	
	addBurst : function() {
	    if (this.family == "light") return; // "Light Soups" is handled by showing/hiding .Net PlaceHolder on condensed_soups_product_details.aspx
		
		if (this.NewBurst == "true")
		{
		    if ($('newburst')) $('newburst').remove();
		    $('nutrition-content-top').insert({before: this._newBurstImage});
		}
		else
		{
		    if ($('newburst') == null) return;
		    $('newburst').remove();
		}
	}
});

var callouts = {
	active : null,
	list : {
		home : ['promo-left-kitchen','promo-right-wellness'],
		all : ['promo-left-kitchen','promo-right-wellness'],
		fun_favorites : ['promo-left-labels','promo-right-myslurp'],
		cooking : ['promo-left-kitchen','promo-right-wellness'],
		family_favorites : ['promo-left-kitchen','promo-right-wellness'],
		classic_selections : ['promo-left-tasting','promo-right-wellness'],
		healthy_requests : ['promo-left-tasting','promo-right-wellness']
	},	
	load : function(category) {
		if(this.active==null) this.active = this.list['home'];
		var _category;
		switch(category) {
			case "fun_favorites" :
				_category = "fun_favorites";
				break;
			case "cooking" :
				_category = "cooking";
				break;
			case "favorites" :
			case "family_favorites" :
				_category = "family_favorites";
				break;
			case "classics" :
			case "classic_selections" :
				_category = "classic_selections";
				break;
			case "healthy" :
			case "healthy_requests" :
				_category = "healthy_requests";
				break;
			default:
				_category = "home";
				break;
		}
//		this.active.each(function(s) {
//			$(s).toggle();
//		})
//		this.list[_category].each(function(s) {
//			$(s).toggle();
//		})
		this.active = this.list[_category];
	},
	setActive : function(category) {
		this.active = this.list[category];
	}
}
function flashPageChange(page) {
	callouts.load(page);
}


// Thrown together
function show_text(t) {
	var text = $(t).select("img.text")[0];
	var leftOffset = ($(t).getWidth() - text.getWidth())/2;
	var topOffset =	($(t).getHeight() - text.getHeight())/2;
	text.setStyle({
		left: leftOffset + "px",
		top: topOffset + "px"
	});
	text.toggle();
}