//http://tinymce.moxiecode.com/punbb/viewtopic.php?id=2755
//http://wiki.moxiecode.com/index.php/TinyMCE:Auto_resize_editor_box
// http://tracker.moodle.org/browse/CONTRIB-1456

function toggleEditor(id) 
{
	// is tinyMCE available?
	if (!tinyMCE) return;
	
	if (!tinyMCE.get(id))
		tinyMCE.execCommand('mceAddControl', false, id);
	else		
		tinyMCE.execCommand('mceRemoveControl', false, id);
}

	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		save_onsavecallback : "on_save",
		height : "750px", 
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,wordcount,fullpage",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,cleanup,code,styleprops,attribs,|,preview,fullscreen,fullpage,|,cut,copy,paste,pastetext,|,search,replace,|,undo,redo,|,removeformat,visualaid,visualchars,help",
		theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect,|,forecolor,backcolor,|,bullist,numlist,|,outdent,indent,blockquote,|,hr,|,sub,sup,",
		theme_advanced_buttons3 : ",link,unlink,anchor,image,|,insertdate,inserttime,tablecontrols,|,charmap,emotions,media,advhr,|,insertlayer,moveforward,movebackward,absolute,|,cite,abbr,acronym,del,ins,|,nonbreaking,pagebreak",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "center",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		// content_css : "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
	

function on_save()
{
	if (loggedOn)
	{
		var filename = document.getElementById('filename').value;
		if (filename == "")
		{
			filename = window.prompt("Filename:");
		}
		if (filename == null) return;
		// check whether file already exists?
		document.getElementById('filename').value = filename;
		document.forms['editing'].setAttribute('action','../save/save.php');
		document.forms['editing'].submit();
	}
	else
	{
		if (tinyMCE) tinyMCE.execCommand('mceRemoveControl', false, 'content');
		alert("Saving restricted. Use 'File: Save page as' to save locally.");	
	}	
}

var loggedOn = false;
function logon()
{
	http.open('GET',"../logon/index.php",true);
	http.onreadystatechange = logon_response;  	
	http.send(null);			
}

function check_logon()
{
	http.open('GET',"../sincludes/user.php",true);
	http.onreadystatechange = logon_response;  	
	http.send(null);				
}

function logon_response()
{	
	if (http.readyState == 4) 
	{	
		if (http.status != 200) 
		{
			return;
		}
		// get response text
		var response = (http.ResponseText)?http.ResponseText:http.responseText;		
		response = unescape(response);
		// delay updating logon for page to load
		setTimeout("update_logon('"+response.substr(0,10)+"')",3000);
		if (response.indexOf("thebrentc") == 0) 
		{
			// will re-authenticate when saving		
			loggedOn = true;		
		}
	}
}
if (document.referrer.indexOf("save.php") > -1) 
{
	logon();
}

function update_logon(user)
{
	//document.getElementById("logon").innerHTML = user;
	document.title += " ("+user+")";
}
