var v4_timer_timeOutIDs = new Array();

function v4_timer_showContainer(typ, hideLoadIcon)
{
	if ($('v4_timerContainer') && $('v4_timerButtonDiv') && $('v4_contentContainer'))
	{
		$('v4_timerContainer').style.right = ($('v4_contentContainer').cumulativeOffset()[0] + 1) + 'px';
		$('v4_timerContainer').style.top = $('v4_timerButtonDiv').cumulativeOffset()[1] + $('v4_timerButtonDiv').getHeight() + 1 + 'px';
		var getTimer = false;
		switch (typ)
		{
			case 'showAdmin':
			case 'showNew':
				getTimer = true;
				break;
		}
		if (getTimer)
		{
			if ((typeof hideLoadIcon == 'undefined') || (hideLoadIcon != true))
			{
				v4_timer_showLoad();
			}
			var url = 'ajax/srv/timer.php?typ='+encodeURIComponent(typ);
			new Ajax.Request(url,
				{
					method: 'get',
					onComplete: function (t)
					{
						var response = t.responseXML;
						var stylesheet = loadStyleSheet('ajax/timer.xsl');
						parseStyleSheet(stylesheet, response, $('v4_timerContainer'));
						($('v4_menuNewTimerText')) ? $('v4_menuNewTimerText').focus() : true;
						if ($('v4_timer_admin_ids'))
						{
							v4_timer_clearTimeOuts();
							v4_timer_countTime($('v4_timer_admin_ids').innerHTML);
						}
					}
				}
			);
			$('v4_timerContainer').show();
		}
	}
}

function v4_timer_clearTimeOuts()
{
	if (v4_timer_timeOutIDs.length > 0)
	{
		for (var i = 0; i < v4_timer_timeOutIDs.length; i++)
		{
			window.clearTimeout(v4_timer_timeOutIDs[i]);
		}
	}
}

function v4_timer_showLoad()
{
	if ($('v4_timerContainer'))
	{
		$('v4_timerContainer').innerHTML = show_load(true);
	}
}

function v4_timer_hideContainer()
{
	if ($('v4_timerContainer'))
	{
		$('v4_timerContainer').hide();
	}
}

function v4_timer_create(e, textElement, rrVal, bugVal)
{
	var theElement = Event.element(e);
	var theText = ($(textElement)) ? $(textElement).value : '';
	var keynum = (e.keyCode) ? e.keyCode : e.which;
	var rrID = (typeof rrVal != 'undefined') ? rrVal : 0;
	var bugID = (typeof bugVal != 'undefined') ? bugVal : 0;
	if ((theElement.type == 'button') || ((theElement.type == 'text') && (keynum == 13)) || (theElement.id == 'bugTimerImg') || (theElement.hasClassName('rrTimer')))
	{
		if (trimString(theText).length > 0)
		{
			showInfoDivContainer('', 'ajax');
			var url = 'ajax/srv/timer.php?typ=create&rrID=' + rrID + '&bugID=' + bugID + '&text='+encodeURIComponent(theText);
			new Ajax.Request(url,
				{
					method: 'get',
					onComplete: function (t)
					{
						var res = t.responseText;
						if (parseInt(res, 10) > 0) {
							showInfoDivContainer(getLang("all", "timerSuccessfulCreated"));
							v4_timer_refreshCurrentTimerCount();
						} else {
							showInfoDivContainer(getLang("all", "timerFailedToCreate"), 'fehler');
						}
						p4_timer_showContainerPortal();
						v4_timer_showContainer('showAdmin');
					}
				}
			);
		}
		else
		{
			alert(getLang("all", "haveToSetName"));
		}
	}
}

function v4_timer_delete(timerID, timerName)
{
	if (confirm(getLang("menu", "portal_delTimerQuestion").replace("#1#", timerName)))
	{
		showInfoDivContainer('', 'ajax');
		var url = 'ajax/srv/timer.php?typ=delete&timerID='+encodeURIComponent(timerID);
		new Ajax.Request(url,
			{
				method: 'get',
				onComplete: function (t)
				{
					var res = t.responseText;
					if (parseInt(res, 10) > 0)
					{
						showInfoDivContainer(getLang("all", "timerSuccessfullyDeleted"));
						v4_timer_showContainer('showAdmin', true);
						v4_timer_refreshCurrentTimerCount();
					}
					else
					{
						showInfoDivContainer(getLang("all", "timerFailedToDelete"), 'fehler');
					}
					p4_timer_showContainerPortal();
				}
			}
		);
	}
}

