// ******************************************************************
// LocalizedJigsawRenderer.js
//
// Version 1.0
// Copyright (c) 2007 Catalog Data Solutions. All Rights Reserved.
//
// Renderer for jigsaw style configurators with all labels being
// localized through the localResources object.  All attribute rows
// will be generated at runtime.
//
// NOTE: We might want to entityify the labels before displaying
//       them?
// ******************************************************************

cds.namespace("configurator");

cds.configurator.LocalizedJigsawRenderer = function() {
	this.styleContainer = "attributeContainer";
	this.styleContainerRequired = "attributeContainerRequired";
	this.styleHeader = "attributeHeader";
	this.styleHeaderUnselected = "attributeHeaderUnselected";
	this.styleSetDefault = "avSetValue";
	this.styleSetStandard = "avSetStandard";
	this.styleSetReadOnly = "avSetReadOnly";
	this.styleSetMouseOver = "avSetValueHovered";
	this.styleSetSelected = "avSetValueSelected";
	this.styleSetMouseConflict = "avSetValueHoverEventConstrained";
	this.styleSetConflict = "avSetValueConstrained";
	this.styleRangeDefault = "avRangeValue";
	this.styleRangeMouseConflict = "avRangeValueHoverEventConstrained";
	this.styleRangeConflict = "avRangeValueConstrained";
	this.styleAttributeSubtext = "attributeSubtext";

	this.render = function(obj) {
		if (obj.id == null) return;

		// for range and text fields, the id to render is id_value, not id
		var id = (obj.classId == "cds.configurator.RangeAttribute" || obj.classId == "cds.configurator.TextAttribute") ? (obj.id + "_value") : obj.id;

		var e = document.getElementById(id);
		if (e == null) return;
		e.manager = this.manager;
		e.controller = obj;
		if (!obj.isReadOnly) {
			e.onmouseover = function() { this.manager.handleEvent(this.controller, "mouseover"); }
			e.onmouseout = function() { this.manager.handleEvent(this.controller, "mouseout"); }
		}

		switch (obj.classId) {
			case "cds.configurator.SetAttribute":
				var atd = document.getElementById(obj.id);
				if (atd != null) {
					var table = document.createElement("table");
					atd.appendChild(table);
					table.className = (obj.isRequired) ? this.styleContainerRequired : this.styleContainer;
					var tbody = document.createElement("tbody");
					table.appendChild(tbody);
					var tr = document.createElement("tr");
					tbody.appendChild(tr);
					var td = document.createElement("td");
					tr.appendChild(td);
					td.className = (!obj.isRequired || obj.hasSelection()) ? this.styleHeader : this.styleHeaderUnselected;
					td.setAttribute("id", obj.id + "_header");
                    var headerText = this.getLabel(obj);
					if (obj.subtextHtml != null) {
                        headerText += '<br /><span class="' + this.styleAttributeSubtext + '">';
                        headerText += obj.subtextHtml;
                        headerText += "</span>";
					}
                    td.innerHTML = headerText;
					var cols = (obj.displayColumns != null && obj.displayColumns > 1) ? obj.displayColumns : 1;

					// display only values which do not have suppressSelect
					var ovlist = new Array();
					for (var i = 0; i < obj.values.length; i++) {
						if (!obj.values[i].suppressSelect) ovlist[ovlist.length] = obj.values[i];
					}

					var rows = Math.ceil(ovlist.length / cols);
					if (cols > 1) {
                        td.colSpan = cols;
                    }
					for (var i = 0; i < rows; i++) {
						tr = document.createElement("tr");
						tbody.appendChild(tr);
						for (var j = 0; j < cols; j++) {
							var v = ovlist[i + j * rows];
							if (v != null && !v.suppressSelect) {
								td = document.createElement("td");
								tr.appendChild(td);
								td.className = (v.isReadOnly) ? this.styleSetReadOnly : this.styleSetDefault;
								if (v.isStandard) {
									td.className += " " + this.styleSetStandard;
								}
								td.setAttribute("id", v.id);
								td.innerHTML = this.getLabel(v);
								this.render(v);
							}
						}
					}
				}
				break;
			case "cds.configurator.SetAttributeValue":
				if (!obj.isReadOnly) e.onclick = function() { this.manager.handleEvent(this.controller, "click"); }
				break;
			case "cds.configurator.RangeAttribute":
			case "cds.configurator.TextAttribute":
				e.onfocus = function() { this.select(); }
				e.onblur = function() { this.manager.handleEvent(this.controller, "change"); }
				break;
			default:
		}
		this.display(obj);
	}

	this.getLabel = function(obj) {
        var l;

        if (typeof localeResources !== "undefined") {
		    l = localeResources[obj.id];
        }
        if (!l) {
            l = obj.label;
        }
		return l;
	}

	this.display = function(obj, event) {
		if (obj == null || obj.id == null) return;
		var e = document.getElementById(obj.id);
		if (e == null) return;

		switch (obj.classId) {
			case "cds.configurator.SetAttribute":
				break;
			case "cds.configurator.SetAttributeValue":
				if (obj.isReadOnly) e.className = this.styleSetReadOnly;
				else if (obj.constraints.isMouseEventConstrained()) e.className = this.styleSetMouseConflict;
				else if (obj.constraints.isConstrained()) e.className = this.styleSetConflict;
				else if (obj.isHovered) e.className = this.styleSetMouseOver;
				else if (obj.isSelected) e.className = this.styleSetSelected;
				else e.className = this.styleSetDefault;
				if (obj.isStandard) {
					e.className += " " + this.styleSetStandard;
				}

				// set attribute background
				var eh = document.getElementById(obj.attribute.id + "_header");
				if (eh != null) eh.className = (!obj.attribute.isRequired || obj.attribute.hasSelection()) ? this.styleHeader : this.styleHeaderUnselected;

				break;

			case "cds.configurator.RangeAttribute":
			case "cds.configurator.TextAttribute":
				var ev = document.getElementById(obj.id + "_value");
				if (ev != null) ev.value = (value != null) ? value : "";
				break;
			default:
		}
	}

	this.destroy = function() {
		this.manager = null;
	}

	this.toString = function() {
		return "cds.configurator.LocalizedJigsawRenderer";
	}
}


