// ********************* initialize GZIP Compressor mceeditor *********************

tinyMCE_GZ.init({
language : "ja",
//elements : "ajaxfilemanager",
mode : "none",
theme : "advanced",
skin : "o2k7",
skin_variant : "black",
height: 100,
width: 270,
cleanup:true,
cleanup_on_startup : true,
force_p_newlines : false,
readonly: true,
disk_cache : true,
convert_fonts_to_spans : false,
force_br_newlines : true,
force_p_newlines : false,
extended_valid_elements : "hr[class|width|size|noshade]",
file_browser_callback : "tinyBrowser",
paste_use_dialog : false,
relative_urls : true,

plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
 
// Theme options
theme_advanced_buttons1 : "fontselect,fontsizeselect,image",
theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull",
theme_advanced_buttons3 : "undo, redo,|,cut,copy,paste,|,forecolor,backcolor,|,bullist,numlist,|,link,unlink,|,fullscreen",
theme_advanced_buttons4 : "",
theme_advanced_add_fonts : "MS P明朝=MS P明朝; MS 明朝=MS 明朝; MS ゴチック=MS ゴチック; Monotype Corsiva=monotype corsiva",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
theme_advanced_resizing_use_cookie : false,


// Example content CSS (should be your site CSS)
content_css : "css/example.css",
 
// Drop lists for link/image/media/template dialogs
template_external_list_url : "js/template_list.js",
external_link_list_url : "js/link_list.js",
external_image_list_url : "js/image_list.js",
media_external_list_url : "js/media_list.js",
 
setup : function (ed) {
	        ed.onChange.add(
	            function (ed, evt) {
	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
					
	            }
	        );
			ed.onEvent.add(
	            function (ed, evt) {
	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
	            }
	        );
	    }	
});


// ********************* initialize title mceeditor *********************

var configArray = [{
language : "ja",
mode : "none",
//elements : "ajaxfilemanager",
theme : "advanced",
skin : "o2k7",
height: 50,
width: 270,
cleanup:true,
cleanup_on_startup : true,
disk_cache : true,
convert_fonts_to_spans : false,
force_br_newlines : true,
force_p_newlines : false,
extended_valid_elements : "hr[class|width|size|noshade]",
file_browser_callback : "tinyBrowser",
paste_use_dialog : false,
relative_urls : true,

plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,",
 
// Theme options
theme_advanced_buttons1 : "fontselect,fontsizeselect,image",
theme_advanced_buttons2 : ",forecolor,backcolor,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,hr",
theme_advanced_buttons3 : "",
theme_advanced_buttons4 : "",
theme_advanced_add_fonts : "MS P明朝=MS P明朝; MS 明朝=MS 明朝; MS ゴチック=MS ゴチック; Monotype Corsiva=monotype corsiva",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
theme_advanced_resizing_use_cookie : false,
	
setup : function (ed) {
        	ed.onChange.add(
	            function (ed, evt) {
	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
	            }
	        );
			ed.onEvent.add(
	            function (ed, evt) {

	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
	            }
	        );
	    }
},

// **************************** initialize content mceeditor ***********************

{
language : "ja",
mode : "none",
//elements : "ajaxfilemanager",
theme : "advanced",
skin : "o2k7",
skin_variant : "black",
height: 100,
width: 270,
cleanup:true,
cleanup_on_startup : true,
disk_cache : true,
convert_fonts_to_spans : false,
force_br_newlines : true,
force_p_newlines : false,
extended_valid_elements : "hr[class|width|size|noshade]",
file_browser_callback : "tinyBrowser",
paste_use_dialog : false,
relative_urls : true,

plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
 
// Theme options
theme_advanced_buttons1 : "fontselect,fontsizeselect,image",
theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,hr,|,link,unlink",
theme_advanced_buttons3 : "undo, redo,|,forecolor,backcolor,|,cut,copy,paste,|,bullist,numlist,|,fullscreen",
theme_advanced_buttons4 : "",
theme_advanced_add_fonts : "MS P明朝=MS P明朝; MS 明朝=MS 明朝; MS ゴチック=MS ゴチック; Monotype Corsiva=monotype corsiva",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
theme_advanced_resizing_use_cookie : false,

// Example content CSS (should be your site CSS)
content_css : "css/example.css",
 
// Drop lists for link/image/media/template dialogs
template_external_list_url : "js/template_list.js",
external_link_list_url : "js/link_list.js",
external_image_list_url : "js/image_list.js",
media_external_list_url : "js/media_list.js",
 
setup : function (ed) {
	        ed.onChange.add(
	            function (ed, evt) {
	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
	            }
	        );
			ed.onEvent.add(
	            function (ed, evt) {
	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
	            }
	        );
	    }	
},

// **************************** initialize content mceeditor keyinput disbaled ***********************

{
// General options
language : "ja",
mode : "none",
//elements : "ajaxfilemanager",
theme : "advanced",
skin : "o2k7",
skin_variant : "black",
height: 100,
width: 270,
cleanup:true,
cleanup_on_startup : true,
force_p_newlines : false,
readonly: true,
disk_cache : true,
convert_fonts_to_spans : false,
force_br_newlines : true,
force_p_newlines : false,
extended_valid_elements : "hr[class|width|size|noshade]",
file_browser_callback : "tinyBrowser",
paste_use_dialog : false,
relative_urls : true,

plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
 
// Theme options
theme_advanced_buttons1 : "fontselect,fontsizeselect,image",
theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull",
theme_advanced_buttons3 : "undo, redo,|,cut,copy,paste,|,forecolor,backcolor,|,bullist,numlist,|,link,unlink,|,fullscreen",
theme_advanced_buttons4 : "",
theme_advanced_add_fonts : "MS P明朝=MS P明朝; MS 明朝=MS 明朝; MS ゴチック=MS ゴチック; Monotype Corsiva=monotype corsiva",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
theme_advanced_resizing_use_cookie : false,


// Example content CSS (should be your site CSS)
content_css : "css/example.css",
 
// Drop lists for link/image/media/template dialogs
template_external_list_url : "js/template_list.js",
external_link_list_url : "js/link_list.js",
external_image_list_url : "js/image_list.js",
media_external_list_url : "js/media_list.js",
 
setup : function (ed) {
	        ed.onChange.add(
	            function (ed, evt) {
	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
					
	            }
	        );
			ed.onEvent.add(
	            function (ed, evt) {
	                // alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
					//	var edcontent = tinyMCE.get('icname2').getContent()
					preview('2',ed.id,'p'+ed.id);
	            }
	        );
	    }	
}];




