/*
Geocaching Bookmark Buttons - v1.4 2010-01-19
(c) 2010, Prime Suspect Software

Greasemonkey user script: see http://greasemonkey.mozdev.org

Compatible with Greasemonkey 0.6.4.

Function:
 Adds 'Check All Active' 'Check All Archived' and 'Check All
 Disabled' buttons next to the existing 'Check All' button.
 Like the 'Check All' button, these toggle between Check and
 Uncheck functions when they are clicked.

 Also, adds the bookmark title as the link title text, as the
 strike-through font can sometimes make it difficult to read.

Usage:
 Just click, to check or uncheck the appropriate type of entry.

Change Log:

* (v1.4) 2010-01-19 Update for site changes.

* (v1.3) 2007-10-11 Updated to accomodate site changes to bookmarks.

* (v1.2) Removed blue-bar code, since it has been incorporated
  in the site.

* (v1.1) Added "blue-bar" display to bookmark table to
  increase readability.

* (v1.1) Updated to accomodate site changes to bookmarks.

* (v1.2) Updated to accomodate site changes to bookmarks.

* (v1.0) Initial release.
*/

// ==UserScript==
// @name           GC Bookmark Buttons
// @namespace      http://gmscripts.locusprime.net/
// @description    (v1.4) Adds buttons to select all archived/disabled
// @include        http://www.geocaching.com/bookmarks/view.aspx?*
// @include        http://www.geocaching.com/bookmarks/bulk.aspx?*



	//  Globals.
	var xButtonID;
	var checkVal = new Array(false, false, false);

	//  Get handle to 'Bulk Delete' button.

	e_BulkDelete = document.getElementById("ctl00_ContentBody_ListInfo_btnDelete");
/* 	ButtonList = document.evaluate(
		"//input[@value='Bulk delete']",
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);
	if (ButtonList.snapshotLength == 1) {
		var e_BulkDelete = ButtonList.snapshotItem(0);
	} else {
		return;
	}
 */

 //  Generate list of checkboxes.
	var CheckBoxList = document.evaluate(
		"//input[@type='checkbox']",
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);

		//  Create spacer 1.
	e_space_span1 = document.createElement("span");
	e_space_span1.id = 'space_span1';
	e_space_span1.innerHTML = '&nbsp;';
	e_BulkDelete.parentNode.insertBefore(e_space_span1, e_BulkDelete.nextSibling);

	//  Create 'Active' button.
	e_btn_AllActive = document.createElement("input");
	e_btn_AllActive.id = 'btn_AllActive';
	e_btn_AllActive.type = 'button';
	e_btn_AllActive.name = 'btn_AllActive';
	e_btn_AllActive.value = 'Check Active';
	e_space_span1.parentNode.insertBefore(e_btn_AllActive, e_space_span1.nextSibling);

	//  Create spacer 2.
	e_space_span2 = document.createElement("span");
	e_space_span2.id = 'space_span2';
	e_space_span2.innerHTML = '&nbsp;';
	e_btn_AllActive.parentNode.insertBefore(e_space_span2, e_btn_AllActive.nextSibling);

	//  Create 'Disabled' button.
	e_btn_AllDisabled = document.createElement("input");
	e_btn_AllDisabled.id = 'btn_AllDisabled';
	e_btn_AllDisabled.type = 'button';
	e_btn_AllDisabled.name = 'btn_AllDisabled';
	e_btn_AllDisabled.value = 'Check Disabled';
	e_space_span2.parentNode.insertBefore(e_btn_AllDisabled, e_space_span2.nextSibling);

	//  Create spacer 3.
	e_space_span3 = document.createElement("span");
	e_space_span3.id = 'space_span3';
	e_space_span3.innerHTML = '&nbsp;';
	e_btn_AllDisabled.parentNode.insertBefore(e_space_span3, e_btn_AllDisabled.nextSibling);

	//  Create 'All Archived' button.
	e_btn_AllArchived = document.createElement("input");
	e_btn_AllArchived.id = 'btn_AllArchived';
	e_btn_AllArchived.type = 'button';
	e_btn_AllArchived.name = 'btn_AllArchived';
	e_btn_AllArchived.value = 'Check Archived';
	e_space_span3.parentNode.insertBefore(e_btn_AllArchived, e_space_span3.nextSibling);

	//  Add event listeners to buttons.
	e_btn_AllActive.addEventListener('click', AllActiveClicked, false);
	e_btn_AllArchived.addEventListener('click', AllArchivedClicked, false);
	e_btn_AllDisabled.addEventListener('click', AllDisabledClicked, false);

	//  Get collection of links.
	var AllLinks = document.evaluate(
		"//a[@href]",
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);
	maxR = AllLinks.snapshotLength;
	//  Look for cache links.
	for (var i = 0; i < maxR; i++) {
		var xLink = AllLinks.snapshotItem(i);
		//  If a cache link.
		if (xLink.href.search(/\/seek\/cache_details\.aspx\?/,"g") >= 0) {
			var zText = xLink.lastChild;
			while ((zText.nodeName != '#text') && (zText)) {
				zText = zText.firstChild
			}
			xLink.title = zText.data;
		}
	}


//	------------------------------ Functions ------------------------------


	//  Event listener for All Active button.
	function AllActiveClicked() {
		xButtonID = e_btn_AllActive.id;
		AllOfType(0);
	}

	
	
	//  Event listener for All Disabled button.
	function AllDisabledClicked() {
		xButtonID = e_btn_AllDisabled.id;
		AllOfType(1);
	}

	
	
	//  Event listener for All Archived button.
	function AllArchivedClicked() {
		xButtonID = e_btn_AllArchived.id;
		AllOfType(2);
	}

	
	
	//  Set checkmarks, based on cache status.
	// Active: 		TD
	//					A
	//						IMG
	//						#text
	// Disabled:	TD
	//					A
	//						IMG
	//						SPAN (class = 'Strike')
	//							#text
	// Archived:	TD
	//					A
	//						IMG
	//						SPAN (class = 'Warning Strike')
	//							#text
	function AllOfType(aot) {
		//  Toggle action value.
		checkVal[aot] = !checkVal[aot];
		//  Process checkbox list.
		var maxR = CheckBoxList.snapshotLength;
		if (maxR > 0) {
			for (var i = 0; i < maxR; i++) {
 				var aCheckBox = CheckBoxList.snapshotItem(i);
				var xNode = aCheckBox.parentNode.parentNode.cells[3].firstChild.lastChild;
				if (xNode.nodeName == 'SPAN') {
					thisClass = xNode.getAttribute('class');
					if (thisClass == 'Strike') {
						status = 1; 
					} else if (thisClass == 'Warning Strike') {
						status = 2; 
					} else { 
						status = 0; 
					}						
				} else { 
					status = 0;
				}
				if (aot == status) {aCheckBox.checked = checkVal[aot]}

			}
		}
		//  Toggle button label.
		var btnLabel = document.getElementById(xButtonID).value;
		if (checkVal[aot]) {
			btnLabel = btnLabel.replace(/Check/, 'Uncheck');
		} else {
			btnLabel = btnLabel.replace(/Uncheck/, 'Check');
		}
		document.getElementById(xButtonID).value = btnLabel;
	}

