/////////////////////////////////////////////////////////////////////////////////
// activateEditor
/////////////////////////////////////////////////////////////////////////////////
function activateEditor(obj) {
	obj.style.backgroundColor = '#fdfdf8';
	obj.style.border = 'dashed red 1px';
	obj.contentEditable = 'true';
}

/////////////////////////////////////////////////////////////////////////////////
// editPage
/////////////////////////////////////////////////////////////////////////////////
function editPage() {
	if(!refract.current_user_id)
		inplaceLogin();
	else
		showEditor();
}

/////////////////////////////////////////////////////////////////////////////////
// inplaceLogin
/////////////////////////////////////////////////////////////////////////////////
function inplaceLogin(){
	var c = document.createElement('div');
	c.id = 'refractInplaceLoginContainer';
	c.style.position = 'absolute';
	c.style.left = '50%';
	c.style.top = '0px';
	c.style.width = '0px';
	c.style.height = '0px';
	document.body.appendChild(c);

	var f = document.createElement('div');
	f.id = 'refractInplaceLoginForm';
	f.style.textAlign = 'left';
	f.style.position = 'absolute';
	f.style.backgroundColor = '#e0e0e0';
	f.style.marginLeft = '-125';
	f.style.marginTop = '0px';
	f.style.width = '250px';
	f.style.height = '100px';
	f.style.border = 'solid black 1px';
	f.style.padding = '0px';
	f.innerHTML = '\
		<div style="width: 100%; background-color: #b0b0b0; font-weight: bold; border-bottom: solid black 1px; text-align: center;">re:fract<sup>&trade;</sup> login</div>\n\
		<div style="padding: 5px; text-align: left; font-size: 12px;">\n\
		<form name="login" id="login" action="" onsubmit="return false;">\n\
			<div style="width: 240px;"><input style="float:right; width: 150px;" type="text" name="username" id="username" />username: </div>\n\
			<div style="width: 240px;"><input style="float:right; width: 150px;" type="password" name="password" id="password" />password: </div>\n\
			<div style="text-align: center; margin-top: 10px;"><img style="cursor: pointer;" src="_common/images/but_login_sm.gif" alt="log in" onclick="checkLogin(document.forms[\'login\'].elements.username.value, document.forms[\'login\'].elements.password.value);"/><img style="cursor: pointer;" src="_common/images/but_cancel.gif" alt="cancel" onclick="document.location.reload();" /></div>\n\
		</form>\n\
		</div>';
	document.body.style.filter = 'alpha(style=0,opacity=25)';
	c.appendChild(f);
	document.getElementById("username").focus();
}

/////////////////////////////////////////////////////////////////////////////////
// checkLogin
/////////////////////////////////////////////////////////////////////////////////
function checkLogin(username, password) {
	var download = new Downloader();
	download.startDownload('_common/login.asp?name=' + escape(username) + '&password=' + escape(password), eval);
	if(refract.current_user_id > 0 && refract.administrator == 'y') {
		document.body.style.filter = '';
		document.getElementById('refractInplaceLoginContainer').removeNode(true);
		showEditor();
	}
	else {
		alert('-- Login Failed --\n\nOnly site administrators may use the editor.');
		document.getElementById("username").focus();
	}
}

/////////////////////////////////////////////////////////////////////////////////
// showEditor
/////////////////////////////////////////////////////////////////////////////////
function showEditor() {
	var divs = document.body.getElementsByTagName('div');
	var d;
/*
	if(!canAccessPage(refract.current_page_id, refract.current_user_id)) {
		alert("Sorry, you do not have permission to edit this page.");
		return;
	}
*/

	for(var i = divs.length - 1 ; i >= 0 ; i--) {
		if(divs[i].className == 'editable') {
			d = divs[i]
			with (d.style) {
				borderStyle = 'dashed';
				borderColor = 'red';
				borderWidth  =  '1px';
			}
			activateForEditing(d);
		}
	}

	if(!d) {
		alert('Sorry, there is no editable content on this page.');
		return;
	}

	d.focus();

	d = document.createElement('div');
	d.style.position = 'absolute';
	d.style.width  = '150px';
	d.style.top    = '0px';
	d.style.right  = '0px';
	d.style.border = 'solid black 1px';
	d.style.backgroundColor = '#e0e0e0';
	d.style.filter = 'alpha(style=0,opacity=75)';
	d.innerHTML = '\
		<div style="width: 100%; background-color: #b0b0b0; font-weight: bold; border-bottom: solid black 1px; text-align: center;" ondblclick="toggleEditorState(this);return false;" unselectable="on">re:fract<sup>&trade;</sup> editor</div>\n\
		<div style="padding: 5px; text-align: left;">\n\
			<img style="cursor: pointer;" src="_common/images/but_savechanges.gif" onclick="inplaceEditorSave();" /><br />\n\
			<img style="cursor: pointer;" src="_common/images/but_cancel.gif" onclick="inplaceEditorCancel();" /><br />\n\
			<img style="cursor: pointer;" src="_common/images/but_logout.gif" onclick="inplaceEditorLogout();" />\n\
		</div>\n\
	';
	document.body.appendChild(d);
}

/////////////////////////////////////////////////////////////////////////////////
// activateForEditing
/////////////////////////////////////////////////////////////////////////////////
function activateForEditing(d) {
	d.contentEditable = true;
}

/////////////////////////////////////////////////////////////////////////////////
// toggleEditorState
/////////////////////////////////////////////////////////////////////////////////
function toggleEditorState(obj) {
	var e;
	if(e = obj.nextSibling) {
		e.style.display = (e.style.display == 'none') ? '' : 'none';
	}
}

/////////////////////////////////////////////////////////////////////////////////
// inplaceEditorSave
/////////////////////////////////////////////////////////////////////////////////
function inplaceEditorSave(doit) {
	if(!doit) {
		document.body.style.filter = 'alpha(style=0,opacity=25)';
		setTimeout('inplaceEditorSave(true)', 10);
	}

	var download = new Downloader();
	var postdata = new Array();
	postdata['command'] = 'save';
	var divs = document.body.getElementsByTagName('div');
	var d;

	for(var i = divs.length - 1 ; i >= 0 ; i--) {
		d = divs[i];
		if(d.className == 'editable') {
			postdata['content_id'] = d.id.replace(/^content_/,'');
			postdata['content_html'] = d.innerHTML;
			postdata['content_plain'] = d.innerText;
			var response = download.postURL('_refract/inplace_editor.asp', postdata);
			if(parseInt(response) != 200)
				alert('ERROR\n' + response);
		}
	}
	document.location.reload();
}

/////////////////////////////////////////////////////////////////////////////////
// inplaceEditorCancel
/////////////////////////////////////////////////////////////////////////////////
function inplaceEditorCancel() {
	if(!confirm('Abandon changes made to this page?'))
		return false;
	document.location.reload();
}

/////////////////////////////////////////////////////////////////////////////////
// inplaceEditorLogout
/////////////////////////////////////////////////////////////////////////////////
function inplaceEditorLogout() {
	if(!confirm('Abandon changes made to this page?'))
		return false;

	var download = new Downloader();
	download.startDownload('_common/login.asp?command=logout;' , eval);
	document.location.reload();
}