// ************************ other functions **************

function AdvanceStartUp(num){
	tinyMCE.settings = configArray[num];
	tinyMCE.execCommand('mceToggleEditor', false, 'icname1');
	tinyMCE.execCommand('mceToggleEditor', false, 'icname2');
	tinyMCE.execCommand('mceToggleEditor', false, 'icname5');
	tinyMCE.execCommand('mceToggleEditor', false, 'testing');
	ViewSource('precontent');
}
AdvanceStartUp(0);

function tmToggleEditor(id1,id2) {
	tinyMCE.settings = configArray[id2];
	tinyMCE.execCommand('mceToggleEditor', false, id1);
}

function Hide(id){
	var el = document.getElementById(id);
	el.style.display = 'none';
}

function Show(id){
	var el = document.getElementById(id);
	el.style.display = 'inline';
}


function preview(type,id1, id2, parent){
	if(type=='1'){
    	var NewText = document.getElementById(id1).value;
	}
	if(type=='2'){					  
 		var NewText = tinyMCE.get(id1).getContent();
 	}
 
//    splitText = NewText.split(/\n/).join("<br />");
//    var DivElement = document.getElementById(id2); 
//	DivElement.innerHTML = splitText;		

    splitText = NewText.split(/\n/).join("<br />");
    var DivElement = document.getElementById(id2); 

	if(DivElement!=null){
		DivElement.innerHTML = splitText;		
	}
	else{
		var parentDiv = document.getElementById(parent);
		var NewDiv = document.createElement('div');
		NewDiv.setAttribute('id',id2);
		NewDiv.setAttribute('name',id2);
		parentDiv.appendChild(NewDiv);
		NewDiv.innerHTML = splitText;		
	}
					  
ViewSource('precontent');
}


function AddCustomElement(cno,cut){
	var n = 1;
	var count = document.getElementById('iclabel' + cno + '-' + n);		
	while(count!=null){
		var count = document.getElementById('iclabel' + cno + '-' + n);		
		n++;
	}
	n--;
	
	var NewSpan = document.createElement('span');
	NewSpan.setAttribute('id','iclabel' + cno + '-' + n);
	NewSpan.innerHTML = "<br />" + (n-cut) + ": ";
	var NewInput = document.createElement('input');
	NewInput.setAttribute('id','cicname' + cno + '-' + n);
	NewInput.setAttribute('name','cicname' + cno + '[]');
	//NewInput.setAttribute('onkeyup',);
	NewInput.onkeyup = function(){preview("1","cicname" + cno + "-" + n,"picname" + cno + "-" + n, "picname" + cno);}
	//NewInput.onkeyup
	var before = document.getElementById('iclabel' + cno);
	before.appendChild(NewSpan);
	before.appendChild(NewInput);


	var parentDiv = document.getElementById('picname' + cno);
	var NewDiv = document.createElement('div');
	NewDiv.setAttribute('id','picname' + cno + '-' + n);
	NewDiv.setAttribute('name','picname' + cno + '[]');
	parentDiv.appendChild(NewDiv);
	
ViewSource('precontent');
}


