/*
 * @description initialize defaults for task windows.
 */
var strSelectedTaskID = '';
var strLastSelectedTaskID = '';
var arrTaskWindows = ['', '', '', '', '', '', '', '', '', ''];
var arrTaskSubWindows = ['', '', '', '', '', '', '', '', '', ''];
var intBorderWidth = 34;
var intBorderHeight = 54;
var intTaskbarHeight = 30;
var intTopOffset = 4;
var intBrowserWidth = 0;
var intBrowserHeight = 0;
/*
 * @description toggle the hover on/off effect on a taskbar item.
 * @strId this is the string id of the element.
 * @strAction this is the action the user is performing.
 */
function taskIDHover(strId, strAction)
{
	if (strAction == 'over')
	{
		if (strId == strSelectedTaskID)
		{
			setIDClass(strId, 'taskbar_item_on_hover');
		}
		else
		{
			setIDClass(strId, 'taskbar_item_off_hover');
		}
	}
	else if (strAction == 'out')
	{
		if (strId == strSelectedTaskID)
		{
			setIDClass(strId, 'taskbar_item_on');
		}
		else
		{
			setIDClass(strId, 'taskbar_item_off');
		}
	}
}
/*
 * @description set a taskbar item to selected.
 * @strId this is the string id of the element.
 * @strHover this sets the hover effect.
 */
function taskIDSelect(strId, strHover)
{
/*
 * KNOWN BUG: When opening an orphan window, we need to determine how to select it, and or other windows.
 * I think, an orphan window should always be on top, thus negating a sub selection issue.
 */
	if (strId.substr(8) < 1000000)
	{
		if (strSelectedTaskID != strId)
		{
			if (strSelectedTaskID != '')
			{
				// IF the selected task id is visible, we should deselect it.
				strLastSelectedTaskID = strSelectedTaskID;
				setIDClass(strSelectedTaskID, 'taskbar_item_off');
				//transitionEffect('window_' + strSelectedTaskID.substr(8), 'marginLeft', 0, 3000, 10, 150);
				// Reduce the z-index for the previous window.
				handleIDStyle('window_' + strSelectedTaskID.substr(8), 'opacity', 0.75);
				handleIDStyle('window_' + strSelectedTaskID.substr(8), 'zIndex', 990);
				// If there is a sub window, hide it.
				if (arrTaskSubWindows[strSelectedTaskID.substr(8)] != '')
				{
					// handleIDStyle('sub_window_' + strSelectedTaskID.substr(8), 'visibility', 'hidden');
					handleIDStyle('sub_window_' + strSelectedTaskID.substr(8), 'opacity', 0.75);
					handleIDStyle('sub_window_' + strSelectedTaskID.substr(8), 'zIndex', 990);
				}
			}
			if (strHover == 1)
			{
				setIDClass(strId, 'taskbar_item_on_hover');
			}
			else
			{
				setIDClass(strId, 'taskbar_item_on');
			}
			// If there is a sub window, show it.
			if (arrTaskSubWindows[strId.substr(8)] != '')
			{
				handleIDStyle('sub_window_' + strId.substr(8), 'visibility', 'visible');
				handleIDStyle('sub_window_' + strId.substr(8), 'opacity', 1);
				handleIDStyle('sub_window_' + strId.substr(8), 'zIndex', 1000);
				handleTitle(strBrowserTitle + ' - ' + arrTaskSubWindows[strId.substr(8)]);
			}
			else
			{
				handleTitle(strBrowserTitle + ' - ' + arrTaskWindows[strId.substr(8)]);
			}
			// Toggle the current window into view.
			//transitionEffect('window_' + strId.substr(8), 'marginLeft', -3000, 0, 10, 150);
			handleIDStyle('window_' + strId.substr(8), 'visibility', 'visible');
			handleIDStyle('window_' + strId.substr(8), 'opacity', 1);
			handleIDStyle('window_' + strId.substr(8), 'zIndex', 1000);
			strSelectedTaskID = strId;
		}
		else
		{
			// Deselect the window if being minimized. 1 = clicked.
			if (strHover == 1)
			{
				setIDClass(strId, 'taskbar_item_off_hover');
				// Set the title of the window, this might be set again, if there is another window coming into view.
				handleTitle(strBrowserTitle);
				//transitionEffect('window_' + strId.substr(8), 'marginLeft', 0, 3000, 10, 150);
				handleIDStyle('window_' + strId.substr(8), 'visibility', 'hidden');
				// If there is a sub window, hide it.
				if (arrTaskSubWindows[strId.substr(8)] != '')
				{
					handleIDStyle('sub_window_' + strId.substr(8), 'visibility', 'hidden');
				}
				strSelectedTaskID = '';
				// If there is another window open and visible, select it.
				for (var intI = (arrTaskWindows.length - 1); intI >= 0; intI--)
				{
					if (arrTaskWindows[intI] != '' && handleIDStyle('window_' + intI, 'visibility') == 'visible')
					{
						taskIDSelect('taskbar_' + intI, 0);
						break;
					}
				}
			}
		}
	}
}
/*
 * @description create a new window.
 * @strId this is the string id of the element.
 * @intWidth the width of the iframe.
 * @intHeight the height of the iframe.
 * @strLink the source url of the iframe.
 * @strIconLink the icon name.
 * @strIcons the icon theme.
 */
