var activeDesktopDir = "C:\\Documents and Settings\\Blake\\Desktop\\My Desktop\\";	// Directory that contains this file

var settingsFile = activeDesktopDir + "settings.ini";
var notesFile    = activeDesktopDir + "notes.dat";
var scheduleFile = activeDesktopDir + "schedule.dat";

var wallpaper,				// 0 (none) / 1 (specified) / 2 (random)
	wallpaperPath,			// eg C:\\wallpapers\\wall.png
	wallpaperDir,			// eg C:\\wallpapers\\
	wallpaperHeight,		// 100% / 960px / auto
	wallpaperWidth, 		// 100% / 1280px / auto
	wallpaperRepeat,		// no-repeat / repeat / repeat-x / repeat-y
	wallpaperX,				// left / center / right
	wallpaperY,				// top / center / bottom
	BGColor,				// color
	linkColorDormant,		// color
	linkColorActive,		// color
	linkBGColorDormant,		// color
	linkBGColorActive,		// color
	linkOpacityDormant,		// 0 <= x <= 100 
	linkOpacityActive,		// 0 <= x <= 100  
	headingColor,			// color
	headingBGColor,			// color
	headingOpacity,			// 0 <= x <= 100 
	textareaColorDormant,	// color
	textareaColorActive,	// color
	textareaBGColorDormant,	// color
	textareaBGColorActive,	// color
	textareaOpacityDormant,	// 0 <= x <= 100 
	textareaOpacityActive,	// 0 <= x <= 100  
	inputColorDormant,		// color
	inputColorActive,		// color
	inputBGColorDormant,	// color
	inputBGColorActive,		// color
	inputOpacityDormant,	// 0 <= x <= 100 
	inputOpacityActive;		// 0 <= x <= 100  

var activeWallpaper;


function init(){
	var settingsArray = new Array ();
	settingsArray = readFile(settingsFile).split('\n');

	wallpaper				= settingsArray[0];
	wallpaperPath			= settingsArray[1];
	wallpaperDir			= settingsArray[2];
	wallpaperHeight			= settingsArray[3];
	wallpaperWidth			= settingsArray[4];
	wallpaperRepeat			= settingsArray[5];
	wallpaperX				= settingsArray[6];
	wallpaperY				= settingsArray[7];
	BGColor					= settingsArray[8];
	linkColorDormant		= settingsArray[9];
	linkColorActive			= settingsArray[10];
	linkBGColorDormant		= settingsArray[11];
	linkBGColorActive		= settingsArray[12];
	linkOpacityDormant		= settingsArray[13];
	linkOpacityActive		= settingsArray[14];
	headingColor			= settingsArray[15];
	headingBGColor			= settingsArray[16];
	headingOpacity			= settingsArray[17];
	textareaColorDormant	= settingsArray[18];
	textareaColorActive		= settingsArray[19];
	textareaBGColorDormant	= settingsArray[20];
	textareaBGColorActive	= settingsArray[21];
	textareaOpacityDormant	= settingsArray[22];
	textareaOpacityActive	= settingsArray[23];
	inputColorDormant		= settingsArray[24];
	inputColorActive		= settingsArray[25];
	inputBGColorDormant		= settingsArray[26];
	inputBGColorActive		= settingsArray[27];
	inputOpacityDormant		= settingsArray[28];
	inputOpacityActive		= settingsArray[29];

	document.body.style.backgroundColor = BGColor;

	SetAll('H1', headingColor, headingBGColor, headingOpacity);
	SetAll('A', linkColorDormant, linkBGColorDormant, linkOpacityDormant);
	SetAll('SPAN', linkColorDormant, linkBGColorDormant, linkOpacityDormant);
	SetAll('INPUT', inputColorDormant, inputBGColorDormant, inputOpacityDormant);
	SetAll('TEXTAREA', textareaColorDormant, textareaBGColorDormant, textareaOpacityDormant);

	document.all.today.style.color = linkColorActive;
	document.all.today.style.backgroundColor = linkBGColorActive;
	document.all.today.filters.alpha.opacity = linkOpacityDormant;

	if (wallpaper != 0){
		if (wallpaper == 1)
			activeWallpaper = wallpaperPath;
		else if (wallpaper == 2)
			activeWallpaper = randomFile(wallpaperDir);
		if (wallpaperRepeat == 'no-repeat')	{
			document.background.src = activeWallpaper;
			document.background.style.width = wallpaperWidth;
			document.background.style.height = wallpaperHeight;
			if (wallpaperY == 'top') {
				document.all.bg.style.top = '0px';
			} else {
				var y = screen.height - document.background.clientHeight;
				if (wallpaperY == 'center')
					document.all.bg.style.top = y / 2;
				else if (wallpaperY == 'bottom')
					document.all.bg.style.top = y;
			}

			if (wallpaperX == 'left') {
				document.all.bg.style.left = '0px';
			} else {
				var x = screen.width - document.background.clientWidth;
				if (wallpaperX == 'center')
					document.all.bg.style.left = x / 2;
				else if (wallpaperX == 'right')
					document.all.bg.style.left = x;
			}
		} else {
			document.background.style.visibility = 'hidden';
			document.body.background = activeWallpaper;
			document.body.style.backgroundRepeat = wallpaperRepeat;
			document.body.style.backgroundPosition = wallpaperY + ' ' + wallpaperX;
		}
	}

	document.notes.pad.value = readFile(notesFile);
	document.schedule.pad.value = readFile(scheduleFile);
}


