//-------------SETUP PAGE----------------

//CSS
//var css_path = "HTML_editor.css"; //default the css near the .js file
//document.getElementsByTagName("HEAD")[0].innerHTML += '<link href="'+css_path+'" rel="stylesheet" type="text/css" />';
//EDITOR MENU BAR

var udt_editor = new Object;
var udt_editor_custom = new Object;
//alert(udt_editor.lang)
udt_editor.lang='en'
//BASE VARIABLE
//udt_editor.layout='/js/html_editor/layout/editor_layout_' + udt_editor.lang + '.html'
udt_editor.layout_target='UDTEditorLayout'
udt_editor.global_image='/picture_manager/show_all/categories';
udt_editor.global_target='SectionProfile';
var editor_on = 0 // enable Editor status -> 0 : disabled ; 1 : enabled
var selected = ''; // Selected Item.
var popup_win_state = 0; // layer_site_window display status -> 1 : inline ; 0 : none
var any_change = 0; // page change status -> 0 : no change ; 1 : any change
var enable_editor_button;

function init_udt_editor(){
//udt_editor.layout_loaded=ajax_object
udt_editor.save_info=document.getElementById('SAVE-INFO')
udt_editor.source = document.getElementById('HTML-EDITOR-SOURCE')
//udt_editor.inline = document.getElementById('HTML-EDITOR-INLINE')
udt_editor.panel = document.getElementById('HTML-EDITOR-PANEL')
udt_editor.panel_option = document.getElementById('HTML-OPTION-PANEL')
udt_editor.panel_option_title=udt_editor.panel_option.getElementsByTagName('H4')[0]
	//alert(udt_editor.panel_option_title.innerHTML)
udt_editor.option = document.getElementById('HTML-EDITOR-OPTION')
udt_editor.pointer = document.getElementById('HTML-EDITOR-POINTER')
udt_editor.panel_image = document.getElementById('HTML-IMAGE-PANEL')
udt_editor.panel_image_title = document.getElementById('HTML-IMAGE-PANEL-TITLE').getElementsByTagName('H4')[0]
udt_editor.image_folder_container = document.getElementById('HTML-IMAGE-FOLDER')
init_editor_image_folder()

udt_editor.msword_source = document.getElementById('HTML-MSWORD-Source')
udt_editor.panel_insert =  document.getElementById('HTML-INSERT-PANEL')
udt_editor.panel_msword =  document.getElementById('HTML-MSWORD-PANEL')
udt_editor.node_class_select =  document.getElementById('NODE-CLASS-SELECT')
udt_editor.node_text_insert =  document.getElementById('NODE-TEXT-INSERT')
enable_udt_editor(enable_editor_button)
}



function init_editor_image_folder() {
var xhtml=''
var text='<a href="' + udt_editor.image_folder_path + '" id="img_[folder_name]" ><img src="'+udt_editor.image_icon_path+'" title="[folder_title]" alt="[folder_title]"/></a>'
	
udt_editor.image_folder_container.innerHTML
//text=replace(text,'[image_folder_path]',udt_editor.image_folder_path)
//text=replace(text,'[image_icon_path]',udt_editor.image_icon_path)
	//alert(text)
var f_name, f_title, temp
var myArray=udt_editor.image_folder_set.split("}");
for ( i = 0; i < myArray.length; i++) {
	f_name=myArray[i].split("{")[0]
	if (f_name.length!=0)	{
	f_name=f_name.replace(/\,/g,"");
	f_name=f_name.replace(/^\s+|\s+$/g,"");
	f_title=myArray[i].split("{")[1]
	temp=text
	temp=replace(temp,'[folder_name]',f_name)
	temp=replace(temp,'[folder_title]',f_title)
	
		xhtml+= temp
	}
}
udt_editor.image_folder_container.innerHTML=xhtml
}


function request_img_folder(e,target) {
var targ=get_event_target(e)
if (targ.tagName=='IMG'){
	if (isObject(document.getElementById(target))){
		var text=udt_editor.panel_image_title.innerHTML
			text=text.split(':')[0]
		udt_editor.panel_image_title.innerHTML=text + ': ' + targ.title
		target_object=document.getElementById(target)
		return request_content(targ.parentNode,'target_object')
		}
	else{
		return true
	}
	}
return false
}

function editor_show_image(e,target,modify_url){
var targ=get_event_target(e)
if (targ.tagName=='IMG'){
if (isObject(document.getElementById(target))){
udt_editor.image_preview=document.getElementById(target)
	var xhtml='<div class="image_list_header">'
	xhtml +='<a class="exit" title="exit" href="javascript:;" onclick="close_image_preview()"><em>exit</em></a>'
	xhtml +='<a class="modify" title="modify" href="' + modify_url + '/' + targ.alt + '" onclick="return request_content(this,\'image-container\')"><em>modify</em></a>'
	xhtml +='<h4>' + targ.title + '</h4></div>'
	xhtml +='<div id="image-container" class="ajax_content"><img onclick="editor_insert_image(this);" style="max-width:90%" src="' + targ.src + '" title="' + targ.title + '"/><span>Click image to insert!</span></div>'
	document.getElementById(target).innerHTML=xhtml
}
}
}

