/*
 * @description Initialize some default values.
 */
var intCurrentSelectedTask = -1;
var intDefaultUpdateManagerCycle = 5000;
var intUpdateManagerCycle = intDefaultUpdateManagerCycle;
var blnUpdateTaskbarNotificationStart = true;
var strSelectedTaskZone = 'taskbar_favourites';
var taskClose = '';
/*
 * @description The update manager handles all server to client background transactions. Its speed is defaulted to 5 seconds, but can be ramped up for specific monitoring.
 */
function updateManager()
{
	ajax3.open('GET', '/ajax/update_manager.php');
	ajax3.onreadystatechange = function()
	{
		if (ajax3.readyState == 4 && ajax3.status == 200)
		{
			var response = ajax3.responseText;
			if (response != '')
			{
				eval(response);
			}
			setTimeout('updateManager()', intUpdateManagerCycle);
		}
	}
	ajax3.send(null);
	updateSystemClock();
}
/*
 * @description Update the update manager cycle speed. If no speed is set, it is reset to default.
 * @param intMilliseconds repeat cycle for the manager.
 * @returns null.
 */
function updateManagerCycle(intMilliseconds)
{
	if (intMilliseconds === undefined)
	{
		intMilliseconds = intDefaultUpdateManagerCycle;
	}
	intUpdateManagerCycle = intMilliseconds;
}
/*
 * @description Update the system clock.
 * @returns null.
 */
function updateSystemClock()
{
	document.getElementById('system_clock').innerHTML = getDateTime();
}
/*
 * @description opens the taskbar.
 * @returns null.
 */
function taskDoOpen()
{
	toggleIDAttribute('main_mnu', 'display', 'block|none');
	setIDFocus('main_mnu_search');
}
/*
 * @description starts closing the taskbar.
 * @param strTaskId taskbar id.
 * @param strTheme the current theme.
 * @returns null.
 */
function taskInitClose(strTaskId, strTheme)
{
	taskClose = setTimeout('taskDoFullClose(\'' + strTaskId + '\', \'' + strTheme + '\')', 200);
}
/*
 * @description stops the timeout on taskbar close, and sets focus to the search bar.
 * @returns null.
 */
function taskStopClose()
{
	if (taskClose != '')
	{
		clearTimeout(taskClose);
	}
	setIDFocus('main_mnu_search');
}
/*
 * @description closes and resets the taskbar.
 * @param strTaskId taskbar id.
 * @param strTheme the current theme.
 * @returns null.
 */
function taskDoFullClose(strTaskId, strTheme)
{
	taskDoClose();
	resetTaskSection(strTaskId, strTheme);
}
/*
 * @description closes the taskbar.
 * @returns null.
 */
function taskDoClose()
{
	document.getElementById('main_mnu_search').blur();
	handleIDStyle('main_mnu', 'display', 'none');
}
/*
 * @description Toggle between favourite programs and all programs.
 * @param strId taskbar id.
 * @param strTheme the current theme.
 * @returns null.
 */
function toggleTaskSection(strId, strTheme)
{
	var objThis = document.getElementById(strId);
	var strLinkText = 'All Applications';
	var strImageSrc = 'more';
	// Update the text for this link, and the icon.
	if (objThis.innerHTML.indexOf('Favourite Applications') == -1)
	{
		strLinkText = 'Favourite Applications';
		strImageSrc = 'less';
	}
	objThis.innerHTML = '<img src=\"theme/' + strTheme + '/media/arrow_' + strImageSrc + '_applications.png" class="taskbar_icon_more" title="' + strLinkText + '"> ' + strLinkText;
	if (handleIDStyle('taskbar_search', 'display') == 'block')
	{
		handleIDStyle('taskbar_search', 'display', 'none');
		if (strLinkText == 'Favourite Applications')
		{
			handleIDStyle('taskbar_favourites', 'display', 'none');
			handleIDStyle('taskbar_applications', 'display', 'block');
			taskSetSelectedTaskZone ('taskbar_applications');
		}
		else
		{
			handleIDStyle('taskbar_favourites', 'display', 'block');
			handleIDStyle('taskbar_applications', 'display', 'none');
			taskSetSelectedTaskZone ('taskbar_favourites');
		}
	}
	else
	{
		toggleIDAttribute('taskbar_favourites', 'display', 'none|block');
		toggleIDAttribute('taskbar_applications', 'display', 'block|none');
		if (handleIDStyle('taskbar_favourites', 'display') == 'block')
		{
			taskSetSelectedTaskZone ('taskbar_favourites');
		}
		else
		{
			taskSetSelectedTaskZone ('taskbar_applications');
		}
	}
	taskStopClose();
}
/*
 * @description Reset the taskbar based on the current theme.
 * @param strId taskbar id.
 * @param strTheme the current theme.
 * @returns null.
 */
