// ==UserScript==
// @name            TumblrLoginS
// @namespace       http://proto.jp
// @description     TumblrLoginS
// @include         http://*
// @exclude         http://mail.google.com/*
// @version            0.6.5
// ==/UserScript==

(function(){
   
    if(parent.document != document){return;};
   
    if(location.hash=="#TumblrLoginS_back")
    {
        history.back();
    }
	
    var createFlagText = function(){}

    createFlagText.prototype.run = function()
    {   
        var self = this;
       
        window.setTimeout(function(){

            var t="###";
            var tf = document.getElementById("TLS_flag");
           
            var gmGVconf = GM_getValue("Conf");
            var gmGVConfC = GM_getValue("ConfCell");
            var dd = document.getElementById("dummyDiv");
			
            if(gmGVconf)
            {
                var s = parseInt(gmGVconf.slice(-1));
                var r = gmGVConfC.split(",");
               
                t = r[s*3+2]    ?    r[s*3+2]    :    r[s*3];

                dd.style.opacity = tf.style.opacity = parseInt(r[30])/100;
                dd.style.top = tf.style.top = r[31]+"px";
                tf.style.display = "block";

				if(location.host == "www.tumblr.com" && location.hash!="#TumblrLoginS_back" && location.href.indexOf("http://www.tumblr.com/login")!=-1)
				{
					LoginTumblr(r[s*3],r[s*3+1]);
				}
            }
            else if(gmGVConfC)
            {
                var r = gmGVConfC.split(",");
                dd.style.opacity = tf.style.opacity = parseInt(r[30])/100;
                dd.style.top = tf.style.top = r[31]+"px";
                tf.style.display = "block";
                return;
            }
            else
            {
                tf.style.opacity = 1;
                tf.style.display = "block";
                return;
            }
           
            var dt = document.getElementById("TLS_flag_text");
            dt.removeChild(dt.firstChild);
            dt.appendChild(document.createTextNode(unescape(t)));
               
            }, 0);
    }
           
    var ctf = new createFlagText();
   
    var LoginTumblr = function(tid,tpw)
    {
        var d = document.getElementById("dummyDiv");
		var hash	=	location.href != "http://www.tumblr.com/dashboard"
					&&	location.href.indexOf("http://www.tumblr.com/login")==-1	?	"#TumblrLoginS_back"	:	"";
		
        var tlf
		='<form id="tLm" name="tumblr_manager_form" action="http://www.tumblr.com/login'+hash+'" method="post" style="margin:0 auto 0 auto;width:95%;height:200px;border:0;">'
        +'<input type="hidden" name="email" value="'+tid+'" />'
        +'<input type="hidden" name="password" value="'+tpw+'" />'
        +'</form>';
       
        // output
        d.innerHTML = tlf;
        // re-login submit
        unsafeWindow.document.tumblr_manager_form.submit();
    }
   
    var popConf = function()
    {
        if (!document.getElementById('tumbID'))
        {
            var e = document.createElement('div');
       
            e.id = "TumblrLoninS";
            e.innerHTML
            ='<style TYPE="text/css">'
            +'#bLayer{display:none;z-index:8900000;position:fixed;top:0;left:0;background-color:#000;width:100%;height:100%;opacity:0.7;}'
            +'#tumbID{display:none;z-index:8900001;margin:0;padding:0;width:380px; background-color:#fff;border:1px solid #B3BBC4;}'
            +'#tumbID input{margin:8px 2px; width:100px;}'
            +'#tumbIDname div{color:#eee;font-size:12px; float:left; margin:0 2px;padding:0; height:15px; width:104px;}'
            +'#tumbIDname {float:none; padding:0 0 16px 20px; background-color:#4C5A69}'
            +'.sc{ padding:0 20px; cursor:pointer; background-color:#B6D5EC;}'
            +'.c0{ padding:0 20px; cursor:pointer; background-color:#eee;}'
            +'.c1{ padding:0 20px; cursor:pointer;}'
            +'</style>'
            +'<div id="bLayer"></div>'
            +'<div id="tumbID">'
                +'<div id="tumbIDname"><div><b>E-mail</b></div><div><b>Password</b></div><div><b>Name</b> (optional)</div></div>'
                +'<div id="tumbID_0" class="c0" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_1" class="c1" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_2" class="c0" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_3" class="c1" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_4" class="c0" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_5" class="c1" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_6" class="c0" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_7" class="c1" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_8" class="c0" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div id="tumbID_9" class="c1" title="click to login"><input type="text" /><input type="password" /><input type="text" /></div>'
                +'<div style="padding:0 0 0 5px;color:#eee;font-size:12px;background-color:#4C5A69;height:15px;"><b>Flag Config</b></div>'
                +'<div style="padding:0 0 0 10px;font-size:14px;">Alpha:<input name="alpha" type="text" maxlength="3" style="margin:2px;padding:0;width:50px;" /><span style="font-size:10px;">(0~100)</span>'
                +'<span style="margin:0 0 0 10px;">OffsetY:</span><input name="OffsetY" type="text" maxlength="3" style="margin:2px;padding:0;width:50px;" /><span style="font-size:10px;">(0~999)</span></div>'
            +'</div>'
           
            document.getElementsByTagName("body").length    ?    document.getElementsByTagName("body").item(0).appendChild(e)
                                                            :    document.getElementsByTagName("html").item(0).appendChild(e);
        }
        else if(document.getElementById('tumbID').style.display == "none")
        {
            document.getElementById("bLayer").style.display = "block";
            document.getElementById("tumbID").style.display = "block";
           
            return;
        }
        else if(document.getElementById('tumbID').style.display == "block")
        {
            return;
        };
               
        var b = document.getElementById("bLayer");
        var t = document.getElementById("tumbID");
       
        b.style.display = "block";
        t.style.display = "block";
        t.style.position = "fixed";
        t.style.top = (innerHeight/2-200)+ "px";
        t.style.left = (innerWidth/2-182)+ "px";
       
        var encCell = function()
        {
            var r = "";
                       
            var v30 = t.getElementsByTagName("input")[30].value;
            if(isNaN(v30) || v30>100 || v30<0 || v30.length==0)
            {
                t.getElementsByTagName("input")[30].value = 100;
            }
           
            var v31 = t.getElementsByTagName("input")[31].value;
            if(isNaN(v31) || v31.length==0)
            {
                t.getElementsByTagName("input")[31].value = 24;
            }
           
            for(var i=0,l=t.getElementsByTagName("input").length; i<l;i++)
            {
                r+=escape(t.getElementsByTagName("input")[i].value);
                i==l-1    ?    null    :    r += ",";
            }
            return r;
        }
       
        var selectCell = function(c , f)
        {
            var se = document.getElementById(c);
            var in0v = se.getElementsByTagName("input")[0].value;
            var in1v = se.getElementsByTagName("input")[1].value;
           
            if(in0v.indexOf("@")==-1 || in1v.length<4)
            {
                alert("please input Tumblr E-mail & password");
                return;
            }
           
            for(var i=0; i<10; i++)
            {
                var te = document.getElementById("tumbID_"+i);
                if(i%2==0)
                {
                    te.className = "c0";
                    te.style.backgroundColor= "#eee";
                }
                else
                {
                    te.className = "c1";
                    te.style.backgroundColor= "#fff";
                }
            }
            se.className ="sc";
            se.style.backgroundColor="#B6D5EC";
           
            GM_setValue("Conf" , c);
            GM_setValue("ConfCell" , encCell());
           
            f    ?    LoginTumblr( in0v , in1v)    :    null;
        }
       
        document.addEventListener("mousedown",function(e)
        {
            if (e.target.className == "c0" || e.target.className == "c1" || e.target.className == "sc")
            {               
                selectCell(e.target.id , true);
                ctf.run();
                b.style.display = "none";
                t.style.display = "none";
            }
            else if(e.target.id == "bLayer")
            {
                GM_setValue("ConfCell" , encCell());
                ctf.run();
                b.style.display = "none";
                t.style.display = "none";
            }
        },false);
       
        document.addEventListener("mouseover",function(e)
        {
            if (e.target.className == "c0")
            {
                e.target.style.backgroundColor= "#ccc";
            }
            else if (e.target.className == "c1")
            {
                e.target.style.backgroundColor= "#ccc";
            }
        },false);
       
        document.addEventListener("mouseout",function(e)
        {               
            if (e.target.className == "c0")
            {
                e.target.style.backgroundColor= "#eee";
            }
            else if (e.target.className == "c1")
            {
                e.target.style.backgroundColor= "#fff";
            }
        },false);
       
        window.addEventListener("unload",function()
        {
            GM_setValue("ConfCell" , encCell());
        },false);
       
        if (GM_getValue("ConfCell"))
        {
            var r = GM_getValue("ConfCell").split(",");
            for (var i = 0, l = r.length; i < l; i++)
            {
                t.getElementsByTagName("input")[i].value = unescape(r[i]);
            }
        }
       
        GM_getValue("Conf")        ?    selectCell(GM_getValue("Conf"), false)
                                :    null;
                               
    }
    GM_registerMenuCommand("TumblrLoginS",popConf);
   
    var e = document.createElement('div');
    e.id = "TLS_flag";
    e.className="tls_flg";
    e.innerHTML
    ='<span id="TLS_flag_text" class="tls_flg" style="font-size:14px;color:#eee;">no select id</span>'
    +'<style TYPE="text/css">'
    +'a.tls_idlistA:hover{text-decoration:underline;color:#EC800D;}'
    +'a.tls_idlistA{text-decoration:underline;color:#333333}'
    +'.tls_point{cursor:pointer}'
    +'</style>'
    +'<div id="dummyDiv" style="margin:0;padding:0;height:0px;width:0px;"></div>'
    +'<div id="idListDiv" style="display:none;margin:0;padding:0;"></div>';
    e.style.cssText='display:none;z-index:8800000;position:fixed;top:24px;left:0px;color:#eee;background-color:#4C5A69;padding:2px 4px;font-size:14px;';

    document.getElementsByTagName("body").length    ?    document.getElementsByTagName("body").item(0).appendChild(e)
                                                    :    document.getElementsByTagName("html").item(0).appendChild(e);

    document.addEventListener("mousedown",function(e)
    {
        if(e.target.className.indexOf("tls_idlist_") != -1)
        {
            var s =parseInt(e.target.className.slice(-1));
            var r = GM_getValue("ConfCell").split(",");
           
            if(r[s*3].indexOf("@")==-1 || r[s*3+1].length<4)
            {
                alert("please input Tumblr E-mail & password");
                return;
            }
           
            LoginTumblr(r[s*3],r[s*3+1]);
            document.getElementById("idListDiv").style.display = "none";
            GM_setValue("Conf" , "tumbID_"+s);
            ctf.run();
        }
    },false);
   
    document.addEventListener("mouseover",function(e)
    {
        if(e.target.className == "tls_flg")
        {
            document.getElementById("dummyDiv").style.opacity = document.getElementById("TLS_flag").style.opacity;
            document.getElementById("TLS_flag").style.opacity = 1;

            if (GM_getValue("ConfCell"))
            {
                var s = GM_getValue("Conf")    ?    parseInt(GM_getValue("Conf").slice(-1))    :    -1;
                var r = GM_getValue("ConfCell").split(",");
               
                var ld = document.getElementById("idListDiv");
                var td = document.createElement('div');
                var d = document.createElement('div');
               
                td.appendChild(document.createTextNode("SELECT ID"));
                td.title = "select tumblr id";
                td.style.cssText="color:#eee;background-color:#4C5A69;padding:2px 4px;text-align:left;font-weight:bold;";
                d.appendChild(td);
               
                for(var i=0; i<10; i++)
                {
                    var t = r[i*3+2]    ?    r[i*3+2]    :    r[i*3];
                   
                    if(t)
                    {
                        var lad = document.createElement('div');
                        var a = document.createElement("a");
                        var c = i==s ? "#E4EFFA" : "#fff";
                       
                        a.href = "javascript:void(0)";
                        a.title = "click to login"
                        a.className = "tls_idlistA tls_idlist_"+i;
                       
                        lad.className = "tls_point tls_idlist_"+i;
                        lad.style.cssText='padding:4px 8px;text-align:left;background-color:'+c;
                       
                        a.appendChild(document.createTextNode(unescape(t)));
                        lad.appendChild(a);
                        d.appendChild(lad);
                    }
                }
               
                var ddt = document.getElementById("dummyDiv").style.top;
                ddt =  parseInt(ddt.slice(0,-2))-10;
               
                d.style.cssText='z-index:8800001;position:fixed;top:'+ddt+'px;left:0px;padding:10px 10px 10px 0;font-size:14px;';
                d.className = "tls_idlist";
               
                ld.firstChild    ?    ld.removeChild(ld.firstChild)    :    null;
                ld.appendChild(d);
                ld.className = "tls_idlist";
                ld.style.display = "block";
            }
        }
    },false);
   
    document.addEventListener("mouseout",function(e)
    {
        if(e.target.className == "tls_idlist")
        {
            document.getElementById("TLS_flag").style.opacity = document.getElementById("dummyDiv").style.opacity;
            document.getElementById("idListDiv").style.display = "none";
        }
    },false);
   
    window.addEventListener("focus",function(e)
    {
        document.getElementById("TLS_flag").style.opacity = document.getElementById("dummyDiv").style.opacity;
        document.getElementById("idListDiv").style.display = "none";
        ctf.run();
       
    },false);
   
    if (!GM_getValue("ConfCell")) {
        popConf();
        document.getElementById("tumbID").getElementsByTagName("input")[30].value = 50;
        document.getElementById("tumbID").getElementsByTagName("input")[31].value = 24;
    }
    else{ctf.run()};
   
})();