//Chat
function init()
{
	refreshSound();
	reload();
}

function chatData(originalRequest)
{
	if(originalRequest.responseText == "")
		reload();
		
	data = originalRequest.responseText.evalJSON();
	if(data.tooManyChats == 1) {
		$("chatContent").innerHTML += '<div style="position: relative; background-color: #a00; color: #fff; font-weight: bold;" align="center">Chat wurde beendet [neuere Chats offen]</div>';
		$("chatContent").scrollTop = $("chatContent").scrollHeight;
		chatIsOnline = false;
		return;
	}
	$("chatContent").innerHTML += data.html;
	$("chatContent").scrollTop = $("chatContent").scrollHeight;

	//Play sound
	if(lastCid != 0 && chatSound)
	{
		var flash = '<object style="visibility:hidden" classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="0" height="0">';
		flash += '<param name="movie" value="./chatSound.swf">';
		flash += '<param name="quality" value="High">';
		flash += '<embed style="visibility:hidden" src="./chatSound.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="0" height="0" />';
		flash += '</object>';
		$("sound").innerHTML = flash;
	}
	
	//Get cid
	lastCid = data.lastCid;
	
	reload();
}

function reload()
{
	url = './api_chat.php';
	pars = 'cid='+lastCid+'&token='+token;
	ajaxRequest = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: chatData
		});
}

function chatSend(text)
{
	if(text == "")
		return;
	if(chatIsOnline == false) {
		return;
	}
	
	text = encodeURI(text);
		
	url = './api_chat.php';
	pars = 'text='+text;
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars
		});
		
	$("chatLine").value = "";
}

function chatLineKey(e) 
{
	if (e.keyCode == 13)
		chatSend($("chatLine").value);
}

function refreshSound()
{
	if(chatSound == 1)
		$('soundImg').src = './images/soundOn.png';
	else
		$('soundImg').src = './images/soundOff.png';
}

function toggleSound() 
{
	chatSound = 1-chatSound;
	refreshSound();
	
	url = './ajax.php';
	pars = 'type=toggleChatSound';
	new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars
		});
}
		
//Counter
var counters = new Array();
var counterItv = null;
var counterItv2 = null;
var pageReload = false;
var lastCounterId = 0;

function startCounter(timeStart, timeEnd, barWidth, showInTitle) {
	if(counterItv == null) {
		counterItv = setInterval(updateCounter, 1000);
		counterItv2 = setInterval(updateCounterBar, 100);
	}
	var counterId = counters.length;
	lastCounterId = counterId;
	var counter = new Object;
	
	counter.endEvent = function() {};
	counter.tickEvent = function(v, max) {};
	counter.timeStart = timeStart;
	counter.timeEnd = timeEnd;
	counter.barWidth = barWidth;
	counter.showInTitle = showInTitle;
	
	counters.push(counter);
	return counterId;
}

function updateCounterBar() {
	//update bar
	timePhpTemp += 0.1;
	var barWidth;
	for(var i = 0; i < counters.length; i++)
	{
		//update bar
		if(counters[i].timeEnd <= timePhpTemp) {
			barWidth = counters[i].barWidth;
			$('progressBarIndicator'+i).style.backgroundColor = '#AAA';
			if(pageReload == false) {
				pageReload = true;
				
    			counters[i].endEvent();
				//setTimeout(reloadPage, 2000);
			}		
		}
		else {
			barWidth = (timePhpTemp - counters[i].timeStart) / (counters[i].timeEnd - 	counters[i].timeStart) * counters[i].barWidth;
			counters[i].tickEvent(timePhpTemp - counters[i].timeStart, counters[i].timeEnd - 	counters[i].timeStart);
			barWidth = Math.min(counters[i].barWidth, barWidth);
		}
		$('progressBarIndicator'+i).style.width = barWidth + 'px';
		
		//Update light
		$('progressBarLight'+i).style.display = 'block';
		$('progressBarLight'+i).style.left = barWidth + 'px';
	}
}