function resetTaskSection(strId, strTheme)
{
	document.getElementById(strId).innerHTML = '<img src="/theme/' + strTheme + '/media/arrow_more_applications.png" class="taskbar_icon_more" title="All Applications"> All Applications';
	handleIDStyle('taskbar_search', 'display', 'none');
	handleIDStyle('taskbar_favourites', 'display', 'block');
	handleIDStyle('taskbar_applications', 'display', 'none');
	taskSetSelectedTaskZone ('taskbar_favourites');
}
/*
 * @description Close the taskbar and relocate.
 * @returns redirect.
 */
function taskDoLogout()
{
	taskDoClose();
	window.location = 'logout.php';
}
/*
 * @description Close the taskbar and relocate.
 * @returns redirect.
 */
function taskDoLock()
{
	taskDoClose();
	window.location = 'lock.php';
}
/*
 * @description update the current task Zone.
 * @param strTaskZone
 * @returns null.
 */
function taskSetSelectedTaskZone (strTaskZone)
{
	taskResetSelectedTask ();
	strSelectedTaskZone = strTaskZone;
}
/*
 * @description If there is a selected task, reset it.
 * @returns null.
 */
function taskResetSelectedTask ()
{
	if (intCurrentSelectedTask != -1)
	{
		var objTasks = document.getElementById(strSelectedTaskZone);
		var arrTasks = objTasks.getElementsByTagName('a');
		if (intCurrentSelectedTask < arrTasks.length)
		{
			// Set the old task item to unselected.
			arrTasks[intCurrentSelectedTask].className = 'taskbar_application';
		}
		else if (intCurrentSelectedTask == arrTasks.length)
		{
			// Set the zone switcher to the selected task.
			document.getElementById('taskbar_section_selection').className = 'taskbar_more_applications';
		}
		intCurrentSelectedTask = -1;
	}
}
function taskClearSearchField(objElement, strDefaultText)
{
	if (objElement.value == strDefaultText)
	{
		objElement.value = '';
	}
}
function taskDoSearch(objElement, objEvent, strTheme)
{
	if (window.event)
	{
		var strKey = window.event.keyCode;
	}
	else if (objEvent)
	{
		var strKey = objEvent.which;
	}
	if (strKey == 13 || strKey == 38 || strKey == 40)
	{
		var objTasks = document.getElementById(strSelectedTaskZone);
		var arrTasks = objTasks.getElementsByTagName('a');
		if (arrTasks.length)
		{
			if (strKey == 38 || strKey == 40) 
			{
				// If the key is up, move the selection up.
				if (intCurrentSelectedTask != -1 && intCurrentSelectedTask < arrTasks.length)
				{
					// Set the old item to unselected.
					arrTasks[intCurrentSelectedTask].className = 'taskbar_application';
				}
				else if (intCurrentSelectedTask == arrTasks.length)
				{
					// Set the zone switcher to the selected item.
					document.getElementById('taskbar_section_selection').className = 'taskbar_more_applications';
				}
				// Update the currently selected task.
				if (strKey == 38) 
				{
					intCurrentSelectedTask = intCurrentSelectedTask - 1;
				}
				else if (strKey == 40)
				{
					intCurrentSelectedTask = intCurrentSelectedTask + 1;
				}
				// If the currently selected task is out of bounds, bring it back into bounds.
				if (intCurrentSelectedTask < 0)
				{
					// Set to the last task.
					intCurrentSelectedTask = arrTasks.length;
				}
				else if (intCurrentSelectedTask > arrTasks.length)
				{
					// Set to the first task.
					intCurrentSelectedTask = 0;
				}
				// If currently selected task is less than bounds, update the main tasks, otherwise select the zone switcher.
				if (intCurrentSelectedTask < arrTasks.length)
				{
					arrTasks[intCurrentSelectedTask].className = 'taskbar_application_selected';
				}
				else
				{
					// Set the zone switcher to the selected item.
					document.getElementById('taskbar_section_selection').className = 'taskbar_more_applications_selected';
				}
			}
			else if (strKey == 13)
			{
				// If the key is enter, select the current item, by eval'ing the onclick event.
				if (intCurrentSelectedTask != -1 && intCurrentSelectedTask <= arrTasks.length)
				{
					if (intCurrentSelectedTask < arrTasks.length)
					{
						var strFunction = arrTasks[intCurrentSelectedTask].onclick.toString();
						intCurrentSelectedTask = -1;
					}
					else
					{
						// Set the zone switcher to the selected item.
						var strFunction = document.getElementById('taskbar_section_selection').onclick.toString();
					}
					strFunction = strFunction.substr(strFunction.lastIndexOf('{') + 1);
					strFunction = strFunction.substr(0, strFunction.lastIndexOf('return false;') - 1);
					eval(strFunction);
				}
			}
		}
	}
	else
	{
		var strValue = objElement.value;
		// If the current value contains the default value, but is not the exact same, remove the default text. Otherwise, if the value is blank, reset the search.
		if (strValue != '')
		{
			//strValue = objElement.value.substr(strDefaultText.length);
			//objElement.value = strValue;
			var strParams = 'q=' + strValue;
			ajax2.open('POST', '/ajax/taskbar_search.php', true);
			ajax2.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			ajax2.setRequestHeader('Content-length', strParams.length);
			ajax2.setRequestHeader('Connection', 'close');
			ajax2.onreadystatechange = function()
			{
				if (ajax2.readyState == 4 && ajax2.status == 200)
				{
					// Set search window to open and close both the other options.
					handleIDStyle('taskbar_favourites', 'display', 'none');
					handleIDStyle('taskbar_applications', 'display', 'none');
					handleIDStyle('taskbar_search', 'display', 'block');
					// Reset the selected task, because the task list is going to change.
					taskSetSelectedTaskZone ('taskbar_search');
					// Update the text box.
					document.getElementById('taskbar_search').innerHTML = ajax2.responseText;
				}
			}
			ajax2.send(strParams);
		}
		else if (strValue == '')
		{
			//objElement.value = strDefaultText;
			//strValue = strDefaultText;
			handleIDStyle('taskbar_favourites', 'display', 'block');
			handleIDStyle('taskbar_applications', 'display', 'none');
			handleIDStyle('taskbar_search', 'display', 'none');
			taskSetSelectedTaskZone ('taskbar_favourites');
		}
	}
}
function updateMainMenu()
{
	var strParams = 'action=update';
	ajax2.open('POST', '/ajax/taskbar_update.php', true);
	ajax2.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	ajax2.setRequestHeader('Content-length', strParams.length);
	ajax2.setRequestHeader('Connection', 'close');
	ajax2.onreadystatechange = function()
	{
		if (ajax2.readyState == 4 && ajax2.status == 200)
		{
			response = ajax2.responseText.split('|');
			try {
				document.getElementById('taskbar_favourites').innerHTML = response[0];
				document.getElementById('taskbar_applications').innerHTML = response[1];
			} catch (e) {}
		}
	}
	ajax2.send(strParams);
}
function updateWindowContent(intWindowId, strKey, strValue)
{
	var objIframe = document.getElementById('main_iframe_' + intWindowId);
	if (strKey !== undefined && strValue !== undefined)
	{
		objIframe.src = objIframe.src + '&' + strKey + '=' + strParam;
	}
	else
	{
		objIframe.src = objIframe.src;
	}
}
/*
 * @description Disable the popup welcome notification. Runs only once.
 * @returns null.
 */
