/*
Geocaching Forum Tools - 0.34 18/08/2008
Copyright (c) 2005, Paul Dixon <paul@elphin.com>
                    Ian Rutson <ian@rutson.com>
                    
Released under the GPL http://www.gnu.org/copyleft/gpl.html

This is a Greasemonkey user script, see http://greasemonkey.mozdev.org/.

This script provides some extra functionality for the geocaching forums,
namely:

 - ignore thread - if there's a thread you don't care for (like a
   congratulations thread) and you don't want to see it getting bumped
   to the top, this lets you ignore it letting you concentrate on other
   threads
   
 - when search is offline, replace it with a Google search box which
   only searches forums.groundspeak.com

 - Watch thread - click the envelope to mark a thread as watched.
   The thread will be highlighted with a dull yellow, if the thread has
   new posts, it will be highlighted with a bright yellow

 - Threads started by the user are automatically watched
    
*/

// ==UserScript==
// @name          Geocaching Forum Tools
// @namespace     http://files.dixo.net
// @description	  Provides useful tools for the geocaching forums
// @include       http://forums.groundspeak.com/GC/index.php*
// ==/UserScript==

var win=window;
if (unsafeWindow)
{
	win=unsafeWindow;
}

var first=1;
var are_hidden_shown = 0;      
var user_id ='';


win.loadList=function()
{
	alert("don't use win.lostList, it's broken, load directly");
	win.rows=GM_getValue('GCForumList', '/');
}

win.saveList=function()
{
    var strData=win.rows;
    window.setTimeout(GM_setValue, 0, 'GCForumList',strData);
};

win.thread=function(rowidx,topic_id,action)
{
	//action= '+' watch
	//        '-' ignore
	//        ''  neither
	
	//main topic table is 3th
	var tables=document.getElementsByTagName('table');
	var table=tables[3];
	var rows=table.getElementsByTagName('tr');
	var row=rows[rowidx];

	var topicpos = win.rows.indexOf(topic_id);
	
	if (topicpos!=-1)	//topic in list
	{	
		if(action==win.rows.charAt(topicpos-1))		//same action, so remove
		{
			var tail = win.rows.substring(topicpos, win.rows.length);
			tail = tail.substring(tail.indexOf("/")+1,tail.length);
			win.rows = win.rows.substring(0,topicpos-1) + tail;
		}
		else	//opposite action
		{
			win.rows = win.rows.substring(0,topicpos-1) + action + win.rows.substring(topicpos, win.rows.length);
		}
	}
	else	//add topic
	{	
		win.rows = win.rows + action + topic_id + "/" ;
	}
	
	
	
	topicpos = win.rows.indexOf(topic_id);
	
	win.shade(rows[rowidx],win.rows.charAt(topicpos-1));
	
	win.saveList();

}


win.showIgnored=function()
{
	var toggle=document.getElementById('ignoretoggle');
	var tables=document.getElementsByTagName('table');
		
	if (are_hidden_shown)
	{
		are_hidden_shown=0;
		toggle.innerHTML="Show Ignored Topics";
	}
	else
	{
		are_hidden_shown=1;
		toggle.innerHTML="Hide Ignored Topics";
	}
	win.processtable();
}

win.shade=function(row,action)
{
	
	if (action=='+') 	bgcol = "#EEDD82"; //lowlight
	else if (action=='-') 	bgcol = "#FFDDCC"; //shade
	else if (action=='*') 	bgcol = "#FFE600"; //highlight
	else 			bgcol = "";        //neither

	var cells=row.getElementsByTagName('td');
	
	for(i=0; i<cells.length; i++) cells[i].style.backgroundColor = bgcol;	
	
	if (!are_hidden_shown && action == '-')	row.style.display="none";
	else					row.style.display="";
	
}	


