// Greasepress
// version 0.11
// 2005-06-10
// Copyright (c) 2005, drac (http://lair.fierydragon.org)
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Greasepress", and click Uninstall.
//
// Important: this script does *NOT* make use of XMLHTTPRequest.
// ie: The script should never generate additional HTTP requests.

// Known bugs: Quick tags never restores to the correct button order. Blech.
// Credits: Learnt lots from Dive into Greasemonkey (the book), Fark Butler and RSSPanel scripts

// changelog:
// 0.1 - initial release. quicktags, categories and trackback toggles, saves options.
// 0.11 - bugfix. detects a draft page and inserts_UI after password fieldset instead of always after title fieldset
// ==UserScript==
// @name          Greasepress
// @namespace   http://lair.fierydragon.org
// @description  Toggle visibility of Wordpress post authoring UI elements. Category selection,  QuickTags and Trackback widgets are currently supported.
// @include       */wp-admin/post.php*
// ==/UserScript==)
var version_String = "0.11";

var original_marginRight = '11em'; // todo: use getComputedStyle instead of hardcoding the value. Didn't work first time round ?

var option_hideCategory;
var option_hideEditTools;
var option_hideTrackback;

function toggle_post_width(post_Div, toggle) {
	if (toggle) { post_Div.style.marginRight = "1em"; 	}
	else { post_Div.style.marginRight = original_marginRight; }	
}

function toggle_visibility (action_element, toggle) {
	if (toggle) { action_element.style.display = 'none'; }
	else { action_element.style.display = 'block'; }
}

function set_handlers() {	
	document.getElementById('toggle_cat').onclick = 
		function() { 
			toggle_visibility(document.getElementById("categorydiv"), document.post.toggle_cat_display.checked); 
			toggle_post_width(document.getElementById("poststuff"), document.post.toggle_cat_display.checked);
			option_hideCategory = document.post.toggle_cat_display.checked;
		};	

	document.getElementById('toggle_ed').onclick = 
		function() { 
			toggle_visibility(document.getElementById('quicktags'), document.post.toggle_ed_display.checked);
			option_hideEditTools = document.post.toggle_ed_display.checked;
		};

	document.getElementById('toggle_track').onclick = 
		function() { 
			if (window.location.href.match(/post.php$/i)) {	
				toggle_visibility(document.evaluate("//div[@id='poststuff']/p", document, null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null).snapshotItem(0), document.post.toggle_track_display.checked);			
			}			
			option_hideTrackback = document.post.toggle_track_display.checked;
		};	
	
	if (GM_setValue) 	{
		document.getElementById('save_grease_options').onclick =
			function() { 
				GM_setValue('hide_categories', option_hideCategory); 
				GM_setValue('hide_edit', option_hideEditTools); 
				GM_setValue('hide_trackback', option_hideTrackback); 

				alert('Greasepress options saved');
			}
	}	
}

function insert_UI() {
	var grease_fieldset = document.createElement("fieldset");
	grease_fieldset.style.height = "4em;";
	// oddity: The diveintogreasemonkey method (insert complex html) produces 2 fieldsets, hence no starting fieldset tag below.
	// <fieldset> instead of ' ' as the first string produces a nested fieldset.
	// This is a bug ? Or undocumented behaviour ? Or I'm just crazy.

	grease_fieldset.innerHTML = ' ' +
	  '<legend><a href="javascript:alert(\'Greasepress '+ version_String + ' is a GreaseMonkey user script. \\n\\nPlease disable or uninstall the script locally if you do not wish to see these options.\\n\\n The latest version can be found at http://lair.fierydragon.org\');">' +
		'Greasepress options</a></legend>' +
	  'Hide categories' +
	  '<input type="checkbox" value ="hide_cat" id = "toggle_cat" name="toggle_cat_display" />&nbsp;&nbsp;' +
	  'Hide quicktags' +
	  '<input type="checkbox" value ="hide_ed" id = "toggle_ed" name="toggle_ed_display"/>&nbsp;&nbsp;' +
	  'Hide trackback' +
	  '<input type="checkbox" value ="hide_track" id = "toggle_track" name="toggle_track_display" />&nbsp;&nbsp;&nbsp;&nbsp;' +	  	 
	 '<span align="right"><input type="button" value ="Save settings" id = "save_grease_options" name="save_opt""/></span>' +	  	 
	  '</fieldset>';
	var page_elem;
	var href = window.location.href;
	if (href.match(/post.php$/i)) {	
		page_elem = document.getElementById('titlediv');
	}
	else {
		page_elem = document.getElementById('postpassworddiv');
	}
	// any other case other than draft or initial post mode and this check is gonna break. Submit bug reports
	if (page_elem) 	{
		page_elem.parentNode.insertBefore(grease_fieldset, page_elem.nextSibling);
	}	
}

function restore_saved_options() {
	if (!GM_getValue) {
		// redundant use of a redundant check ;)
		alert('This browser is running a version of Greasemonkey below 0.3. Please consider upgrading');
		return;
	}	
	document.post.toggle_cat_display.checked = option_hideCategory = GM_getValue('hide_categories', false);	
	document.post.toggle_ed_display.checked = option_hideEditTools = GM_getValue('hide_edit', false);	
	document.post.toggle_track_display.checked = option_hideTrackback = GM_getValue('hide_trackback', false);
		
	document.getElementById('toggle_cat').onclick();
	document.getElementById('toggle_ed').onclick();
	document.getElementById('toggle_track').onclick();
}

function setup_environment() {
	insert_UI();
	set_handlers();
}

setup_environment();
if (GM_getValue) {
	restore_saved_options(); // if GM_get and setValue is not available, this function can be commented out
}
