var hx_ed_current = null;
var hx_ed_currentElement = null;


function hx_installToolbar(objid,small){
	if (typeof(small) == "undefined"){
		small = false;
	}
	var obj = xg.obj(objid);
	if (!small) obj.style.backgroundImage = "url('http://imgs.heeloo.net/icons/edit/toolbar.bg.gif')";
	obj.style.padding = "2px";
	var _toolbars = new Array("bold","italic","underline","StrikeThrough","_sep","SuperScript","SubScript",
			"_sep","align_left","align_center","align_right","align_justify",
			"_sep","indent_more","indent_less",
			"_sep","InsertUnorderedList","InsertOrderedList",
			"_sep","x_image","x_movie","x_doc","x_music"
	);
	if (small){
		_toolbars = new Array("bold","italic","underline","StrikeThrough");
	}
	
	var _html = "";
	for (var i=0;i<_toolbars.length ;i++){
			var src = _toolbars[i]+".gif";
			if (_toolbars[i] == "x_movie") src = "film.png";
			if (_toolbars[i] == "x_music") src = "sound.png";
			if (_toolbars[i] == "x_doc") src = "report.png";
			if (_toolbars[i] == "x_image") src = "image.gif";

			var _tb = "<img src='http://imgs.heeloo.net/icons/edit/ed_"+src+"' />";

			if (_toolbars[i]=="_sep") _tb = "<img style='' src='http://imgs.heeloo.net/icons/edit/toolbar.end.gif' border='0'/>";
			_html += "<div style='margin:2px;float:left;' onmousedown='hx_ed_click(this,&quot;"+_toolbars[i]+"&quot;);return false;' onclick='return false;';onfocus = 'return false'>"+_tb+"</div>";
	}

	obj.innerHTML = _html;
}

function hx_ed_click(obj,command){
//	var range = hx_getRange(obj);
	if (command.startsWith("align_")) command = "Justify"+command.substring(6).firstUpperCase();
	if (command == "indent_more") command = "Indent";
	if (command == "indent_less") command = "Outdent";
	
	if (command.startsWith("x_")){
		hx_ed_execute(command);
	}else{
		if(hx_ed_currentElement != null){
			if (command == "JustifyLeft"){
				try{
					hx_ed_currentElement.style.float = "left";
				}catch(E){}
			hx_ed_currentElement.align="left";
			}else
				if (command == "JustifyRight"){
					hx_ed_currentElement.style.float = "right";
					hx_ed_currentElement.align = "right";
				}

		}else{
			document.execCommand(command,false,null);
		}
	}
	try{hx_ed_current.focus();}catch(e){}
}