function updateTaskbarNotificationStart()
{
	if (blnUpdateTaskbarNotificationStart)
	{
		blnUpdateTaskbarNotificationStart = false;
		ajax.open('GET', '/ajax/taskbar_notification_start.php');
		ajax.send(null);
	}
}
/*
 * @description Disable the current stylesheet, update its source, and then enable it.
 * @param strId the string ID of the link.
 * @param strHref the string reference to the new stylesheet.
 * @returns null.
 */
function updateIDCSSTheme (strId, strHref)
{
	var objLink = document.getElementById(strId);
	if (objLink)
	{
		objLink.disabled = true;
		objLink.href = strHref;
		objLink.disabled = false;
	}
}
/*
 * @description When mousing over a lock key, change it's value.
 * @param objThis the key/div object.
 * @returns null.
 */
function doLockHover(objThis)
{
	if (objThis.className != 'lock_on')
	{
		if (objThis.className != 'lock_hover')
		{
			objThis.className = 'lock_hover';
		}
		else
		{
			objThis.className = 'lock_off';
		}
	}
}
/*
 * @description When clicked, this toggles the lock status.
 * @param objThis the key/div object.
 * @returns null.
 */
function doLockSelect (objThis)
{
	if (objThis.className != 'lock_on')
	{
		objThis.className = 'lock_on';
	}
	else
	{
		objThis.className = 'lock_hover';
	}
}
/*
 * @description Process and evaluate the keys entered against the users key code.
 * @returns true/false.
 */
function doEvalLock()
{
	dualTransitionEffect('lock_pad', 'lock_pad', 'height', 200, 0, 10, 10, 1000);
	var objLockPad = document.getElementById('lock_pad');
	var arrLockKeys = objLockPad.getElementsByTagName('div');
	var strKeyCode = '';
	// Loop through each record, determine if it is 'on', and collect it.
	for (var i = 0; i < arrLockKeys.length; i++)
	{
		if (arrLockKeys[i].className == 'lock_on')
		{
			strKeyCode += arrLockKeys[i].id.substr(5);
		}
		arrLockKeys[i].className = 'lock_off';
	}
	if (strKeyCode != '' && strKeyCode == '000102111222')
	{
		return true;
	}
	return false;
}