function SetAll(tag, color, BGColor, opacity) {
	var elements = document.getElementsByTagName(tag);	
	for(var i = 0; i < elements.length; i++) {
		elements.item(i).style.color = color;
		elements.item(i).style.backgroundColor = BGColor;
		elements.item(i).filters.alpha.opacity = opacity;
	}
}


function writeSettings(){
	var settings = wallpaper + "\n" 
				 + wallpaperPath + "\n"
				 + wallpaperDir + "\n"
				 + wallpaperHeight + "\n"
				 + wallpaperWidth + "\n"
				 + wallpaperRepeat + "\n"
				 + wallpaperX + "\n"
				 + wallpaperY + "\n"
				 + BGColor + "\n"
				 + linkColorDormant + "\n"
				 + linkColorActive + "\n"
				 + linkBGColorDormant + "\n"
				 + linkBGColorActive + "\n"
				 + linkOpacityDormant + "\n"
				 + linkOpacityActive + "\n"
				 + headingColor + "\n"
				 + headingBGColor + "\n"
				 + headingOpacity + "\n"
				 + textareaColorDormant + "\n"
				 + textareaColorActive + "\n"
				 + textareaBGColorDormant + "\n"
				 + textareaBGColorActive + "\n"
				 + textareaOpacityDormant + "\n"
				 + textareaOpacityActive + "\n"
				 + inputColorDormant + "\n"
				 + inputColorActive + "\n"
				 + inputBGColorDormant + "\n"
				 + inputBGColorActive + "\n"
				 + inputOpacityDormant + "\n"
				 + inputOpacityActive;

	writeFile(settingsFile, settings)
}


// Execute a program or open a file
function execute(path){
	var folder = path.substr(0, path.lastIndexOf('\\'));
	var objShell = new ActiveXObject("Shell.Application");
	objShell.ShellExecute(path, '', folder, 'open', 1);
}


// Opens Explorer in the specified directory
function explore(directory){ 
	var objShell = new ActiveXObject("Shell.Application"); 
	objShell.ShellExecute('explorer', '/n,/e,' + directory, directory, 'open', 1); 
} 


// Opens a specified file in EditPlus
function editPlus(path){
	var objShell = new ActiveXObject("Shell.Application");
	objShell.ShellExecute('C:\\Program Files\\EditPlus 2\\editplus.exe', path, 'C:\\Program Files\\EditPlus 2\\', 'open', 1);
}


// Return the contents of a specified file
function readFile(path){
	var myActiveXObject = new ActiveXObject("Scripting.FileSystemObject");
	try {
		var file = myActiveXObject.OpenTextFile(path, 1);
		var content = file.ReadAll();
		file.Close();
		return content;
	} catch (error) {
		writeFile(path, "")
		return "";
	}
}


// Write the specifed content to the specified file
function writeFile(path, content){
	var myActiveXObject = new ActiveXObject("Scripting.FileSystemObject");
	var file = myActiveXObject.CreateTextFile(path, true);
	file.Write(content);
	file.Close();
}


// Eject DVD/CD tray 
function openDrive(driveNumber){
	var oWMP = new ActiveXObject("WMPlayer.OCX"); 
	oWMP.cdromCollection.Item(driveNumber).Eject();
}


