// ==UserScript==
// @name          One Line Results
// @namespace     http://digitalcreations.cc
// @description   Rearranges search results such that only one line of text is displayed for each result
// @include       *tacticalgamer.com/search.php*
// ==/UserScript==

var threadslist, rows, cells;
threadslist = document.getElementById('threadslist')
threadslist.style.display = 'none'
rows = threadslist.getElementsByTagName('tr');
headerRow = rows[1]
if (headerRow) {
	headerRow.style.display = 'none'
}
for (var i = 2; i < rows.length; i++) {
	row = rows[i]
	cells = row.getElementsByTagName('td');
	if (cells.length >= 6) {
		forumIconCell = cells[0]
		if (forumIconCell) {
			forumIconCell.style.width = '20px'
		}
		threadIconCell = cells[1]
		if (threadIconCell) {
			threadIconCell.style.width = '20px'
		}
		titleCell = cells[2]
		if (titleCell) {
			titleCell.style.whiteSpace = 'nowrap'
			//titleCell.style.border = '2px solid blue'
			titleCellDivs = titleCell.getElementsByTagName('div')
			if (titleCellDivs.length >= 2) {
				titleCellFirstDiv = titleCellDivs[0]
				titleCellSpans = titleCellDivs[1].getElementsByTagName('span')
				if (titleCellSpans.length > 0) {
					authorSpan = titleCellSpans[0]
					if (authorSpan) {
						authorSpan.style.cssFloat = 'right'
						authorSpan.style.paddingRight = '5px'
						//authorSpan.style.border = '2px solid green'
						titleCellLinks = titleCellDivs[0].getElementsByTagName('a')
						if (titleCellLinks.length > 0) {
							titleLink = titleCellLinks[0]
							if (titleLink) {
								//titleLink.style.border = '2px solid orange'
								//titleLink.insertBefore(authorSpan, titleLink.childNodes[0])
								titleCellFirstDiv.insertBefore(authorSpan, titleCellFirstDiv.childNodes[0])
								images = titleCell.getElementsByTagName('img')
								for (var j = 0; j < images.length; j++) {
									image = images[j]
									if (image.title.indexOf('Sticky Thread') != -1) {
										image.style.display = 'none'
									}
								}
							}
						}
					}
				}
			}
		}

		lastPostCell = cells[3]
		if (lastPostCell) {
			onClickDo = "function(){toggleLastPostInfo()}"
			lastPostCell.addEventListener('click', eval(onClickDo), false)
			if (titleCell) {
				titleCell.addEventListener('click', eval(onClickDo), false)
			}

			lastPostStamp = lastPostCell.innerHTML.replace(/\n/g,"");
			lastPostStamp = lastPostStamp.replace(/\r/g,"");
			lastPostStamp = lastPostStamp.replace(/.*nowrap;"\>/, "")
			lastPostStamp = lastPostStamp.replace(/\<br\>.*/, "")
			lastPostCellAnchors = lastPostCell.getElementsByTagName('a')
			if (lastPostCellAnchors.length > 0) {
				lastPostAuthor = lastPostCellAnchors[0]
				lastPostAuthor.style.marginRight = '20px'
				lastPostAuthor.style.cssFloat = 'left'
				lastPostAnchor = lastPostCellAnchors[1]
				if (lastPostAnchor) {
					lastPostAnchor.style.cssFloat = 'right'
					lastPostStampAnchor = document.createElement('a')
					lastPostStampAnchor.href = lastPostAnchor.href
					lastPostStampAnchor.style.marginRight = lastPostAuthor.style.marginRight
					lastPostStampAnchor.style.cssFloat = lastPostAuthor.style.cssFloat
					lastPostStampAnchor.innerHTML = lastPostStamp
					lastPostCell.style.whiteSpace = 'nowrap'
					lastPostCell.innerHTML = ''
					lastPostCell.insertBefore(lastPostAuthor, null)
					lastPostCell.insertBefore(lastPostAnchor, lastPostCell.childNodes[0])
					lastPostCell.insertBefore(lastPostStampAnchor, lastPostCell.childNodes[0])
				}
			}
		}
		forumNameCell = cells[6]
		if (forumNameCell) {
			forumNameCell.style.display = 'none'
			if (forumIconCell) {
				if (lastPostCell) {
					forumURL = forumNameCell.getElementsByTagName('a')[0].href
					onClickDo = "function(){ location.href = '" + forumURL + "'; }"
					forumIconCell.addEventListener('click', eval(onClickDo), false)
					onMouseoverDo = "function(){ window.status = '" + forumURL + "'; return true; }"
					forumIconCell.addEventListener('mouseover', eval(onMouseoverDo), false)
					onMouseoutDo = "function(){ window.status = ''; return true; }"
					forumIconCell.addEventListener('mouseout', eval(onMouseoutDo), false)
					forumIconCell.style.cursor = 'pointer';
				}
			}
		}
	}
}
threadslist.style.display = ''
toggleLastPostInfo()

function toggleLastPostInfo() {
	cells = threadslist.getElementsByTagName('td')
	for (var i = 0; i < cells.length; i++) {
		cell = cells[i]
		if (cell.cellIndex == 3) {
			toggleLastPostLinks(cell)
		}
	}
}

function toggleLastPostLinks(cell) {
	anchors = cell.getElementsByTagName('a')
	if (anchors.length > 1) {
		if (anchors[0].style.display == 'none') {
			anchors[0].style.display = '';
		}
		else {
			anchors[0].style.display = 'none';
		}
		if (anchors[0].style.display == 'none') {
			anchors[2].style.display = '';
		}
		else {
			anchors[2].style.display = 'none';
		}
	}
}