// ==UserScript==
// @namespace http://blog.futtta.be/tag/defixactie
// @name defixactie
// @description light-css-versie van enkele nieuwssites (v0.15.0.0.0.3, 19-01-2008).
// @include *
// ==/UserScript==

// todo:
// * debug -> refresh even if timestamp ok + add random number to requests to force cache update
// * check site exceptions in settings with GM_getValue('forbiddenSites',''), iterate over it before applying style!
// * soft- & hard cachelimit, soft updates after, hard before fetching css
// * if in css: 'removed', (soft-)update after fetching css

// some functions
function showUserMessage(message) {
	timeOut=3700;
	boxWidth=200;
	boxHeight=35;
	boxPosLeft=((window.innerWidth-boxWidth)/2);
	boxPosTop=((window.innerHeight-boxHeight)/5);

	var hello = document.createElement("div");
	hello.innerHTML = '<div id="hellodiv" style="' +
	    'display:block;position:absolute;' +
	    'top:' + boxPosTop + 'px;' +
	    'left:' + boxPosLeft + 'px;' +
	    'width:' + boxWidth + 'px;' +
	    'height:' + boxHeight +'px;' +
	    'text-align:center;display:table;' +
	    'background-color:#ffffff;z-index:99999999;opacity:0.7;border-width:5px;border-style:dashed;border-color:#000000;-moz-border-radius:10%;">' +
	    '<p id="defixactie" style="font-size:14px;font-weight:bold;display:table-cell;vertical-align:middle;">'+message+'</p>' +
	    '<script>' +
		'var hd=document.getElementById(\'hellodiv\');' +
		'setTimeout("hd.style.display=\'none\'",' + timeOut + ');' +
	    '</script>' +
	    '</div>';
	document.body.insertBefore(hello, document.body.firstChild);
}

function getArrayStringfromServer() {
	GM_xmlhttpRequest({
		method: "GET",
		url: 'http://futtta.be/defixactie.php?url=gimmeall',
		onload: function(response) {
			GM_setValue('sitesString',response.responseText);
			var date = new Date();
			now = 't:'+date.getTime(); // dirty hack; adding 't:' to ensure timestamp is stored as string
			GM_setValue('sitesUpdate', now);
			}
		});

	var arrayString=GM_getValue('sitesString','');
	return arrayString;
	}

function getArrayStringfromSetting() {
	maxCache=60*60*24*7;

	var date = new Date();
	now = date.getTime();

	oldTime=GM_getValue('sitesUpdate','000');
	oldTime=oldTime.substring(2);

	timeDiff=Math.floor((now-oldTime)/1000);
	
	var arrayString=GM_getValue('sitesString','');

	if ((arrayString=="") || (!arrayString) || (timeDiff>maxCache)) {
		arrayString=getArrayStringfromServer();
		}

	return arrayString;	
	}

// main
d=document;
dlh=d.location.host;

var arrayString = getArrayStringfromSetting();
var sites = new Array();
sites = arrayString.split(' ');

var x = 0;
do {
	if ((dlh.indexOf(sites[x]) != '-1') && sites[x]!="") {
		showUserMessage('<a href="http://blog.futtta.be/tag/defixactie" target="_new" style="font-size:14px;color:#FF8040;text-decoration:underline;"}">defixactie</a> in actie!');
		s=d.createElement('link');
		s.rel='stylesheet';
		s.href='http://futtta.be/defixactie.php?url='+escape(dlh);
		// s.href='http://localhost/test.css';
		d.documentElement.childNodes[0].appendChild(s);
		break;
		}
	x++;
	}
while (sites[x]);