// version 0.9.1 BETA!
// 2007/11/14
// Copyright (c) 2007, Hiroyuki Nakamura <hiroyuki@maloninc.com>
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
// 2007-11-14 v0.9.1 
//     * fixNav() : Adjust upper links line top position(Thanx! dy)
//
// 2007-11-14 v0.9.0
//     * Initial release
// --------------------------------------------------------------------
//
// ==UserScript==
// @name           Gmail Fixed Navigation for Newer Gmail
// @namespace      http://www.maloninc.com/greasemonkey
// @description    It allows your gmail navigation to be fixed.
// @include        http://mail.google.com/*
// @include        https://mail.google.com/*
// ==/UserScript==

var switchHeight = 15;
var activeBodyHeight = -1;

window.addEventListener('load', function() {
  if (unsafeWindow.gmonkey) {
    unsafeWindow.gmonkey.load('1.0', function(gmail) {
		
		function fixNav(){
			var navPane = gmail.getNavPaneElement();
			var mastHead = gmail.getMastheadElement();
			var activeView   = gmail.getActiveViewElement();
			var activeType   = gmail.getActiveViewType();
			var gbh          = mastHead.firstChild.firstChild.firstChild.firstChild.nextSibling;
			
			navPane.style.overflowX = 'hidden';
			navPane.style.overflowY = 'auto';
			navPane.style.position  = 'fixed';
			navPane.style.height   = (document.body.clientHeight - mastHead.clientHeight - switchHeight) + 'px';
			navPane.style.width = '150px'; 
			navPane.style.zIndex = 1; 
			
			gbh.style.top = '33px'; /* upper links line top position */
			
			if(activeView && activeType == 'tl'){
				var adBar         = activeView.parentNode.previousSibling;
				var topActMenu    = activeView.firstChild.childNodes[0];
				var activeBody    = activeView.firstChild.childNodes[2];
				var spacer        = activeView.firstChild.childNodes[3];
				var bottomActMenu = activeView.firstChild.childNodes[4];
				
				if( activeBodyHeight == -1 ){
					activeBodyHeight = (document.body.clientHeight - mastHead.clientHeight - topActMenu.clientHeight - adBar.clientHeight - switchHeight*2);
				}
				activeBody.style.height = activeBodyHeight + 'px';
				activeBody.style.overflowX = 'hidden';
				activeBody.style.overflowY = 'auto';
				activeBody.style.backgroundColor = 'white';
				bottomActMenu.style.display = 'none';
				spacer.style.display = 'none';
			}
			else if(activeView && activeType == 'cv'){
				var adBar         = activeView.parentNode.previousSibling;
				var topActMenu    = activeView.firstChild.childNodes[0];
				var activeBodyOrg = activeView.firstChild.childNodes[1];
				var activeBody    = document.createElement('div');
				var bottomActMenu = activeView.firstChild.childNodes[2];
				
				if( activeBodyHeight == -1 ){
					activeBodyHeight = (document.body.clientHeight - mastHead.clientHeight - topActMenu.clientHeight - adBar.clientHeight - switchHeight*2);
				}
				activeBody.style.height = activeBodyHeight + 'px';
				activeBody.style.overflowX = 'hidden';
				activeBody.style.overflowY = 'auto';
				bottomActMenu.style.display = 'none';
				
				activeView.firstChild.removeChild(activeBodyOrg);
				activeBody.appendChild(activeBodyOrg);
				activeView.firstChild.insertBefore(activeBody, bottomActMenu);
			}
		}
		
		function makeFootSwitch(){
			var footer   = gmail.getFooterElement();
			var mastHead = gmail.getMastheadElement();
			var navPane  = gmail.getNavPaneElement();
		    var div      = document.createElement('div')
		    var btn      = document.createElement('div');
		    btn.className = GM_getValue("footSwitch", "closed");;
		    btn.id = "footSwitchButton";
		    div.style.cursor     = "pointer";
		    div.style.marginLeft = ((mastHead.clientWidth-navPane.clientWidth)/2) + 'px';
		    div.addEventListener('click', function(){toggleSwitch(btn, footer, "footSwitch");}, false);
		    div.appendChild(btn)
		    footer.parentNode.appendChild(div);
		    
			if(btn.className == 'opened'){
				footer.style.display = 'block';
			}else{
				footer.style.display = 'none';
			}

		}
		
		function makeHeadSwitch(){
			var footer   = gmail.getFooterElement();
			var mastHead = gmail.getMastheadElement();
			var navPane  = gmail.getNavPaneElement();
		    var div      = document.createElement('div')
		    var btn      = document.createElement('div');
		    btn.className = GM_getValue("headSwitch", "closed");;
		    btn.id = "headSwitchButton";
		    div.style.cursor     = "pointer";
		    div.style.marginLeft = ((mastHead.clientWidth-navPane.clientWidth)/2) + 'px';
		    div.addEventListener('click', function(){toggleSwitch(btn, mastHead, "headSwitch");}, false);
		    div.appendChild(btn)
		    mastHead.parentNode.insertBefore(div, mastHead);
		    
			if(btn.className == 'opened'){
				mastHead.style.display = 'block';
			}else{
				mastHead.style.display = 'none';
			}
		}

		function toggleSwitch(sw, footer_header, label){
			var swValue = GM_getValue(label, "closed");
			if(swValue == 'opened'){
				sw.className = 'closed';
				GM_setValue(label, "closed");
				footer_header.style.display = 'none';
			}else{
				sw.className = 'opened';
				GM_setValue(label, "opened");
				footer_header.style.display = 'block';
			}
			
			activeBodyHeight = -1;
			fixNav();
		}
		
		
		makeFootSwitch();
		makeHeadSwitch();
		fixNav();
		gmail.registerViewChangeCallback(fixNav);
   });
  }
}, true);


GM_addStyle(<><![CDATA[
   #headSwitchButton {
     width: 0;
     height: 0;
     border: 9px none transparent;
     -moz-border-radius:  2px;
     cursor: pointer;
     -moz-opacity: 0.3;
   }
   #headSwitchButton:hover {
     -moz-opacity: 0.6;
   }
   #headSwitchButton {
     border-style: none solid;
   }
   #headSwitchButton.opened {
     border-top-style: solid;
     -moz-border-top-colors: transparent blue;
   }
   #headSwitchButton.closed {
     border-bottom-style: solid;
     -moz-border-bottom-colors: transparent blue;
   }


   #footSwitchButton {
     width: 0;
     height: 0;
     top-margine: 1em;
     border: 9px none transparent;
     -moz-border-radius:  2px;
     cursor: pointer;
     -moz-opacity: 0.3;
   }
   #footSwitchButton:hover {
     -moz-opacity: 0.6;
   }
   #footSwitchButton {
     border-style: none solid;
   }
   #footSwitchButton.opened {
     border-top-style: solid;
     -moz-border-top-colors: transparent blue;
   }
   #footSwitchButton.closed {
     border-bottom-style: solid;
     -moz-border-bottom-colors: transparent blue;
   }
]]></>);
