/**@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-searchreplace-' + cls;
	}

	setplus.SearchReplaceDialog = new Class({
		'Extends': setplus.EditorDialog,

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

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

			var search, replace, casesensitive, forward, backward;
			var rows = [
				[
					new Element('span', {
						'class': _class('search')
					}),
					search = new Element('input', {
						'type': 'text'
					})
				],
				[
					new Element('span', {
						'class': _class('replace')
					}),
					replace = new Element('input', {
						'type': 'text'
					})
				]
			];

			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('div').adopt(
					casesensitive = new Element('input', {
						'type': 'checkbox'
					}),
					new Element('span', {
						'class': _class('casesensitive')
					})
				),
				new Element('div').adopt(
					wholeword = new Element('input', {
						'type': 'checkbox'
					}),
					new Element('span', {
						'class': _class('wholeword')
					})
				),
				new Element('div').adopt(
					forward = new Element('input', {
						'name': 'direction',
						'type': 'radio',
						'checked': true
					}),
					new Element('span', {
						'class': _class('forward')
					}),
					backward = new Element('input', {
						'name': 'direction',
						'type': 'radio'
					}),
					new Element('span', {
						'class': _class('backward')
					})
				),
				new Element('button', {
					'type': 'button',
					'class': 'setplus-find',
					'events': {
						'click': self.invoke.bind(self, function (editor) {
							var str = search.get('value');
							if (str) {
								editor.find(str, casesensitive.get('checked'), backward.get('checked'), wholeword.get('checked'));
							}
						})
					}
				}),
				new Element('button', {
					'type': 'button',
					'class': 'setplus-replace',
					'events': {
						'click': self.invoke.bind(self, function (editor) {
							var str = search.get('value');
							if (str) {
								editor.replace(str, replace.get('value'), casesensitive.get('checked'), backward.get('checked'), wholeword.get('checked'));
							}
						})
					}
				})
			);
		}
	});
})();