// ==UserScript==
// @name           Automatic reloader
// @namespace      http://www.orangeninja.com
// @description    Automatic page refresher
// @include        http://*.facebook.com/home.php
// ==/UserScript==




// Time in minutes for refresh
var REFRESH_TIME = 5;
var BG_COLOUR = 'f99229';

var CSS_RULES = '#on_refresher { position: absolute; top: 10px; left: 10px; border: 1px solid #ccc; background-color: #'+BG_COLOUR+'; padding: 10px 0; -moz-opacity: 0.8; font-weight: bold; cursor: move; width: 11em; text-align: center; font-size: 11px; font-family: Trebuchet MS, Tahoma, Helvetica, sans-serif; } #on_time_remaining { display: block; }';


var time_remaining, countdown, refresher, running, refresh_delay;




/*************************************************
 Creates the countdown div to show remaining time
*************************************************/
function createCounter(){
	injectCSS(CSS_RULES);
	var div = document.createElement('div');
	div.setAttribute('id', 'on_refresher');
	var span = document.createElement('span');
	span.setAttribute('id', 'on_time_remaining');
	
	GM_setValue('delay_'+document.location.href, refresh_delay);
	
	var mins = Math.floor((refresh_delay * 1000) / 60);
	var secs = (refresh_delay * 1000) % 60;
	if(secs<10) secs = '0' + secs;
	span.appendChild(document.createTextNode('Refreshing in '+mins+':'+secs));
	div.appendChild(span);
	
	var stopLink = document.createElement('a');
	stopLink.style.cursor = 'pointer';
	stopLink.addEventListener('click', function(event){
		var stopLink = document.getElementById('on_time_remaining').nextSibling;
		if(running){
			window.clearTimeout(refresher);
			window.clearInterval(countdown);
			stopLink.textContent = 'Resume';
		}else{
			countdown = window.setInterval(updateCounter, 1000);
			refresher = window.setTimeout(doRefresh, time_remaining * 1000);
			stopLink.textContent = 'Stop';
		}
		running = !running;
	}, true);
	stopLink.textContent = 'Stop';
	div.appendChild(stopLink);
	div.appendChild(document.createTextNode(' | '));
	
	var refreshLink = document.createElement('a');
	refreshLink.style.cursor = 'pointer';
	refreshLink.addEventListener('click', function(event){
		window.location.href = window.location.href;
	}, true);
	refreshLink.textContent = 'Refresh now';
	div.appendChild(refreshLink);
	
	document.getElementsByTagName('body')[0].appendChild(div);
	updateCounter();
	makeDragable(div);
	if(GM_getValue('top_'+document.location.href)){
		div.style.top = GM_getValue('top_'+document.location.href) + 'px';
		div.style.left = GM_getValue('left_'+document.location.href) + 'px';
	}
	countdown = window.setInterval(updateCounter, 1000);
	running = true;
}






/**********************************************
 Makes the status box dragable
**********************************************/
function makeDragable(obj){
	obj.style.zIndex = 100;
	obj.style.position = 'absolute';
	var dragObj = null, offset = { X : null, Y : null };

	window.addEventListener('mousemove', function(ev){
		if(dragObj){
			dragObj.style.left = (ev.clientX - offset.X) +'px';
			dragObj.style.top = (ev.clientY - offset.Y) +'px';
		}
	}, true);
	
	window.addEventListener('mouseup', function(event){
		dragObj = null;
		GM_setValue('top_'+document.location.href, obj.offsetTop);
		GM_setValue('left_'+document.location.href, obj.offsetLeft);
	}, true);
	
	obj.addEventListener('mousedown', function(event){
		dragObj = this;
		offset.X = event.clientX - this.offsetLeft;
		offset.Y = event.clientY - this.offsetTop;
	}, true);
}





/**********************************************
 Updates the countdown with the remaining time
**********************************************/
function updateCounter(){
	if(!time_remaining) time_remaining = (refresh_delay * 60) + 1;
	time_remaining--;
	var mins = Math.floor(time_remaining / 60);
	var secs = time_remaining % 60;
	if(secs<10) secs = '0' + secs;
	var cont = document.getElementById('on_time_remaining');
	cont.textContent = 'Refreshing in '+mins+':'+secs;
	if(time_remaining == 0){
		cont.textContent = 'Refreshing...';
		window.clearInterval(countdown);
	}
}




/*****************************************************
 Stops the refresher and (removes it from the page ?)
*****************************************************/
function stopRefresher(){
	window.clearTimeout(refresher);
	window.clearInterval(countdown);
}




/*****************************************************
 Does the actual refresh
*****************************************************/
function doRefresh(){
	window.location.href = window.location.href;
}





/*****************************************************
 Injects some CSS rules into the document
*****************************************************/
function injectCSS(rules){
	var style = document.createElement('style');
	style.setAttribute('type', 'text/css');
	style.textContent = rules;
	document.getElementsByTagName('head')[0].appendChild(style);
}





refresh_delay = GM_getValue('delay_'+document.location.href);
if(!refresh_delay) refresh_delay = REFRESH_TIME;

refresher = window.setTimeout(doRefresh, refresh_delay * 1000 * 60);
createCounter();