function table_of_contents(target,preview) {
var ob=document.getElementById(target)
var xhtml=''
	var allObj = ob.childNodes;
  	for(i=0;i<allObj.length;i++){
    	if (allObj[i].tagName == 'H2'){xhtml += '<br/><b> • ' + allObj[i].innerHTML + ': </b>';}
		else if (allObj[i].tagName == 'H3'){xhtml += ' ' + allObj[i].innerHTML + ' | ';}
	}
xhtml=xhtml.substring(5, xhtml.length)
document.getElementById(preview).innerHTML=xhtml
document.getElementById(preview).contentEditable='true'	
}
//----------------EXCEPTION TAGS,IDs(forbidden to select and change, deletable when save)----------------

//var exception_tags = new Array("BODY","INPUT","FORM","BUTTON","HTML","TABLE");
//var exception_id = new Array("html_editor_header", "html_editor_title");
//var deletable_id = new Array("html_editor_header","layer_site_window");
function get_HTMLtoXML(text) {
text=HTMLtoXML(text)
	text = text.replace(/%5B/g,"[") 
	text = text.replace(/%5D/g,"]")

var currentURL = window.location;
var out = currentURL.protocol + '//' + currentURL.hostname

text = replace(text,out,'')
text = replace(text,'&nbsp;','')
//alert ( currentURL.href );     // Displays 'http://www.example.com:80/example.cgi?x=3&y=4#results'
//alert ( currentURL.protocol ); // Displays 'http:'
//alert ( currentURL.host );     // Displays 'www.example.com:80'
//alert ( currentURL.hostname ); // Displays 'www.example.com'

return text;
}

function show_hide_object(objname) {
var ob=document.getElementById(objname)
if (ob.style.display!='block'){
ob.style.display='block'
}
else {
ob.style.display='none'
}
}


function get_xhtml(html_div,html_input) {
	var ob=document.getElementById(html_input)
	ob.style.display='block'
	ob.value=get_HTMLtoXML(document.getElementById(html_div).innerHTML)
	orderXhtml(ob)


}

function get_editor_source(ob) {
	udt_editor.source.value=get_HTMLtoXML(ob.innerHTML);
	orderXhtml(udt_editor.source)
	document.getElementById('NODE-SOURCE').style.display='block'
}

function orderXhtml(ob) {

	var curtop=ob.scrollTop
	var current_pre = ob.value

	current_pre = current_pre.replace(/\n\n\n/g,"\n\n")
	current_pre = current_pre.replace(/\r/g,"")
	current_pre = current_pre.replace(/<\/a/g,"#</a")
	current_pre = current_pre.replace(/<span/g,"#<span")
	current_pre = current_pre.replace(/<\/span>/g,"</span>#")
		current_pre = current_pre.replace(/<strong/g,"#<strong")
	current_pre = current_pre.replace(/<\/strong>/g,"</strong>#")
	current_pre = current_pre.replace(/<em/g,"#<em")
	current_pre = current_pre.replace(/<\/em>/g,"</em>#")
	current_pre = current_pre.replace(/<li>/g,"<li>#")
	current_pre = current_pre.replace(/<\/li>/g,"#</li>")
	
	current_pre = current_pre.replace(/></g,">\n<") 
	current_pre = current_pre.replace(/#</g,"<")
		current_pre = current_pre.replace(/>#/g,">")
	//current_pre = current_pre.replace(/>/g,">\n")
	//current_pre = current_pre.replace(/\n\n/g,"\n")

	ob.value= current_pre
	ob.scrollTop=curtop;
}


function xxxxinsertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
}
else {
myField.value += myValue;
}

}
// calling the function


function insertAtCursor(myValue) {
var myField=udt_editor.source
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = '<' + myValue + '>' + sel.text + '</' + myValue + '>';
}

//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
if (endPos!=startPos){
	myField.value =  myField.value.substring(0, startPos) + '<' + myValue + '>' +  myField.value.substring(startPos, endPos) + '</' + myValue + '>' + myField.value.substring(endPos, myField.value.length);
}
}
else {
//myField.value += myValue;
}
selected.innerHTML=myField.value
}

function check_document_selection() {
	//var str =''

if (window.getSelection) {
		
    var str = window.getSelection();
	//if (selection.rangeCount > 0) {
		//var range = selection.getRangeAt(0);
		//alert(str.toString())
		if (str.toString().length>0){ return true}
	
  } 
else if (document.selection && document.selection.createRange().text) {
	var range = document.selection.createRange();
    //var str = range.text;
	if (range.text.length>0){return true}
  }
//else if (document.selection) { return true}
	
  return false
}


function clear_document_selection() {
//if (window.getSelection() && window.getSelection().getRangeAt(0)) { window.getSelection().removeAllRanges()}
//else if (document.selection && document.selection.createRange().text) { document.selection.empty()}

}

function selection_create_link(newNode,selection) {
	var reply_href = prompt("Edit link url:", selection)
	if (reply_href=='' || reply_href==null){reply_href='#'}
	var reply_title = prompt("Edit link text:", 'read more')
	if (reply_title=='' || reply_title==null){reply_title='read more'}
newNode.href=reply_href
newNode.title=reply_href
newNode.innerHTML=reply_title
	var answer = confirm("Do you want to create this link?\n" + reply_title  + ":\n" + reply_href)
	
return answer
}

function selection_create_list(newNode,selection) {
var xhtml=selection.toString()
xhtml=xhtml.replace(/\n\n/g,"</li><li>")
xhtml=xhtml.replace(/\n/g,"</li><li>")
xhtml=replace(xhtml,'<li></li>','')
newNode.innerHTML="<li>" + xhtml + "</li>"
	var answer = confirm("Do you want to create list?")
	
return answer
}

