
/**
 * WYSIWYG Editor
 * 
 * Web-Dynamic Fork
 * 
 * @author PCSG - Henning
 * @package com.pcsg.ptools.editor
 * 
 * @version 1.0
 */

if (typeof _peditor == 'undefined') {
	var _peditor = {};
	_peditor._active = null;
};

if (typeof _ptools == 'undefined') {
	var _ptools = {};
};

_ptools.Editor = function( settings )
{
	var t = this;
	var System = _ptools._System;
	var Path   = System.getAttribute('path');
	
	var attributes = settings ? settings : {};
	
	var iFrame         = null;
	var EditorBody     = null;
	var EditorDocument = null;
	var textarea       = null;
	var cssfiles       = null;
	
	t.obj = null;
	
	t.type    = '_ptools::Editor';
	t.range   = null;
	t.Toolbar = [];
	t.Buttons = [];
	
	// Get Attribute
	t.getAttribute = function( name ) 
	{
		if (attributes[ name ]) {
			return attributes[ name ];
		};
		
		return false;
	};
	
	// Set Attribute
	t.setAttribute = function(name, value)
	{
		attributes[ name ] = value;
	};
	
	t.asyncPost = function(php, func, params) 
	{
		var _ajax_post_async = new _ptools.Ajax({
			'host' : System.getAttribute('host'),
			'name' : '_ajax_post_async',
			'request_url' : Path +'editor/ajax.php',
			type : 'POST'
		});
		
		if (params)
		{
			_ajax_post_async.request(php, func, params);
		} else
		{
			_ajax_post_async.request(php, func);
		};
	};
	
	t.syncPost = function(php, params)
	{
		var _ajax_post_sync = new _ptools.Ajax({
			host : System.getAttribute('host'),
			name : '_ajax_post_sync',
			request_url : Path +'editor/ajax.php',
			type : 'POST'
		});
		
		var r = '';
		
		if (params)
		{
			var r = _ajax_post_sync.request(php, '', params);
		} else
		{
			var r = _ajax_post_sync.request(php);
		};
		
		_ajax_post_sync = null;
		delete _ajax_post_sync;
		
		return r;
	};
	
	// Standard Settings
	if (t.getAttribute('content_name') == false) {
		t.setAttribute('content_name', 'content');
	};
	
	if (t.getAttribute('bodyclass') == false) {
		t.setAttribute('bodyclass', 'content');
	};
	
	if (t.getAttribute('name') == false) {
		t.setAttribute('name', new Date().getMilliseconds());
	};
	
	t.setAttribute('toolbar', 'Toolbar');
	t.setAttribute('version', '2.0');
	
	// Load Settings from Ajax
	if (t.getAttribute('loadWithAjax') == true)
	{
		_attributes = t.syncPost('ajax_load_editor', {
			name    : t.getAttribute('content_name'),
			project : t.getAttribute('project')
		});	
		
		for (i in _attributes) {
			t.setAttribute(i, _attributes[i]);
		};
	};
	
	// Create Editor
	t.create = function() 
	{
		var width  = t.getAttribute('width');
		var height = t.getAttribute('height');
		
		t.obj = document.createElement('div');
		t.obj.style.width  = width +'px';
		t.obj.style.height = height +'px';
		
		t.Toolbar = new _ptools.Toolbar({
			name   : 'pEditorToolbar',
			width  : width,
			slide  : false,
			Editor : t
		});
		
		iFrame = document.createElement('iframe');
		iFrame.frameBorder  = 1;
		iFrame.border       = 1;
		iFrame.src          = System.getAttribute('host') + Path + 'editor/editor.php?editor='+ t.getAttribute('name');
		iFrame.height       = height-20;
		iFrame.style.height = height-20 +'px';
		iFrame.style.width  = width +'px';
		iFrame.width        = width;
		iFrame.style.border = '1px solid #494949';
		
		if (t.getAttribute('nodelist')) 
		{
			var oNodes = document.createElement('div');
			oNodes.id  = 'editor_nodelist';
		};
		
		t.obj.appendChild( t.Toolbar.create() );
		t.obj.appendChild( iFrame );
		
		if (t.getAttribute('nodelist')) {
			t.obj.appendChild( oNodes );
		};
		
		_peditor[ t.getAttribute('name') ] = t;
		_peditor._active = t;
		
		return t.obj;
	};
	
	// Load Editor from Frame
	t.load = function()
	{
		// CSS Dateien
		var config = t.getAttribute('config');
		
		if (iFrame)
		{
			t.designmode();
			
			EditorDocument = iFrame.contentWindow.document;
			EditorBody = EditorDocument.body;
			
			if(t.getAttribute('bodyid')) {
				EditorBody.id = t.getAttribute('bodyid');
			};
			
			if(t.getAttribute('bodyclass')) {
				EditorBody.className = t.getAttribute('bodyclass');
			};
			
			t.onload();
			t.focus();
		};
		
		// Eventhandling
		if (window.addEventListener)
		{ // Mozilla, Netscape, Firefox
			EditorDocument.addEventListener('mouseup', t.mouseup, false);
			EditorDocument.addEventListener('keyup', t.keyup, false);
			EditorDocument.addEventListener('keydown', t.keydown, false);
		} else 
		{ // IE
			EditorDocument.attachEvent('onmouseup', t.mouseup);
			EditorDocument.attachEvent('onkeyup', t.keyup);
			EditorDocument.attachEvent('keydown', t.keydown);
		};
		
		//t.getRange().collapse( true );
		if (!_ptools._Browser.isMSIE)
		{
			setTimeout(function () {
				t.getDocument().execCommand('SelectAll', false, null);
				t.getRange().collapse(true);
				t.getWin().focus();
			}, 100);
		};
	};
	
	// onload funktion
	t.onload = function()
	{
		t.loadPlugins( false );
		
		var _plg = _peditor.plugins.getPlugins();
		
		for (var i = 0, len = _plg.length; i < len; i++)
		{
			if(typeof _plg[i].onLoad == 'function') {
				_plg[i].onLoad(t);
			};
		};
		
		if (typeof t.getAttribute('onload') == 'function') {
			t.getAttribute('onload')(t);
		};
	};
	
	// designmode aktivieren
	t.designmode = function()
	{
		var frameDoc = iFrame.contentWindow.document;
		
		try 
		{
			frameDoc.body.onblur = function(){ _peditor._active.onblur() }; 
			frameDoc.body.contentEditable = "True"; // **** IE
			frameDoc.designMode = "On";
			
			//this.obj.contentWindow.document.execCommand("undo", false, null);
			
		}  catch (e) 
		{
			alert("Ihr Browser unterstützt leider kein WYSIWYG Editor "+e);
			return false;
		};
		
		var cssfiles = t.getAttribute('cssfiles');
		var header   = frameDoc.getElementsByTagName("head");
		
		if (header != null && header[0] && cssfiles && cssfiles.length)
		{
			// CSS einbinden
			for (var i = 0, len = cssfiles.length; i < len; i++)
			{
				var elm  = frameDoc.createElement("link");
	
				elm.rel  = "stylesheet";
				elm.href = cssfiles[i];
				
				header[0].appendChild( elm );
			};
		};
		
		if (header != null && header[0])
		{
			var elm  = frameDoc.createElement("link");
			elm.rel  = "stylesheet";
			elm.href = Path +'editor/editor.css';
			header[0].appendChild( elm );
		};
	};
	
	// textarea setzen um post zu setzen
	t.setForm = function()
	{
		if (!textarea)
		{
			textarea = document.createElement('textarea');
			textarea.id = 'editor_content';
			textarea.style.position = 'absolute';
			textarea.style.top = '-1000px';
			textarea.style.left = '-1000px';
			textarea.setAttribute('name', t.getAttribute('content_name'), 1);
			
			t.obj.appendChild(textarea);
		};
		
		textarea.value = t.getContent();
	};
	
	// plugins laden
	t.loadPlugins = function( str )
	{
		t.asyncPost('ajax_get_editor_buttons', function(params, AJAX) 
		{
			// '_Editor.loadPlugins'
			var _name = params.ename; 
			
			if (!_name) {
				return false;
			};
			
			if (!_peditor[_name]) {
				return false;
			};
			
			var Editor  = _peditor[_name];
			var Toolbar = Editor.Toolbar;
			var buttons = params.buttons;
			var files   = params.files;
			
			var System = _ptools._System;
			
			for (var f = 0, len = files.length; f < len; f++) {
				System.loadScript( files[f] );
			};
			
			for (var i = 0, len = buttons.length; i < len; i++)
			{
				var btn = eval(buttons[i]);
				
				if (typeof btn == 'undefined') {
					continue;
				};
				
				//btn.setAttribute('Editor', t);
				
				// Einzelner Button
				if (btn.type == '_ptools::Button') 
				{
					if(btn.getAttribute('wysiwyg') == false) {
						continue;
					};
					
					Editor.setCollector(btn);
					
					btn.setAttribute('Editor', t);
					Toolbar.appendChild( btn );
					
					continue;
				};
				
				if (btn.type == '_ptools::Select') 
				{
					if (btn.getAttribute('wysiwyg') == false) {
						continue;
					};
					
					btn.setAttribute('Editor', t);
					var _options = _Button.getChildren();
					
					for (var o = 0, olen = _options.length; o < olen; o++) {
						Editor.setCollector( _options[o] );
					};
					
					Toolbar.appendChild( btn );
					
					continue;
				};
				
				// ToolbarBlock
				if (btn.type == '_ptools::ToolbarBlock') 
				{
					Toolbar.appendChild( btn );
					
					var _btns = btn.getChildren();
					var _w = 0;
					
					for (var b = 0, btnlength = _btns.length; b < btnlength; b++)
					{
						var _Button = _btns[b];
						_Button.setAttribute('Editor', t);
						
						if (_Button.getAttribute('wysiwyg') == false || 
							_Button.getAttribute('name') == false) 
						{
							//_w = _w+_Button.obj.offsetWidth+5;
							_w = _w+3;
						};
						
						if (_Button.type == '_ptools::Button') 
						{
							Editor.setCollector( _Button );
							//_w = _w+_Button.obj.offsetWidth+8;
							_w = _w+3;
						};
						
						if (_Button.type == '_ptools::Select') 
						{
							var _options = _Button.getChildren();
							
							for(var o = 0, olen = _options.length; o < olen; o++) {
								Editor.setCollector( _options[o] );
							};
							
							//_w = _w+_Button.obj.offsetWidth+10;
							_w = _w+5;
						};
						
						_w = _w+_Button.obj.offsetWidth+11;
					};
					
					btn.setAttribute('width', _w+15);
				};
			};
			
			var h  = Toolbar.obj.offsetHeight;
			var ih = iFrame.offsetHeight;
			
			iFrame.style.height = (ih-h) +'px';
		}, {
			ename : t.getAttribute('name')
		});
	};
	
	t.setCollector = function(btn) 
	{	
		var Collector = _peditor.nodeCollector;
		var wysiwyg   = btn.getAttribute('wysiwyg');
		
		if (wysiwyg['active'] && wysiwyg['active']['node'])
		{
			activenode = wysiwyg['active']['node'];
			
			for(c = 0, lenc = activenode.length; c < lenc; c++)
			{
				Collector.onNodeSetActive( activenode[c], btn );
			};
		};
		
		if (wysiwyg['normal'] && wysiwyg['normal']['node'])
		{
			normalnode = wysiwyg['normal']['node'];
			
			for (c = 0, lenc = normalnode.length; c < lenc; c++) {
				Collector.onNodeSetNormal( normalnode[c], btn );
			};
		};
		
		if (wysiwyg['style'] &&  wysiwyg['style']['active'])
		{
			var style = wysiwyg['style']['active'];
			
			for (attribute in style)
			{
				if (typeof style[attribute] == 'string')
				{
					Collector.onStyleSetActive(attribute, style[attribute], btn );
				} else if (typeof style[attribute] == 'object')
				{
					for (c = 0, lenc = style[attribute].length; c < lenc; c++) {
						Collector.onStyleSetActive(attribute, style[attribute][c], btn );
					};
				};
			};
		};
	};
	
	t.focus = function()
	{
		t.range = null;
		t.getWin().focus();
		
		_peditor._active = t;
	};
	
	t.getBody = function()
	{
		return EditorBody;
	};
	
	t.getDocument = function()
	{
		return EditorDocument;
	};
	
	t.getWin = function()
	{
		return iFrame.contentWindow;
	};
	
	t.getSelection = function()
	{
		if (typeof t.getWin().getSelection == 'function') {
			return t.getWin().getSelection();
		};
		
		return t.getDocument().selection;
	};
	
	t.getHTMLSelection = function()
	{
		if (typeof XMLSerializer != 'undefined')
		{
			var rng    = this.getRange();
			var xmlSel = new XMLSerializer();
			return xmlSel.serializeToString(rng.cloneContents());
		};
		
		// IE
		var Sel = t.getSelection();
		
		if (typeof Sel.createRange == 'undefined') {
			return '';
		};
		
		var Range = Sel.createRange();
		
		// Falls ein Bild selektiert wurde
		if (Range.length == 1 && Range(0).tagName == 'IMG') {
			return Range(0).outerHTML;
		};
		
		if (Range.htmlText) {
			return Range.htmlText;
		};
		
		return '';
	};
	
	t.getRange = function()
	{
		var sel = t.getSelection();
		
		if (sel && sel.rangeCount > 0) {
			return sel.getRangeAt(0)
		}
		
		rng = sel.rangeCount > 0 ? sel.getRangeAt(0) : (sel.createRange ? sel.createRange() : t.getDocument().createRange());
		
		if (rng) {
			return rng;
		};
		
		if (_ptools._Browser.isMSIE)
		{
			rng = t.getBody().createTextRange();
		} else
		{
			rng = t.getDocument().createRange();
		};
		
		return rng;
	};
	
	t.selectNode = function( node )
	{
		var range = t.getRange();
		
		if (range.moveToElementText)
		{
			var range = t.getRange();
			range.moveToElementText(node);
			range.select();

		} else
		{
			var range = t.getRange();
			range.selectNode( node );
		}; 
	};
	
	t.onblur = function()
	{
		//t.range = t.getRange();
	};
	
	t.refreshToolbar = function()
	{
		t.nodes = new Array();
		t.nodes.push(t.getActiveNode());
		t.recursivParentsNodes( t.getActiveNode() );
		
		var n = t.nodes.reverse();
		_peditor.nodeCollector.exec( n );
	};
	
	t.mouseup = function( event )
	{
		t.refreshToolbar();
	};
	
	t.keyup = function( event )
	{
		var key = (event.charCode) ? event.charCode :((event.which)? event.which : event.keyCode);
		
		if (key != 37 &&
			key != 38 &&
			key != 39 &&
			key != 40)
		{
			return true;
		};
		
		t.refreshToolbar();
		
		return false;
	};
	
	t.keydown = function( event )
	{
		var key = (event.charCode) ? event.charCode :((event.which)? event.which : event.keyCode);
		
		if(event.which == 86 && event.ctrlKey) 
		{	
			// Wenn es kein Word Paste Plugin gibt
			if(typeof _peditor.plugins.pcsg_plugins_paste == 'undefined')
			{
				return true;
			} else
			{
				_peditor.plugins.pcsg_plugins_paste.pasteword();
			};
			
			// Event Cancel
			if(typeof event.preventDefault == 'function')
			{
				event.preventDefault();
				event.stopPropagation(); 
			} else
			{
				event.returnValue = false;
			};
			
			return false; 
		};
		
		if(event.which == 13 && event.ctrlKey)
		// Enter und CTRL in Tabelle fügt eine Zeile nach der Tabelle ein
		{
			var oTable = t.getParentElm('TABLE', t.getActiveNode());
			
			if(oTable)
			{
				var oP = t.getDocument().createElement('p');	
					oP.innerHTML = '&nbsp;';		
				oTable.parentNode.insertBefore( oP, oTable );
			};
			
			return;
		};
		
		if (event.which == 13 && event.altKey)
		// Enter und ALT in Tabelle fügt eine Zeile nach der Tabelle ein
		{
			var oTable = t.getParentElm('TABLE', t.getActiveNode());
			
			if(oTable)
			{
				var oP = t.getDocument().createElement('p');	
					oP.innerHTML = '&nbsp;';
						
				oSibling = oTable.nextSibling;
				
				if(!oSibling)
				{
					oTable.parentNode.appendChild( oP );
					return;
				};

				oSibling.parentNode.insertBefore( oP, oSibling );
			};
			
			return;	
		};
		
		if (event.which == 9)
		// Tabs einfügen
		{
			t.insertAtSelection('&nbsp;&nbsp;&nbsp;&nbsp;');
			
			if (event.preventDefault) 
			{
				event.preventDefault();
			} else 
			{
				event.returnValue = false;
			};
			
			return false;
		};
		
		//$('test').innerHTML = event.which;
	};
	
	t.recursivParentsNodes = function( node )
	{
		if (node.parentNode && node.nodeName != 'BODY')
		{
			t.nodes.push( node.parentNode );
			t.recursivParentsNodes(node.parentNode);
		};
	};
	
	t.repaint = function()
	{
		if (_ptools._Browser.isGecko) 
		{
			try 
			{
				sel = _Editor.getSelection();
				
				if (sel) {
					sel.selectAllChildren(_Editor.getBody());
				};
				
				t.getRange().collapse(true);
			} catch (e) 
			{
				// Ignore
			};
		};
	};
};

