/*
	Copyright (c) 2006-2007, ePages GmbH
	All Rights Reserved.

	epages.cartridges.de_epages.product.widget.VariationDefaultList $Revision: 1.7 $

*/
dojo.provide("epages.cartridges.de_epages.product.widget.VariationDefaultList");
dojo.require("epages.widget.LocalizedWidget");
dojo.require("epages.widget.FormElement");
dojo.require("epages.widget.ContentList");

dojo.declare("epages.cartridges.de_epages.product.widget.VariationDefaultList",
	[epages.widget.LocalizedWidget],
	{
		/**
		 * public properties
		 */
		attributeAlias: "",
		nextButtonId:   "",
		inputName:      "DefaultAttributeValue",

		/**
		 * private properties
		 */
		_values:         undefined,
		_widgetsOfClass: [], // list of widgets (used by all widgets of this class)

		/**
		 * widget properties
		 */
		templatePath:    dojo.moduleUrl('epages.cartridges.de_epages.product.widget', "templates/VariationDefaultList.html"),
		translationName: dojo.moduleUrl('epages.cartridges.de_epages.product.widget', 'templates/translation'),

		templateTable: ['<table class="ContentList BottomMargin">'+
											'<tbody>','</tbody>'+ // 0+1
										'</table>' // 1
									],
		templateRow: ['<tr','>'+ // 0+1
									'<td class="Checkbox"><input class="jsInput ep-js" data-js="ep.uiInput()" type="radio" name="','" value="','" /></td><td>','</td>'+ // 1+2+3+4
									'</tr>' // 4
									],

		postMixInProperties: function() {
			this.inherited("postMixInProperties", arguments);
			this._values = [];
			this._widgetsOfClass.push(this);
		},

		postCreate: function() {
			this.inherited("postCreate", arguments);
			var node = this.targetNode;
			this.connect(node,'onclick', '_onListEvent');
		},

		addValue: function (value) {
			this._values.push(value);
		},

		showTable: function () {
			// **** build table ****
			var template = this.templateTable;
			var rows = [template[0]];

			// build tbody (rows)
			template=this.templateRow;
			var isAlternate = true;
			dojo.forEach(this._values, function (value) {
				isAlternate = !isAlternate;
				rows.push(template[0] + (isAlternate ? ' class="alternate" ' : '') + template[1]+this.inputName+template[2]+value+template[3]+value+template[4]);
			}, this);
			rows.push(this.templateTable[1]);
			this.targetNode.innerHTML = rows.join("\n");
			dojo.parser.parse(this.targetNode);
		},

		_onListEvent: function (evt) {
			if (evt.target.nodeName != 'INPUT'){
				return;
			}
			this.enableNextButton();
		},

		getInputs: function () {
			return dojo.filter(dojo.query('.jsInput', this.domNode), function (el) { return el.nodeName == 'INPUT'; });
		},

		isValid: function () {
			var radios = dojo.filter(this.getInputs(), function (el) { return el.checked; });
			return radios.length > 0;
		},

		getValue: function () {
			var radios = dojo.filter(this.getInputs(), function (el) { console.warn(el.checked); return el.checked; });
			if (radios.length == 0){
				return undefined;
			}
			return radios[0].value;
		},

		selectFirst: function () {
			var radios = this.getInputs();
			if (radios.length > 0){
				radios[0].checked = true;
			}
		},

		areAllValid: function () {
			var allWidgets = epages.cartridges.de_epages.product.widget.VariationDefaultList.prototype._widgetsOfClass;
			if (allWidgets.length == 0){
				return true;
			}
			var validWidgets = dojo.filter(allWidgets, function (el) { return el.isValid(); });
			return allWidgets.length == validWidgets.length;
		},

		enableNextButton: function () {
			if (this.nextButtonId == ''){
				return;
			}
			var valid = this.areAllValid();
			var b = $$(this.nextButtonId);

			if (b === undefined){
				$(this.nextButtonId).disabled=!valid;
			}else if (valid){
				b.enable();
			}else{
				b.disable();
			}
		}

});