function document_selection_create(node_name) {
if (check_document_selection())	{
	
var newNode = document.createElement(node_name);
var do_insert=false
//moz
if (window.getSelection) {
	var selection = window.getSelection();
		if (selection.rangeCount > 0) {
			var range = selection.getRangeAt(0);
			if (node_name=='a')	{do_insert=selection_create_link(newNode,selection)}
			else if (node_name=='ul'){do_insert=selection_create_list(newNode,selection)}
			else {newNode.innerHTML=selection;do_insert=true}
			if (do_insert){			
				range.deleteContents();
				range.insertNode(newNode);
				//show_node_form(newNode)
			}
		}
   		
}
//ie
else if (document.selection && document.selection.createRange) {
			/*var textRange = document.selection.createRange();
                textRange.collapse(true);
				newNode.innerHTML='teszt';
                textRange.pasteHTML(newNode.outerHTML);*/

	var textRange = document.selection.createRange();
//alert(textRange.text)
  //              textRange.collapse(true);
			if (node_name=='a')	{do_insert=selection_create_link(newNode,textRange.text)}
			else if (node_name=='ul'){do_insert=selection_create_list(newNode,textRange.text)}
			else {newNode.innerHTML=textRange.text;do_insert=true}
            if (do_insert){	textRange.pasteHTML(newNode.outerHTML);}
				//show_node_form(newNode)
} 
}
if (isObject(udt_editor.node_text_insert)){udt_editor.node_text_insert.style.display='none'}
}

function get_options_value(string, key) {
	var keyindex = string.indexOf(String(key))
	if(keyindex != -1) {
		var startindex = string.indexOf(":",keyindex)+1;
		var lastindex = string.indexOf(",",startindex);
		var value = string.substring(startindex,lastindex);
		return value;
	}
	else {return false}     
}

//---------------Comment Editor--------------
function create_xhtml_from_text(xhtml) {
	//alert("create_xhtml_from_text")
if (xhtml==''){xhtml="<br/>"}
xhtml=xhtml.replace(/\n\n/g,"</p><p>")
xhtml=xhtml.replace(/\n/g,"<br/>")
xhtml=replace(xhtml,'<p></p>','')
xhtml= '<p>' + xhtml + '</p>'
return xhtml
}

function init_comment_editor(target) {
var comm_editor=''
comm_editor +='<img src="/js/html_editor/icons/insert_p.png" onclick="insert_p(\'commentField\');" title="Insert paragraph" />'
comm_editor +='<img src="/js/html_editor/icons/text-bold.png" onclick="document_selection_create(\'strong\');update_textarea(\'commentField\')" title="Insert Strong" />'
comm_editor +='<img src="/js/html_editor/icons/text-italic.png" onclick="document_selection_create(\'em\');update_textarea(\'commentField\')" title="Insert EM" />'
comm_editor +='<img src="/js/html_editor/icons/link_add.png" onclick="document_selection_create(\'a\');update_textarea(\'commentField\')" title="Insert Link" />'
comm_editor +='<img src="/js/html_editor/icons/application_view_list.png" onclick="document_selection_create(\'ul\');update_textarea(\'commentField\')" title="Transform to list" />'
comm_editor +='<img src="/js/html_editor/icons/html.png" class="html_view" onclick="show_hide_comment_html(\'commentField\')" title="Show/hide HTML" />'
comm_editor +='<div class="comment_editor" id="commentField.wrapper"><div id="commentField.editor" onkeyup="update_textarea(\'commentField\')"></div></div>'

comm_editor=replace(comm_editor,'commentField',target)
document.getElementById(target + ".script").innerHTML=comm_editor

var editor=document.getElementById(target + ".editor")
var xhtml=document.getElementById(target).value
if (xhtml.indexOf('</') == -1 ){
	xhtml=create_xhtml_from_text(xhtml)
		document.getElementById(target).value=get_HTMLtoXML(xhtml)
		}
editor.innerHTML=xhtml

	document.getElementById(target).style.display='none'
editor.contentEditable='true'
}

function insert_p(target) {
var editor=document.getElementById(target + ".editor")
editor.innerHTML+= '<p><br/></p>'
update_textarea(target) 

}

function update_textarea(target) {
var editor=document.getElementById(target + ".editor")
document.getElementById(target).value=get_HTMLtoXML(editor.innerHTML)

}

function show_hide_comment_html(target) {
if (document.getElementById(target).style.display=='none'){
//show html source
update_textarea(target)
document.getElementById(target).style.display='block'
document.getElementById(target + ".wrapper").style.display='none'
}
else {
//back to editor view
document.getElementById(target).style.display='none'
document.getElementById(target + ".wrapper").style.display='block'
var editor=document.getElementById(target + ".editor")
editor.innerHTML=document.getElementById(target).value
}
}
//---------------EDITOR MODE ENABLE/DISABLE FNCTIONS--------------
function maxWindow() {
	alert('maxWindow')
        window.moveTo(0, 0);


        if (document.all) {
            top.window.resizeTo(screen.availWidth, screen.availHeight);
        }

        else if (document.layers || document.getElementById) {
		
            if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
                top.window.outerHeight = screen.availHeight;
                top.window.outerWidth = screen.availWidth;
            }
        }
}


