

Event.observe(window,"load",function(){
	
	$('entry_content_wrapper').style.position = "relative";
	$('entry_more_wrapper').style.position = "absolute";
	$('entry_content_wrapper').style.visibility = "visible";
	
	Event.observe($('entry_content_switch'),"click",function(){
		$('entry_content_wrapper').style.position = "relative";
		$('entry_content_wrapper').style.visibility = "visible";
		
		$('entry_more_wrapper').style.position = "absolute";
		$('entry_more_wrapper').style.visibility = "hidden";
		
		$('entry_content_switch').setAttribute("class","content_tab_active");
		$('entry_content_switch').className = "content_tab_active";
		
		$('entry_more_switch').setAttribute("class","content_tab_inactive");
		$('entry_more_switch').className = "content_tab_inactive";
	});


	Event.observe($('entry_more_switch'),"click",function(){
		$('entry_more_wrapper').style.position = "relative";
				
		$('entry_content_wrapper').style.position = "absolute";
		$('entry_content_wrapper').style.visibility = "hidden";
		
		$('entry_more_switch').setAttribute("class","content_tab_active");
		$('entry_more_switch').className = "content_tab_active";
		
		$('entry_content_switch').setAttribute("class","content_tab_inactive");
		$('entry_content_switch').className = "content_tab_inactive";
		
		//safari rendering is too slow.
		$('entry_more_wrapper').style.visibility = "visible";
	});
	
	//toggle label
	$$('#labels input[type="checkbox"]').each(function(ele){
		toggle_labelmemo(ele.value,ele.checked);
	});

	//CKEditor適用
	editor_content = CKEDITOR.replace('entry_content',{
		contentsCss : entry_css_path	//スタイルの適用
	});
	editor_more = CKEDITOR.replace('entry_more',{
		contentsCss : entry_css_path	//スタイルの適用
	});
	
	var ckfinder_root = "";
	scripts = document.getElementsByTagName("script");
	for(var i=0; i<scripts.length; i++){
		var src = scripts[i].getAttribute("src");
		if(src == null || src.length < 1)continue;
		
		if(src.match(/\/ckfinder\.js/)){
			ckfinder_root = src.substr(0,src.indexOf("/ckfinder.js"));
		}
	}
	
	/**
	 * @ToDo CKFinder保留
	 */
	CKFinder.setupCKEditor( editor_content, ckfinder_root ) ;
	CKFinder.setupCKEditor( editor_more, ckfinder_root ) ;
},false);


function applyTemplate(){
	var template = document.getElementById("list_templates").value;
	
	if(template.length == 0){
		return;
	}
	
	var post = "id=" + template;
	
	var callback = function(oResponse) {
			var result = eval('('+oResponse.responseText+')');
			
			//本文・追記より先にスタイルシートをセットしないと反映されない
			$("style").value = result['templates']['style'];
			CKEDITOR.instances["entry_content"].addCss(result['templates']['style']);
			CKEDITOR.instances["entry_more"].addCss(result['templates']['style']);
			
			CKEDITOR.instances["entry_content"].setData(result['templates']['content']);
			CKEDITOR.instances["entry_more"].setData(result['templates']['more']);

			CKEDITOR.instances["entry_content"].updateElement();
			CKEDITOR.instances["entry_more"].updateElement();
	};
	
	var myAjax = new Ajax.Request(
		templateAjaxURL, 
		{
			method: 'post', 
			parameters: post, 
			onComplete: callback
		});
}

