// iCloak Release Candidate
// Ben Younkins
// based on "Invisibility Cloak v0.2" by Gina Trapani  2006-01-03
//
// ==UserScript==
// @name          iCloak
// @description   Blocks time-wasting web pages during work hours
// @include       http://*.slashdot.org/*
// @include       http://slashdot.org/*
// @include       http://*.slashdot.com/*
// @include       http://slashdot.com/*
// @include       http://metafilter.com/*
// @include       http://*.metafilter.com/*
// @include       http://*.digg.com/*
// @include       http://digg.com/*
// ==/UserScript==
//
// ==RevisionHistory==
// Version 0.1:
// Released: 2006-01-03.
// Initial release.
//
// Version 0.2:
// Released: 2006-01-18.
// Includes option to not apply cloak on the weekends.
//
// Version 0.3
// Released: Unreleased
// Creates surfing time-window which can go past midnight, moved weekend detect
//
// Version 0.4
// Released: Unreleased
// major rewrite of time code with arrays but trying to remain friendly to user editing
//
// Version 0.5
// Released: Unreleased
// added verification code to prevent errors by out-of-range input, or midnight as '24' errors
// ==/RevisionHistory==



(function () { // start encapsulation 
// ********************************************************************************
// The defaults are set for the stereotypical workweek:
//	* 8 AM to 5 PM workday
//	* an hour-long lunchbreak at noon
//	* Workdays are Monday thru Friday
// ********************************************************************************
// YOU CAN EDIT THE NEXT FEW LINES TO CUSTOMIZE FOR YOURSELF! :)
// NOTE:	Hours have to be set in military time: 0 = midnight, 5 = 5 AM, 12 = noon, 18 = 6PM
//		Shifts that cross midnight are okay, ie start at 8PM (20) & end at 5 AM (5)
//		Days are numbered with Sunday = 1, Monday = 2 ... Saturday = 7
//		Shifts that go through the weekend are ok, ie Thursday (5) through Monday (2)
	var day_start 	= 	 8;
	var day_end 	= 	17;
	var break_start = 	12;
	var break_end 	= 	13;
	var first_day 	= 	 2;
	var last_day	=	 6;
// END EDITABLE SECTION
// ********************************************************************************

// ------------------------------------------------------------------------------------------------------------------------------
// Start checking input
// ------------------------------------------------------------------------------------------------------------------------------
	if(day_start == 24)		{day_start = 0;}
	if(day_end == 24) 		{day_end = 0;}
	if(break_start == 24)	{break_start = 0;}
	if(break_end == 24)		{break_end = 0;}
	day_start 	= limitRange(day_start, 0, 23);
	day_end 	= limitRange(day_end, 0, 23);
	break_start = limitRange(break_start, 0, 23);
	break_end	= limitRange(break_end, 0, 23);
// ------------------------------------------------------------------------------------------------------------------------------
// Start checking input
// ------------------------------------------------------------------------------------------------------------------------------
	
// ------------------------------------------------------------------------------------------------------------------------------
// Start main body
// ------------------------------------------------------------------------------------------------------------------------------


	var dailySchedule = new Array();
	var workDays = new Array();

	setDailySchedule(dailySchedule, day_start, day_end, break_start, break_end);
	setWorkDays(workDays, first_day, last_day);
		
	var timeNow = new Date();
	var d = timeNow.getDay() + 1;
	var h = timeNow.getHours();
	
	if(workDays[d])
	{ // only fires on work days
		if(dailySchedule[h])
		{ // only fires if hour is "true"ly blocked
			blockPage();
			alert("PAGE BLOCKED BY iCloak:\nNo surfing during work hours!");
		}
		else
		{
			// alert("DEBUG: Surf allowed during off hours");
		}
	}
	else
	{
		// alert("DEBUG: Surf allowed on day off\n Today is: " + d + 1 + " ");
	}
	
// ------------------------------------------------------------------------------------------------------------------------------
// End main body
// ------------------------------------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------------------------------------
// title: setWorkDays
// input: workDays array (any data already in array will be lost), first_day, last_day
// output: workDays (passed by ref) with individual days marked true/false for work/free
// notes: if workDays is not an array, nothing happens
// 	=dilemma=	An 8 hour workday is described as 8 AM to 5 PM, but no work is done during the 5 PM hour
//			A 5 day workweek is described as Mon thru Fri, and work is done during Friday
// ------------------------------------------------------------------------------------------------------------------------------
function setWorkDays(workDays, first_day, last_day) {
	var i = 0;
	if(workDays.constructor == Array)
	{
		workDays.length = 0; // empty
		for(i = 0; i < 24; i++) { workDays[i] = false; } // initialize
		// true == monitored days, false indicated free days
		i = first_day;
		while(i != last_day)
		{
			workDays[i] = true;
		
			i++;
			if(i == workDays.length) { i = 0 }; // wrap around weekend
		}
		workDays[last_day] = true;
		
	}
}

// ------------------------------------------------------------------------------------------------------------------------------
// title: setDailySchedule
// input: 	dailySchedule array (any data already in array will be lost),
//		 day_start, day_end, break_start, break_end
// output: dailySchedule (passed by ref) with individual hours marked true/false for work/free
// notes: if dailySchedule is not an array, nothing happens
// ------------------------------------------------------------------------------------------------------------------------------
function setDailySchedule(dailySchedule, day_start, day_end, break_start, break_end) {
	var i = 0;
	if(dailySchedule.constructor == Array)
	{
		dailySchedule.length = 0; // empty
		for(i = 0; i < 24; i++) { dailySchedule[i] = false; } // initialize
		// true == monitored time, false indicated free time
		i = day_start;
		while(i != day_end)
		{
			dailySchedule[i] = true;
		
			i++;
			if(i == dailySchedule.length) { i = 0 }; // wrap around midnight
		}
		
		i = break_start;
		while(i != break_end)
		{
			dailySchedule[i] = false;
		
			i++;
			if(i == dailySchedule.length) { i = 0 }; // wrap around midnight
		}
	}
}

// ------------------------------------------------------------------------------------------------------------------------------
// title: limitRange
// input: some number X, lowest desired value, highest desired value
// output: if X falls outside of the range from the lowest to the highest (including the lowest/highest)
//		the output will be whichever of the lowest and highest that comes closest, otherwise the
//		output will be X
// notes: if lowend > highend, then X is returned
// ------------------------------------------------------------------------------------------------------------------------------
function limitRange(X, lowend, highend) {
	if(lowend > highend) { return X; }
	else if(X < lowend) { return lowend; }
	else if(X > highend) { return highend; }
	else { return X; }
}

// ------------------------------------------------------------------------------------------------------------------------------
// title: blockPage
// input: none
// output: none
// notes: based on "Invisibility Cloak v0.2" by Gina Trapani  2006-01-03
// ------------------------------------------------------------------------------------------------------------------------------
function blockPage() {
	var pageBody = (document.getElementsByTagName("body")[0]);
		pageBody.setAttribute('style', 'display:none!important');
}

})// end encapsulation
();

// ------------------------------------------------------------------------------------------------------------------------------
// title: 
// input: 
// output: 
// notes: 
// ------------------------------------------------------------------------------------------------------------------------------