// Return an array of paths to all the files in a specified directory 
function getFileArray(directory){
	var myActiveXObject = new ActiveXObject("Scripting.FileSystemObject"); 
	var files = new Enumerator(myActiveXObject.GetFolder(directory).Files); 
	var fileArray = new Array ();
	while(!files.atEnd()) { 
		fileArray[fileArray.length] = files.item();
		files.moveNext(); 
	} 
	return fileArray;
}


// Pick a random file from a specified directory
function randomFile(directory){
	var fileArray = getFileArray(directory);
	return fileArray[Math.round(Math.random() * (fileArray.length-1))]; 
}


// Write html for calendar showing current month and day
function writeCalendar(){
	var day_of_week		= new Array('S','M','T','W','T','F','S');
	var month_of_year	= new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var Calendar = new Date();
	var year	= Calendar.getYear();	// Returns year
	var month	= Calendar.getMonth();  // Returns month (0-11)
	var today	= Calendar.getDate();   // Returns day (1-31)
	var weekday = Calendar.getDay();    // Returns weekday (0-6)
	var cal;	// Used for printing

	Calendar.setDate(1);			// Start the calendar day at '1'
	Calendar.setMonth(month);		// Start the calendar month at now

	cal = '<h1>' + month_of_year[month]  + ' ' + year + '</h1>';
	for(i=0; i<7; i++){
		if(weekday == i)
			cal += '<b style="font-weight: bold">' + day_of_week[i] + '</b>';
		else
			cal += '<b>' + day_of_week[i] + '</b>';
	}
	cal += '<br />';
	for(i=0; i<Calendar.getDay(); i++)
		cal += '<span style="visibility:hidden;"></span>';
	for(i=0; i<31; i++){
		if(i < Calendar.getDate()){
			var week_day = Calendar.getDay();
			if(i>0 && week_day == 0)
				cal += '<br />';
			var day = Calendar.getDate();
			if(day == today)
				cal += '<span id="today" title="' + day + "/" + (month+1) + "/" + year + '">' + day + '</span>';
			else
				cal += '<span title="' + day + "/" + (month+1) + "/" + year + '">' + day + '</span>';
		}
		Calendar.setDate(Calendar.getDate()+1);
	}
	document.write(cal);
}


if (document.all) {
    document.onmouseover = mouseOverEvent;
    document.onmouseout = mouseOutEvent;
    document.onfocusin = focusInEvent;
    document.onfocusout = focusOutEvent;
}


function mouseOverEvent(){
	var eSrc = window.event.srcElement;

	if (eSrc.tagName == 'A')	{
		eSrc.filters.alpha.opacity = linkOpacityActive; 
		eSrc.style.color = linkColorActive; 
		eSrc.style.backgroundColor = linkBGColorActive; 
	} else if (eSrc.tagName == 'INPUT') {
		eSrc.style.color = inputColorActive; 
		eSrc.style.backgroundColor = inputBGColorActive; 
	} else if (eSrc.tagName == 'TEXTAREA') {
		eSrc.style.color = textareaColorActive; 
		eSrc.style.backgroundColor = textareaBGColorActive; 
	}
}

function mouseOutEvent(){
	var eSrc = window.event.srcElement;

	if (eSrc.tagName == 'A')	{
		eSrc.filters.alpha.opacity = linkOpacityDormant; 
		fade(eSrc, linkColorDormant, linkBGColorDormant);
	} else if (eSrc.tagName == 'INPUT')	{
		fade(eSrc, inputColorDormant, inputBGColorDormant);
	} else if (eSrc.tagName == 'TEXTAREA')	{
		fade(eSrc, textareaColorDormant, textareaBGColorDormant);
	}
}

function focusInEvent(){
	var eSrc = window.event.srcElement;

	if (eSrc.tagName == 'INPUT')	{
		eSrc.filters.alpha.opacity = inputOpacityActive; 
		eSrc.value = '';
	} else if (eSrc.tagName == 'TEXTAREA') {
		eSrc.filters.alpha.opacity = textareaOpacityActive; 
	}
}

function focusOutEvent(){
	var eSrc = window.event.srcElement;

	if (eSrc.tagName == 'INPUT')	{
		eSrc.filters.alpha.opacity = inputOpacityDormant; 
		eSrc.value = '...';
	} else if (eSrc.tagName == 'TEXTAREA') {
		eSrc.filters.alpha.opacity = textareaOpacityDormant; 
	}
}

function fade(object, color, BGColor){
  object.filters[0].apply(); 
  object.style.color = color; 
  object.style.backgroundColor = BGColor; 
  object.filters[0].play();
}