// ==UserScript==
// @name            DeliciousAutoLogin
// @namespace       http://proto.jp
// @description     DeliciousAutoLogin
// @include         http://*
// @include         https://*
// @exclude         http://mail.google.com/*
// @version			0.1.0
// ==/UserScript==

(function(){
	
	GM_registerMenuCommand("DeliciousAutoLogin",setAccount);
	if(parent.document != document || !checkPreLogin()){return;};
		
	var actionUrl = "https://secure.delicious.com/login";	
	var Login = function(tid,tpw)
	{
		var d = document.createElement('div');
		
		d.innerHTML
		='<iframe name="dummy_window" src="#" style="width:0px;height:0px;padding:0;border:0;"></iframe>'
		+'<form id="tLm" name="pjp_DAL_dummy_form" action='+actionUrl+' method="post" target="dummy_window">'
		+'<input type="hidden" name="username" id="username" value="'+tid+'" >'
		+'<input type="hidden" name="password" id="password" value="'+tpw+'">'
		+'<input type="hidden" name="rememberme" id="rememberme" value="1">'
		+'</form>';
		
		try
		{
			document.getElementsByTagName("body").item(0).appendChild(d);
			unsafeWindow.document.pjp_DAL_dummy_form.submit();
		}
		catch(e){return;}
		
		var day = new Date();
		GM_setValue("Conf" , day.getDate());
	}
	
	function setAccount()
	{
		var def = GM_getValue("ConfUsername")	?	GM_getValue("ConfUsername")	:	"";
		
		var dUsername = window.prompt("Delicious Username",def);
		var dPassword = window.prompt("Delicious Password","");
		
		GM_setValue("ConfUsername" , dUsername);
		GM_setValue("ConfPassword" , dPassword);
	}
	
	function checkPreLogin()
	{
		var day = new Date();
		
		if(GM_getValue("Conf"))
		{
			if(parseInt(GM_getValue("Conf")) == day.getDate() || !document.getElementsByTagName("body").length)
			{
				return false;	
			}
		}
		
		return true;
	}
	
	if(!GM_getValue("ConfUsername"))
	{
		setAccount();
	}
	
	Login(GM_getValue("ConfUsername"), GM_getValue("ConfPassword"));
})();