win.processtable=function()
{
	//prepare stuff
	var findtopicid = /showtopic=(\d+)/;
	var newtopic = /newpost.gif/;
	var finduserid = /showuser=(\d+)/;	
	var topics=new Object();
	
	var icon = document.createElement('img');
	icon.src = 'data:image/gif,GIF87a%08%00%08%00%80%02%00%B3N%04%FF%FF%FF%2C%00%00%00%00%08%00%08%00%00%02%0F%84%8F%A1q%AC%C0%60%7C%D2P%E7%DAB%1C%14%00%3B';

	//main topic table is 3rd
	var tables=document.getElementsByTagName('table');

	//main topic table
	table=tables[3];
	
	
	
	//look at all the rows in the table
	rows=table.getElementsByTagName('tr');
	for (var i=0; i<rows.length; i++)
	{
		/* heres a row
		<tr> 
			<td align="center" class="row1" id='tid-folder-113640' onclick='return topic_toggle_folder("113640");'><img src='style_images/1/f_norm.gif' border='0'  alt='New Posts' /></td>
			<td align="center" class="row1">&nbsp;</td>
			<td class="row1" valign="middle">
			    <div style='float:right'></div>
				<div>
		
					<a href='http://forums.groundspeak.com/GC/index.php?showtopic=113640&amp;view=getnewpost'><img src='style_images/1/newpost.gif' border='0'  alt='Goto last unread' title='Goto last unread' hspace=2></a>Pinned:  <span id='tid-span-113640'><a id="tid-link-113640" href="http://forums.groundspeak.com/GC/index.php?showtopic=113640" title="This topic was started: Nov 11 2005, 09:39 PM">Uk Geocoin Group</a></span> 
					<div class="desc"><span onclick='return span_desc_to_input("113640");' id='tid-desc-113640'>Collectors of Worldwide GeoCoins</span></div>
				</div>
			</td>
			<td align='center' class="row2">
		     <a href="javascript:who_posted(113640);">26</a>    </td>
			<td align="center" class="row2"><a href='http://forums.groundspeak.com/GC/index.php?showuser=339831'>Moote</a></td>
		
			<td align="center" class="row2">2,021</td>
			<td class="row2"><span class="lastaction">31st January 2006 - 09:04 PM<br /><a href="http://forums.groundspeak.com/GC/index.php?showtopic=113640&amp;view=getlastpost">Last post by:</a> <b><a href='http://forums.groundspeak.com/GC/index.php?showuser=192704'>Happy Humphrey</a></b></span></td>
		</tr>
		*/

		
		//get all cells, verify this is a topic row
		var cells=rows[i].getElementsByTagName('td');
		
		if (cells.length>=7)
		{
			var envelope	=cells[0];
			var topic	=cells[2];
			var usercell 	=cells[4];

			var original=topic.innerHTML;
			var topicmatch=original.match(findtopicid);
			var usermatch=usercell.innerHTML.match(finduserid);
			if (topicmatch)
			{
				var topic_id=topicmatch[1];
				
				var topicpos=-1;
				
				if (win.rows)
				{
					topicpos = win.rows.indexOf(topic_id);
				}
				
				
				
				if(first) 
				{
					var anchors=topic.getElementsByTagName('a');
					
					var a=anchors[0];
					
					var newlink = document.createElement('span');
					newlink.innerHTML=" <a href=\"#\" title=\"Click to ignore and remove this topic\" onclick=\"window.thread("+i+","+topic_id+",'-');return false;\"><img border=\"0\" src=\""+icon.src+"\"></a> ";
					
					a.parentNode.insertBefore(newlink, a);
					
					//topicdivs[1].innerHTML='arse';//"<a href=\"#\" title=\"Click to ignore and remove this topic\" onclick=\"window.thread("+i+","+topic_id+",'-');return false;\"><img border=\"0\" src=\""+icon.src+"\"></a> "+original;
					envelope.innerHTML="<a href=\"#\" title=\"Click to watch this topic\" onclick=\"window.thread("+i+","+topic_id+",'+');return false;\">"+envelope.innerHTML+"</a> ";
				}
				
				
				if (topicpos!=-1)
				{
					
					win.shade(rows[i], win.rows.charAt(topicpos-1));
				}
				else if (usermatch)	
					//topic not currently flagged either way
					//if topic started by user, watch it
				{
					var topic_user=usermatch[1];
					if(topic_user==user_id)
					{
					win.thread(i,topic_id,'+');
					}
				}
				
			}

			//highlight/bold rows with new topics
			if (original.match(newtopic))
			{
				var cells=rows[i].getElementsByTagName('td');	
				cells[2].style.fontWeight="bold";	
				
				if (win.rows.indexOf('+'+topic_id)!=-1)
				{
					win.shade(rows[i],"*");
				}
				
			}
			
		}
		
		
	}
	
	first = 0;
}	

//this is called to enhance a geocaching.com cache listing page with OS Get-a-Map links
//and Google Maps links
function enhanceForumListing()
{
	//load the list of topics we don't care about
	//win.loadList(); - this won't work, but doing it directly solves
	//the problem http://wiki.greasespot.net/0.7.20080121.0_compatibility
	win.rows=GM_getValue('GCForumList', '/');


/*	
<div id="userlinks">
	<p class="home"><b>Logged in as:  <a href="http://forums.groundspeak.com/GC/index.php?showuser=270386">lordelph</a></b> ( <a href="http://forums.groundspeak.com/GC/index.php?act=Login&amp;CODE=03&amp;k=d3c70a279f34913c81333eedcca5a20e">Log Out</a> )</p>

	<p>&nbsp;<b><a href="http://forums.groundspeak.com/GC/index.php?act=UserCP&amp;CODE=00" title="Edit my settings, such as signature, avatar and more...">My Controls</a></b> &middot;&nbsp;<a href="http://forums.groundspeak.com/GC/index.php?act=Search&amp;CODE=getnew">View New Posts</a>&nbsp;&middot;&nbsp;<a href="javascript:buddy_pop();" title="View posts since your last visit, moderator lists and more...">My Assistant</a>&nbsp;&middot;&nbsp;<a href="http://forums.groundspeak.com/GC/index.php?act=Msg&amp;CODE=01">0 New Messages</a>	</p>
</div>
*/
	var userlinks=document.getElementById('userlinks');
	var para=userlinks.getElementsByTagName('p');
	
	//get username
	var finduserid = /showuser=(\d+)/;
	var matches=para[0].innerHTML.match(finduserid);
	if (matches) 	
		user_id = matches[1];
	
	para[1].innerHTML=para[1].innerHTML+
			" &middot; <a id=\"ignoretoggle\" href=\"#\" onclick=\"window.showIgnored();return false;\">Show Ignored Topics</a>";	
	
	
	//do all our main table processing
	win.processtable();	
}


//we're a multi-domain script, so here we decide what we're going to do...
var url=new String(document.location);
if (url.match(/^http:\/\/forums\.groundspeak\.com\/GC\/index\.php\?.*(showforum=|act=SF)/i))
{
	enhanceForumListing();
}



