// ==UserScript==
// @name        qtalk_n_markAsReadInline
// @namespace   http://www.qtalk.de/
// @description Marks boards as read by doubleclicking the board icon in the board list.
// @include     http://www.qtalk.de/*
// @version     1
// ==/UserScript==

// find the original "mark boards as read" link
var navLink, hash;
var nav = document.getElementsByClassName('nav');
for (i in nav) {
	if (nav[i].tagName == 'TR') {
		navLink = nav[i].getElementsByTagName('td')[1].getElementsByTagName('a')[0];
		break;
	}
	else if (nav[i].tagName == 'A') {
		navLink = nav[0];
		break;
	}
}

// extract hash and session id from url
var SID = '';
var hash = '';
if (navLink != undefined) {
	hash = navLink.href.replace(/.*hash=([a-z0-9]*).*/gi, '$1');
}
if (document.documentURI.match(/sid=([a-z0-9]+)/gi)) {
	SID = '&sid=' + RegExp.$1;
}

// loop through board links, find boards with unread posts and attach event listener to their icons
var icon, board, boardID;
var boards = document.getElementsByClassName('forumlink');
for (i in boards) {
	board = boards[i];
	
	if (boards[i].parentNode != undefined) {
		icon = boards[i].parentNode.parentNode.getElementsByTagName('td')[0].getElementsByTagName('img')[0];
		// check if board has unread posts
		if (icon.src.indexOf('_unread') != -1) {
			// get board ID
			board.href.match(/f=([0-9]*)/gi);
			boardID = RegExp.$1;
			icon.setAttribute('id', 'boardIcon' + boardID);
			board.setAttribute('id', 'board' + boardID);
			
			// add event listener to icon and change title
			icon.title = 'Ungelesene Beiträge (Forum durch Doppelklick als gelesen markieren)';
			icon.ondblclick = function() {
				// get boardID
				boardID = this.getAttribute('id').replace(/boardIcon/gi, '');
				
				// mark board as read
				var sAJAX = new XMLHttpRequest();
				sAJAX.open('GET','http://www.qtalk.de/forum/viewforum.php?hash=' + hash + '&f=' + boardID + '&mark=topics' + SID,true);
				sAJAX.send();
				
				// remove event listener, change image, change mouse over text, change board link appearance (if necessary)
				icon = document.getElementById('boardIcon' + boardID);
				icon.title = 'Keine ungelesenen Beiträge';
				icon.src = icon.src.replace(/_unread/, '_read');
				icon.ondblclick = '';
				board = document.getElementById('board' + boardID);
				board.className = board.className.replace(/ boardNew/, '');
			}
		}
	}
}

// loop through topic links, find topics with unread posts and attach event listeners to their icons
var topic, topicID, border;
var topics = document.getElementsByClassName('topictitle');

for (j in topics) {
	topic = topics[j];
	if (topics[j] != undefined && topics[j].parentNode != undefined && topics[j].parentNode.parentNode != undefined) {
		icon = topics[j].parentNode.parentNode.getElementsByTagName('td')[0].getElementsByTagName('img')[0];
		
		// check if topic has unread posts
		if (icon.src.indexOf('_unread') != -1) {
			// get topic ID
			topic.href.match(/\&t=([0-9]*)/gi);
			topicID = RegExp.$1
			icon.setAttribute('id', 'topicIcon' + topicID);
			topic.setAttribute('id', 'topic' + topicID);
			
			// add event listener to icon and change title
			icon.title = 'Ungelesene Beiträge (Thema durch Doppelklick als gelesen markieren)';
			icon.ondblclick = function() {
				// get topicID
				topicID = this.getAttribute('id').replace(/topicIcon/gi, '');
				
				// mark topic as read
				var sAJAX = new XMLHttpRequest();
				sAJAX.open('GET','http://www.qtalk.de/forum/viewtopic.php?t=' + topicID + '&start=1000000' + SID,true);
				sAJAX.send();
				
				// remove event listener, change image, change mouse over text, remove first unread post link, change topic link appearance (if necessary)
				icon = document.getElementById('topicIcon' + topicID);
				icon.title = 'Keine ungelesenen Beiträge';
				icon.src = icon.src.replace(/_unread/, '_read');
				icon.ondblclick = '';
				topic = document.getElementById('topic' + topicID);
				topic.parentNode.getElementsByTagName('img')[0].style.display = 'none';
				topic.className = topic.className.replace(/ topicNew/, '');
				
				// also remove blue border
				border = icon.parentNode.parentNode.getElementsByTagName('td')[0];
				border.style.border = 'none';
			}
		}
	}
}