function enable_udt_editor(xbutton) {

//alert(udt_editor.lang)
if (enable_editor_button != xbutton){
	udt_editor.layout='/js/html_editor/layout/editor_layout_' + udt_editor.lang + '.html'
	//alert(udt_editor.layout_target)
enable_editor_button = xbutton; //Button that call the function
makeRequest(udt_editor.layout,'',udt_editor.layout_target,'','udt_editor')
	return false
}

//maxWindow()    

//JSON atalakitas


//var serialized = '{' + xbutton.title + '}'
//var serialized = "{Link:'valami.hu',Images:'proba.jpg',Galery:'public'}";
//var temp_array = eval('(' + serialized + ')')

udt_editor.target =  get_options_value(xbutton.title, 'target')
	if (udt_editor.target=="")
	{udt_editor.target = udt_editor.global_target;}
//alert(udt_editor.target)
//udt_editor.target=temp_array.target;
udt_editor.save= get_options_value(xbutton.title, 'save')
udt_editor.field_name= get_options_value(xbutton.title, 'name')
//alert(udt_editor.save)
// end


	
	//var save_button = document.getElementById("editor_save_button");
	//var restore_button = document.getElementById("editor_restore_button");
	if(enable_editor_button.value == "Enable Editor") {
		enable_editor_button.value = "Disable Editor";
		editor_on = 1;
		udt_editor.doc=document.getElementById(udt_editor.target)
		udt_editor.doc.contentEditable='true'
		//Bind Events
		udt_editor.doc.onmouseover = editor_onmouseover;
		udt_editor.doc.onmouseout = editor_onmouseout;
		udt_editor.doc.onclick =  editor_onclick;
		udt_editor.doc.onmouseup =  editor_mouseup;
		//udt_editor.doc.onkeyup =  editor_onkeyup;
		udt_editor.panel.style.display='block'
		udt_editor.panel_option.style.display='block'
		//disable_links(true);
			//var imgs = udt_editor.doc.getElementsByTagName("IMG");
			//for (var i = 0; i < imgs.length; ++i) {
				//imgs[i].contentEditable = false;
			//}

	}
	else {
		close_editor()	
	}

}

function close_editor() {
	if (isObject(enable_editor_button))	{
		enable_editor_button.value = "Enable Editor"
udt_editor.doc.contentEditable='false'
		//Bind Events
		udt_editor.doc.onmouseover.nothing;
		udt_editor.doc.onmouseout.nothing;
		udt_editor.doc.onclick.nothing;
		udt_editor.doc.onmouseup.nothing;
		//udt_editor.doc.onkeyup.nothing;
editor_on = 0;
udt_editor.pointer.style.display='none'	
udt_editor.panel.style.display='none'
udt_editor.panel_option.style.display='none'
	}

}

function editor_onmouseout(e) {

	hide_tip()
}

function editor_onclick(e) {

if(editor_on) {
var targ = get_event_target(e);

	if (targ==udt_editor.doc){return false}
		hide_tip();
		show_node_form(targ);
		
	return false;	
	}
return false;	
}


function editor_mouseup(e) {
if(editor_on) {
	var targ = get_event_target(e);
	udt_editor.node_text_insert.style.display='none'
		if (!(targ.tagName=='IMG')){
			if (check_document_selection()){
				//alert('node_text_insert')
				udt_editor.node_text_insert.style.display='block'
			}
		}
		//else if ((targ.tagName=='IMG' || targ.tagName=='DIV' || targ.tagName=='BLOCKQUOTE') && document.selection)
			//{document.selection.empty()}
		 //else if ((targ.tagName=='IMG' || targ.tagName=='DIV' || targ.tagName=='BLOCKQUOTE') && window.getSelection) {
                //window.getSelection().removeAllRanges();
				//alert('clear');
					//}


		else {return false}
}
}

function editor_onkeyup(e) {

if(editor_on) {
var targ = get_event_target(e);
	//if (selected!=targ)	{show_node_form(targ)}
}
}


function restore_default() {
	//Defeat reload bug, the buttons stack in enabled state
	any_change=0;
	document.getElementById("editor_save_button").disabled = true;
	document.getElementById("editor_restore_button").disabled = true;
	location.reload();

}


function get_plain_text(ob) {
var xtitle = (document.all) ? ob.innerText : ob.textContent;
xtitle=xtitle.substring(0,140)
return xtitle;
}

function get_inner_text(ob) {
var xhtml = (document.all) ? ob.innerText : ob.textContent;
xhtml=xhtml.replace(/^\s+/g, '').replace(/\s+$/g, '');


return xhtml;
}


//----------------SELECT FUNCTION & MANAGE POPUP WINDOW, CONTENT-----------------






//----EVENTS------