// execcommand ausführen
_ptools.Editor.prototype.Command = function( com )
{
	this.focus();
	
	if (com && com.type == '_ptools::Button')
	{
		if (eval('typeof '+com.settings.exec) == 'function')
		{
			var self = this;
			eval(com.settings.exec+'(self)');
		};
	} else
	{
		this.getDocument().execCommand( com, false, null );
	};
	
	/*
	if(range.select)
	{
		range.select();
	} else
	{
		if(sel) 
		{
			sel.removeAllRanges();
			sel.addRange(range);
		};
	};
	*/
	return true;
};
	
// Aktives Node bekommen
_ptools.Editor.prototype.getActiveNode = function()
{
	var elm, range = this.getRange(), sel = this.getSelection();
	
	if (_ptools._Browser.isMSIE) {
		return range.item ? range.item(0) : range.parentElement();
	};
	
	if(!range) {
		return this.getBody();
	};
	
	elm = range.commonAncestorContainer;
	
	if (!range.collapsed) 
	{
		if (range.startContainer == range.endContainer) 
		{
			if (range.startOffset - range.endOffset < 2) 
			{
				if (range.startContainer.hasChildNodes()) {
					elm = range.startContainer.childNodes[range.startOffset];
				};
			};
		};
	};

	if (elm.nodeType == 1) {
		return elm;
	};

	return elm.parentNode;
};

