// ==UserScript==
// @name           Better Google for iPhone v2
// @namespace      http://userscripts.org/scripts/show/47573
// @description    Improves mobile view of Google services for iPhone
// @version        0.7
//
// @include        http*://www.google.com/m*
// @include        http*://mail.google.com/*
// @include        http*://www.google.com/reader/m/*
// @include        http*://www.google.com/reader/i/*
// @include        http*://docs.google.com/m*
// @include        http*://docs.google.com/*/m*
// ==/UserScript==

(function() {
	function onGmailLoad()
	{
        var inboxDiv = document.getElementById("inbox");
        if (inboxDiv && unsafeWindow.N)
        {
            // avoid shrinking to 320px
            injectJavaScript('L.prototype.ca = function(){};');

            // fix mail subject width
            injectStyle('.tlsubjread, .tlsubjunread, div.cvsubjshorter { width: 600px; }');
        }
    }

    function onDocsLoad()
    {
        // avoid shrinking to 320px
        injectStyle('html, body { width: auto; }');
    }

    function onReaderLoad()
    {
        // remove google reader logo
        injectStyle('a.logo { display: none }');
    }

    function onPropsLoad()
    {
        // checkmarks doesn't display properly in microb by default, fix this
        injectStyle( '.checkmark { position: relative; display: block; top: -60px; }' );
        // allow to add up to 10 tabs at top
        injectJavaScript( "v = 10;" );
    }

    function onGpVersionLoad()
    {
        // remove paddings/margins for body and borders for frames
        // and add styles for topbar
        injectStyle( 'body { margin: 0; padding: 0; }\n'+ 
                     '.gpframe { border: 0; width: 100%; }\n'+
                     '.topbar { background: #1e3384; padding: 5px; }\n'+
                     '.topbar a.gpbtn, .topbar a.gpbtn:visited { padding: 7px; font-family: Arial, Helvetica, sans-serif; text-decoration: none; color: #fff; }\n'+
                     '.topbar a.gpbtn-on, .topbar a.gpbtn-on:visited { padding: 6px; font-weight:bold; }\n'+
                     '.topbar a.gpbtn-off, .topbar a.gpbtn-off:visited {  }' );

        // adjust inner window height
        unsafeWindow.onresize = function() {
            // set height of .gpframe = window height - topbar height
            for (var i = 0; i < document.body.childNodes.length; i++)
            {
                try
                {
                    var node = document.body.childNodes[i];
                    if (node.className == "gpframe")
                    {
                        node.style.height = (window.innerHeight - document.getElementById('topbar').offsetHeight) + "px";
                    }
                } catch (e) { }
            }
        };
        unsafeWindow.onresize();
    }

    function injectStyle( styleDef )
    {
        var headID = document.getElementsByTagName("head")[0];         
        var styleNode = document.createElement('style');
        styleNode.type = 'text/css';
        styleNode.appendChild( document.createTextNode(styleDef) );
        headID.appendChild(styleNode);
    }

    function injectJavaScript( scriptText )
    {
        var headID = document.getElementsByTagName("head")[0];         
        var newScript = document.createElement('script');
        newScript.type = 'text/javascript';
        newScript.text = scriptText;
        headID.appendChild(newScript);
        document.getElementById("inbox").style.width = "";
    }

    if (document.domain == "mail.google.com")
    {
        onGmailLoad();
    }
    else if (document.domain == "docs.google.com")
    {
        onDocsLoad();
    }
    else if( document.documentURI.match("(google.com/m/p)|(google.com/m/a/.+/p)") )
    {
        onPropsLoad();
    }
    else if( document.documentURI.match("google.com/m/(gp|a)") )
    {
        onGpVersionLoad();
    }
    else
    {
        onReaderLoad();
    }
})();