function editor_onmouseover(e) {

if(editor_on) {
var targ = get_event_target(e);
var xhtml='';
		if (targ!=udt_editor.doc){
		drag=false;
		show_tip(e);
		}

		switch(targ.tagName) {
		case 'P':
			xhtml='<p><b>Paragraph:</b></p><p>' + get_plain_text(targ) + '</p>'
		break;
		case 'H2':
			xhtml='<p><b>Heading 2:</b></p><p>' + get_plain_text(targ) + '</p>'
		break;	
		case 'H3':
			xhtml='<p><b>Heading 3:</b></p><p>' + get_plain_text(targ) + '</p>'
		break;
		case 'STRONG':
			xhtml='<p>STRONG: <b>' + get_plain_text(targ) + '</b></p>'
		break;
		case 'EM':
			xhtml='<p>EM: <b>' + get_plain_text(targ) + '</b></p>'
		break;
		case 'DIV':
			xhtml='<p><b>Layer (div) :</b></p><p>id: <b>' + targ.id + '</b><br/>class: <b>' + targ.className + '</b></p>'
		break;
		case 'UL':
			xhtml='<p><b>Unorderd List (ul) :</b></p><p>id: <b>' + targ.id + '</b><br/>class: <b>' + targ.className + '</b></p>'
		break;
		case 'LI':
			xhtml='<p>List Item (li) : <b>' + get_plain_text(targ) + '</b></p>'
		break;
		case 'IMG':
			xhtml='Image: <img class="alignleft icon" src="/js/html_editor/icons/pictures.png"/><div style="float:left;width:140px;"><b>' + targ.alt + '</b><br/>class: <b>' + targ.className + '</b><br/>width: <b>' + targ.style.width + '</b><br/>height: <b>' + targ.style.height + '</b></div>'
		break;
		case 'A':
			xhtml='<p>Link: <b>' + get_plain_text(targ) + '</b></p><p>href: <b>' + targ.href + '</b></p><p>id: <b>' + targ.id + '</b></p>'
		break;
		default:
			xhtml='<p>Node: <b>' + targ.nodeName + '</b></p>'

		 }
		 document.getElementById('page_tip').innerHTML=xhtml
	}
}


/*------------------- Save methods ---------------------------*/
function editor_save() {

var answer = confirm("Do you want to save document?")
	if (answer){
		var	xvalue=get_HTMLtoXML(udt_editor.doc.innerHTML)
		xvalue=replace(xvalue,"&", "[#38#]")
		xvalue=replace(xvalue,"+", "[#43#]")
		
		var poststr = udt_editor.field_name + "=" + encodeURI(xvalue);
		disable_editor_forms()

			udt_editor.save_info.style.display='block'
			udt_editor.save_info.innerHTML='<img src="/js/html_editor/icons/reload_anim.gif" title="Save"/>'
			target_object=udt_editor.save_info
				//alert(udt_editor.save)
		makeRequest(udt_editor.save,poststr,'','POST','');
			show_panel(udt_editor.panel_option)
	}


}

/*-------------------CHECK NODES---------------------------*/

function check_selected(e) {
	
if (!e) var e = window.event;
var targ = (window.event) ? e.srcElement : e.target;
alert(targ.nodeName)
//if (targ!=selected){show_node_form(targ)}
}


function check_block_element(e) {

if (document.all && editor_on){
var targ = e.srcElement;
	if (targ.nodeName=='BLOCKQUOTE' || targ.nodeName=='DIV'){
					// prevent IE from trying to drag an image 
		//targ.onselectstart = function () { return false; }; 
			targ.ondragstart = function() { return false; }; 
		//selected.ondragenter=function() { return false; }; 
			 targ.ondragover=function() { return false; }; 
			 targ.onresizestart=function() { return false; }; 
			targ.ondragleave=function() { return false; }; 
			targ.ondrop=function() { return false; }; 
			show_node_form(targ)
	return false;
	}
	if (targ.parentNode.nodeName=='BLOCKQUOTE'  || targ.nodeName=='IMG' || targ.nodeName=='DIV'){
	show_node_form(targ)
	return false;
	}
	//else if (targ.parentNode.nodeName=='DIV' || targ.parentNode.parentNode.nodeName=='DIV'){
	//show_node_form(targ)
	//return false;
	//}
}
return true;
}



//- SELECTED NODE FORM ------------------
function editor_source_replace(ob) {
var xob=ob.parentNode
var out=xob.getElementsByTagName("INPUT")[0].value
var add=xob.getElementsByTagName("INPUT")[1].value
var text=udt_editor.source.value

text=replace(text,out,add)
udt_editor.source.value=text
udt_editor.doc.innerHTML=udt_editor.source.value
}


function show_document_source() { 
selected=udt_editor.doc
disable_editor_forms()
show_panel(udt_editor.panel_option)
get_editor_source(selected);
show_pointer(selected)
document.getElementById('Document-Option').style.display='block'
build_node_class_select('document')
udt_editor.panel_option_title.innerHTML='Document'
udt_editor.source.className='open'
udt_editor.panel_option.className='open'
}

function open_panel(ob) {
	var target=ob.id.split("-Button")[0]
if (ob.className!='current'){
	if (isObject(document.getElementById(target))){
	document.getElementById(target).style.display='block'
	ob.className='current'
	}
}
else {
close_panel(ob)
}
}

function close_panel(ob) {
	var target=ob.id.split("-Close")[0]
if (target==ob.id){target=ob.id.split("-Button")[0]}
if (isObject(document.getElementById(target))){
document.getElementById(target).style.display='none'
}
if (isObject(document.getElementById(target + '-Button'))){
document.getElementById(target + '-Button').className='none'
}
}

function hide_panel(ob) {
	ob.style.display='none'
if (isObject(document.getElementById(ob.id + '-Button'))){
	ob=document.getElementById(ob.id + '-Button')
	ob.className='none'
}
}