function taskNewWindow(intApplicationId, strTitle, intWidth, intHeight, strLink, strIconLink, strIcons)
{
	taskDoClose();
	var blnFound = false;
	var blnSlotAvailable = false;
	var strHtml = '', strNewTaskbar = '';
	// First, see if it is already open. If it is, switch to it.
	for (var intI = 0; intI < arrTaskWindows.length; intI++)
	{
		if (strTitle == arrTaskWindows[intI])
		{
			blnFound = true;
			blnSlotAvailable = true;
			taskIDSelect('taskbar_' + intI, 0);
		}
	}
	// Find the first available slot.
	if (!blnFound)
	{
		for (var intI = 0; intI < arrTaskWindows.length; intI++)
		{
			if (arrTaskWindows[intI] == '')
			{
				// This is the window to work on.
				arrTaskWindows[intI] = strTitle;
				blnSlotAvailable = true;
				// Determine the size of the window.
				intBrowserWidth = getWindowWidth();
				intBrowserHeight = getWindowHeight();
				// Determine if the window is bigger than the browser window.
				if ((intWidth + intBorderWidth) >= intBrowserWidth)
				{
					intWidth = intBrowserWidth - intBorderWidth;
				}
				if ((intHeight + intBorderHeight + intTaskbarHeight) >= intBrowserHeight)
				{
					intHeight = intBrowserHeight - intBorderHeight - intTaskbarHeight;
				}
				// Create taskbar item.
				strHtml = '<div class="taskbar_item_off" id="taskbar_' + intI + '" title="' + strTitle + '" onclick="taskIDSelect (this.id, 1);" onmouseover="taskIDHover (this.id, \'over\');" onmouseout="taskIDHover (this.id, \'out\');" onmousedown="return false;">';
				strHtml += '<img src="/media/icons/' + strIcons + '/search_loading.gif" class="taskbar_item_icon">';
				strHtml += ' ' + strTitle + '</div>';

				document.getElementById('taskbar').innerHTML += strHtml;
				// Determine the location of the window. Down the road, we should remember the last position of the window and place it there.
				var strParams = 'application_id=' + intApplicationId;
				ajax.open('POST', '/ajax/application_location_lookup.php', true);
				ajax.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
				ajax.setRequestHeader('content-length', strParams.length);
				ajax.setRequestHeader('connection', 'close');
				ajax.onreadystatechange = function()
				{
					if (ajax.readyState == 4 && ajax.status == 200)
					{
						var arrResults = ajax.responseText.split('|');
						intWinLeft = arrResults[0];
						intWinTop = arrResults[1];
						if (intWinLeft < 0 || intWinTop < 0 || ((parseInt(intWinLeft) + parseInt(intWidth) + parseInt(intBorderWidth)) > intBrowserWidth) || ((parseInt(intWinTop) + parseInt(intHeight) + parseInt(intBorderHeight)) > intBrowserHeight))
						{
							intWinLeft = (intBrowserWidth - (intWidth + intBorderWidth)) / 2;
							intWinTop = ((intBrowserHeight - (intHeight + intBorderHeight + intTaskbarHeight)) / 2) + intTopOffset;
						}
						// Create window item.
						strHtml = '<table class="main_window" onmousedown="taskIDSelect (\'taskbar_' + intI + '\', 0);" id="window_' + intI + '" style="top: ' + intWinTop + 'px; left: ' + intWinLeft + 'px;"><tr><td class="tl"></td>';
						strHtml += '<td class="tc drag" onmousedown="return false;" onmouseup="taskSetWindowLocation(' + intApplicationId + ', ' + intI + ');"><div class="title" title="Close">' + strTitle + '</div><div class="close" onclick="taskKill (' + intI + ');" onmouseover="this.className=\'close_over\'" onmouseout="this.className=\'close\'"></div></td>';
						strHtml += '<td class="tr"></td></tr>';
						strHtml += '<tr><td class="ml"></td><td class="mc">';
						strHtml += '<div onkeyup="taskIDSelect (\'taskbar_' + intI + '\', 0);" style="width: ' + intWidth + 'px; height: ' + intHeight + 'px;"><iframe name="main_iframe_' + intI + '" id="main_iframe_' + intI + '" class="main_window_iframe" style="width: ' + intWidth + 'px; height: ' + intHeight + 'px;" src="/applications/' + strLink + '?window_id=' + intI + '&window_title=' + strTitle + '" frameborder="0" scrolling="no"></iframe></div>';
						strHtml += '</td><td class="mr"></td></tr>';
						strHtml += '<tr><td class="bl"></td><td class="bc"></td><td class="br"></td></tr></table>';
						document.getElementById('main_window_cache').innerHTML = strHtml;
						var objWindow = document.getElementById('window_' + intI);
						document.getElementById('main_window_container').appendChild(objWindow);
						// Switch to this window.
						taskIDSelect('taskbar_' + intI, 0);
						// Update taskbar wait.
						strNewTaskbar = '<img src="/media/icons/' + strIcons + '/' + strIconLink + '_16.png" class="taskbar_item_icon">';
						strNewTaskbar += ' ' + strTitle + '</div>';
						document.getElementById('taskbar_' + intI).innerHTML = strNewTaskbar;
					}
				}
				ajax.send(strParams);
				break;
			}
		}
	}
	if (!blnSlotAvailable)
	{
		alert('Warning!\n\n You have reached the maximum number of applications.\n\nMaximum Set: ' + arrTaskWindows.length);
	}
}
/*
 * @description update user preference for window location.
 * @intApplicationId the id of the application.
 * @intI the if of the window.
 */
