/**
 * LightEditor
 * v1.0
 *
 **/

var LightEditor={
	editorBorder  : 'solid 1px gray',
	editorBg      : 'white',

	toolbarHeight : 24,
	toolbarBtnPadd: 3,
	toolbarBgColor: '#efefef',
	toolbarIconExt: 'gif',
	toolbarHasSize: true,

	editors : new Array(),
	content : new Array(),
	buttons : new Array(
		'bold','italic','underline',
		'justifyleft','justifycenter','justifyright',
		'insertorderedlist','insertunorderedlist',
		'outdent','indent',
		'undo'
	),
	captions : new Array(
		'félkövér','dőlt','aláhúzás',
		'balra igazítás','középre igazítás','jobba iazítás',
		'számozott lista','számozatlan lista',
		'kisebb margó','behúzás',
		'mégse'
	),

	init : function(){
		var txtArea=document.getElementsByTagName('textarea');
		for(var i=0;i<txtArea.length;i++)
			if(txtArea[i].getAttribute('replace')=='lighteditor')
				this.editors[this.editors.length]=txtArea[i].id;

		var editor,iView;
		for(var i=0;i<this.editors.length;i++){
			txtArea=document.getElementById(this.editors[i]);
			editor=this.createEditor(txtArea);
			txtArea.parentNode.replaceChild(editor,txtArea);

			iView=document.getElementById(this.editors[i]);
			iView.contentWindow.document.write(txtArea.value);
			iView.contentWindow.document.close();
			iView.contentWindow.document.designMode="on";

			this.prepareForm(this.findForm(editor));
		}
	},

	onsubmit : function(frm){
		var hidden;
		for(var i=0;i<this.editors.length;i++){
			hidden=document.getElementById('content_'+this.editors[i]);
			hidden.value=document.getElementById(this.editors[i]).contentWindow.document.body.innerHTML;
		}
	},

	doEvent : function(btn){
		var id=btn.getAttribute('id').substr(4);
		var iframes=btn.parentNode.parentNode.getElementsByTagName('iframe');
		var iView=iframes[0];

		iView.contentWindow.document.execCommand(id, false, null);
	},

	select : function(select){
		var cursel = select.selectedIndex;
		var iframes=select.parentNode.parentNode.getElementsByTagName('iframe');
		var iView=iframes[0];
		if (cursel != 0) {
			var selected = select.options[cursel].value;
			iView.contentWindow.document.execCommand(select.getAttribute('command'), false, selected);
			select.selectedIndex = 0;
		}
		iView.contentWindow.focus();
	},

	prepareForm : function(frm){
		if(!frm||(frm.getAttribute('prepared')=='ok')) return false;
		var oldQueue = frm.onsubmit? frm.onsubmit: function() {};
	    frm.onsubmit = function() {
	    	eval("LightEditor.onsubmit(this)");
	        oldQueue();
	    }
	    frm.setAttribute('prepared','ok');
		return true;
	},

	findForm : function(elm){
		if(!elm) return false;
		return (elm.tagName.toLowerCase()=='form')?elm:this.findForm(elm.parentNode);
	},

	createEditor : function(txtArea){
		var editor,toolbar,iView,hidden;

		if(this.isIE()){
			editor=document.createElement('<div class="lighteditor">');
			toolbar=document.createElement('<div class="toolbar">');
			iView=document.createElement('<iframe id="'+txtArea.getAttribute('id')+'">');
			hidden=document.createElement('<input type="hidden" name="'+txtArea.name+'" id="content_'+txtArea.getAttribute('id')+'" value="'+txtArea.value+'" />');
		} else {
			editor=document.createElement('div');
			editor.setAttribute('class','lighteditor');
			toolbar=document.createElement('div');
			toolbar.setAttribute('class','toolbar');
			iView=document.createElement('iframe');
			iView.setAttribute('id',txtArea.getAttribute('id'));
			hidden=document.createElement('input');
			hidden.setAttribute('type','hidden');
			hidden.setAttribute('name',txtArea.name);
			hidden.setAttribute('id','content_'+txtArea.getAttribute('id'));
			hidden.setAttribute('value',txtArea.value);
		}

		editor.style.width=txtArea.offsetWidth+'px';
		editor.style.height=(txtArea.offsetHeight+this.toolbarHeight)+'px';
		editor.style.border=this.editorBorder;
		editor.style.background=this.editorBg;

		iView.style.position='relative';
		iView.style.width=(txtArea.offsetWidth-1)+'px';
		iView.style.height=txtArea.offsetHeight+'px';
		iView.style.top='0px';
		iView.style.left='0px';
		iView.style.border='0px';
		iView.style.background='none';

		editor.appendChild(toolbar);
		editor.appendChild(iView);
		editor.appendChild(hidden);

		this.createToolBar(toolbar);

		return editor;
	},

	createToolBar : function(toolbar){
		var btn,btnImage;

		toolbar.style.position='relative';
		toolbar.style.width='100%';
		toolbar.style.height=this.toolbarHeight+'px';
		toolbar.style.top='0px';
		toolbar.style.left='0px';
		toolbar.style.background=this.toolbarBgColor;

		if(this.toolbarHasSize){
			var selSize;
			if(this.isIE()){
				selSize=document.createElement('<select onchange="LightEditor.select(this)" command="fontsize">');
			} else {
				selSize=document.createElement('select');
				selSize.setAttribute('onchange','LightEditor.select(this)');
				selSize.setAttribute('command','fontsize');
			}

			selSize.options[0]=new Option('méret',0);
			for(var i=1;i<7;i++)
				selSize.options[i] = new Option(i,i);

			selSize.style.margin='2px';
			selSize.style.height=(this.toolbarHeight-4)+'px';
			selSize.style.fontSize='10px';
			selSize.style.textAlign='center';
			if(this.isIE()) selSize.style.styleFloat='left';
			else selSize.style.cssFloat='left';

			toolbar.appendChild(selSize);
		}

		for(var i=0;i<this.buttons.length;i++){
			if(this.isIE()){
				btn=document.createElement('<div id="btn_'+this.buttons[i]+'" class="button">');
				btnImage=document.createElement('<img src="images/lighteditor/'+this.buttons[i]+'.'+this.toolbarIconExt+'" alt="'+this.buttons[i]+'" title="'+this.captions[i]+'" />');
			} else {
				btn=document.createElement('div');
				btn.setAttribute('id','btn_'+this.buttons[i]);
				btn.setAttribute('class','button');

				btnImage=document.createElement('img');
				btnImage.setAttribute('src','images/lighteditor/'+this.buttons[i]+'.'+this.toolbarIconExt);
				btnImage.setAttribute('alt',this.buttons[i]);
				btnImage.setAttribute('title',this.captions[i]);
			}

			btnImage.style.height=(this.toolbarHeight-(2*this.toolbarBtnPadd)-2)+'px';

			btn.style.width=(this.isIE())?(this.toolbarHeight-2)+'px':(this.toolbarHeight-(2*this.toolbarBtnPadd)-2)+'px';
			btn.style.border='solid 1px '+this.toolbarBgColor;
			btn.style.padding=this.toolbarBtnPadd+'px';
			btn.style.cursor=(this.isIE())?'hand':'pointer';
			btn.style.overflow='hidden';
			if(this.isIE()) btn.style.styleFloat='left';
			else btn.style.cssFloat='left';
			btn.onmouseover=function(){ this.style.border='solid 1px blue'; }
			btn.onmouseout=function(){ this.style.border='solid 1px '+LightEditor.toolbarBgColor; }
			btn.onmousedown=function(e){
				var evt = e ? e : window.event;

				this.style.border="inset 1px";
				if(evt.returnValue) evt.returnValue = false;
				else if(evt.preventDefault) evt.preventDefault( );
				else return false;
			}
			btn.onmouseup=function(){ this.style.border='solid 1px blue'; }
			btn.onclick=function(){ LightEditor.doEvent(this); }

			btn.appendChild(btnImage);
			toolbar.appendChild(btn);
		}
	},

	isIE : function(){
		return (this.strpos(navigator.userAgent.toLowerCase(),"msie",0)>0);
	},

	strpos : function( haystack, needle, offset){
	    var i = haystack.indexOf( needle, offset ); // returns -1
	    return i >= 0 ? i : false;
	},

	addInit : function(){
	    var oldQueue = window.onload? window.onload: function() {};
	    window.onload = function() {
	    	eval("LightEditor.init()");
	        oldQueue();
	    }
	}
}

LightEditor.addInit();
