function on_load_design_mode(){
	if(WysiwygEditor.iframeList.length){
		for(var i=0;i<WysiwygEditor.iframeList.length;i++){
			var iframe_id=WysiwygEditor.iframeList[i];
			var iframe_doc=WysiwygEditor.getDocument(iframe_id);

			iframe_doc.designMode="On";
			//ieではデザインモードの際に初期化される
			if(WysiwygEditor.browseIE){
				iframe_doc.writeln('<body></body>');
			}
			iframe_doc.body.setAttribute('id',iframe_id+"_body");

			//iframeの初期値設定を施す
			initialyze_iframe(iframe_doc,iframe_id);

			//ロールオーバー処理を施す
			for(var button in WysiwygEditor.smartRolloverList){
				if(document.getElementById(iframe_id+"_"+button+"_button")) {
					var button_object=document.getElementById(iframe_id+"_"+button+"_button");
					if(button_object.getAttribute("src").match("_off.")){
						button_object.onmouseover = function() {
							this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
						}
						button_object.onmouseout = function() {
							this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
						}
					}
				}
			}
			
			//フォーカス処理を施す
			//WysiwygEditor.setEventObject(iframe_id);
		}
	}
}
function initialyze_iframe(iframe_doc,iframe_id){
	var variable=WysiwygEditor.iframeVariableList[iframe_id];

	var heads=document.getElementsByTagName('link');
	for(var i=0;i<heads.length;i++){
		if(heads[i].rel && heads[i].rel=='stylesheet'){
			var href=heads[i].href;
			WysiwygEditor.setCSSFile(iframe_doc,href);
		}
	}
	if(variable.include_css.length){
		var include_css_list=variable.include_css.split(',');
		for(var i=0;i<include_css_list.length;i++){
			WysiwygEditor.setCSSFile(iframe_doc,variable.return_path+include_css_list[i]);
		}
	}

	if(!variable.background.length){
		iframe_doc.body.style.backgroundColor=check_parent_node_style(document.getElementById(iframe_id),"background");
	}else{
		iframe_doc.body.style.backgroundColor=variable.background;
	}
	if(!variable.color.length){
		iframe_doc.body.style.color=check_parent_node_style(document.getElementById(iframe_id),"color");
	}else{
		iframe_doc.body.style.color=variable.color;
	}

	iframe_doc.body.style.height='auto';
	iframe_doc.body.style.margin='0';
	iframe_doc.body.style.padding='10px';
	iframe_doc.body.style.backgroundImage='none';

	if(variable.include_js.length){
		var include_js_list=variable.include_js.split(',');
		for(var i=0;i<include_js_list.length;i++){
			WysiwygEditor.setJSFile(iframe_doc,variable.return_path+include_js_list[i]);
		}
	}
	if(document.getElementById(variable.data_id)){
		if(WysiwygEditor.browseIE){
			var text=document.getElementById(variable.data_id).innerText;
			iframe_doc.body.innerHTML=text;
		}else{
			var html=iframe_doc.body.ownerDocument.createRange();
			html.selectNodeContents(document.getElementById(variable.data_id));
			iframe_doc.body.innerHTML=html;
		}
		if(variable.align=="center"){
			iframe_doc.body.style.textAlign="center";
		}else if(variable.align=="right"){
			iframe_doc.body.style.textAlign="right";
		}else{
			iframe_doc.body.style.textAlign="left";
		}
	}
}

function check_parent_node_style(node, mode){
	var style=node.currentStyle || document.defaultView.getComputedStyle(node,'');
	if(mode=="background"){
		if(node.style && node.style.backgroundColor && node.style.backgroundColor.length && node.style.backgroundColor!="transparent"){
			return node.style.backgroundColor;
		}else if(style.backgroundColor && style.backgroundColor.length && style.backgroundColor!="transparent"){
			return style.backgroundColor;
		}
	}
	if(mode=="color"){
		if(node.style && node.style.color && node.style.color.length){
			return node.style.color;
		}else if(style.color && style.color.length){
			return style.color;
		}
	}
	if(node.parentNode){
		return check_parent_node_style(node.parentNode, mode);
	}else{
		return "";
	}
}

