function fs_get_postdiv() {
	var postdiv = document.getElementById('postdiv');
	var postdivrich = document.getElementById('postdivrich');
	if (!postdiv) postdiv = postdivrich;
	return postdiv;
}

function fs_get_editbox() {
	return document.getElementById('content');
}

function fs_get_toolbar() {
	return document.getElementById('ed_toolbar');
}

function fs_postdiv_exists() {
	if (fs_get_postdiv()) return true;
	return false;
}

function fs_using_mce() {
	if (typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1) return false;
	return true;
}

function fs_is_fullscreen() {
	var postdiv = fs_get_postdiv();
	return postdiv.getAttribute("fullscreen");
}

function recalculate_resize_editbox() {

	var content = fs_get_editbox();
	var toolbar = fs_get_toolbar();
	var postdiv = fs_get_postdiv();

	if (fs_is_fullscreen()) {
		if (content && toolbar) {
			var difference = postdiv.offsetHeight - content.offsetTop - toolbar.offsetTop;
			content.setAttribute("style","width: 99%; height: " + difference.toString() + "px;");
		}
		setTimeout('recalculate_resize_editbox()', 250);
	}

}

function show_legends(under_this_element) {
	var legends = under_this_element.getElementsByTagName("legend");
	var i = 0; for (i = 0; i < legends.length; i++) legends[i].setAttribute("style","");
}

function hide_legends(under_this_element) {
	var legends = under_this_element.getElementsByTagName("legend");
	var i = 0; for (i = 0; i < legends.length; i++) legends[i].setAttribute("style","display: none;");
}

function fs_show_close_button() {
	var e = document.getElementById("ed_fullscreen");
	e.value = "Restore";
	e.title = "Turns off full screen mode";
	if (fs_using_mce()) {
		e.setAttribute("style","position: fixed; z-index: 30; top: 1em; right: 1em;");
	}
}

function fs_hide_close_button() {
	var e = document.getElementById("ed_fullscreen");
	e.value = "Go full screen";
	e.title = "Makes the post editor go full screen";
	if (fs_using_mce()) {
		e.setAttribute("style","");
	}
}


function fs_go_fullscreen() {
	var postdiv = fs_get_postdiv();
	postdiv.setAttribute("fullscreen","1");

	postdiv.setAttribute("style","position: fixed; z-index: 20; top: 1em; left: 1em; right: 1em; bottom: 1em; padding: 0em; background: white;");

	hide_legends(postdiv);

	if (fs_get_editbox()) { fs_get_editbox().focus(); }

	fs_show_close_button();

	recalculate_resize_editbox(); // spawn separate resizing thread
}

function fs_restore() {
	var postdiv = fs_get_postdiv();
	postdiv.setAttribute("fullscreen","");

	fs_hide_close_button();

	show_legends(postdiv);

	if (fs_get_editbox()) { fs_get_editbox().focus(); }

	postdiv.setAttribute("style",""); /* we finally restore the postdiv to a sane state */
}

function toggle_fullscreen() {

	if (!fs_postdiv_exists()) return;
	var postdiv = fs_get_postdiv();

	if (fs_is_fullscreen()) fs_restore();
	else fs_go_fullscreen();

}

function fs_add_fullscreen_link() {

	if (!fs_postdiv_exists()) return;
	var postdiv = fs_get_postdiv();

	var theButton = document.createElement('input');
	theButton.type = 'button';
	theButton.value = 'Go full screen';
	theButton.onclick = toggle_fullscreen;
	theButton.title = "Makes the post editor go full screen";
	theButton.id = "ed_fullscreen";

	if ( fs_using_mce() ) {
		if (document.getElementById('save')) {
			var save = document.getElementById('save').parentNode;
			save.insertBefore(document.createTextNode(" "),save.firstChild);
			save.insertBefore(theButton,save.firstChild);
		}
	} else  {
		if (document.getElementById('ed_toolbar')) {
			theButton.className = 'ed_button';
			document.getElementById('ed_toolbar').appendChild(theButton);
		}
	}

}

function fs_make_preview_full_height() {
	var preview = document.getElementById("preview"); if (!preview) return;
	var iframes = preview.getElementsByTagName("iframe"); if (!iframes) return;
	var iframe = iframes[0]; if (!iframe) return;
	iframe.height = iframe.contentWindow.document.height;
	setTimeout('fs_make_preview_full_height()', 1000);
}

fs_add_fullscreen_link();
fs_make_preview_full_height();