function show_panel(ob) {
	ob.style.display='block'
if (isObject(document.getElementById(ob.id + '-Button'))){
	ob=document.getElementById(ob.id + '-Button')
	ob.className='current'
}
}

function zoom_source(ob) {

	if (udt_editor.panel_option.className!='open'){
		udt_editor.panel_option.className='open'
	}
	else {
		udt_editor.panel_option.className='closed'
	}
}

function plus_source(ob) {

	if (udt_editor.source.className!='open'){
		ob.src=replace(ob.src,'plus','minus')
		udt_editor.source.className='open'
	}
	else {
		udt_editor.source.className='closed'
		ob.src=replace(ob.src,'minus','plus')
			}
}




function create_option_element(xob,value,title) {
	var newelement = document.createElement('option');
	newelement.value=value;
	newelement.innerHTML=title
	xob.appendChild(newelement);

}

function build_node_class_select(mytag) {
	var xob=udt_editor.node_class_select
xob.innerHTML=''
		create_option_element(xob,'none','Class none')
var str=udt_editor.class_set
var myArray = new Array();
myArray=str.split("{");

if (mytag=='a'){mytag='link'}
	for ( i = 0; i < myArray.length; i++) {
		if (myArray[i].split("}")[0].indexOf(mytag)>-1){
			var myClass = new Array();
			var myClass=myArray[i].split("}")[1].split(";");
			for ( n = 0; n < myClass.length; n++) {
				create_option_element(xob,myClass[n].split(":")[1],myClass[n].split(":")[0])
				//xhtml += '<option value="' + myClass[n].split(":")[1] + '" selected="selected">' + myClass[n].split(":")[0] + '</option>'
			}
		}
	}
var curr_option=-1
//udt_editor.node_class_select.innerHTML=xhtml
var allObjects = xob.getElementsByTagName("OPTION");
		for(var i=0;i<allObjects.length;i++){
			if (allObjects[i].value==selected.className){
				allObjects[i].selected="selected";
				curr_option=i
				}
		}


if (curr_option==-1 && !(selected.className==null || selected.className=='' || selected.className=='none')){
		create_option_element(xob,selected.className,selected.className)
	//xhtml = '<option value="' + selected.className + '" selected="selected">' + selected.className + '</option>'
	//udt_editor.node_class_select.innerHTML += xhtml
	curr_option=1000
}
if (curr_option==-1) {
	xob.getElementsByTagName("OPTION")[0].selected="selected"
}

}


function show_node_form(referenceNode) {

if (referenceNode==udt_editor.doc){return false}
selected=referenceNode
disable_editor_forms()
build_node_class_select(selected.tagName.toLowerCase())
udt_editor.panel_option_title.innerHTML=selected.tagName
switch(selected.tagName) {
case 'P':
case 'H1':
case 'H2':
case 'H3':
case 'H4':
case 'H5':
case 'ADDRESS':
	get_editor_source(selected);
	document.getElementById('NODE-TEXT-Option').style.display='block'
	document.getElementById('NODE-TEXT-Style').style.display='block'
	//document.getElementById('NODE-Insert').style.display='block'
	clear_current_options(udt_editor.option)
	if (isObject(document.getElementById('icon-' + selected.tagName))){
		document.getElementById('icon-' + selected.tagName).className='current'
		udt_editor.panel_option_title.innerHTML=document.getElementById('icon-' + selected.tagName).title  + ' : <em>' + get_plain_text(selected).substring(0,40) + '</em>'
		}

break;
case 'IMG':
	//udt_editor.doc.contentEditable='false'
	selected.removeAttribute('width')
	selected.removeAttribute('height')
	//document.getElementById('IMG-NODE-CLASS').value=selected.className
	//document.getElementById('IMG-STYLE-WIDTH').value=selected.style.width
	//document.getElementById('IMG-STYLE-HEIGHT').value=selected.style.height
	//document.getElementById('NODE-IMG').style.display='block'
	document.getElementById('NODE-IMG-Option').style.display='block'
	//selected.setAttribute('_moz_resizing','false')

	//document.getElementById('NODE-IMG-Preview').src=selected.src
	//udt_editor.doc.contentEditable='true'
	udt_editor.panel_option_title.innerHTML=selected.tagName  + ' : <em>' + selected.alt + '</em>'
break;
case 'BLOCKQUOTE':
	get_editor_source(selected);
	document.getElementById('NODE-BLOCKQUOTE-Option').style.display='block'
break;
case 'UL':
case 'LI':
	get_editor_source(selected);
		udt_editor.panel_option_title.innerHTML=selected.tagName  + ' : <em>' + get_plain_text(selected).substring(0,40) + '</em>'
	document.getElementById('NODE-TEXT-Style').style.display='block'
	clear_current_options(udt_editor.option)
break;
case 'STRONG':
case 'EM':
	get_editor_source(selected);
		udt_editor.panel_option_title.innerHTML=selected.tagName  + ' : <em>' + get_plain_text(selected).substring(0,40) + '</em>'
	document.getElementById('NODE-TEXT-Style').style.display='block'
	clear_current_options(udt_editor.option)
break;
case 'TD':
case 'TH':
	get_editor_source(selected);
		udt_editor.panel_option_title.innerHTML=selected.tagName  + ' : <em>' + get_plain_text(selected).substring(0,40) + '</em>'
	//document.getElementById('NODE-TEXT').style.display='block'
		if (selected.getAttribute('colspan')!=undefined){
			document.getElementById('NODE-TD-colspan').value=selected.getAttribute('colspan')
		}
		else {document.getElementById('NODE-TD-colspan').value=1}
	document.getElementById('NODE-TEXT-Style').style.display='block'
	document.getElementById('NODE-TD-Option').style.display='block'
	clear_current_options(udt_editor.option)
	if (isObject(document.getElementById('icon-' + selected.tagName))){
		document.getElementById('icon-' + selected.tagName).className='current'
		}
break;
default:
	get_editor_source(selected);
}
show_pointer(selected)
}


