﻿		// Define object arrays
		
		// Get by Id method for our arrays
		Array.prototype.getById = function(id)
		{
			for (var i=0; i < this.length; i++)
			{
				if (this[i].Id == id)
				{
					return this[i];
				}
			}
			return null;
		}
		
        var arrConventional = new Array();
        var arrAmounts = new Array();
        var arrSubstitutions = new Array();      
        
        
        // Conventional Class
        function ConventionalObj(id, text, value, image, description)
        {
            this.Id = id;
            this.Text = text;
            this.Value = value;
            this.Image = image;
            this.Description = description;
        }

        function parseConventionalDoc(xmlData)
        {
            jQuery(">root>item", xmlData).each(function(){
                    var convObj = new ConventionalObj(
                        jQuery(this).attr('id'),
                        jQuery("text", this).text(),
                        jQuery("value", this).text(),
                        jQuery("image", this).text(),
                        jQuery("description", this).text());
                    arrConventional.push(convObj);
                    jQuery("<option>").attr("value", convObj.Id).text(convObj.Text).appendTo("#conventional");
                });
             jQuery("#conventional").change(update);
        }
        
        // Amounts Class
        function AmountsObj(id, text, value)
        {
            this.Id = id;
            this.Text = text;
            this.Value = value;
        }

        function parseAmountsDoc(xmlData)
        {
            jQuery(">root>item", xmlData).each(function(){
                    var amtObj = new ConventionalObj(
                        jQuery(this).attr('id'),
                        jQuery("text", this).text(),
                        jQuery("value", this).text());
                    arrAmounts.push(amtObj);
                    jQuery("<option>").attr("value", amtObj.Id).text(amtObj.Text).appendTo("#amounts");
                });
            
            // Set default amount to 8oz            
			jQuery("#amounts").val("8");
			jQuery("#amounts").change(update);
        }

        // Substitution Class
        function SubstitutionObj(id, text, value, image)
        {
            this.Id = id;
            this.Text = text;
            this.Value = value;
            this.Image = image;
        }

        function parseSubstitutesDoc(xmlData)
        {
            jQuery(">root>item", xmlData).each(function(){
                    var substObj = new SubstitutionObj(
                        jQuery(this).attr('id'),
                        jQuery("text", this).text(),
                        jQuery("value", this).text(),
                        jQuery("image", this).text());
                    arrSubstitutions.push(substObj);
                    jQuery("<option>").attr("value", substObj.Id).text(substObj.Text).appendTo("#substitutes");
                });
                jQuery("#substitutes").change(update)
        }        
        
        jQuery(document).ready(function () 
        {        
			// Load Amounts XML
			jQuery.get("../assets/xml/substitution/amounts.xml", function(data){
				 parseAmountsDoc( data);
			},"xml");
	        
			// Load Amounts XML
			jQuery.get("../assets/xml/substitution/substitutes.xml", function(data){
				 parseSubstitutesDoc( data);
			},"xml");
	        
			// Load Conventional XML
			jQuery.get("../assets/xml/substitution/conventional.xml", function(data){
				 parseConventionalDoc( data);
			},"xml");	
						
        });
        
        
        // Caluculator functions
        function update(e)
        {
			// Get selected ids from dropdowns
			var cid = jQuery('#conventional option:selected').val();
			var aid = jQuery('#amounts option:selected').val();
			var sid = jQuery('#substitutes option:selected').val();   
			
			// Get objects matching id
			var cObj = arrConventional.getById(cid);
			var aObj = arrAmounts.getById(aid);
			var sObj = arrSubstitutions.getById(sid);
			
			// Calculate fat values
			
			// Fat values are per 8oz. serving in conventional list.  
			// Need to convert this to fat per gram, the multiply by selected amount
			var convFatPerServ = Math.round((cObj.Value / 227) * aObj.Value * 10) / 10;
			var substFatPerServ = Math.round((sObj.Value / 227) * aObj.Value * 10) / 10;
			
			var savingsInGrams = Math.round((convFatPerServ - substFatPerServ) * 10) / 10;			
			jQuery("#Span3").text(savingsInGrams);
			
			var savingsInPct = 100 - Math.round((substFatPerServ / convFatPerServ) * 100);
			jQuery("#Span4").text(savingsInPct);			
        
			// Set conventional image
			jQuery("#imgConventional").attr("src", '../assets/images/substitution/' + cObj.Image);	
			jQuery("#imgConventional").attr("alt",cObj.Name);
			
			// Set conventional product name
			jQuery("#Span1").text(cObj.Text);
			// Set conventional product fat per serving
			jQuery("#Span2").text(convFatPerServ);		
			
			// Set substitute image
			jQuery("#imgSubstitute").attr("src", '../assets/images/substitution/' + sObj.Image);
			jQuery("#imgSubstitute").attr("alt", sObj.Name);
			
			// Set substitute product name
			jQuery("#Span5").text(sObj.Text);			
			// Set substitute product fat per serving
			jQuery("#Span6").text(substFatPerServ);
			
			// Set description content
			jQuery("#Div1").html(cObj.Description);
			
			// Fix pngs in IE6
			if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) == 6) {
				if (jQuery('div.calResults ul li shape')[0]) { jQuery('div.calResults ul li shape').remove(); }
				DD_belatedPNG.fixPng(document.images['imgConventional']);
				DD_belatedPNG.fixPng(document.images['imgSubstitute']);
			}		
			
        }
