
var Customizer = {
	categories: {},
	partNumberBits: null,
	partNumberBase: null,
	images: {},
	currentImage: null,
	addOption: function(cat, key, name, selected) {
		if (!this.categories[cat]) this.categories[cat] = new CustomizerCategory(cat);
		return this.categories[cat].addOption(key, name, selected);
	},
	get: function(cat) {
		return this.categories[cat];
	},
	// takes an array of category keys and returns the selected options keys for each category (as a single string)
	getBitsString: function(bits) {
		var t = "";
		bits.each(function(key) {
			if (this.categories[key].selected) t += this.categories[key].selected.key;
		}.bind(this));
		return t;
	},
	updatePartNumber: function() {
		if (this.partNumberBase != null && this.partNumberBits != null) {
			var partNum = this.partNumberBase + this.getBitsString(this.partNumberBits);
			// update up to 5 part number displays
			(5).times(function(n) {
				var elm = $("part_number_"+n);
				if (elm) elm.innerHTML = partNum;
				document.getElementById('mn').value = partNum;
			});
		}
	},
	setupPartNumber: function(base, bits) {
		this.partNumberBase = base;
		this.partNumberBits = bits;
		this.updatePartNumber();
	},
	addImage: function(key, base, bits, suffix) {
		this.images[key] = new CustomizerImages(base, bits, suffix);
	},
	setImageSet: function(key) {
		this.currentImage = key;
		this.updateImage();
	},
	updateImage: function() {
		if (this.images[this.currentImage]) {
			var imageSet = this.images[this.currentImage];
			var imgSrc = imageSet.imageBase + '_' + this.currentImage + '_' + this.getBitsString(imageSet.imageBits) + imageSet.imageSuffix;
			$("primary_image_preview").src = imgSrc;
			document.getElementById('in').value = imgSrc;
			//alert(imgSrc);
		}
	},
	getOptionDisplayName: function(name) {
		return name + ' &nbsp;<img src="template/en/images/arrow_selector_lg.gif" width="4" height="6" align="absmiddle" border="0">';
	},
	downloadButton: function() {
		if (this.partNumberBase != null && this.partNumberBits != null) {
			var partNum = this.partNumberBase + this.getBitsString(this.partNumberBits);
			alert("(Prototype Only) Downloading for " + partNum);
		}
	},
	printButton: function() {
		window.print();
	},
	toString: function() {
		var n = 0;
		for (var a in this.categories) n++;
		return "[object Customizer: " + n + " categories]";
	}
};


/* ====================================
class CustomizerImages - tracks a set
of images
==================================== */
var CustomizerImages = Class.create();
CustomizerImages.prototype = {
	initialize: function(base, bits, suffix) {
		this.imageBase = base;
		this.imageBits = bits;
		this.imageSuffix = suffix;
	}
}


/* ====================================
class CustomizerCategory - used to
store manage a category
==================================== */
var CustomizerCategory = Class.create();
CustomizerCategory.OPTION_ONCLICK = function() {
	Customizer.optionClicked(this);
};
CustomizerCategory.prototype = {
	initialize: function(cat) {
		this.options = new Object();
		this.selected = null;
		this.cat = cat;
		this.menu = "div_menu_" + cat;
		this.display = "div_display_" + cat;
	},
	addOption: function(key, name, selected) {
		this.options[key] = new CustomizerOption(this.cat, key, name);
		var cat = this.cat;
		// add to DOM
		var a = document.createElement("A");
		a.href = "javascript:;";
		a.innerHTML = Customizer.getOptionDisplayName(name);
		a.id = this.options[key].tagId;
		a.onclick = function() {
			Customizer.get(cat).select(key);
		};
		$(this.menu).appendChild(a);
		if (selected) this.select(key);
		// return new object
		return this.options[key];
	},
	select: function(key) {
		try {
			this.unselect();
			this.selected = this.options[key];
			$(this.selected.tagId).className = "selectedOption";
			$(this.display).innerHTML = this.selected.name;
			Customizer.updatePartNumber();
			Customizer.updateImage();
			if (typeof(Customizer.onChange) == "function") Customizer.onChange(key);
		} catch (e) {
			alert("Unable to select for " + this.toString());
		}
	},
	unselect: function() {
		if (this.selected == null) return;
		$(this.selected.tagId).className = "";
	},
	toString: function() {
		var n = 0;
		for (var a in this.options) n++;
		return "[object CustomizerCategory:" + this.cat + "]";
	}
}


/* ====================================
class CustomizerOption - used to
store manage a category
==================================== */
var CustomizerOption = Class.create();
CustomizerOption.prototype = {
	initialize: function(cat, key, name) {
		this.key = key;
		this.name = name;
		this.tagId = "div_generated_customizeroption_" + cat + "_" + key + "_" + Math.floor(Math.random() * 999999);
	}
}