_ptools.Editor.prototype.getParentElm = function( search, startnode )
{
	//var oNode = this.getActiveNode();
	if (!startnode) {
		return false;
	};
	
	while (startnode)
	{
		if (startnode.nodeName == search)
		{
			return startnode;
			break;
		} else
		{
			startnode = startnode.parentNode;
		};
		
		if (!startnode || startnode.nodeName == 'BODY')
		{
			return false;
			break;
		};
	};
	
};

// inhalt vom Editor bekommen
_ptools.Editor.prototype.getContent = function()
{
	var html = this.syncPost('ajax_editor_cleanup_html', {
		html : this.getBody().innerHTML
	});

	return html;
};

// Inhalt vom Editor setzen
_ptools.Editor.prototype.setContent = function( html )
{
	if (!_ptools._System.empty(html))
	{
		var html = this.syncPost('ajax_editor_create_html', {
			html : html
		});
	
		this.getBody().innerHTML = html;
	} else
	{
		this.getBody().innerHTML = '&nbsp;';
	};
	
	this.focus();
};
	
	
// Setzt einen String oder DOM Node an die Stelle des Cursors oder der Selektion
_ptools.Editor.prototype.insertAtSelection = function( html )
{
	var doc   = this.getDocument();
	var range = this.getRange();
	
	if (_ptools._Browser.isMSIE && this.range) {
		range = this.range;
	}; 

	if (_ptools._Browser.isMSIE && range == null) {
		range = this.getSelection().createRange();
	};
	
	if (typeof html == 'object')
	{
		var oDiv = document.createElement('div');
		oDiv.appendChild(html);
		
		html = oDiv.innerHTML;
	};
	
	if (range.insertNode) 
	{
		if (doc.queryCommandEnabled('insertHTML'))
		{
			doc.execCommand('insertHTML', false, html);
		} else
		{
			range.deleteContents();
			range.insertNode(this.getRange().createContextualFragment(html));
		};
		
	} else 
	{
		this.focus();
		
		if (range.item)
		{
			range.item(0).outerHTML = html;
		} else
		{
			// IE
			range.pasteHTML(html);
		};
	};
};

