Ext.define("Ext.ux.stackorganizer.column.Properties", {

	extend: "Ext.grid.column.Column",

	alias: ["widget.stackpropertiescolumn"],

	config: {
		stack: false
	},

	initComponent: function () {

		var i_cmp = this;

		Ext.apply(i_cmp, {
			renderer: function (s_value, o_meta, i_record, n_row, n_col, i_store, i_view) {

				var s_id = i_record.internalId;
				var s_lang = ip2.session.getLanguage();
				var o_prop = i_record.get("properties") || {};
				var s_output;
				var a_output = [];

				// TOPLINK TITLE
				a_output.push((function () {

					var o_title = i_record.get("title");
					var o_placeholder = o_title;

					if (ip2.isStringedObject(o_title)) {
						o_title = Ext.decode(o_title);
					}
					else {

						o_placeholder = {};
						if (Ext.isObject(o_title)) {
							o_placeholder = o_title;
						}
						else {
							o_placeholder[s_lang] = o_title;
						}
					}

					if (!o_title) {

						o_placeholder = o_prop.title || o_prop.header || o_prop.name;

						if (ip2.isStringedObject(o_placeholder)) {
							o_placeholder = Ext.decode(o_placeholder);
						}
						else {
							if (!Ext.isObject(o_placeholder)) {
								var o_temp = {};
								o_temp[s_lang] = o_placeholder;
								o_placeholder = o_temp;
							}
						}
					}

					o_title = o_placeholder;

					return {
						id: "toplink",
						label: ip2.i18n({
							da: "Toplink",
							en: "Toplink"
						}),
						value: ip2.i18n(o_title)
					};
				})());

				// TITLE
				/*
					a_output.push((function () {

						var value = o_prop.title,
							s_value = value;

						if ( Ext.isObject(value) ) {
							s_value = value[s_lang];
						}

						return o_prop.title ? {
							id: "title",
							label: ip2.i18n({
								da: "Titel",
								en: "Title"
							}),
							value: s_value
						} : null
					})());
				*/

				// HEADER
				a_output.push((function () {

					return o_prop.header ? {
						id: "header",
						label: ip2.i18n({
							da: "Rubrik",
							en: "Header"
						}),
						value: o_prop.header
					} : null;
				})());

				// SUB HEADER
				/*
					a_output.push((function () {
						return o_prop.subheader ? {
							id: "subheader",
							label: ip2.i18n({
								da: "Subheader",
								en: "Subheader"
							}),
							value: o_prop.subheader
						} : null
					})());
				*/

				// LINK
				a_output.push((function () {
					return o_prop.url ? {
						id: "link",
						label: ip2.i18n({
							da: "Link",
							en: "Link"
						}),
						value: o_prop.url.slice(0, 30) + "..."
					} : null;
				})());

				// TYPE/ID
				a_output.push((function () {

					var o_xpd = i_view.up().supportXpd;
					var s_token = i_record.get("token");
					var o_collection;
					var s_type = "";

					if (o_xpd) {
						o_xpd = ip2.xpd.flatten(o_xpd);

						var n_id = i_record.get(o_xpd.idProperty);

						if (n_id) {
							o_collection = o_xpd.collections[n_id];
							s_type = o_collection ? ip2.xpd.translate(o_xpd, o_collection.title) : i_record.get("ptype");

							//s_type = ip2.xpd.translate(o_xpd, o_collection.title);
						}
					}
					else {
						s_type = i_record.get("ptype");
					}

					return {
						id: "id",
						label: ip2.i18n({
							da: "ID",
							en: "ID"
						}),
						value: [s_type, s_token].join(" / ")
					};
				})());

				s_output = ["<table class=\"x-ux-stack-table\">",
					"<tr><td valign=\"middle\">",
						"<div id=\"x-ux-stack-record-" + s_id + "\" class=\"x-ux-stack-cell\">"].join("");

				s_output += i_cmp.renderOutput(a_output);

				s_output = [s_output, "</div>",
					"</td></tr>",
				"</table>"].join("");

				return s_output;
			}
		});

		i_cmp.callParent();

		return i_cmp;
	},
	renderOutput: function (a_output) {

		var s_output = "";

		Ext.Array.each(a_output, function (o_output) {

			if (o_output) {

				s_output += [
					"<div class=\"x-ux-stack-cell-" + o_output.id + "\">",
						"<div class=\"x-ux-stack-label\">" + o_output.label + ": </div>",
						"<div class=\"x-ux-stack-value\">" + o_output.value + "</div>",
					"</div>"
				].join("");
			}
		});

		return s_output;
	}
});