function taskSetWindowLocation(intApplicationId, intI)
{
	var objWindow = document.getElementById('window_' + intI);
	var strParams = 'application_id=' + intApplicationId;
	strParams += '&pos_x=' + objWindow.style.left;
	strParams += '&pos_y=' + objWindow.style.top;
	ajax.open('POST', '/ajax/application_location_update.php', true);
	ajax.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
	ajax.setRequestHeader('content-length', strParams.length);
	ajax.setRequestHeader('connection', 'close');
	ajax.onreadystatechange = function()
	{
		if (ajax.readyState == 4 && ajax.status == 200)
		{
			//alert (ajax.responseText);
		}
	}
	ajax.send(strParams);
}
/*
 * @description close a window, and select the next most eligible window.
 * @intI the if of the window.
 */
function taskKill(intI)
{
	if (arrTaskWindows[intI] != '')
	{
		var elTask = document.getElementById('taskbar_' + intI);
		document.getElementById('taskbar').removeChild(elTask);
		var elWindow = document.getElementById('window_' + intI);
		document.getElementById('main_window_container').removeChild(elWindow);
		arrTaskWindows[intI] = '';
		strSelectedTaskID = '';
		handleTitle(strBrowserTitle);
		// If there is another window open and visible, select it.
		for (var intI = (arrTaskWindows.length - 1); intI >= 0; intI--)
		{
			if (arrTaskWindows[intI] != '' && handleIDStyle('window_' + intI, 'visibility') == 'visible')
			{
				taskIDSelect('taskbar_' + intI, 0);
				break;
			}
		}
	}
}
/*
 * @description create a new sub window.
 * @strId this is the string id of the element.
 * @intWidth the width of the iframe.
 * @intHeight the height of the iframe.
 * @strLink the source url of the iframe.
 * @strParentWindowId the id of the parent window.
 * @strId the id of the data value being passed to the sub window.
 */