function submit_wysiwyg_data(form){
	if(WysiwygEditor.iframeList.length){
		for(var i=0;i<WysiwygEditor.iframeList.length;i++){
			var iframe_id=WysiwygEditor.iframeList[i];
			var variable=WysiwygEditor.iframeVariableList[iframe_id];

			//編集モードのものは元に戻してから作業
			if(!variable.html_mode){
				WysiwygEditor.changeMode(iframe_id);
			}

			var iframe_doc=WysiwygEditor.getDocument(iframe_id);
			if(document.getElementById(variable.data_id)){
				var wysiwyg_data=iframe_doc.body.innerHTML;
				wysiwyg_data=wysiwyg_data.replace(/<P/g,'<p');
				wysiwyg_data=wysiwyg_data.replace(/<\/P/g,'</p');
				wysiwyg_data=wysiwyg_data.replace(/<DIV/g,'<div');
				wysiwyg_data=wysiwyg_data.replace(/<\/DIV/g,'</div');
				wysiwyg_data=wysiwyg_data.replace(/<SPAN/g,'<span');
				wysiwyg_data=wysiwyg_data.replace(/<\/SPAN/g,'</span');
				wysiwyg_data=wysiwyg_data.replace(/<FONT/g,'<font');
				wysiwyg_data=wysiwyg_data.replace(/<\/FONT/g,'</font');
				wysiwyg_data=wysiwyg_data.replace(/<IMG/g,'<img');
				wysiwyg_data=wysiwyg_data.replace(/<STRONG/g,'<strong');
				wysiwyg_data=wysiwyg_data.replace(/<\/STRONG/g,'</strong');
				wysiwyg_data=wysiwyg_data.replace(/<EM/g,'<em');
				wysiwyg_data=wysiwyg_data.replace(/<\/EM/g,'</em');
				wysiwyg_data=wysiwyg_data.replace(/<U/g,'<u');
				wysiwyg_data=wysiwyg_data.replace(/<\/U/g,'</u');
				wysiwyg_data=wysiwyg_data.replace(/<UL/g,'<ul');
				wysiwyg_data=wysiwyg_data.replace(/<\/UL/g,'</ul');
				wysiwyg_data=wysiwyg_data.replace(/<OL/g,'<ol');
				wysiwyg_data=wysiwyg_data.replace(/<\/OL/g,'</ol');
				wysiwyg_data=wysiwyg_data.replace(/<LI/g,'<li');
				wysiwyg_data=wysiwyg_data.replace(/<\/LI/g,'</li');
				wysiwyg_data=wysiwyg_data.replace(/<A/g,'<a');
				wysiwyg_data=wysiwyg_data.replace(/<\/A/g,'</a');
				wysiwyg_data=wysiwyg_data.replace(/<BR>/g,'<br />');
				wysiwyg_data=wysiwyg_data.replace(/<br>/g,'<br />');
				wysiwyg_data=wysiwyg_data.replace(/\r/g,'');
				wysiwyg_data=wysiwyg_data.replace(/\n/g,'');
				wysiwyg_data=wysiwyg_data.replace(/\t/g,'');

				if(wysiwyg_data.match(/<FORM/i)){
					alert("<form>タグが存在するため、登録できません。\n「直接編集」にて<form>タグを除去してください。");
					return false;
				}
				document.getElementById(variable.data_id).value=wysiwyg_data;
			}
		}
		form.submit();
	}
}
var wysiwyg_x_position;
var wysiwyg_y_position;
$(document).ready(function(){
	$("body").mousemove(function(e){
		wysiwyg_x_position=e.pageX+10;
		wysiwyg_y_position=e.pageY+10;
	});
	on_load_design_mode();
});