function hx_ed_execute(cmd){

	if (cmd == "x_image"){
		var dlg_show_pictures = new xgDialog(myServer+"/apps/procs/dialogfiles.html?type=gallery&token="+GenerateID(),500,400);
		dlg_show_pictures.skin="black";
		dlg_show_pictures.create(null);
		dlg_show_pictures.show();
		dlg_show_pictures.onclose = function(res){
		if (res){
			var _width = 450;
			var imgURL = res[3];
			var th = "<img style='width:"+_width+"px; TEXT-ALIGN: center; MARGIN: 0px auto 10px; DISPLAY: block; CURSOR: hand' src='"+imgURL+"' border='0'  class='image_placeholder' border='0'/>";
			var html =  hx_ed_current.innerHTML;
			hx_ed_current.focus();
			hx_ed_pasteHTML("<div>&nbsp;</div>");
			hx_ed_pasteHTML(th);
			hx_ed_current.focus();
				
			}
		this.destroy();
	}
	}else
	if (cmd == "x_movie"){
		var dlg_show_movies = new xgDialog(myServer+"/apps/procs/dialogfiles.html?type=movie&token="+GenerateID(),500,400);
		dlg_show_movies.skin="black";
		dlg_show_movies.create(null);
		dlg_show_movies.show();
		dlg_show_movies.onclose = function(res){
		if (res){
			var _width = 450;
			var imgURL = res[0];
			var th = "<div class='movie_placeholder' src='"+res[2]+"' CONTENTEDITABLE=false style='background-image:url("+res[2]+");background-repeat:no-repeat;background-position:center center'>$apps.movie(id="+imgURL+")</div>";
			hx_ed_current.focus();
			hx_ed_pasteHTML("<div>&nbsp;</div>");
			hx_ed_pasteHTML(th);
			hx_ed_current.focus();
				
			}
		this.destroy();
	}
	}else
		if (cmd == "x_music"){
		var dlg_show_music = new xgDialog(myServer+"/apps/procs/dialogfiles.html?type=music&token="+GenerateID(),500,400);
		dlg_show_music.skin="black";
		dlg_show_music.create(null);
		dlg_show_music.show();
		dlg_show_music.onclose = function(res){
		if (res){
			var _width = 450;
			var imgURL = res[0];
			var th = "<div class='music_placeholder' src='"+res[2]+"' CONTENTEDITABLE=false style='background-image:url("+res[2]+");background-repeat:no-repeat;background-position:center center'>$apps.music(id="+imgURL+")</div>";

			hx_ed_current.focus();
			hx_ed_pasteHTML("<div>&nbsp;</div>");
			hx_ed_pasteHTML(th);
			hx_ed_current.focus();
				
			}
		this.destroy();
	}

	}else
		if (cmd == "x_doc")	{
			var dlg_show_music = new xgDialog(myServer+"/apps/procs/dialogfiles.html?type=docs&token="+GenerateID(),500,400);
		dlg_show_music.skin="black";
		dlg_show_music.create(null);
		dlg_show_music.show();
		dlg_show_music.onclose = function(res){
		if (res){
			
			var _width = 450;
			var imgURL = res[0];
			
			var th = "<div class='docs_placeholder' src='http://filetypes.heeloo.net/"+res[2]+"' ><img src='http://filetypes.heeloo.net/"+res[2]+"'/><div><a href='"+res[3]+"' target='_self'>"+res[1]+"</a></div></div>";
			hx_ed_current.focus();
			hx_ed_pasteHTML("<div>&nbsp;</div>");
			hx_ed_pasteHTML(th);
			hx_ed_current.focus();
				
			}
		this.destroy();
	}
		
		}



}

function hx_ed_pasteHTML(html){
		if (xg.browser.sName == "ie"){
			document.selection.createRange().pasteHTML(html);
		}else{
			var selection = window.getSelection ();
			var w = document.createElement("DIV");
				w.innerHTML = html;
                if (selection.rangeCount > 0) {
                    var range = selection.getRangeAt (0);
                   range.collapse (false);
				  // alert(w.firstChild);
                    var node = range.insertNode (w.firstChild);
                }
		}
}

function hx_installEdit(objid){
	var obj = xg.obj(objid);
	obj.contentEditable = true;
	obj.style.overflow = "auto";

	var _lastSpace = 0;
	
	obj.onclick = function(){
		hx_ed_current = this;
		var src = xg.event.srcElement;
		if (src.tagName == "IMG"){
			src.style.border = "dotted gray 1px";
			hx_ed_currentElement = src;
		}
		else{
			if (hx_ed_currentElement != null){
				hx_ed_currentElement.style.border = "none";
				hx_ed_currentElement = null;
			}
		}

	}

	obj.onfocus = function(){
		hx_ed_current = this;
	}

	obj.onkeypress = function(evt){
		if (!evt) evt = event;
		var kc = evt.keyCode;

		if (kc == 32){ //space
			var word = this.innerText.substring(_lastSpace);

			if (word.startsWith("http://") || word.startsWith("www.")){
					//alert("link");
			}

			_lastSpace = this.innerText.length+1;
		}

	}

	obj.onpaste = function(){
		//return false;
	}
}

function hx_getRange(obj){
if (xg.browser.sName == "ie"){
		var trange = obj.selection.createRange();
		return trange.htmlText;
	}else{
		var selection = document.getSelection();
		var range = selection.getRangeAt(0);
		return range.toString();
	}
	
	return null;
}