function subTaskNewWindow(strTitle, intWidth, intHeight, strLink, strParentWindowId, strId)
{
	if (strId === undefined)
	{
		doConfirm('No Record Selected', 'Please select a record first.', 150, 100);
		return;
	}
	var intI = strParentWindowId;
	var strHtml = '';
	// Check the sub window slot.
	if ((intI < 1000000 && arrTaskSubWindows[intI] == '') || intI > 1000000)
	{
		// This is the window to work on.
		arrTaskSubWindows[intI] = strTitle;
		// Determine the size of the window.
		var intBrowserWidth = getWindowWidth();
		var intBrowserHeight = getWindowHeight();
		// Determine if the window is bigger than the browser window.
		if ((intWidth + intBorderWidth) >= intBrowserWidth)
		{
			intWidth = intBrowserWidth - intBorderWidth;
		}
		if ((intHeight + intBorderHeight + intTaskbarHeight) >= intBrowserHeight)
		{
			intHeight = intBrowserHeight - intBorderHeight - intTaskbarHeight;
		}
		// Determine the center of the window.
		intWinLeft = (intBrowserWidth - (intWidth + intBorderWidth)) / 2;
		intWinTop = ((intBrowserHeight - (intHeight + intBorderHeight + intTaskbarHeight)) / 2) + intTopOffset;
		// Create window item.
		strHtml = '<table class="sub_window" onmousedown="taskIDSelect (\'taskbar_' + intI + '\', 0);" id="sub_window_' + intI + '" style="top: ' + intWinTop + 'px; left: ' + intWinLeft + 'px;"><tr><td class="tl"></td>';
		strHtml += '<td class="tc drag" onmousedown="return false;"><div class="title" title="Close">' + strTitle + '</div><div class="close" onclick="subTaskKill (' + intI + ');" onmouseover="this.className=\'close_over\'" onmouseout="this.className=\'close\'"></div></td>';
		strHtml += '<td class="tr"></td></tr>';
		strHtml += '<tr><td class="ml"></td><td class="mc">';
		strHtml += '<div style="width: ' + intWidth + 'px; height: ' + intHeight + 'px;"><iframe name="sub_iframe_' + intI + '" id="sub_iframe_' + intI + '" class="main_window_iframe" style="width: ' + intWidth + 'px; height: ' + intHeight + 'px;" src="' + strLink + '?sub_window_id=' + intI + '&window_id=' + strParentWindowId + '&id=' + strId + '" frameborder="0" scrolling="no"></iframe></div>';
		strHtml += '</td><td class="mr"></td></tr>';
		strHtml += '<tr><td class="bl"></td><td class="bc"></td><td class="br"></td></tr></table>';
		// Add window to screen.
		document.getElementById('main_window_cache').innerHTML = strHtml;
		var objWindow = document.getElementById('sub_window_' + intI);
		document.getElementById('main_window_container').appendChild(objWindow);
		// Show this window.
		handleIDStyle('sub_window_' + intI, 'visibility', 'visible');
		handleTitle(strBrowserTitle + ' - ' + arrTaskSubWindows[intI]);
		// Update the taskbar name.
		// Lock the parent window.
		if (intI < 1000000)
		{
			handleIDStyle('window_' + intI, 'display', 'none');
		}
	}
	else
	{
		alert('An anomaly has occurred. Please refresh the page to clear any current errors.\n\nERROR: Failed to find an available sub window slot.');
	}
}
/*
 * @description kill a sub task window.
 * @intId this is the id of the sub window.
 */
