/**@license setplus: a lightweight HTML editor
 * @author  Levente Hunyadi
 * @version 0.2.0
 * @remarks Copyright (C) 2010-2011 Levente Hunyadi
 * @remarks Licensed under GNU/GPLv3, see http://www.gnu.org/licenses/gpl-3.0.html
 * @see     http://hunyadi.info.hu/projects/setplus
 **/

/*
* setplus: a lightweight HTML editor
* Copyright 2010-2011 Levente Hunyadi
*
* setplus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* setplus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with setplus.  If not, see <http://www.gnu.org/licenses/>.
*/

(function () {
	function _class(cls) {
		return 'setplus-linkproperties-' + cls;
	}

	setplus.LinkPropertiesDialog = new Class({
		'Extends': setplus.EditorDialog,
		
		//_el: null,
		//_controls: null,

		'initialize': function () {
			var self = this;
			self.parent();

			self._title.adopt(
				new Element('span', {
					'class': _class('title')
				})
			);

			var protocol, url, target, title, rel;
			var rows = [
				[
					new Element('span', {
						'class': _class('url')
					}),
					[  // many elements into the same cell
						protocol = new Element('select').adopt(
							['','http://','https://','ftp://','mailto:'].map(function (protocol) {
								return new Element('option', {
									'value': protocol,
									'html': protocol
								});
							})
						),
						url = new Element('input', {
							'type': 'text'
						})
					]
				],
				[
					new Element('span', {
						'class': _class('target')
					}),
					target = new Element('select').adopt(
						['','self','blank','parent','top'].map(function (target) {
							return new Element('option', {
								'class': target ? _class(target) : '',
								'value': target ? '_' + target : '',
								'html': target
							});
						})
					)
				],
				[
					new Element('span', {
						'class': _class('tooltip')
					}),
					title = new Element('input', {
						'type': 'text'
					})
				],
				[
					new Element('span', {
						'class': _class('rel')
					}),
					rel = new Element('input', {
						'type': 'text'
					})
				]
			];

			// maps HTML <a> properties to form controls
			self._controls = {
				'href': url,
				'target': target,
				'title': title,
				'rel': rel
			};

			self._content.adopt(
				new Element('table').adopt(
					rows.map(function (cells) {
						return new Element('tr').adopt(
							cells.map(function (contents) {
								return new Element('td').adopt(contents);
							})
						)
					})
				),
				new Element('button', {
					'type': 'button',
					'class': 'setplus-ok',
					'events': {
						'click': self.invoke.bind(self, function (editor) {
							// insert link
							var values = Object.filter({
								'href': protocol.get('value') + url.get('value'),
								'target': target.get('value'),
								'title': title.get('value'),
								'rel': rel.get('value')
							}, function (value) {
								return value;
							});
							if (Object.getLength(values) > 0) {
								editor.link(self._el ? self._el.set(values) : values);
							}

							// close pop-up window
							self.hide();
						})
					}
				})
			);
		},

		/**
		* Assigns values to form controls based on element properties.
		*/
		'assign': function (elem) {
			var self = this;
			
			// clear current values
			self._content.getElements('input[type=text],select').set('value', '');

			// set values based on element passed in
			self._el = elem;
			if (elem) {
				// clone and store element
				self._el = elem.clone(true,true);  // copy element identifier

				Object.each(self._controls, function (ctrl, key) {
					ctrl.set('value', elem.getProperty(key));
				});
			}
		}
	});
})();