function _initEdit(){
	uHc = new _initValue();
	//nvHtmlEdit.document.open();
	//nvHtmlEdit.document.write(_Edit_InitHtml + document.all['nvContents'].value + _Edit_InitHtml2);
	//nvHtmlEdit.document.close();
	nvHtmlEdit.document.designMode = 'on';
	//document.ComposerForm.to.focus();
}

function _initValue(){
	this.selection = null;
	this.ReSelect = _nHc_ReSelect;
	this.InSelect = _nHc_InSelect;
	this.GetSelect = _nHc_GetSelect;
}

function _nHc_ReSelect(){
	if( this.selection ) this.selection.select();
}

function _nHc_GetSelect(){
	var nvHtmlSelection = nvHtmlEdit.document.selection;
	var nvHtmlSelection2 = this.selection;

	if( !nvHtmlSelection2 )	{
		nvHtmlSelection2 = nvHtmlSelection.createRange();
		nvHtmlSelection2.type = nvHtmlSelection.type;
	}
	return nvHtmlSelection2;
}

function _nHc_InSelect(){
	var nvHtmlSelection = nvHtmlEdit.document.selection;
	uHc.selection = nvHtmlSelection.createRange();
	uHc.selection.type = nvHtmlSelection.type;
}

function nvTbClick(obj,layer_num){
	if( layer_num )nvTbViewLayer(obj);
	else nvTbCloseLayer(obj);
	switch(obj)	{
		case "button_bold":
		nvTbClickMenu('bold');
		break;
		case "button_italic":
		nvTbClickMenu('italic');
		break;
		case "button_underline":
		nvTbClickMenu('underline');
		break;
		case "button_centerline":
		nvTbClickMenu('strikethrough');
		break;
		case "button_left":
		nvTbClickMenu('justifyleft');
		break;
		case "button_center":
		nvTbClickMenu('justifycenter');
		break;
		case "button_right":
		nvTbClickMenu('justifyright');
		break;
		case "button_justifyfull":
		nvTbClickMenu('justifyfull');
		break;
		case "button_head_number":
		nvTbClickMenu('insertorderedlist');
		break;
		case "button_head_symbol":
		nvTbClickMenu('insertunorderedlist');
		break;
		case "button_inner":
		nvTbClickMenu('outdent');
		break;
		case "button_outer":
		nvTbClickMenu('indent');
		break;
		case "button_viewsouce":
		nvViewSouce();
		break;
	}
}

function nvTbLayerClick(obj,opt){
	uHc.ReSelect();
	if( obj == "backcolor1" )	{
		nvTbClickMenu("forecolor","#404040");
		obj = "backcolor";
	}else if( obj == "backcolor2" )	{
		nvTbClickMenu("forecolor","#FFFFFF");
		obj = "backcolor";
	}
	nvTbClickMenu(obj,opt);
	nvTbCloseLayer(obj);
	nvHtmlEdit.focus();
}

function change_emoPage(page){
	var _top = "<table border='0' cellpadding='0' cellspacing='0'><tr><td class='box_g'><table border='0' cellpadding='0' cellspacing='1' bgcolor='FFFFFF'><tr><td><table border='0' cellpadding='0' cellspacing='0' bgcolor='F5F5F5' width='165'><tr><td style='padding:3 3 0 0' align='right'><a onClick='javascript:nvTbCloseLayer(\"Layer_emoticon_l\")' style='cursor:hand'><img src='http://static.naver.com/new_mail/images/edit_close.gif'></a></td></tr>";
	var _bottom = "</table></td></tr></table></td></tr></table>";
	var emoticon = make_emoticon(page);
	document.all.Layer_emoticon_l.innerHTML = _top + emoticon + _bottom;
}

function nvTbCloseLayer(obj){
	var i = 0;
	var LayerView = new String;
	var ButtonClick = new String;

	for( i = 0 ; i < nvTbMenu_Layers.length ; i++ )	{
		if( obj != nvTbMenu_Layers[i] )	{
			LayerView = eval( "document.all.Layer_" + nvTbMenu_Layers[i] );
			LayerView.style.display = "none";
		}
	}
}

