var editor = null;
var loadTimer = -1;
var rangeCache = null;
var editorMode = "H"; //html default
var isIE = (navigator.userAgent.toLowerCase().indexOf("msie")!=-1 && window.document.all) ? true:false;

InitEditor = function(){
	/*
	editor = document.getElementById("htmlEditor").contentWindow;
	editor.document.designMode = "on";

	//SetHtml();
	loadTimer = setTimeout("SetHtml()",200);
	*/

	editor = document.getElementById("htmlEditor").contentWindow;
	SetHtml(); // ¼öÁ¤ÇÒ ³»¿ë ÀÔ·Â
	editor.document.designMode = "on";		
}

SetHtml = function(){	
	/*
	if($("pos_content").value!=""){
		if(editor.document.body==null){
			if(loadTimer>0) clearTimeout(loadTimer);
			loadTimer = setTimeout("SetHtml()",1000);
			return;
		}
		editor.document.body.innerHTML = $("pos_content").value;
	}
	*/
	if($("pos_content").value!=""){
		editor.document.body.innerHTML = $("pos_content").value;
	}

}

SavePost = function(){
	with(document.fraBlogWrite){
		if(editorMode=="H") pos_content.value = editor.document.body.innerHTML;
		if(!checkForm(document.fraBlogWrite)) return;

		// ÅÂ±×µ¥ÀÌÅÍ È®ÀÎ
		var tagstr = '';
		if(tag_name.value!=''){
			for(i=0;i<tag_name.value.split(",").length;i++){
				var tmpStr = Trim(tag_name.value.split(",")[i]);
				if(getByte(tmpStr)>20){
					alert('ÅÂ±×´Â 20¹ÙÀÌÆ®ÀÌÇÏ(ÇÑ±Û10ÀÚ)·Î ÀÔ·ÂÇØ ÁÖ¼¼¿ä');
					return;
				}else{
					if(tagstr=='')
						tagstr = tmpStr;
					else
						tagstr = tagstr + ',' + tmpStr;						
				}
			}				
			tag_name.value = tagstr; 
		}
		submit();
	}
}


CancelPost = function(){
	document.fraBlogWrite.reset();
}


ShowJEditor = function(){
	popupWindow("/_dev/activeX/JEditor.html","JEditor",860,655);
}


AddImg = function(paths){
	strImgTag = "";
	for(i=0;i<paths.split(";").length;i++){
		strImgTag += "<br><br><center><img src='"+ paths.split(";")[i] +"'></center>"
	}
	editor.document.body.innerHTML = editor.document.body.innerHTML + strImgTag;

	with(document.fraBlogWrite){
		if(pos_img.value=="")
			pos_img.value = paths;
		else
			pos_img.value += ";"+ paths;
	}
}


ExecCmd = function(action, values){
	editor.focus();
	if(isIE){			
		if(action=="inserthtml")
			editor.document.body.innerHTML = editor.document.body.innerHTML + "<br>" + values;
		else if (values == null)
			editor.document.execCommand(action);
		else
			editor.document.execCommand(action, "", values);
	}else{
		try{
			if(action=="inserthtml")
				editor.document.body.innerHTML = editor.document.body.innerHTML + "<br>" + values;
			else
				document.getElementById('htmlEditor').contentDocument.execCommand(action, false, values);
		}catch (e) {
			//alert("This demo is not supported on your level of Mozilla.");
		}
	}
}	

SetFontColor = function(color){
	if(color=="") color = "#ffffff";
	if(isIE) rangeCache.select();  
	ExecCmd("forecolor",color);
	HideDialog();		
}

SetBGColor = function(color){
	if(color=="") color = "#ffffff";
	if(isIE) rangeCache.select();  
	ExecCmd("backcolor",color);
	HideDialog();			
}

ColorPicker = function(idx){
	if(isIE) rangeCache = editor.document.selection.createRange();

	if(idx==1){
		ShowColorPicker('imgFontColor',SetFontColor);
	}else{
		ShowColorPicker('imgBGColor',SetBGColor);
	}
}


MenuBT = function(obj,action){
	if(action=="over")
		obj.src = obj.src.replaceAll("_n.gif","_r.gif");
	else if(action=="out")
		obj.src = obj.src.replaceAll("_r.gif","_n.gif");
	else if(action=="down")
		obj.src = obj.src.replaceAll("_r.gif","_p.gif");
	else if(action=="up")
		obj.src = obj.src.replaceAll("_p.gif","_n.gif");
}


//-------- movie °ü·Ã -------------	
var movieCallBack = null;
ShowMovieSelect = function(popCtrl, callBack){
	movieCallBack = callBack;
	getAjaxPage("/_common/ajax_dialog_movie.asp","divDialog");	
	displayLayer("divDialog", popCtrl, -262, 3);
}

MovieSelect = function(url){
	movieCallBack(url);
	HideDialog();
}

AddMovie = function(url) {
	var preDefinedUrl = {
		defaultContent : "<embed autostart='true' src='{URL}'>",
		videoList : [
			["^http://video.google.com/googleplayer.swf","<embed style='width:500px; height:408px;' type='application/x-shockwave-flash' src='{URL}'></embed>"],
			["^http://serviceapi.nmv.naver.com/flash/NFPlayer.swf","<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='500' height='408' id='NFPlayer' align='middle'>\
				<param name='allowScriptAccess' value='always' />\
				<param name='movie' value='{URL}' />\
				<param name='quality' value='high' />\
				<param name='bgcolor' value='#000000' />\
				<param name='wmode' value='transparent' />\
				<embed src='{URL}' quality='high' wmode='transparent' width='500' height='408' name='NFPlayer' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />\
				</object>"]
			],

			getContents : function( url ){
				var sContents = new String();
				var sCase = new String();

				var rgUrlExp = null;
				var rTemp = null;
				var vlLength = this.videoList.length;

				for( var i = 0; i < vlLength ; i++ ){
					sExpList = this.videoList[i][0];
					rgUrlExp = new RegExp( sExpList,"gi");
					rTemp = rgUrlExp.test(url);
					if( rTemp == true ){
						sCase = this.videoList[i][1];
						break;
					}
				}

				if( rTemp == false ){
					sCase = this.defaultContent;
				}
				sContents = sCase.replace(/{URL}/gi,url);
				return sContents;
			}
		};

	var getContents = preDefinedUrl.getContents(url);	
	ExecCmd('inserthtml',"<center>" + getContents +"</center>");
}


SetEditorMode = function(mode){
	editorMode = mode;

	var tabImgs = $("divEditorTab").select("img[class=tabImg]");

	if(editorMode=="H"){
		SetHtml();
		$("divEditor").show();
		$("pos_content").hide();				
		tabImgs[0].src = tabImgs[0].src.replace("_off","_on");
		tabImgs[1].src = tabImgs[1].src.replace("_on","_off");
	}else{
		$("pos_content").value = editor.document.body.innerHTML;
		$("divEditor").hide();
		$("pos_content").show();

		tabImgs[0].src = tabImgs[0].src.replace("_on","_off");
		tabImgs[1].src = tabImgs[1].src.replace("_off","_on");
	}
}