function RemoveCustomElement(cno,lmt,button){
	var n = 1;
	var count = document.getElementById('iclabel' + cno + '-' + n);		
	while(count!=null){
		var count = document.getElementById('iclabel' + cno + '-' + n);		
		n++;
	}
	n-=2;
	var parent = document.getElementById('iclabel' + cno);		
	var remlabel = document.getElementById('iclabel' + cno + '-' + n);		
	var remname = document.getElementById('cicname' + cno + '-' + n);		
	var txtname = document.getElementById('picname' + cno + '-' + n);		
	var txtparent = document.getElementById('picname' + cno);		
	parent.removeChild(remlabel);
	parent.removeChild(remname);
	txtparent.removeChild(txtname);
	if(n==lmt) Hide(button.id);
	
ViewSource('precontent');
}


function ToggleCheckBox(id,num){

	var sid = id.value;
	var VChkBox = document.getElementById('icname' + num);
	var parentDiv = document.getElementById('picname' + num );
	var targetDiv = document.getElementById(sid);
	
	if(id.checked == true){
		
		var txtArea = document.getElementById('icname' + num );
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id',sid);
		newDiv.setAttribute('name',sid);
		newDiv.innerHTML = sid;
		parentDiv.appendChild(newDiv);
		}
	else{
		var tparentDiv = targetDiv.parentNode;
		tparentDiv.removeChild(targetDiv);
	}
	
ViewSource('precontent');
}


// Function that copy / selects the Generated Source Code
function copyit(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")
}

// Function that Displays Generated Source Code
function ViewSource(id) {
var srccde = document.getElementById('tempoutput');
srccde.value = document.getElementById(id).innerHTML;
}


// Function that disable enter key in HTML Forms
function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}
document.onkeypress = stopRKey;


function SetValues()
{
    var s = 'X=' + window.event.clientX +  ' Y=' + window.event.clientY ;
    document.getElementById('divCoord').innerText = s;

}  


function putcolor(colorval,tdname1,tdname2,tdname3,tdname4){
	
	if (tdname1 != ""){
		tdid1 = document.getElementById(tdname1);
		tdid1.setAttribute('bgcolor','#' + colorval);
		tdid1.style.backgroundColor='#' + colorval;
	}
	if (tdname2 != ""){
		tdid2 = document.getElementById(tdname2);
		tdid2.setAttribute('bgcolor','#' + colorval);
		tdid2.style.backgroundColor='#' + colorval;
	}
	if (tdname3 != ""){
		tdid3 = document.getElementById(tdname3);
		tdid3.setAttribute('bgcolor','#' + colorval);
		tdid3.style.backgroundColor='#' + colorval;
	}
	if (tdname4 != ""){
		tdid4 = document.getElementById(tdname4);
		tdid4.setAttribute('bgcolor','#' + colorval);
		tdid4.style.backgroundColor='#' + colorval;
	}
}

/* AJAX image uploader code
function ajaxfilemanager(field_name, url, type, win) {
			var ajaxfilemanagerurl = "../../../tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php";
			switch (type) {
				case "image":
					break;
				case "media":
					break;
				case "flash": 
					break;
				case "file":
					break;
				default:
					return false;
			}
            tinyMCE.activeEditor.windowManager.open({
                url: "../../../tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php",
                width: 782,
                height: 440,
                inline : "yes",
                close_previous : "no"
            },{
                window : win,
                input : field_name
            });
            
            return false;			
			var fileBrowserWindow = new Array();
			fileBrowserWindow["file"] = ajaxfilemanagerurl;
			fileBrowserWindow["title"] = "Ajax File Manager";
			fileBrowserWindow["width"] = "782";
			fileBrowserWindow["height"] = "440";
			fileBrowserWindow["close_previous"] = "no";
			tinyMCE.openWindow(fileBrowserWindow, {
			  window : win,
			  input : field_name,
			  resizable : "yes",
			  inline : "yes",
			  editor_id : tinyMCE.getWindowArg("editor_id")
			});
			
			return false;
		}
*/