function updateCounter() {
	timePhp += 1;
	timePhpTemp = timePhp;
	
	//update counter
	for(var i = 0; i < counters.length; i++)
	{
		//update text
		timeText = formate_time_rel(counters[i].timeEnd - Math.floor(timePhp));
		$('progressBarText'+i).innerHTML = timeText;
		
		//Update title
		if(counters[i].showInTitle) {
			document.title = timeText + ' - ' + pageTitle;
		}
	}
}

function reloadPage() {
	window.location.reload();
}

function formate_time_rel(time)
{
	if(time<=0) 
	{
		return "-";
	}
	else
	{
		var sekunde = Math.floor(time%60);
		if(time < 60)
		{
			return sekunde;
		}
		else if(sekunde<10)
		{
			sekunde="0"+sekunde;
		}		
		
		var minute = (Math.floor(time/60)%60);
		if(time < 3600)
		{
			return minute + ":" + sekunde;
		}
		if(minute<10)
		{
			minute="0"+minute;
		}			

		var stunde = (Math.floor(time/3600)%24);
		if(time < 86400)
		{
			return (stunde + ":" + minute + ":" + sekunde);  
		}
		if(stunde<10)
		{
			stunde="0"+stunde;
		}		
												
		var tage = Math.floor(time/(3600*24));
		if(tage>1)
			return (tage + " Tage " + stunde + ":" + minute + ":" + sekunde); 
		else
			return (tage + " Tag " + stunde + ":" + minute + ":" + sekunde); 
	}
}

//Ticker
var intSteps = 100, intCountSteps = 0;
var intCurrent, objUpdate, intIntervalSize, intInterval;
function startTicker(intFrom, intTo, intDur, intDelay, objUpdateF)
{
	objUpdate = objUpdateF;
	intCurrent = intFrom;
	intIntervalSize = (intTo - intFrom) / intSteps;
	intInterval = intDur / intSteps;
	setTimeout("updateTicker()", intDelay);
}

function updateTicker()
{
	intCurrent += intIntervalSize;
	intCountSteps++;
	objUpdate.innerHTML = Math.round(intCurrent);
	
	if(intCountSteps != intSteps)
		setTimeout("updateTicker()", intInterval);
}

//Limit textarea-Chars
function imposeMaxLength(Object, MaxLen)
{
	return (Object.value.length <= MaxLen);
}

//Confirming Links
function confirmClick(message, url)
{
	if(confirm(message)) location.href = url;
}

//Confirm Submits
function confirmSubmit(message)
{
	agree=confirm(message);
	if (agree)
		return true ;
	else
		return false ;
}

//Popup-Box
var mousex, mousey, popupShown, popupTimeout, dontShowPopup;
dontShowPopup = false;

function popUpMove(e) 
{
	clearTimeout(popupTimeout);
	if(!e) e = window.event;
	var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? 
	window.document.documentElement : window.document.body;
	mousey = e.pageY ? e.pageY : e.clientY + body.scrollTop - body.clientTop;
	mousex = e.pageX ? e.pageX : e.clientX + body.scrollLeft  - body.clientLeft;
	
	elem = $("popUp");
	elem.style.left = (mousex + 15) + 'px';
	elem.style.top = (mousey + 15) + 'px';
}

function popUpShow(text, trigger)
{
	if(dontShowPopup == false)
	{
		trigger.onmousemove = popUpMove;
		elem = $("popUp");
		elem.innerHTML = text;
		elem.style.display = 'block';
	}
}

function popUpUnshow()
{
	clearTimeout(popupTimeout);
	popupTimeout = setTimeout("popUpHide()", 500);
}

function popUpHide()
{
	window.document.onmousemove = null;
	elem = $("popUp");
	elem.innerHTML = '';
	elem.style.display = 'none';
}