//- EDITOR SYSTEM ------------------
function change_editor_layout(ob,xlayout) {
clear_current_options(ob.parentNode)
ob.className = 'current'
udt_editor.panel.className=xlayout
}


function clear_current_options(ob) {
var allObj = ob.getElementsByTagName('IMG');
  	for(i=0;i<allObj.length;i++){
    	if (allObj[i].className == 'current'){allObj[i].className = 'none';}
	}
}

function disable_editor_forms() {
	var allObj = udt_editor.panel_option.getElementsByTagName('LI');
  	for(i=0;i<allObj.length;i++){
    			if (allObj[i].className == 'system'){allObj[i].style.display = 'none';}
	}
}



function current_top(obj) {
	var curtop = 0;
			if (obj.offsetParent) {
				curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curtop += obj.offsetTop
			}
		}
	curtop = curtop - 220;
	//var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	window.scroll(0,curtop);
}

function show_pointer(obj) {
//current_top(obj)
var curtop = 0;
var curleft = 0;

	if (obj.offsetParent) {
		curtop = obj.offsetTop
		curleft = obj.offsetLeft
	while (obj = obj.offsetParent) {
		curtop += obj.offsetTop
			curleft += obj.offsetLeft
	}
	
}
var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
udt_editor.pointer.style.display='block'

udt_editor.pointer.style.top=curtop - 20  + "px"
udt_editor.pointer.style.left=curleft - 100  + "px"
}
//- EDIT Blockquote ------------------
function move_node(xdirection) {
		switch(xdirection) {
		case 'up':
		selected.parentNode.insertBefore(selected,selected.previousSibling);
		show_pointer(selected)
		break;
		case 'down':
		selected.parentNode.insertBefore(selected,selected.nextSibling.nextSibling);
		show_pointer(selected)
		break;
}
}



//- EDIT IMG------------------

function open_image_folder(ob) {
	udt_editor.panel_image_title.innerHTML=ob.title
ob=document.getElementById(udt_editor.panel_image.id + '-Button')
	open_panel(ob)
}

function editor_window_close() {
ob=document.getElementById(udt_editor.panel_image.id + '-Button')
	open_panel(ob)

}

function close_image_preview() {
	if (isObject(udt_editor.image_preview))	{
		udt_editor.image_preview.innerHTML=''
	}
}

function editor_insert_image(xob) {
var node_list='A;P;EM;STRONG;SPAN;LI;TD;TH';
var node_list_index=node_list.indexOf(selected.tagName + ';');

if (selected.tagName=='IMG')
	{
	var reply = prompt("Edit image title before insert:", xob.title)
	if (reply=='' || reply==null){reply=xob.title}

	selected.src=xob.src;
	selected.alt=reply;
	selected.title=reply;
	selected.style.width='auto';
	selected.style.height='auto';
	close_image_preview()
	show_node_form(selected)
}

else if (node_list_index!= -1) {
selected.innerHTML='<img class="alignleft" src="' + xob.src + '" alt="' + xob.title + '" class="" style="height:auto;width:auto"/>' + selected.innerHTML
close_image_preview()
show_node_form(selected.getElementsByTagName('IMG')[0])
}


else {
var newNode = document.createElement('P');
newNode.innerHTML='<img class="alignright" src="' + xob.src + '" alt="' + xob.title + '" class="" style="height:auto;width:auto"/> New paragraph text'
insertAfter(selected, newNode )
show_node_form(newNode.getElementsByTagName('IMG')[0])
}
editor_window_close()
}


function apply_att(att_name,ob) {
selected.setAttribute(att_name,document.getElementById(ob).value)
}

function set_img_size(xmode) {
		switch(xmode) {
		case 'increase':
			//alert(selected.offsetWidth + 10)
			selected.style.width= selected.offsetWidth + 10 - get_img_border() + 'px'
		break;
		case 'decrease':
			//alert(selected.offsetWidth - 10)
			selected.style.width= selected.offsetWidth - 10 - get_img_border() + 'px'
		break;
		case 'optimize':
			//alert(selected.parentNode.offsetWidth)
		//alert(get_img_border())
		selected.style.width='100%'
			selected.style.width=  selected.offsetWidth - get_img_border() - get_img_max_place() + 'px' 
		break;
		case 'half':
			//alert(selected.offsetWidth - 10)
			selected.style.width= '50%'
		break;
		case 'orig':
				remove_img_size()
		break;

		
	}
	show_node_form(selected)
}

function set_img_class(xclass) {
selected.className=xclass
show_node_form(selected)
}

function get_img_border() {
var img_style_width=selected.style.width
var img_offset_width=selected.offsetWidth
selected.style.width= selected.offsetWidth + 'px'
img_border_width=selected.offsetWidth - img_offset_width 
selected.style.width=img_style_width
return img_border_width

}

