// ==UserScript==
// New Date v1.3
// (c) 2006-2007by Patric Peters <me@papet.de>
// 
// @name          New Date
// @namespace	  http://www.papet.de
// @description   Ersetzt einige Datumsangaben
// @version	      1.3 (05.08.2007)
// @include       http://82.149.226.131/bb/*
// @include       http://forum.counter-strike.de/bb/*
// @include       http://forum.cstrike.de/bb/*
// @include       http://forum.mods.de/bb/*
// ==/UserScript==

// --- PROMPT -- GREASEMONKEY ONLY ---
gmOffset = GM_getValue("newdateoffset");
function conprom (e) {
  GM_setValue("newdateoffset", (prompt('Zeitverschiebung zum Mods.de Server (in Sekunden)',gmOffset)));
}
GM_registerMenuCommand("\"New Date\" Zeitverschiebung", conprom);
if(gmOffset == null)
{
	gmOffset = 0;
}
else
{
	gmOffset = gmOffset*1000;
}
// --- PROMPT -- GREASEMONKEY ONLY ---

// ------------------------------
var offset   = parseInt(gmOffset); // Sekunden
// ------------------------------

// ------------------------------
var Location = window.location.href.replace('http://'+window.location.host+'/bb/', '');

//
//  Board�bersicht
//
if(Location.match("index") || Location.length == 0)
{
	anchors = document.getElementsByTagName("a");
	index   = 0;
	lol = 0;
	while(index < anchors.length)
	{
		if(anchors[index].innerHTML.match('^([0-9]*)\.([0-9]*)\.([0-9]*) <i>([0-9]*):([0-9]*)</i>$'))
		{			
			drawDate(anchors[index], RegExp.$3, RegExp.$2, RegExp.$1, RegExp.$4, RegExp.$5, 00);
		}
		index++;
	}
}

//
//  Thread�bersicht
//
else if(Location.match("board"))
{
	anchors = document.getElementsByTagName("a");
	index   = 0;
	lol = 0;
	while(index < anchors.length)
	{
		// 20.11.2006 um 0:57
		if(anchors[index].innerHTML.match('^([0-9]*)\.([0-9]*)\.([0-9]*) um ([0-9]*):([0-9]*) Uhr'))
		{			
			// Wurde etwas ge�ndert?
			if(drawDate(anchors[index], RegExp.$3, RegExp.$2, RegExp.$1, RegExp.$4, RegExp.$5, 00))
			{
				// das "am" ersetzen
				anchors[index].parentNode.innerHTML = anchors[index].parentNode.innerHTML.replace('&nbsp;am','&nbsp;');
			}
		}
		index++;
	}
}

//
// Thread
//
else if(Location.match("thread"))
{
	anchors = document.getElementsByTagName("a");
	index   = 0;
	lol = 0;
	while(index < anchors.length)
	{
		// 20.11.2006 um 0:57
		if(anchors[index].innerHTML.match('^([0-9]*)\.([0-9]*)\.([0-9]*) ([0-9]*):([0-9]*):([0-9]*)&nbsp;'))
		{			
			drawDate(anchors[index], RegExp.$3, RegExp.$2, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6);
		}
		index++;
	}
}

/**
 * drawDate
 *
 * @param object   DOM Object
 * @param integer  Year
 * @param integer  Month
 * @param integer  Day
 * @param integer  Hour
 * @param integer  Minute
 * @param integer  Second
 * @return bool
**/
function drawDate(obj, year, month, day, hour, minute, second)
{
	// Originalzeit
	thisdate = new Date();
	// Postzeit
	posttime = new Date(month+" "+day+", "+year+" "+hour+":"+minute+":"+second);

	// Offset
	posttime.setTime(posttime.getTime()+offset);
	
	// Post ist in den letzten 24h gepostet worden
	if(posttime.getTime() >= thisdate.getTime()-(60*60*24*1000))
	{
		newdate = new Date(thisdate.getTime()-posttime.getTime());
		
		// Vars
		date     = ((posttime.getDate() < 10) ? "0" + posttime.getDate() : posttime.getDate());
		month    = ((posttime.getMonth()+1 < 10) ? "0" + (posttime.getMonth()+1) : (posttime.getMonth()+1));
		hours    = ((posttime.getHours() < 10) ? "0" + posttime.getHours() : posttime.getHours());
		minutes  = ((posttime.getMinutes() < 10) ? "0" + posttime.getMinutes() : posttime.getMinutes());
		seconds  = ((posttime.getSeconds() < 10) ? "0" + posttime.getSeconds() : posttime.getSeconds());
		
		// Mehrere Stunden
		if(newdate.getTime()/1000 > 3600)
		{
			// Vars
			hours_text = "Stunden";
			minutes_text = "Minuten";
			
			thisHours = Math.floor((newdate.getTime()/1000)/3600);
			thisMinutes = Math.ceil((newdate.getTime()-(thisHours*3600000))/1000/60);
			
			if(thisHours == 1)
			{
				hours_text = "Stunde";
			}
			if(thisMinutes == 1)
			{
				minutes_text = "Minute";
			}

			obj.innerHTML = "<i> vor "+thisHours+" "+hours_text+" "+thisMinutes+" "+minutes_text+"</i>";
			obj.title = date+"."+month+"."+posttime.getFullYear()+" um "+hours+":"+minutes+":"+seconds;
			return true;
		}
		else
		{
			thisMinutes = Math.ceil(newdate.getTime()/1000/60);

			if(thisMinutes==1)
			{
				obj.innerHTML = "<i>vor "+thisMinutes+" Minute</i>";
				obj.title = date+"."+month+"."+posttime.getFullYear()+" um "+hours+":"+minutes+":"+seconds;
			}
			else
			{
				obj.innerHTML = "<i>vor "+thisMinutes+" Minuten</i>";
				obj.title = date+"."+month+"."+posttime.getFullYear()+" um "+hours+":"+minutes+":"+seconds;
			}
			return true;
		}
	}
	return false;
}