SubProductCollection.prototype.constructor = SubProductCollection;

function SubProductCollection () { // class constructor
	this.subproduct_array = new Array();
	this.currently_shown = 0;
} 
	
	SubProductCollection.prototype.setup = function (i) {
		this.addSubProducts();
		this.hideSubProducts();
		this.hideSubNames();
		this.showSubProduct(0); // show first one, initially
		this.buildRadioSelector();
	}
	
	SubProductCollection.prototype.hideSubProducts = function () {
			$("div.action form").hide();	
	}
	
	SubProductCollection.prototype.hideSubNames = function () {
			$("div.action form > h3").hide();	
	}
	
	SubProductCollection.prototype.showSubProduct = function (i) {
		$("form.order_form"+this.currently_shown).hide();
		$("form.order_form"+i).show();
		this.currently_shown = i;
	}
	
	SubProductCollection.prototype.buildRadioSelector = function () {
		// use names from subproduct array to build a radio button selector
		// bind switchSubProduct to this' events
		radioSelectorHTML = '<table class="radio">\n';
		
		for(i=0;this.subproduct_array[i];i++) {
		if (i == 0) { var selected = "checked"; } else { var selected = "";}
		radioSelectorHTML += '<tr valign="top">\n'
		+ '<td><input type="radio" name="select_product" value="'+i+'" onclick="subproductcollection.showSubProduct('+i+');" '+selected+'></td>\n'
		+ '<td>'+this.subproduct_array[i]+'</td>\n'
		+ '</tr>\n';
		}
		
		radioSelectorHTML += '</table>\n';
		
		$("div.action h2").after(radioSelectorHTML);
	}
	
	SubProductCollection.prototype.addSubProduct = function (i) { 
		// adds to array of subproducts
		// array has: subproduct name, html
		//alert(this.subproduct_array);
		this.subproduct_array[i] = $("form.order_form"+i+" > h3").html();
	}
	
	SubProductCollection.prototype.addSubProducts = function () { 
		// adds to array of subproducts
		// array has: subproduct name, html
		var self = this;
		$("div.action form").each(function (i) {self.addSubProduct(i)});
	}

var subproductcollection = new SubProductCollection();
$(document.body).ready(function() {
	subproductcollection.setup();
});