function get_img_max_place() {
var img_style_width=selected.style.width
var img_parent_width=selected.parentNode.style.width
var parent_offset_width=selected.parentNode.offsetWidth
selected.style.width='100%'
img_max_place=parent_offset_width - selected.offsetWidth
selected.style.width=img_style_width

return img_max_place

}

function remove_img_size() {

			selected.removeAttribute('width')
			selected.removeAttribute('height')
selected.removeAttribute('style')
//var newImg = new Image();
//newImg.src = selected.src;
//var height = newImg.height;
//var width = newImg.width;
selected.style.width='auto'
selected.style.height='auto'

}


//- EDIT REPLACE NODE------------------
function insertAfter( referenceNode, newNode ) {
	referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );

	}

function insert_paragraph(referenceNode) {
var newNode = document.createElement('P');
newNode.innerHTML='New paragraph text'
insertAfter( referenceNode, newNode )
	show_node_form(newNode)
}

function  show_msword_code() {
	udt_editor.source.value=udt_editor.msword_source.innerHTML
		orderXhtml(udt_editor.source)
	document.getElementById('NODE-SOURCE').style.display='block'
	//var xhtml=udt_editor.msword_source.innerHTML
var xhtml=extract_msword_code(udt_editor.msword_source)
	udt_editor.msword_source.innerHTML=xhtml

}

function  save_msword_code(ob)  {
	show_msword_code()
var text= udt_editor.msword_source.innerHTML
		//text=replace(text,'&nbsp;','');
	text=text.replace(/\&nbsp\;/g, ' ');
	text=text.replace(/\t/g, '');
	text=text.replace(/\n/g, ' ');
udt_editor.doc.innerHTML=udt_editor.doc.innerHTML + text
	hide_panel(udt_editor.panel_insert)
hide_panel(udt_editor.panel_msword)
}


function extract_msword_code(ob) {
var xhtml=''
var allObjects = ob.childNodes
		for(var i=0;i<allObjects.length;i++){
			if (allObjects[i].nodeType==1) {
				switch(allObjects[i].tagName) {
				case 'P':
				case 'H1':
				case 'H2':
				case 'H3':
				case 'H4':
				case 'H5':
				case 'LI':
					var text=get_inner_text(allObjects[i])
					if (text.length>0){	
					//text=replace(text,'&','&amp;');
					//text=replace(text,'&amp;nbsp;','');
					xhtml +='<' + allObjects[i].tagName.toLowerCase() + '>'
					xhtml += text
					xhtml +='</' + allObjects[i].tagName.toLowerCase() + '>\n'
					}

				break;
				case 'TD':
				case 'TH':
					var text=get_inner_text(allObjects[i])
					//text=text.replace(/\n/g, '<br/>');
					//text=replace(text,'&','&amp;');
					text=replace(text,'&amp;nbsp;','');
					//xhtml +='<' + allObjects[i].tagName.toLowerCase() + '>'
					xhtml +='<p>'
					xhtml += text
					//xhtml +='</' + allObjects[i].tagName.toLowerCase() + '>\n'
					xhtml +='</p>'
				break;
				case 'DIV':
				case 'TABLE':
				case 'TR':
				case 'THEAD':
				case 'TBODY':
				case 'TFOOT':
				//xhtml +='<' + allObjects[i].tagName.toLowerCase() + '>'
				xhtml +=extract_msword_code(allObjects[i])
				//xhtml +='</' + allObjects[i].tagName.toLowerCase() + '>\n'
				break;
				case 'UL':
				case 'OL':
				xhtml +='<' + allObjects[i].tagName.toLowerCase() + '>'
				xhtml +=extract_msword_code(allObjects[i])
				xhtml +='</' + allObjects[i].tagName.toLowerCase() + '>\n'
				break;
				}
			}
		}
return xhtml
}

function change_node_name(referenceNode, newNodeName) {
var newNode = document.createElement(newNodeName);
newNode.innerHTML=referenceNode.innerHTML
insertAfter( referenceNode, newNode )
referenceNode.parentNode.removeChild(referenceNode)
	show_node_form(newNode)
}

function set_class(new_class_name) {
if (selected.className==new_class_name)	{
selected.removeAttribute('class')
	}
else {
selected.className=new_class_name
}
}

//- EDIT TEXT NODE------------------

function set_style(ob,s_name,s_value) {
document.getElementById('clear-style').className = 'none'
clear_current_options(ob.parentNode)
ob.className = 'current'
switch(s_name) {
		case 'textAlign':
			selected.style.textAlign=s_value;
		break;
		case 'fontStyle':
			if (selected.style.fontStyle!=s_value){selected.style.fontStyle=s_value;}
			else {selected.style.fontStyle='normal';ob.className = 'none'}
		break;
		case 'fontWeight':
			if (selected.style.fontWeight!=s_value){selected.style.fontWeight=s_value;}
			else {selected.style.fontWeight='normal';ob.className = 'none'}
		break;
		case 'textDecoration':
			selected.style.textDecoration=s_value;
		break;
		}
}

function remove_style_attribute(ob) {
selected.removeAttribute('style');
clear_current_options(ob.parentNode);
ob.className='current';
}




function editor_refresh() {
selected.innerHTML=udt_editor.source.innerHTML;
udt_editor.source.innerHTML="";
udt_editor.source.innerHTML=selected.innerHTML;
}


