// ==UserScript==
// @name           Postbit Toggle
// @namespace      http://digitalcreations.cc
// @description    Allows the user to toggle the display of each postbit
// @include        http://*tacticalgamer.com/*
// ==/UserScript==

htmlNode = document.evaluate('/html', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue
if (htmlNode.innerHTML.indexOf('templates/wow') == -1) {
	postAnchors = document.evaluate("//a[@name]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	for (var i = 0; i < postAnchors.snapshotLength; i++) {
		postAnchor = postAnchors.snapshotItem(i);
		if (postAnchor.name.indexOf('post') == 0) {
			postId = postAnchor.name.replace('post','')
			postTable = document.getElementById(postAnchor.name)

			if (postTable) {
				if (postTable.rows) {
					postbitRow = postTable.rows[1]
					postbitCell = postbitRow.cells[0]
					postbitCellDivs = postbitCell.getElementsByTagName('div')
					for (var j = 0; j < postbitCellDivs.length; j++) {
						if (postbitCellDivs[j].getElementsByTagName('div').length >= 3) {
							postbitDiv = postbitCellDivs[j]
							postbitDiv.id = 'postbitDiv' + postId
							postbitDiv.style.display = 'none'
							postbitCell.addEventListener('click',eval("function(){togglePostBitDisplay('"+postbitDiv.id+"')}"),false)

							postbitAwardsDiv = postbitCellDivs[postbitCellDivs.length - 1]
							if (postbitAwardsDiv.style.display != 'none') {
								postbitAwardsDiv.id = 'postbitAwardDiv' + postId
								postbitAwardsDiv.style.display = 'none'
								postbitCell.addEventListener('click',eval("function(){togglePostBitDisplay('"+postbitAwardsDiv.id+"')}"),false)
							}
						}
					}
				}
			}
		}
	}

	if (location.href.indexOf('#') != -1) {
		location.href = location.href
	}
}

function togglePostBitDisplay(id) {
	obj = document.getElementById(id)
	if (obj) {
		if (obj.style.display == 'none') {
			obj.style.display = ''
		}
	else {
		obj.style.display = 'none'
	}
}
}