function subTaskKill(intId)
{
	if (arrTaskSubWindows[intId] != '')
	{
		var objWindow = document.getElementById('sub_window_' + intId);
		try 
		{
			document.getElementById('main_window_container').removeChild(objWindow);
		}
		catch (e){}
		if (intId < 1000000)
		{
			arrTaskSubWindows[intId] = '';
			handleTitle(strBrowserTitle + ' - ' + arrTaskWindows[intId]);
			// Unlock the parent window.
			handleIDStyle('window_' + intId, 'display', 'block');
		}
		else
		{
			handleTitle(strBrowserTitle);
		}
	}
}
/*
 * @description create a new confirmation window with no taskbar item.
 * @strTitle the title of the window.
 * @strMessage the message for the window.
 * @intWidth the width of the window.
 * @intHeight the height of the window.
 * @strAction the action to perform when confirming.
 */
var currentConfirmAction = '';
function doConfirm(strTitle, strMessage, intWidth, intHeight, strAction)
{
	document.getElementById('loading').style.display = 'block';
	strHtml = '<table class="confirm_box">';
	strHtml += '<tr><td>';
	strHtml += '<div style="width: ' + (intWidth + intBorderWidth) + 'px; height: ' + (intHeight + intBorderHeight) + 'px; margin: 0 auto;">';
	strHtml += '<table class="main_window"><tr><td class="tl"></td>';
	strHtml += '<td class="tc" onmousedown="return false;"><div class="title" title="Close">' + strTitle + '</div><div class="close" onclick="doConfirmAction(\'cancel\');" onmouseover="this.className=\'close_over\'" onmouseout="this.className=\'close\'"></div></td>';
	strHtml += '<td class="tr"></td></tr>';
	strHtml += '<tr><td class="ml"></td><td class="mc">';
	strHtml += '<div class="confirm_box_content" style="width: ' + intWidth + 'px; height: ' + intHeight + 'px;">';
	// If this is a simple message, display it,
	// Otherwise, load the page content via ajax.
	strHtml += strMessage;
	strHtml += '<br/>';
	strHtml += '<div style="text-align: right;">';
	if (strAction !== undefined)
	{
		currentConfirmAction = strAction;
		strHtml += '<input type="button" value="Cancel" onclick="doConfirmAction(\'cancel\');"/>';
		strHtml += '<input type="button" value="OK" onclick="doConfirmAction(\'ok\');"/>';
	}
	else
	{
		strHtml += '<input type="button" value="OK" onclick="doConfirmAction(\'cancel\');"/>';
	}
	strHtml += '</div>';
	strHtml += '</td><td class="mr"></td></tr>';
	strHtml += '<tr><td class="bl"></td><td class="bc"></td><td class="br"></td></tr></table>';
	strHtml += '</div>';
	strHtml += '</td></tr>';
	strHtml += '</table>';
	document.getElementById('confirm_box').innerHTML = strHtml;
}
/*
 * @description Send a confirmation window to the screen.
 */
function doConfirmAction(strAction)
{
	if (strAction == 'ok' && currentConfirmAction != '')
	{
		eval (currentConfirmAction);
		currentConfirmAction = '';
	}
	document.getElementById('confirm_box').innerHTML = '';
	document.getElementById('loading').style.display = 'none';
}
