// ==UserScript==
// @name           Google Calendar Tighten
// @namespace      http://www.danielslaughter.com/
// @description    Slims down and gains realestate for Google Clanedar (includes a toggle)
// @version        5/28/2010
// @include        http*://www.google.com/calendar/*
// ==/UserScript==

// Change this if you want:
var tightenOnLoad = true;
// Do not edit below this line unless you're feeling dangerous
versionNumber = '5/28/2010';
var refCon = 0;
var ref = null;
var chk = null;
var ids = new Array("topBar","sidebar","nav","funbox"); // "gbar","guser" (these are the topbar with your info and google links, if you wana add it to the list)
var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (chrome) {
	init();
} else {
	window.addEventListener('load',function(){
		init();
	},true);
}
function init() {
	var el = document.getElementsByTagName('td');
	if (el.length) {
		for(var i=0;i<el.length;i++) {
			if (el[i].innerHTML.match(/class="date-top"/g)) {
				refCon = el[i].getElementsByTagName('div');
				refCon = refCon[0];
			}
		}
	}
	scriptUpdate();
	var el = document.getElementsByTagName('a');
	if (el.length) {
		for(var i=0;i<el.length;i++) {
			if (el[i].getAttribute('class') == 'mg-refresh' && el[i].innerHTML == 'Refresh') {
				ref = el[i];
			}
		}
	}
	var label = document.createElement('label');
	chk = document.createElement('input');
	chk.setAttribute('type','checkbox');
	chk.setAttribute('onchange','window.setTimeout(function(){_Refresh()},' + (chrome?'500':'100') + ');');
	chk.addEventListener('change',function() {
		toggleView();
	},false);
	chk.checked = tightenOnLoad;
	label.appendChild(chk);
	label.appendChild(document.createTextNode('Tighten'));
	refCon.appendChild(document.createTextNode(' '));
	refCon.appendChild(label);
	toggleView();
	window.setTimeout(toggleView,600); // wait and re-toggle. some elements on page load (like "funbox") are generated post the function call
}
function toggleView() {
	for(var i=0;i<ids.length;i++) {
		if (document.getElementById(ids[i])) {
			document.getElementById(ids[i]).style.display = (chk.checked?'none':'');
		}
	}
}
// everyone loves bug and improvement updates, so check for them
function scriptUpdate() {
	GM_xmlhttpRequest({
	    method:'GET',
	    url: 'http://www.danielslaughter.com/projects/greasemonkey_gcaltightener/update.php?versionNumber=' + versionNumber,
		onload:function(response){
			var div = document.createElement('div');
			div.innerHTML = response.responseText;
			div.style.display = 'none';
			refCon.appendChild(div);
		}
	});
}
