// ==UserScript==
// @name           Futtersammler II
// @namespace      de.derprogger
// @include        http://www.facebook.com/group.php?*
// ==/UserScript==


var dpFSMain = {
 dpFBCWFS: function(){

	var atags=document.getElementsByTagName("a");
	var turl="";
	var key="";
	var id="";
	for(i=0;i<atags.length;i++){
		turl=atags[i].href;		
		if( (turl.indexOf('/cafeworld/accept_chef_special.php')>0) || (turl.indexOf('/cafeworld/accept_taste_testing.php')>0) || (turl.indexOf('/cafeworld/accept_achievement_bonus.php')>0)|| (turl.indexOf('/cafeworld/track.php')>0)){
			if(! atags[i].innerHTML.match(/(Got it|got it before|getting Meal)/)){
				key=turl.replace(/&amp;/g,'&');
				key=key.replace(/[^a-zA-Z0-9=.&]/g,'_');
				key=key.replace(/[_]{2,}/g,'_');
				key=key.replace(/[&=.]/g,"_");
				/*
				GM_log('Schlüssel: '+key);
				GM_log('innerHTML: '+atags[i].innerHTML);
				GM_log('Wert: '+GM_getValue(key,'NOTOK'));
				*/
				if(GM_getValue(key,'NOTOK')=='OK'){
					atags[i].innerHTML='got it before.';	
				}
				if(GM_getValue(key,'NOTOK')!='OK'){
					id="id_"+key;
					atags[i].innerHTML='<span id="'+id+'"><i style="color:#FF0000">getting Meal...</i>'+'<iframe src="'+turl+'" width="4" height="4" frameborder="0" style="border: 1px solid black;" onload="document.getElementById(\''+id+'\').innerHTML=\'Got it!\';"></iframe></span>';
					window.setTimeout(dpFSMain.dpFBCWFS,5000);
GM_setValue(key,'OK');
/*					GM_xmlhttpRequest({
						method: 'GET',
						url: turl,
						onload: function(r){
							atags[i].innerHTML='<b>Got it!</b>';
							GM_setValue(key,'OK');
							dpFSMain.dpFBCWFS();
						},
						onerror: function(r){
							GM_log([
								response.status,
								response.statusText,
								response.readyState,
								response.responseHeaders,
								response.responseText,
								response.finalUrl,
								response.responseXML
							].join("\n"));
							atags[i].innerHTML='<b>ERROR!</b>';
							GM_setValue(key,'ERROR');
							dpFSMain.dpFBCWFS();
						}
					});
					*/
					return;
				}
			}
		}
	}

	window.setTimeout(dpFSMain.dpFBCWFS,25000);
}
}

function menu_futtersammlerII_run(){	
	dpFSMain.dpFBCWFS();
}

GM_registerMenuCommand('Futtersammler II manuell ausführen', menu_futtersammlerII_run);

window.setTimeout(dpFSMain.dpFBCWFS, 3000);

window.addEventListener("load", function(){
	
	dpFSMain.dpFBCWFS();

}, false);