_ptools.Editor.prototype.insert = function(range, html)
{
	var doc = this.getDocument();
	
	if (typeof html == 'object')
	{
		var oDiv = document.createElement('div');
		oDiv.appendChild(html);
		
		html = oDiv.innerHTML;
	};
	
	if (range.insertNode) 
	{
		if (doc.queryCommandEnabled('insertHTML'))
		{
			doc.execCommand('insertHTML', false, html);
		} else
		{
			range.deleteContents();
			range.insertNode(this.getRange().createContextualFragment(html));
		};
		
	} else 
	{
		this.focus();
		
		if (range.item)
		{
			range.item(0).outerHTML = html;
		} else
		{
			range.pasteHTML(html);
		};
	};
};


/**
 * PLUGINS
 */
_peditor.plugins = 
{
	plugins : new Array(),
	
	appendChild : function( n, p ) 
	{
		if(typeof p.onLoad == 'function') {
			p.onLoad(_peditor._active);
		};

		eval('this.'+n+' = p');
		
		this.plugins.push( p );
	},
	
	getPlugins : function() 
	{
		return this.plugins;
	}
};

/**
 * Node Collector
 */

_peditor.nodeCollector = 
{		
	_nodes  : {active : [], inactive : [], normal : []},
	_styles : {active : [], inactive : [], normal : []},
	
	_active : [],
	_normal : [],
	_inactive : [],
	
	_nodeTranslation : {
		'BODY':'Inhalt',
		'P':'Paragraf',
		'TABLE':'Tabelle',
		'TBODY':'Kopf',
		'TR':'Zeile',
		'TD':'Zelle',
		'OL':'Liste',
		'UL':'Liste',
		'LI':'Eintrag',
		'U':'unterstrichen',
		'I':'kursiv',
		'STRIKE':'durchgestrichen',
		'STRONG':'fett',
		'SUB':'hochgestellt',
		'SUP':'tiefgestellt',
		'SPAN':'Text Element',
		'H1':'&Uuml;berschrift 1',
		'H2':'&Uuml;berschrift 2',
		'H3':'&Uuml;berschrift 3',
		'H4':'&Uuml;berschrift 4',
		'H5':'&Uuml;berschrift 5',
		'H6':'&Uuml;berschrift 6',
		'IMG':'Bild',
		'BLOCKQUOTE':'Zitat',
		'ADDRESS':'Adresse'
	},
	
	/**
	 * Setzt einen Button Active wenn das Node aktiv ist
	 *
	 * @param unknown_type $node
	 * @param unknown_type $button
	 */
	onNodeSetActive : function( node, _Button )
	{
		if (!this._nodes.active[node]) {
			this._nodes.active[node] = new Array()
		};
		
		this._nodes.active[node].push(_Button);
	},
	
	/**
	 * Setzt einen Button InActive wenn das Node aktiv ist
	 *
	 * @param unknown_type $node
	 * @param unknown_type $button
	 */
	onNodeSetInActive : function( node, _Button )
	{
		if (!this._nodes.inactive[node]) {
			this._nodes.inactive[node] = [];
		};
		
		this._nodes.inactive[node].push(_Button);
	},
	
	/**
	 * Setzt einen Button Normal wenn das Node aktiv ist
	 *
	 * @param String $node
	 * @param Button $button
	 */
	onNodeSetNormal : function( node, _Button )
	{
		if (!this._nodes.normal[node]) {
			this._nodes.normal[node] = [];
		};
		
		this._nodes.normal[node].push(_Button);
	},
	
	/**
	 * Setzt einen Button Active wenn das Node aktiv ist
	 *
	 * @param unknown_type $node
	 * @param unknown_type $button
	 */
	onStyleSetActive : function( attribute, value, _Button )
	{
		if (!this._styles.active[attribute]) {
			this._styles.active[attribute] = [];
		};
		
		if (value)
		{
			if (!this._styles.active[attribute][value]) {
				this._styles.active[attribute][value] = [];
			};
			
			this._styles.active[attribute][value].push(_Button);
		} else
		{
			if (!this._styles.active[attribute]['_active_']) {
				this._styles.active[attribute]['_active_'] = [];
			};
			
			this._styles.active[attribute]['_active_'].push(_Button);
		};
	},
	
	/**
	 * Setzt einen Button InActive wenn das Node aktiv ist
	 *
	 * @param unknown_type $node
	 * @param unknown_type $button
	 */
	onStyleSetInActive : function( attribute, value, _Button )
	{
		if (!this._styles.active[attribute]) {
			this._styles.active[attribute] = [];
		};
				
		this._styles.inactive[attribute].push(_Button);
	},
	
	/**
	 * Setzt einen Button Normal wenn das Node aktiv ist
	 *
	 * @param String $node
	 * @param Button $button
	 */
	onStyleSetNormal : function( attribute, value, _Button)
	{
		if (!this._styles.active[attribute]) {
			this._styles.active[attribute] = [];
		};
		
		this._styles.normal[attribute].push(_Button);
	},
	
	/**
	 * Führt das Aktivieren / Deaktivieren aus
	 */
	exec : function(nodes)
	{
		var oNodeList = document.getElementById('editor_nodelist');
		
		// Toolbar zurück setzen
		for (var i = 0, len = this._active.length; i < len; i++) {
			this._active[i].setNormal();
		};
		
		for (var i = 0, len = this._normal.length; i < len; i++) {
			this._normal[i].setDisable();
		};
		
		this._active = [];
		this._normal = [];
		
		if (oNodeList) {
			oNodeList.innerHTML = '';
		};
		
		var _active = this._styles.active;
		
		for (var n = 0, len_node = nodes.length; n < len_node; n++)
		{
			var node = nodes[n];
			
			if (typeof node.nodeName == 'undefined') {
				continue;
			};
			
			var nodeName = node.nodeName;
			
			if (oNodeList)
			{
				
				var text = nodeName;
				
				if (this._nodeTranslation[nodeName]) {
					text = this._nodeTranslation[nodeName];
				};
				
				oNodeList.appendChild( 
					new _peditor.CollectorSpan({
						text : text,
						node : node
					}).create() 
				);
				
			};
			
			if (this._nodes.active[nodeName])
			{
				var active = this._nodes.active[nodeName];

				for (var i = 0, len = active.length; i < len; i++) 
				{
					active[i].setActive();
					this._active.push(active[i]);
				};
			};
			
			if (this._nodes.normal[nodeName])
			{
				var normal = this._nodes.normal[nodeName];

				for (var i = 0, len = normal.length; i < len; i++) 
				{
					normal[i].setEnable();
					this._normal.push(normal[i]);
				};
			};
			
			// Styles
			
			for (_attribute in _active)
			{
				if (node.style && node.style[_attribute] && _active[_attribute][ node.style[_attribute] ])
				{
					var _arr = _active[_attribute][ node.style[_attribute] ];
					
					for (var i = 0, len = _arr.length; i < len; i++)
					{
						_arr[i].setActive();
						this._active.push(_arr[i]);
					};
				};
				
				if (_attribute == '_active_')
				{
					for(var i = 0, len = _attribute['_active_'].length; i < len; i++)
					{
						_attribute['_active_'][i].setActive();
						this._active.push(_arr[i]);
					};
				};
			};
		};
	}
};

_peditor.CollectorSpan = function( settings )
{
	var t = this;
	var settings = settings;
	
	t.obj  = null;
	t.node = settings.node;
	
	t.old_bgc = '';
	t.active  = 0;
	
	t.create = function()
	{
		t.obj = document.createElement('span');
		t.obj.className = '_peditorNode';
		
		t.obj.onmouseover = function()
		{
			t.onmouseover();
		};
		
		t.obj.onmouseout = function()
		{
			t.onmouseout();
		};
		
		t.obj.onclick = function()
		{
			t.onclick();
		};
		
		t.obj.innerHTML = settings.text;
		
		return t.obj; 
	};
	
	t.onmouseover = function()
	{
		var oNode = t.node;
		
		if (oNode.style && oNode.style.backgroundColor) {
			t.old_bgc = oNode.style.backgroundColor;
		};
		
		t.old_border = oNode.style.backgroundColor;
		oNode.style.backgroundColor = '#CCFF80';
	};
	
	t.onmouseout = function()
	{
		t.node.style.backgroundColor = t.old_bgc;
	};
	
	t.onclick = function()
	{
		// nothing
	};
};

	