function nvTbViewLayer(obj){
	uHc.InSelect();
	var LayerView = eval( "document.all.Layer_" + obj );
	if(  LayerView.style.display == "none" ) LayerView.style.display = "inline";
	else LayerView.style.display = "none";

	if( obj == "hyperlink_l")	{
		var nvEdit = nvHtmlEdit.document;
		var hEdit = nvEdit.selection.createRange();
		var URL_link = hEdit.htmlText;
		var pattern = "<A href=\"*\"";
		var matches = ExtractMatches( new RegExp( pattern, "gi" ), hEdit.htmlText );

		if ( matches.length > 0 ) document.all.hyper_layer.value = "http://" + URL_link.substring(URL_link.indexOf("://")+3,URL_link.indexOf("/\""));
	}
	nvTbCloseLayer(obj);
}

function ExtractMatches( re, text ){
	var results = new Array() ;
	var arr = re.exec(text)
	while(arr != null)	{
		var match = new Match() ;
		match.Text = arr[0] ;
		for (var i=1; i<arr.length; i++){
			match.Groups[i-1] = arr[i] ;
		}
		results[results.length] = match ;
		arr = re.exec(text) ; 
	}
	return results ;
}

function Match(){
	this.Text = null ;
	this.Groups = new Array() ;
}

function nvTbClickMenu(menu_id,opt){
	nvHtmlEdit.focus();
	var nvEdit = nvHtmlEdit.document;

	//if(document.all.nvSourceEdit_area.style.display == "inline" ){ alert('¼Ò½ºº¸±â ÇØÁ¦ÇÏ°í »ç¿ëÇØÁÖ¼¼¿ä.'); return; }

	if( menu_id == "insert_emoticon" ){
		var emoticon = _EMOTICON_DIR + "emo_" + opt + ".gif";
		var eEdit = nvEdit.selection.createRange();
		eEdit.pasteHTML("<img src='"+emoticon+ "'>");
	} else if( menu_id == "insert_hyperlink" ){
		var hyper_link = document.all.hyper_layer.value;
		var hEdit = nvEdit.selection.createRange();

		if(hEdit.htmlText=='' || hEdit.htmlText==' '){
			hEdit.pasteHTML("<a href='"+hyper_link+"' target='_blank'>"+hyper_link+"</a>");
		}else{
			var thisLink = hEdit.htmlText;
			var pattern = "<A href=\"*\"";
			var matches = ExtractMatches( new RegExp( pattern, "gi" ), hEdit.htmlText );
			if ( matches.length > 0 ){
				var UnLink = thisLink.substring(thisLink.indexOf("blank>")+6,thisLink.indexOf("</A>"));
				hEdit.execCommand("UnLink",false,UnLink);
				thisLink = UnLink;
			}
			if ( hyper_link != '' && hyper_link != ' ') hEdit.pasteHTML("<a href='"+hyper_link+"' target='_blank'>"+thisLink+"</a>");
		}
		document.all.hyper_layer.value = "http://";
	}else if( menu_id == "button_hr" ){
			var eEdit = nvEdit.selection.createRange();
			//eEdit.pasteHTML("<p><img src=\"\" width='99%' height='2' border=0></p>");
			eEdit.execCommand('InsertHorizontalRule');
		}else if( menu_id == "bgcolor" ){
			document.nvHtmlEdit.document.body.style.background = opt;
		}else{
			if( !opt ) nvEdit.execCommand(menu_id);
		else nvEdit.execCommand(menu_id,"",opt);
	}
	nvHtmlEdit.focus();
}


function nvViewSouce(){
/*	var nvText = document.all.nvSourceEdit_area;
	if( nvText.style.display == "inline" ){
		nvText.style.display = "none";
		document.all.nvHtmlEdit.style.display = "inline";
		document.nvHtmlEdit.document.body.innerHTML = document.all.nvSourceEdit.value;
		//nvHtmlEdit.focus();
	}else{
		nvText.style.display = "inline";
		document.all.nvHtmlEdit.style.display = "none";
		document.all.nvSourceEdit.value = nvHtmlEdit.document.body.innerHTML;
		//document.all.nvSourceEdit.focus();
	}
*/
}