function v4_timer_toggle(timerID)
{
	showInfoDivContainer('', 'ajax');
	var url = 'ajax/srv/timer.php?typ=toggle&timerID='+encodeURIComponent(timerID);
	new Ajax.Request(url,
		{
			method: 'get',
			onComplete: function (t)
			{
				hideShowInfoDivContainer();
				v4_timer_showContainer('showAdmin', true);
				v4_timer_refreshCurrentTimerCount();
				p4_timer_showContainerPortal();
			}
		}
	);
}

function v4_timer_refreshCurrentTimerCount()
{
	if ($('currentTimerCountMenuDiv'))
	{
		var url = 'ajax/srv/timer.php?typ=refreshCurrentTimerCount';
		new Ajax.Request(url,
			{
				method: 'get',
				onComplete: function (t)
				{
					var res = t.responseText;
					$('currentTimerCountMenuDiv').innerHTML = res;
				}
			}
		);
	}
}

function v4_timer_feed(e, timerID)
{
	var theElement = Event.element(e);
	var theText = theElement.value;
	var keynum = (e.keyCode) ? e.keyCode : e.which;
	if (keynum == 13)
	{
		showInfoDivContainer('', 'ajax');
		var url = 'ajax/srv/timer.php?typ=feed&timerID='+encodeURIComponent(timerID)+'&text='+encodeURIComponent(theText);
		new Ajax.Request(url,
			{
				method: 'get',
				onComplete: function (t)
				{
					var json = t.responseText.evalJSON();
					showInfoDivContainer(json.meldung, json.klasse);
					if (json.ok == true) {
						theElement.value = '';
					}
				}
			}
		);
	}
}

function v4_timer_secondsToString(theSeconds)
{
	var minutes = parseInt((theSeconds / 60), 10);
	var seconds = theSeconds - (minutes * 60);
	var hours = parseInt((minutes / 60), 10);
	minutes = minutes - (hours * 60);
	hours = (hours < 10) ? hours = '0' + hours : hours;
	minutes = (minutes < 10) ? minutes = '0' + minutes : minutes;
	seconds = (seconds < 10) ? seconds = '0' + seconds : seconds;
	var ret = hours+':'+minutes+':'+seconds;
	return ret.replace(/^00:/, '');
}

function v4_timer_countTime(jsonStr)
{
	var objArr = eval(jsonStr);
	for (var i = 0; i < objArr.length; i++)
	{
		if ($('v4_timer_admin_td_'+objArr[i].id))
		{
			if (objArr[i].start == 0)
			{
				$('v4_timer_admin_td_'+objArr[i].id).innerHTML = v4_timer_secondsToString(objArr[i].dauer);
			}
			else
			{
				v4_timer_refreshElement(((v4_timer_getCurrentTime() - parseInt(objArr[i].start, 10)) + parseInt(objArr[i].dauer, 10)), 'v4_timer_admin_td_'+objArr[i].id);
			}
		}
	}
}

function v4_timer_refreshElement(theSeconds, theElement)
{
	if ($(theElement))
	{
		$(theElement).innerHTML = v4_timer_secondsToString(theSeconds++);
		v4_timer_timeOutIDs[v4_timer_timeOutIDs.length] = window.setTimeout('v4_timer_refreshElement('+theSeconds+', \''+theElement+'\')', 1000);
	}
}

function v4_timer_getCurrentTime()
{
	return parseInt(((new Date()).getTime() / 1000), 10);
}

function v4_timer_toggleAdminNotizTr(theTr, theImg, theID)
{
	if ($(theTr))
	{
		$(theTr).toggle();
		if ($(theImg))
		{
			$(theImg).src = ($(theTr).style.display == 'none') ? 'media/katbrowser/tree2.gif' : 'media/katbrowser/tree1.gif';
			new Ajax.Request('ajax/srv/timer.php?typ=toggleNotiz&timerID=' + theID);
		}
	}
}

function v4_timer_createBugTimer(e)
{
	if ($('hidden_ticketTimerText') && $('bugAjax_bugID') && $('bugAjaxInput_ueberschrift'))
	{
		$('hidden_ticketTimerText').value = '[Ticket #' + $F('bugAjax_bugID') + '] ' + $F('bugAjaxInput_ueberschrift');
		v4_timer_create(e, 'hidden_ticketTimerText', 0, $F('bugAjax_bugID'));
		$('hidden_ticketTimerText').value = '';
	}
}

function v4_timer_createRRTimer(e, id)
{
	if ($('hidden_RRTimerText') && $('rr_hidText_' + id))
	{
		$('hidden_RRTimerText').value = $F('rr_hidText_' + id);
		v4_timer_create(e, 'hidden_RRTimerText', id);
		$('hidden_RRTimerText').value = '';
	}
}
