// ==UserScript==
// @name           TE Usability
// @namespace      http://dani.pew.cc
// @include        http://tyrngaardes-en.de/*
// @include        http://www.tyrngaardes-en.de/*
// @exclude        http://tyrngaardes-en.de/raid/*
// @exclude        http://www.tyrngaardes-en.de/raid/*
// ==/UserScript==

// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

/*
// Ok, this kind works
// URL where to  block: http://www.tyrngaardes-en.de/raid/
var wo = document.createElement('script');
wo.innerHTML = 'var window_open = window.open;' + "\n" +
'function mywinopen() {' + "\n" +
' var args = mywinopen.arguments;' + "\n" +
' if (args[0] != \'http://www.tyrngaardes-en.de/raid/\')' + "\n" +
'  window_open(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);' + "\n" +
'}' + "\n" +
'window.open = mywinopen;';

document.getElementsByTagName('head')[0].appendChild(wo);
*/

// Check if jQuery's loaded
function GM_wait() {
if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
    else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();

// All your GM code must be inside this function
function letsJQuery() {
	// Hide chat windows when not in use
	$('#chatscroll').css({'display': 'none'});
	$('#tchat_friends').click(function() {
		if ($('#chatscroll').css('display') == 'none') {
			$('#chatscroll').css({'display': 'block'});
		}
	});
	$('#tchat_close').click(function() {
		if ($('#chatscroll').css('display') == 'block') {
			$('#chatscroll').css({'display': 'none'});
		}
	});
	
	// Proper menu
	// Kill the new windows that opens for raid signup
	$('head').append(	'<script type="text/javascript">' + "\n" + 
						'var orig_winopen = window.open;' + "\n" +
						'function trell_winopen() {' + "\n" +
						' var args = trell_winopen.arguments;' + "\n" +
						' if (args[0] != \'http://www.tyrngaardes-en.de/raid/\')' + "\n" +
						'  orig_winopen(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);' + "\n" +
						'}' + "\n" +
						'window.open = trell_winopen;' + "\n" +
						'</script>');
	var sites = new Object();
	sites['News Archiv'] = '/home/archiv';
	sites['Geschichte'] = '/die-gilde/geschichte';
	sites['Gildensatzung'] = '/die-gilde/gildensatzun';
	sites['Mitglieder'] = '/die-gilde/community';
	sites['Organigramm'] = '/die-gilde/organigramm';
	sites['Anmeldung'] = '/raid/';
	sites['Raid Blog'] = '/der-raid/raid-blog';
	sites['Raidregeln'] = '/der-raid/raidregeln';
	sites['Wie melde ich mich an?'] = '/der-raid/wie-melde-ich-mich-an';
	sites['Teamspeak'] = '/myte/teamspeak';
	sites['Bild hochladen'] = '/bildergalerie?func=showupload';
	sites['Private Nachrichten'] = '/myte/private-nachrichten';
	sites['mein Profil editieren'] = '/myte/mein-profil-editieren';
	$('#subwrap .item td').each(function() {
		var text = $(this).text();
		//$(this).empty();
		$(this).empty().append('<a class="trell_menulink" href="' + sites[text] + '">' + text + '</a>');
	});
	$('.trell_menulink').css({
		'color': '#F4C400',
		'font-family': 'Arial,Helvetica,sans-serif',
		'font-size': '11px',
		'font-weight': 'bold',
		'padding': '2px 5px',
		'text-align': 'left',
		'text-decoration': 'none',
	});
	
	// Style code, just some simple example
	$('.fbcode').css({
		'background-color': '#333333',
		'border': '1px solid #cccccc',
		'padding': '5px',
		'margin': '10px 0 10px 0',
	});
	
	// Style history, just to demonstrate it
	// Not all is styled, and the color is wrong, but I'm too lazy now to fix it
	$('.fb_review_body1, .fb_review_body0').css({
		'background-color': '#000000',
	});
	
	// Kill sounds from chat
	$('head').append(	'<script type="text/javascript">' + "\n" + 
						'function playSound(arg1, arg2) {}' + "\n" +
						'</script>');
						
	// Add wowhead to site
	$('head').append('<script src="http://static.wowhead.com/widgets/power.js"></script>');
	
	// After editing a post jump to the post, not the end of the thread
	var loc = new String(document.location);
	if (loc.substr(loc.length - 10) == '/post/edit') {
		var ref = new String(document.referrer);
		var start = ref.match(/\?limit=[0-9]+\&start=[0-9]+/);
		if (!start) start = '';
		$('[name=postform]').attr('action', $('[name=postform]').attr('action') + '#' + start + '&post=' + $('[name=id]').val());
	}
	// On post save, construt the new url
	if (loc.search(/\/forum\/[0-9]+[a-z-]+\/post\#.*post=[0-9]+$/) != -1) {		
		$('#Kunena table a').each(function() {
			var href = new String(this.href);
			if (href.search(/\/forum(\/[0-9]+-[0-9a-z-]+){2}/) != -1) {
				// Remove redirect, see kunenaRedirectTimeout
				$('body').click();
				
				// Stupid string manipulation is stupid
				var newhref = href.match(/\/forum\/[0-9]+[a-z-]+\/[0-9]+-[a-z0-9-]+/);
				newhref += loc.match(/\?limit=[0-9]+\&start=[0-9]+/);
				loc = loc.match(/\&post=[0-9]+/);
				loc = new String(loc);
				newhref += '#' + loc.match(/[0-9]+/);
				this.href = newhref;
				$('head').append('<script type="text/javascript"> kunenaRedirectTimeout("http://tyrngaardes-en.de' + newhref + '", 3500); </script>');
			};
			//http://tyrngaardes-en.de/forum/14-raids/19555-25er-raid-logs?limit=20&start=40#20990
		});
	}
	
	// Armory Model viewer ^_^
	GM_log($("title").text());
	// Get the second cbClr div
	$('#cbProfileInner .cbClr:eq(1)').after('<div><iframe src="http://eu.wowarmory.com/character-model-embed.xml?r=Die+Todeskrallen&cn=' + $("title").text() + '&rhtml=true" scrolling="no" height="588" width="321" frameborder="0"></iframe></div><div class="cbClr">&nbsp;</div>');
}

