<?php include_once( "../../../spider.inc.php" ); ?>{charset:UTF-8}<?php header( 'Content-type: text/plain;charset=UTF-8' ); ?>
//
// require TextFormer
FormerContentEditor	= function() {
	// オブジェクト作成
	var self	= document.createElement('div');
	// 定義
	var agentHash	= new Array();
<?php foreach( $GLOBALS['SPIDER_USER_AGENT_CLASS_HASH'] as $key => $val ) { ?>	agentHash['<?php echo $key; ?>']	= '<?php echo $val; ?>';<?php echo "\n"; ?><?php } ?>
	// 引数処理
	if( arguments.length < 2 ) {
		alert('require 2 arguments!');
		return;
	} else if( arguments.length == 2 ) {
		self.targetBlock		= arguments[0];
		self.targetFileUri		= arguments[1];
		self.targetType			= 'public';
		self.agentType			= 'default';
		self._execConfirmSave	= true;
	} else if( arguments.length == 3 ) {
		self.targetBlock		= arguments[0];
		self.targetFileUri		= arguments[1];
		self.targetType			= arguments[2];
		self.agentType			= 'default';
		self._execConfirmSave	= true;
	} else if( arguments.length == 4 ) {
		self.targetBlock		= arguments[0];
		self.targetFileUri		= arguments[1];
		self.targetType			= arguments[2];
		self.agentType			= arguments[3];
		self._execConfirmSave	= true;
	} else if( arguments.length == 5 ) {
		self.targetBlock		= arguments[0];
		self.targetFileUri		= arguments[1];
		self.targetType			= arguments[2];
		self.agentType			= arguments[3];
		self._execConfirmSave	= arguments[4];
	}
	var isExistAgent	= false;
	for (var key in agentHash) {
		if( key == self.agentType){ isExistAgent=true; break; }
	}
	if( !isExistAgent ) {
		self.agentType	= 'default';
	}
	// ベースパス判断
	var scriptTags 				= document.getElementsByTagName('script');
	for(var i=0,n=scriptTags.length;i<n;i++) {
		if(scriptTags[i].src.match('editor.js.php')) {
			self.basePath = scriptTags[i].src.replace('editor.js.php','');
		}
	}
	// id
	self.id				= self.targetBlock.id + '_editor';
	// http object
	self.httpObject;
	
	// formの作成
	self.formObject			= document.createElement('form');
	self.formObject.id		= self.targetBlock.id + '_editor_form';
	self.formObject.action	= self.basePath + 'confirm.php';
	self.formObject.method	= 'post';
	self.appendChild(self.formObject);
	
	// 隠しフォームの作成
	self.hiddenTarget			= document.createElement('input');
	self.hiddenTarget.id		= self.targetBlock.id + '_editor_target';
	self.hiddenTarget.type		= 'hidden';
	self.hiddenTarget.name		= 'target_uri';
	self.hiddenTarget.value		= self.targetFileUri;
	self.formObject.appendChild(self.hiddenTarget);
	
	// 編集テキストエリアの追加
	self.editTextArea			= document.createElement('textarea');
	self.editTextArea.id		= self.targetBlock.id + '_editor_text';
	self.editTextArea.name		= 'text_body';
	self.formObject.appendChild(self.editTextArea);
	
	// 確認メソッド
	self.confirm	= function() {
		var contents	= "command=0";
		contents	+= "&" + encodeURIComponent('agent_type')+ "="+encodeURIComponent(this.agentType);
		contents	+= "&" + encodeURIComponent('target_type')+ "="+encodeURIComponent(this.targetType);
		contents	+= "&" + encodeURIComponent('target_uri')+ "="+encodeURIComponent(this.targetFileUri);
		contents	+= "&" + encodeURIComponent('target_body')+ "="+encodeURIComponent(this.textFormerObject.getText());
		this.httpObject	= FormerCommon.createHttpRequest("document.getElementById('" + this.id + "').confirmResult()");
		var url	= self.basePath + 'confirm.php';
		if (this.httpObject) {
			FormerCommon.displayGrayOut(9999);
			this.httpObject.open("POST",url,true);
			this.httpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.httpObject.send(contents);
			FormerCommon.displayGrayOut(9999);
		} else {
			alert('サーバーへの接続作成に失敗しました。');
		}
	};
	self.confirmResult	= function() {
		if ((this.httpObject.readyState == 4) && (this.httpObject.status == 200)){
		    var msg = document.getElementById(this.id).confirmResponse(this.httpObject.responseText);
		    if( msg.length > 0 ) {
		    	alert('一時保存に失敗しました\n\n'+msg);
				setTimeout(FormerCommon.displayWhiteIn, 100);
				this.httpObject	= null;
		    } else if( this._execConfirmSave ){
				this.httpObject	= null;
				setTimeout("document.getElementById('" + this.id + "').save()", 100);
		    } else {
				setTimeout(FormerCommon.displayWhiteIn, 1000);
				this.httpObject	= null;
		    }
		} else if ((this.httpObject.readyState == 4) && (this.httpObject.status != 200)){
			alert('一時保存先が見つからないかエラーになりました');
			setTimeout(FormerCommon.displayWhiteIn, 100);
			this.httpObject	= null;
		}
	};
	// 保存メソッド
	self.save	= function() {
		var contents	= "command=0";
		contents	+= "&" + encodeURIComponent('agent_type')+ "="+encodeURIComponent(this.agentType);
		contents	+= "&" + encodeURIComponent('target_type')+ "="+encodeURIComponent(this.targetType);
		contents	+= "&" + encodeURIComponent('target_uri')+ "="+encodeURIComponent(this.targetFileUri);
		contents	+= "&" + encodeURIComponent('target_body')+ "="+encodeURIComponent(this.textFormerObject.getText());
		this.httpObject	= FormerCommon.createHttpRequest("document.getElementById('" + this.id + "').saveResult()");
		var url	= self.basePath + 'execute.php';
		if (this.httpObject) {
			FormerCommon.displayGrayOut(9999);
			this.httpObject.open("POST",url,true);
			this.httpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.httpObject.send(contents);
			FormerCommon.displayGrayOut(9999);
		} else {
			alert('サーバーへの接続作成に失敗しました。');
		}
	};
	self.saveResult	= function() {
		if ((this.httpObject.readyState == 4) && (this.httpObject.status == 200)){
		    var msg = document.getElementById(this.id).confirmResponse(this.httpObject.responseText);
		    if( msg.length > 0 ) {
		    	alert('保存に失敗しました\n\n'+msg);
		    } else {
		    	alert('保存しました');
		    }
			setTimeout(FormerCommon.displayWhiteIn, 100);
			this.httpObject	= null;
		} else if ((this.httpObject.readyState == 4) && (this.httpObject.status != 200)){
			alert('保存先が見つからないかエラーになりました');
			setTimeout(FormerCommon.displayWhiteIn, 100);
			this.httpObject	= null;
		}
	};
	// レスポンスメッセージ解析
	self.confirmResponse	= function(msg) {
	    var lines	= msg.cr2lf().split(LF);
	    var isError = false;
	    var error_message	= '';
	    for( var i=0; i<lines.length; i++ ){
			if( String(lines[i]).cr2lf().trim().length>0 ){
				var result	= String(lines[i]).cr2lf().trim();
				if( result == 'OK' ) {
					break;
				} else if( isError ) {
					error_message += result + "\n";
				} else {
					isError	= true;
				}
			}
		}
		if( isError ) {
			return error_message;
		} else {
			return '';
		}
	};
	// 終了メソッド
	self.closeEdit	= function() {
		if( confirm('編集を終了してひとつ前に表示していたページに戻ります。\n保存していない内容は消えてしまいます。\n本当に戻ってよろしいですか?') ) {
			window.history.back();
		}
	};
	// 編集するファイルを切り替え
	self.changeEditFile	= function() {
		if( arguments.length < 1 ) {
			alert('読み込み方法が正しくありません!');
			return;
		} else if( arguments.length == 1 ) {
			this.targetFileUri		= arguments[0];
			this.agentType			= 'default';
		} else if( arguments.length == 2 ) {
			this.targetFileUri		= arguments[0];
			this.agentType			= arguments[1];
		}
		this.reload();
	};
	// 文章再読み込みメソッド
	self.reload		= function() {
		this.httpObject	= FormerCommon.createHttpRequest("document.getElementById('" + this.id + "').reloadResult()");
		FormerCommon.displayGrayOut(9999);
		var url	= self.basePath + 'getbody.php?ctagent=' + encodeURIComponent(this.agentType)
			+ '&cttype=' + encodeURIComponent(this.targetType)
			+ '&cturi=' + encodeURIComponent(this.targetFileUri)
		;
		if (this.httpObject) {
			this.httpObject.open("GET", url, true);
			this.httpObject.send(null);
		} else {
			alert('サーバーへの接続作成に失敗しました。');
		}
		
	};
	self.reloadResult	= function() {
		if ((this.httpObject.readyState == 4) && (this.httpObject.status == 200)){
			this.textFormerObject.setText(this.httpObject.responseText);
			setTimeout(FormerCommon.displayWhiteIn, 100);
			this.httpObject	= null;
		} else if ((this.httpObject.readyState == 4) && (this.httpObject.status != 200)){
			alert('読み込みの通信に失敗しました');
			setTimeout(FormerCommon.displayWhiteIn, 100);
			this.httpObject	= null;
		}
	};
	
	// ターゲットブロックの子として追加
	self.targetBlock.appendChild(self);

	// textformer作成
	var saveMethodStr		= "document.getElementById('" + self.id + "').confirm()";
	var closeMethodStr		= "document.getElementById('" + self.id + "').closeEdit()";
	self.textFormerObject	= TextFormer( self.editTextArea, saveMethodStr, closeMethodStr );
	self.textFormerObject.setSubject(self.targetFileUri + 'の編集');
	// 文章再読み込み
	self.reload();
	
	return self;
}

