// ==UserScript==
// @name			GYX
// @namespace		http://labs.lucien144.net/scripts/
// @include			http://www.nyx.cz/*
// @include			https://www.nyx.cz/*
// @version			0.1
// @author			Lucien144
// @homepage		http://www.lucien144.net
// ==/UserScript==

var l2h1		= document.evaluate('//div[@class="l2h1"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
var l2h2 		= document.evaluate('//div[@class="l2h2"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
var main_menu	= document.evaluate('//ul[@class="m l1"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);

var box_history 		= GM_getValue('box_history', true);
var box_premium 		= GM_getValue('box_premium', true);
var box_newboards 		= GM_getValue('box_newboards', true);
var add_menu_history 	= GM_getValue('add_menu_history', true);

GM_registerMenuCommand( box_history ? '✓ Gate: panel s historií' : 'Gate: panel s historií', function() { toggle_menu_command('history', ! box_history, true) });
GM_registerMenuCommand( box_premium ? 'Gate: panel s premium' : '✓ Gate: panel s premium', function() { toggle_menu_command('premium', ! box_premium, true) });
GM_registerMenuCommand( box_newboards ? '✓ Gate: nové nahoře' : 'Gate: nové nahoře', function() { toggle_menu_command('newboards', ! box_newboards, true) });
GM_registerMenuCommand( add_menu_history ? '✓ Přidat historii do menu' : 'Přidat historii do menu', function() { toggle_menu_command('menu_history', ! add_menu_history, true) });

toggle_menu_command('menu_history', add_menu_history, false);

if(l2h1 != null && l2h2 != null) {
	
	toggle_menu_command('history', box_history, false);
	toggle_menu_command('premium', box_premium, false);
	toggle_menu_command('newboards', box_newboards, false);
	
}

/**
* Display and toggle menu items in GM context menu
* @param string		Type/name of action to provide
* @param bool		Display(1) or show(0) the item
* @param bool		Refresh page. Important because of toggle
*/
function toggle_menu_command(box_type, box_status, reload) {

	switch(box_type) {
		
		// Display history in menu
		case 'menu_history':

			GM_setValue('add_menu_history', box_status);

			if( ! box_status ) {
			
				var menu_item = document.getElementById('menu-history');
				if( menu_item != undefined ) 
					menu_item.style.display = 'none';
					
				GM_log('History menu hidden');
					
			
			} else {
				
				var li_history		= document.createElement('li');
				var a_history		= document.createElement('a');
				var span_history	= document.createElement('span');
				
				li_history.id			= "menu-history";
				span_history.innerHTML	= 'history';
				a_history.href			= '?l=book;l2=2';

				li_history.appendChild(a_history);
				a_history.appendChild(span_history);
				main_menu.appendChild(li_history);	
				
				GM_log('History menu added');
				
			}
			
			break;
		
		// History box
		case 'history':

			GM_setValue('box_history', box_status);

			if( ! box_status ) {
				
				if( document.getElementById('wrapper-book_historie') != undefined ) 
					l2h1.removeChild(l2h1.firstChild);
				GM_log('History box hidden');
			
			} else {
				
				get_history();
				GM_log('History box shown');
				
			}
			
			break;
		
		case 'newboards':

			var new_topics = document.getElementById('gate_new_topics');
			GM_setValue('box_newboards', box_status);

			if( ! box_status ) {

				var newboards_panel = document.getElementById('wrapper-newboards');
				
				if( newboards_panel != undefined ) 
					l2h2.removeChild(l2h2.firstChild);
				
				new_topics.style.display = 'block';
				
				GM_log('New boards box hidden');

			} else {

				var newboards_panel = document.createElement('div');
				newboards_panel.id = 'wrapper-newboards';
				newboards_panel.innerHTML = '<div id="gate_new_topics" class="b">' + new_topics.innerHTML + '</div>';
				l2h2.insertBefore(newboards_panel, l2h2.firstChild);
				
				new_topics.style.display = 'none';
				
				GM_log('New boards box shown');

			}
		
			break;
		
		case 'premium':

			var premium = document.getElementById('gate_nyx_summary');
			GM_setValue('box_premium', box_status);

			if( ! box_status ) {
				
				premium.style.display = 'block';
				GM_log('Premium box shown');
				
			} else {

				premium.style.display = 'none';
				GM_log('Premium box hidden');

			}
			
			break;
		
	}
	
	if( reload ) location.reload();

}

/**
* This function display a history box in left panel on top
*/
function get_history() {
	GM_xmlhttpRequest({
	  method: "GET",
	  url: "http://www.nyx.cz/index.php?l=book;l2=2",
	  headers: {
	    "User-Agent": navigator.userAgent,
	    "Accept": "text/html"
	  },
	  onload: function(response) {

		var history = response.responseText.match(/<div class="b  select-rows" id="book_historie">(.*?((<div[^>]*>.*?<\/div>)*).*?)*?<\/div>/);
		var history_panel = document.createElement('div');
		history_panel.id = 'wrapper-book_historie';
		l2h1.insertBefore(history_panel, l2h1.firstChild);
		history_panel.innerHTML = history[0];
		
		unsafeWindow.history_show_new();

	    GM_log([
	      response.status,
	      response.readyState,
	      response.responseHeaders,
	      response.finalUrl
	    ].join("\n"));
	  }
	});
}